def get_hit_point_draw(combined_ray, linecolor): start = np.array([0.0, 0.0, 0.0]) rot = np.array([0.0, 1.0, 0, 0.0]) # rotate a vector by quaternion rotation dir = rotate_vec_by_quaternion(rot, combined_ray) end = start + dir #get hitting point sphere = Sphere(Point3(0, 0, 0), 1) ray = Ray3(Point3(start[0], start[1], start[2]), Vector3(dir[0], dir[1], dir[2])) hitpoint = sphere.intersect(ray) #check if hitting point is on sphere surface # check_point_on_sphere(0, 0, 0, hitpoint, 1) # convert unity coordinate to python coordinate unity_to_python_point(start) unity_to_python_point(end) unity_to_python_point(hitpoint) return hitpoint
O = ray.start D = ray.dir S = self.center R = self.radius a = D.dot(D) OS = O - S b = 2 * D.dot(OS) c = OS.dot(OS) - R * R disc = b * b - 4 * a * c if disc > 0: distSqrt = sqrt(disc) q = (-b - distSqrt) / 2.0 if b < 0 else (-b + distSqrt) / 2.0 t0 = q / a t1 = c / q t0, t1 = min(t0, t1), max(t0, t1) if t1 >= 0: return t1 if t0 < 0 else t0 return float("inf") def __repr__(self): return "Sphere(%s, %.3f)" % (str(self.center), self.radius) # Two simple sanity tests if module is run directly if __name__ == "__main__": sphere = Sphere(Point3(1, 0, 0), 1) ray = Ray3(Point3(1, 0, 5), Vector3(0, 0, -1)) missingRay = Ray3(Point3(1, 0, 5), Vector3(0, 0, 1)) assert abs(sphere.intersect(ray) - 4.0) < 0.00001 assert sphere.intersect(missingRay) is None
SHINY_RED = Material(Colour(0.7, 0.1, 0.2), Colour(0.4, 0.4, 0.4), 100, .2) SHINY_BLUE = Material(Colour(0.2, 0.3, 0.7), Colour(0.8, 0.8, 0.8), 200, .3) MATT_GREEN = Material(Colour(0.1, 0.85, 0.1)) CHECK_FLOOR = Material( None, None, None, None, Texture_Check( 6, Colour( 0, 0, 0), Colour( 0.5, 0.5, 0.5))) scene = Scene([ Sphere(Point3(0.35, 0.6, 0.5), 0.25, SHINY_BLUE), Difference([ Intersection([ # Bowl Plane( Point3( 0.1, 0.175, 0.8), Vector3( 0.4, 1, 0.3), SHINY_BLUE), Sphere(Point3(0.1, 0.175, 0.8), 0.175, SHINY_BLUE), ]), Sphere(Point3(0.1, 0.175, 0.8), 0.165, SHINY_BLUE)]), Sphere(Point3(0.75, 0.17, .8), 0.17, SHINY_RED), Plane(Point3(0, 0, 0), Vector3(0, 1, 0), CHECK_FLOOR), Difference([ Intersection([ # Cube Plane(Point3(0.2, 0.0, 0.5), Vector3(0, -1, 0), MATT_GREEN), Plane(Point3(0.1, 0.08, 0.8), Vector3(0, 1, 0), MATT_GREEN), Plane(Point3(0.3, 0.1, 0.5), Vector3(-1, 0, 0), MATT_GREEN), Plane(Point3(0.5, 0.1, 0.5), Vector3(1, 0, 0), MATT_GREEN), Plane(Point3(0.5, 0.1, 1.3), Vector3(0, 0, 1), MATT_GREEN), Plane(Point3(0.5, 0.1, 1), Vector3(0, 0, -1), MATT_GREEN)]), Sphere(Point3(0.4, .1, 1.3), 0.1, SHINY_RED)]), ])
from texture import * from CSG import * from camera import Camera WIN_SIZE = 300 # Screen window size (square) SHINY_RED = Material(Colour(0.7, 0.7, 0.7), Colour(0.4, 0.4, 0.4), 100, .2) SHINY_SPHERE = Material(Colour(0.2, 0.3, 0.7), Colour(0.8, 0.8, 0.8), 200, .3) MATT_CUBE = Material(Colour(0.7, 0.7, 0.7), Colour(0.9, 0.9, 0.9), 300, .5) CHECK_FLOOR = Material( None, None, None, None, Texture_Check(6, Colour(.1, .1, .1), Colour(0.7, 0.7, 0.7))) scene = Scene([ Intersection([ # Cube Plane(Point3(0.5, 0.0, 0.5), Vector3(0, -1, 0), MATT_CUBE), Plane(Point3(0.5, 0.1, 0.5), Vector3(0, 1, 0), MATT_CUBE), Plane(Point3(0.2, 0.0, 0.5), Vector3(-1, .3, 0), MATT_CUBE), Plane(Point3(0.8, 0.0, 0.5), Vector3(1, .3, 0), MATT_CUBE), Plane(Point3(0.5, 0.0, 0.8), Vector3(0, .3, 1), MATT_CUBE), Plane(Point3(0.5, 0.0, 0.2), Vector3(0, .3, -1), MATT_CUBE) ]), Sphere(Point3(0.5, 0.3, 0.5), 0.2, SHINY_SPHERE), Plane(Point3(0, 0, 0), Vector3(0, 1, 0), CHECK_FLOOR), ]) scene.lights = [ SpotLight(scene, Point3(1, 1, 1), Point3(0.5, 0.5, 0.5), 20, Colour(.8, 0, 0)), SpotLight(scene, Point3(1.2, 1, -1), Point3(0.5, 0.5, 0.5), 25, Colour(0, 1, 0)),
SHINY_RED = Material(Colour(0.7, 0.1, 0.2), Colour(0.4, 0.4, 0.4), 100, .2) SHINY_BLUE = Material(Colour(0.2, 0.3, 0.7), Colour(0.8, 0.8, 0.8), 200, .3) MATT_GREEN = Material(Colour(0.1, 0.85, 0.1)) CHECK_FLOOR = Material( None, None, None, None, Texture_Check( 6, Colour( 0, 0, 0), Colour( 0.5, 0.5, 0.5))) scene = Scene([ Sphere(Point3(0.35, 0.6, 0.5), 0.25, SHINY_BLUE), # Difference([ Intersection([ # Cube #Plane(Point3(0.2,0.0,0.5), Vector3(0,-1,0), CHECK_FLOOR), Plane(Point3(0.1, 0.175, 0.8), Vector3(0.5, 1, 0.1), SHINY_BLUE), #Plane(Point3(0.1,0.1,0.5), Vector3(-1,0,0), CHECK_FLOOR), #Plane(Point3(0.4,0.1,0.5), Vector3( 1,0,0), CHECK_FLOOR), #Plane(Point3(0.5,0.1,0.8), Vector3(0,0, 1), CHECK_FLOOR), #Plane(Point3(0.5,0.1,0.5), Vector3(0,0,-1), CHECK_FLOOR), Sphere(Point3(0.1, 0.175, 0.8), 0.175, SHINY_BLUE), ]), # Sphere(Point3(0.1,0.175,0.8), 0.165, SHINY_BLUE)]), #Sphere(Point3(0.75,0.15,.2), 0.15, SHINY_RED), Plane(Point3(0, 0, 0), Vector3(0, 1, 0), CHECK_FLOOR) ]) scene.lights = [ #Light(scene, unit(Vector3(2,5,3)), Colour(0.6, 0.6, 0.6)), #Light(scene, unit(Vector3(-4,3,0)), Colour(0.7, 0.7, 0.7)), PointLight(scene, Point3(.5, 1.1, 1.2), Colour(0.9, 0.9, 0.9)),
from scene import Scene from light import * from texture import * from CSG import * from camera import Camera WIN_SIZE = 300 # Screen window size (square) SHINY_RED = Material(Colour(0.7, 0.7, 0.7), Colour(0.4,0.4,0.4), 100, .2) SHINY_SPHERE = Material(Colour(0.2, 0.3, 0.7), Colour(0.8,0.8,0.8), 200, .3) MATT_CUBE = Material(Colour(0.7,0.7, 0.7), Colour(0.9,0.9,0.9), 300, .5) MATT_PLANE = Material(Colour(0.6,0.6,0.6)) scene = Scene([ Intersection([ # Cube Plane(Point3(0.5,0.0,0.5), Vector3(0,-1,0), MATT_CUBE), Plane(Point3(0.5,0.1,0.5), Vector3(0, 1,0), MATT_CUBE), Plane(Point3(0.2,0.0,0.5), Vector3(-1,.3,0), MATT_CUBE), Plane(Point3(0.8,0.0,0.5), Vector3( 1,.3,0), MATT_CUBE), Plane(Point3(0.5,0.0,0.8), Vector3(0,.3, 1), MATT_CUBE), Plane(Point3(0.5,0.0,0.2), Vector3(0,.3,-1), MATT_CUBE)]), Intersection([ # Cube Plane(Point3(0.5,0.0,0.5), Vector3(0,-1,0), MATT_CUBE), Plane(Point3(0.5,0.1,0.5), Vector3(0, 1,0), MATT_CUBE), Plane(Point3(0.2,0.0,0.5), Vector3(-1,.3,0), MATT_CUBE), Plane(Point3(0.8,0.0,0.5), Vector3( 1,.3,0), MATT_CUBE), Plane(Point3(0.5,0.0,0.8), Vector3(0,.3, 1), MATT_CUBE), Plane(Point3(0.5,0.0,0.2), Vector3(0,.3,-1), MATT_CUBE)]), Plane(Point3(0.5,0.0,0.0), Vector3(0,1,0) MATT_PLANE), Plane(Point3(0.5,0.0,-1), Vector3(0,0,1) MATT_PLANE), ])