def render_scene(self,objects,lights,res): #create a viewport and image v = ViewPort(res[0],res[1]) im = Image.new("RGB",(v.w,v.h)) pix = im.load() #define a ray #FIX BUG -- STILL ORTHO ray = Ray(np.array([0,0,0]),np.array([0,0,-1])) # Perform perspective ray-tracing print("...generating "+str(v)+" image") for col in range(v.w): for row in range(v.h): color = np.zeros(3) print(color) ray.o = v.getPixelCenter(col,row) for s in objects: t = s.intersectRay(ray) if (t != None): xp = ray.getPoint(t) for light in lights: color+= phongShader(xp,s.getNormal(xp),s.material,light,self.eye) print(color) #pix[col,v.h-1-row] = color pix[col,v.h-1-row]=(1,1,1) # Show the image in a window im.show()
def render_scene(self, objects, lights, res): #create a viewport and image v = ViewPort(res[0], res[1]) im = Image.new("RGB", (v.w, v.h)) pix = im.load() #define a ray #FIX BUG -- STILL ORTHO ray = Ray(np.array([0, 0, 0]), np.array([0, 0, -1])) # Perform perspective ray-tracing print("...generating " + str(v) + " image") for col in range(v.w): for row in range(v.h): color = np.zeros(3) print(color) ray.o = v.getPixelCenter(col, row) for s in objects: t = s.intersectRay(ray) if (t != None): xp = ray.getPoint(t) for light in lights: color += phongShader(xp, s.getNormal(xp), s.material, light, self.eye) print(color) #pix[col,v.h-1-row] = color pix[col, v.h - 1 - row] = (1, 1, 1) # Show the image in a window im.show()
def can_see(point, object): cast_ray = Ray() cast_ray.o = point cast_ray.d = Vector3D(-260, -100, 0) cast_ray.d = cast_ray.d.normalize() hit = False for thing in World: intersection = thing.hit(cast_ray) #if it's not False, it hit something. if intersection != False: #is this the first time we've hit something? if hit == False: hit = True hit_distance = cast_ray.o.distance(intersection.hit_point) closest_object = intersection else: #check if the latest hit is closer if cast_ray.o.distance(intersection.hit_point) < hit_distance: closest_object = intersection hit_distance = cast_ray.o.distance(intersection.hit_point) if hit == False: return False else: if closest_object == object: return True else: return False
def getReflectedNormal(self, ray, t): p = Point(*(ray.d.scale(t)).v )+ray.o normal = self.normal rr = Ray() rr.o=p rr.d = ray.d - normal.scale(2*ray.d.dot(normal)) rr.d=Vector(*rr.d.v) return rr, normal
def getReflectedNormal(self, ray, t): # need to find the point, then find normal at point #then get reflected ray, origin=pt p = Point(*(ray.d.scale(t)).v )+ray.o normal = Vector(*(p-self.origin).v) normal.normalize() rr = Ray() rr.o=p rr.d = ray.d - normal.scale(2*ray.d.dot(normal)) rr.d=Vector(*rr.d.v) return rr, normal
def raytrace(cast_ray, r = recurse): hit = False for thing in World: intersection = thing.hit(cast_ray) #if it's not False, it hit something. if intersection != False: #is this the first time we've hit something? if hit == False: hit = True hit_distance = cast_ray.o.distance(intersection.hit_point) closest_object = intersection else: #check if the latest hit is closer if cast_ray.o.distance(intersection.hit_point) < hit_distance: closest_object = intersection hit_distance = cast_ray.o.distance(intersection.hit_point) if hit == False: return background_color else: # At this point, closest_object[4] contains the closest item the ray intersects with. #Check for end of recursion if r == 1: #Now we apply lighting lit_color = lighting(closest_object) return RGB(1, 1, 1) return lit_color #Check for Reflectance if closest_object.object.mat.reflect > 0: reflect_ray = Ray() reflect_ray.o = closest_object.hit_point c1 = 0 - (closest_object.normal * cast_ray.d) reflect_ray.d = cast_ray.d + (2 * closest_object.normal * c1) reflect_ray.d = reflect_ray.d.normalize() reflect_color = raytrace(reflect_ray, r - 1) tcolor = lighting(closest_object) tcolor = tcolor + (reflect_color * closest_object.object.mat.reflect) return tcolor else: # The object isn't reflective. lit_color = lighting(closest_object) return lit_color
def render_scene(objects,res): #create a viewport and image v = ViewPort(res[0],res[1]) im = Image.new("RGB",(v.w,v.h)) pix = im.load() #define a ray ray = Ray(np.array([0,0,0]),np.array([0,0,-1])) # Perform perspective ray-tracing for col in range(v.w): for row in range(v.h): ray.o = v.getPixelCenter(col,row) t = s.intersectRay(ray) if (t != None): xp = ray.getPoint(t) pix[col,(v.h-1)-row] = phongShader(xp,s.getNormal(xp),s.material,light,eye) # Show the image in a window im.show()
def render_scene(objects, res): #create a viewport and image v = ViewPort(res[0], res[1]) im = Image.new("RGB", (v.w, v.h)) pix = im.load() #define a ray ray = Ray(np.array([0, 0, 0]), np.array([0, 0, -1])) # Perform perspective ray-tracing for col in range(v.w): for row in range(v.h): ray.o = v.getPixelCenter(col, row) t = s.intersectRay(ray) if (t != None): xp = ray.getPoint(t) pix[col, (v.h - 1) - row] = phongShader(xp, s.getNormal(xp), s.material, light, eye) # Show the image in a window im.show()
temp[2] = 1.0 if temp[2] > 1.0 else temp[2] temp[2] = 0 if temp[2] < 0 else temp[2] temp[0] = int(temp[0] * 255) temp[1] = int(temp[1] * 255) temp[2] = int(temp[2] * 255) return temp #if multijittered is on which is very expensive multijittered = 0 for col in range(v.w): if col % 10 == 0: print col for row in range(v.h): if multijittered == 0: ray.o = v.getPixelCenter(row, col) # print ray.o ray.d = ray.o - v.e ray.d = normalize(ray.d) # ray.d = np.array([0.0,0.0,-1.0]) ls = [] objarr = tree.get(ray) for item in objarr: t = item.intersectRay(ray) if t != None: xp = ray.getPoint(t) # ls.append((t, item.k, item.color, item.mat, item.getNormal(xp), xp, v.e)) ls.append((t, item, xp)) try: #if len(ls) == 1: # pix[col, (v.h - 1) - row] = ls[0][1]
def phongDiffuse(x,n,mat): """Implements a Phong-style diffuse shading function Args: x: is a point on a surface n: is the unit normal at that point mat: is an RGB tuple of the surface color Returns: A tuple representing an RGB color with values in [0,255] """ factor = kd*illum*max(0,n.dot(ldir)) color = np.rint(factor*mat).astype(int) return (color[0],color[1],color[2]) # Perform orthographic ray-tracing of the sphere for col in range(v.w): for row in range(v.h): ray.o = v.getPixelCenter(col,row) t = s.intersectRay(ray) if (t != None): xp = ray.getPoint(t) pix[col,(v.h-1)-row] = phongDiffuse(xp,s.getNormal(xp),s.material) # Show the image in a window im.show()
def phongDiffuse(x, n, mat): """Implements a Phong-style diffuse shading function Args: x: is a point on a surface n: is the unit normal at that point mat: is an RGB tuple of the surface color Returns: A tuple representing an RGB color with values in [0,255] """ factor = kd * illum * max(0, n.dot(ldir)) color = np.rint(factor * mat).astype(int) return (color[0], color[1], color[2]) # Perform orthographic ray-tracing of the sphere for col in range(v.w): for row in range(v.h): ray.o = v.getPixelCenter(col, row) t = s.intersectRay(ray) if (t != None): xp = ray.getPoint(t) pix[col, (v.h - 1) - row] = phongDiffuse(xp, s.getNormal(xp), s.material) # Show the image in a window im.show()