def parseRect(entity): '''{"type":"rectangle","A":"1.2,1.5,3","B":"1.0,6,1","C":'0,0,0','color':'0,1,0'}''' if entity['type'] != 'rectangle': raise ValueError("entity passed to parseTriangle is not a rectangle") A_pos = entity['A'].split(',') B_pos = entity['B'].split(',') C_pos = entity['C'].split(',') assert len(A_pos) == 3 A = Vertex(position=A_pos) B = Vertex(position=B_pos) C = Vertex(position=C_pos) D = Vertex(position=Vec3(A_pos) + Vec3(C_pos) - Vec3(B_pos)) print(D.position) r_color_txt = entity['color'].split(',') r_color = Vec3(r_color_txt) r_color.normalize() if "reflectivity" in entity: r_reflect = float(entity['reflectivity']) r_material = PhongMaterial(color=r_color, reflectivity=r_reflect) else: r_material = PhongMaterial(color=r_color) return [ Triangle(A, B, C, material=r_material), Triangle(A, C, D, material=r_material) ]
def make_frame(t): w = 320 h = 240 # Create a camera with width, height and field of view: camera = PerspectiveCamera(w, h, 60) # Set View matrix of camera: define where to look at camera.setView(Vec3(0, -t - 10, 0), Vec3(0, 0, 0), Vec3(0, 0, 1)) # Create view-projection matrix (right to left) vp = camera.projection * camera.view # Create a scene scene = Scene() # Add a light to the scene scene.addLight(PointLight(Vec3(0, -2, 0))) # Create a triangle t1 = Triangle(Vertex(position=(-5, -4, 5), texcoord=(0, 0)), Vertex(position=(5, -4, 5), texcoord=(1, 0)), Vertex(position=(5, -4, -5), texcoord=(1, 1)), material= TextureMaterial(texturepath=['tex17-1.png', 'tex17-2.png'])) t2 = Triangle(Vertex(position=(-5, -4, 5), texcoord=(0, 0)), Vertex(position=(5, -4, -5), texcoord=(1, 1)), Vertex(position=(-5,-4, -5), texcoord=(0, 1)), material= TextureMaterial(texturepath=['tex17-1.png', 'tex17-2.png'])) # Add triangle and sphere to the scene: scene.add(t1) scene.add(t2) # Now tell the scene which camera we use scene.setCamera(camera) # Create a raytracer using "SimpleRT" engine = SimpleRT() # Render the scene: image = engine.render(scene) return image.data
def parseTriangle(entity): '''{"type":"triangle","A":"1.2,1.5,3","B":"1.0,6,1","C":'0,0,0','color':'0,1,0'}''' if entity['type'] != 'triangle': raise ValueError("entity passed to parseTriangle is not a triangle") A_pos = entity['A'].split(',') B_pos = entity['B'].split(',') C_pos = entity['C'].split(',') assert len(A_pos) == 3 A = Vertex(position=A_pos) B = Vertex(position=B_pos) C = Vertex(position=C_pos) t_color_txt = entity['color'].split(',') t_color = Vec3(t_color_txt) t_color.normalize() if "reflectivity" in entity: t_reflect = float(entity['reflectivity']) t_material = PhongMaterial(color=t_color, reflectivity=s_reflect) else: t_material = PhongMaterial(color=t_color) return Triangle(A, B, C, material=t_material)
def make_frame(t): w = 320 h = 240 image = RGBImage(w, h) # Create a camere with width, height and field of view: camera = PerspectiveCamera(w, h, 60) posy = -90 + t * 25 # move camera along y-axis. t is time in seconds. # Set View matrix of camera: define where to look at camera.setView(Vec3(0, posy, 0), Vec3(0, 0, 0), Vec3(0, 0, 1)) # Create view-projection matrix (right to left) vp = camera.projection * camera.view # Create a triangle t = Triangle(Vertex(position=(-5, 1, 0)), Vertex(position=(0, 1, 5)), Vertex(position=(5, 1, 0))) # Triangle in normalized device coordinates: at = vp * t.a.position bt = vp * t.b.position ct = vp * t.c.position # Triangle in Screen coordinates: a_screenpos = Vec2(int(w * 0.5 * (at.x + 1.) / at.z), int(h * 0.5 * (at.y + 1.) / at.z)) b_screenpos = Vec2(int(w * 0.5 * (bt.x + 1.) / bt.z), int(h * 0.5 * (bt.y + 1.) / at.z)) c_screenpos = Vec2(int(w * 0.5 * (ct.x + 1.) / ct.z), int(h * 0.5 * (ct.y + 1.) / at.z)) # Draw the Triangle (wireframe) in image: color = Vec3(1, 1, 1) image.drawLine(a_screenpos, c_screenpos, color) image.drawLine(c_screenpos, b_screenpos, color) image.drawLine(b_screenpos, a_screenpos, color) return np.asarray(image.data)
# Set View matrix of camera: define where to look at camera.setView(Vec3(0, -10, 0), Vec3(0, 0, 0), Vec3(0, 0, 1)) # Create view-projection matrix (right to left) vp = camera.projection * camera.view # Create a scene scene = Scene() # Add a light to the scene scene.addLight(PointLight(Vec3(0, -5, 0))) # Create a triangle t = Triangle(Vertex(position=(-5, 1, 0), texcoord=(0, 0)), Vertex(position=(0, 1, 5), texcoord=(1, 0)), Vertex(position=(5, 1, 0), texcoord=(1, 1)), material=TextureMaterial(texturepath=['tex16.png'])) s = Sphere(center=Vec3(0, -3, 0), radius=1, material=TextureMaterial(texturepath=['tex16.png'])) # Add triangle and sphere to the scene: scene.add(t) scene.add(s) # Now tell the scene which camera we use scene.setCamera(camera) # Create a raytracer using "SimpleRT" engine = SimpleRT()
from PIL import Image # Specify width/height as in example 5 width = 320 height = 240 # now create a camera and a view like in example 5: camera = PerspectiveCamera(width, height, 60) camera.setView(Vec3(0, -10, 0), Vec3(0, 0, 0), Vec3(0, 0, 1)) # Create a scene scene = Scene() # Add a triangle (same as example 5) to the scene: scene.add( Triangle(Vertex(position=(-5, 1, 0), color=(1, 0, 0)), Vertex(position=(0, 1, 5), color=(0, 1, 0)), Vertex(position=(5, 1, 0), color=(0, 0, 1)))) # Now tell the scene which camera we use scene.setCamera(camera) # Create a raytracer using "SimpleRT" engine = SimpleRT() # Render the scene: image = engine.render(scene) # Save the resulting image using pillow image.save("08.png")
from pyrt.camera import OrthographicCamera, PerspectiveCamera from pyrt.renderer import SimpleRT from PIL import Image # Specify width/height as in example 5 width = 320 height = 240 # now create a camera and a view like in example 5: camera = PerspectiveCamera(width, height, 60) camera.setView(Vec3(0, -10, 0), Vec3(0, 0, 0), Vec3(0, 0, 1)) # Create a scene scene = Scene() # Add a triangle (same as example 5) to the scene: scene.add( Triangle(Vertex(position=(-5, 1, 0)), Vertex(position=(0, 1, 5)), Vertex(position=(5, 1, 0)))) # Now tell the scene which camera we use scene.setCamera(camera) # Create a raytracer using "SimpleRT" engine = SimpleRT() # Render the scene: image = engine.render(scene) image.save("07.png")
# create some materials: floormaterial = PhongMaterial(color=Vec3(0.5, 0.5, 0.5)) sphere0material = PhongMaterial(color=Vec3(1., 0., 0.)) sphere1material = PhongMaterial(color=Vec3(0., 1., 0.)) sphere2material = PhongMaterial(color=Vec3(0., 0., 1.), reflectivity=0.5) sphere3material = PhongMaterial(color=Vec3(1., 1., 0.)) # Add "floor" A = Vertex(position=(-5.0, -5.0, 0.0)) B = Vertex(position=(5.0, -5.0, 0.0)) C = Vertex(position=(5.0, 5.0, 0.0)) D = Vertex(position=(-5.0, 5.0, 0.0)) scene.add(Triangle(A, B, C, material=floormaterial)) scene.add(Triangle(A, C, D, material=floormaterial)) # Add some spheres #scene.add(Sphere(center=Vec3(-2.5,-2.5,1.75), radius=1.75, material=sphere0material)) scene.add( Sphere(center=Vec3(2.5, -2.5, 1.75), radius=1.75, material=sphere1material)) scene.add( Sphere(center=Vec3(2.5, 2.5, 1.75), radius=1.75, material=sphere2material)) scene.add( Sphere(center=Vec3(-2.5, 2.5, 1.75), radius=1.75, material=sphere3material)) # Now tell the scene which camera we use
def make_frame(t): # Specify width/height as in example 5 width = 320 height = 240 # now create a camera and a view like in example 5: camera = PerspectiveCamera(width, height, 45) camera.setView(Vec3(0., -10., 10.), Vec3(0., 0., 0.), Vec3(0., 0., 1.)) # Create a scene scene = Scene() # Add a light to the scene radius = 3.0 p = 2 * pi / 4 x = radius * cos(t * p) y = radius * sin(t * p) scene.addLight(PointLight(Vec3(x, y, 10))) # create some materials: floormaterial = PhongMaterial(color=Vec3(0.5, 0.5, 0.5)) sphere0material = PhongMaterial(color=Vec3(1., 0., 0.)) sphere1material = PhongMaterial(color=Vec3(0., 1., 0.)) sphere2material = PhongMaterial(color=Vec3(0., 0., 1.)) sphere3material = PhongMaterial(color=Vec3(1., 1., 0.)) # Add "floor" A = Vertex(position=(-5.0, -5.0, 0.0)) B = Vertex(position=(5.0, -5.0, 0.0)) C = Vertex(position=(5.0, 5.0, 0.0)) D = Vertex(position=(-5.0, 5.0, 0.0)) scene.add(Triangle(A, B, C, material=floormaterial)) scene.add(Triangle(A, C, D, material=floormaterial)) # Add some spheres scene.add( Sphere(center=Vec3(-2.5, -2.5, 1.75), radius=1.75, material=sphere0material)) scene.add( Sphere(center=Vec3(2.5, -2.5, 1.75), radius=1.75, material=sphere1material)) scene.add( Sphere(center=Vec3(2.5, 2.5, 1.75), radius=1.75, material=sphere2material)) scene.add( Sphere(center=Vec3(-2.5, 2.5, 1.75), radius=1.75, material=sphere3material)) # Now tell the scene which camera we use scene.setCamera(camera) # Create a raytracer using "SimpleRT" engine = SimpleRT(shadow=True) # Render the scene: image = engine.render(scene) return image.data