def render_moon(scene): light = Light(Color(1.0, 1.0, 1.0, 1.0), 1.0) light.transform.position = Vector3(0.0, 2.0, -2.0) scene.set_light(light) lambertianTintMaterial = Material() lambertianTintMaterial.albedo = Color(1.0, 1.0, 1.0, 1.0) lambertianTintMaterial.shader = LambertianTintShader() s1_earth = Sphere(0.6) s1_earth.transform.position = Vector3(0, 0, 1.5) s1_earth.material = lambertianTintMaterial.clone() scene.add_objects(s1_earth) s2_moon = Sphere(0.4) s2_moon.transform.position = Vector3(-0.2, -0.5, 1.2) s2_moon.material = lambertianTintMaterial.clone() s2_moon.material.set_texture(Texture("images/moon.jpg")) scene.add_objects(s2_moon) v1 = Vector3(0.0, 1.5, 0.5) v2 = Vector3(0.5, -1.0, 0.0) animation_velocity = 0.4 def update_method(t): p = Vector3(-0.2, -0.5, 1.2) p = p.add(v1.multiply(np.cos(animation_velocity * t * np.pi))) p = p.add(v2.multiply(np.sin(animation_velocity * t * np.pi))) s2_moon.transform.position = p s2_moon.transform.rotation = Vector3( 0.0, np.mod(0.5 * animation_velocity * t, 360), 0.0) return update_method
import math from camera import Camera from color import Color from light import PointLight from material import Material from sphere import Sphere from transformations import Transformations from tuple import Point, Vector from world import World if __name__ == '__main__': # The floor is an extremely flattened sphere with a matte texture floor = Sphere() floor.transform = Transformations.scaling(10, 0.1, 10) floor.material = Material() floor.material.color = Color(1, 0.9, 0.9) floor.material.specular = 0 # The wall on the left has the same scale and color as the floor, but is also rotated and translated into place left_wall = Sphere() left_wall.transform = Transformations.translation(0, 0, 5) left_wall.transform = left_wall.transform.dot( Transformations.rotation_y(-math.pi / 4)) left_wall.transform = left_wall.transform.dot( Transformations.rotation_x(math.pi / 2)) left_wall.transform = left_wall.transform.dot( Transformations.scaling(10, 0.1, 10)) left_wall.material = floor.material # The wall on the right is identical to the left wall, but is rotated the opposite direction in y
from point import Point from color import Color from ray import Ray from sphere import Sphere from material import Material from light import Light from time import time from world import World from camera import Camera if __name__ == '__main__': t1 = time() floor = Sphere() floor.transform = scale(10, 0.01, 10) floor.material = Material() floor.material.color = Color(1, 0.9, 0.9) floor.material.specular = 0 left_wall = Sphere() left_wall.transform = translate(0, 0, 5) * \ rotate_y(-pi/4) * \ rotate_x(pi/2) * \ scale(10, 0.01, 10) left_wall.material = floor.material right_wall = Sphere() right_wall.transform = translate(0, 0, 5) * \ rotate_y(pi/4) * \ rotate_x(pi/2) * \ scale(10, 0.01, 10)
def parse(self, filename): with open(filename) as f: for s in f.readlines(): if not s or s == '' or s[0] == '\n' or s[0] == '#': continue s = s.split() if s[0] == 'size': width, height = map(int, s[1:]) self.scene.film.init_image(width, height) self.scene.sampler.tile_width = width self.scene.sampler.tile_height = height elif s[0] == 'maxdepth': max_depth = int(s[1]) self.scene.ray_tracer.max_depth = max_depth elif s[0] == 'output': out_filename = s[1] self.scene.film.filename = out_filename elif s[0] == 'camera': eye_x, eye_y, eye_z, look_at_x, look_at_y, look_at_z, upx, upy, upz, fov = map(float, s[1:]) self.scene.camera.eye = np.array([eye_x, eye_y, eye_z, 1]) self.scene.camera.look_at = np.array([look_at_x, look_at_y, look_at_z, 1]) self.scene.camera.up = np.array([upx, upy, upz, 0]) self.scene.camera.fov = fov elif s[0] == 'sphere': x, y, z, radius = map(float, s[1:]) sphere = Sphere(x, y, z, radius) sphere.set_transformation(copy.copy(self._top())) sphere.material = copy.copy(self._material) self.scene.ray_tracer.primitives.append(sphere) elif s[0] == 'vertex': x, y, z = map(float, s[1:]) self._vertex.append(np.array([x, y, z, 1])) elif s[0] == 'tri': v1, v2, v3 = map(int, s[1:]) tri = Triangle(self._vertex[v1], self._vertex[v2], self._vertex[v3]) tri.set_transformation(copy.copy(self._top())) tri.material = copy.copy(self._material) self.scene.ray_tracer.primitives.append(tri) elif s[0] == 'translate': x, y, z = map(float, s[1:]) self._transform_stack[-1] = np.matmul(self._transform_stack[-1], transform.translate(x, y, z)) elif s[0] == 'rotate': x, y, z, angle = map(float, s[1:]) a = np.array([x, y, z]) r = np.eye(4) r[0:3, 0:3] = transform.rotate(angle, a) self._transform_stack[-1] = np.matmul(self._transform_stack[-1], r) elif s[0] == 'scale': x, y, z = map(float, s[1:]) self._transform_stack[-1] = np.matmul(self._transform_stack[-1], transform.scale(x, y, z)) elif s[0] == 'pushTransform': self._push() elif s[0] == 'popTransform': self._pop() elif s[0] == 'directional': x, y, z, r, g, b = map(float, s[1:]) l = DirectionalLight(np.array([x, y, z, 0]), np.array([r, g, b])) self.scene.ray_tracer.lights.append(l) elif s[0] == 'point': x, y, z, r, g, b = map(float, s[1:]) l = PointLight(np.array([x, y, z, 1]), np.array([r, g, b])) l.attenuation = copy.copy(self._attenuation) self.scene.ray_tracer.lights.append(l) elif s[0] == 'attenuation': const, linear, quadratic = map(float, s[1:]) self._attenuation = np.array([const, linear, quadratic]) elif s[0] == 'ambient': r, g, b = map(float, s[1:]) self._material['ambient'] = np.array([r, g, b]) elif s[0] == 'diffuse': r, g, b = map(float, s[1:]) self._material['diffuse'] = np.array([r, g, b]) elif s[0] == 'specular': r, g, b = map(float, s[1:]) self._material['specular'] = np.array([r, g, b]) elif s[0] == 'shininess': shininess = float(s[1]) self._material['shininess'] = shininess elif s[0] == 'emission': r, g, b = map(float, s[1:]) self._material['emission'] = np.array([r, g, b]) return self.scene
from ray import * from sphere import Sphere from intersect import * from lights import * from world import * import pygame as pg import timeit from camera import Camera w = World() floor = Sphere(transform=scaling(10, 0.01, 10)) floor.material = Material(Vector(1, 0.9, 0.9), specular=0) left_wall = Sphere(transform=mult( translation(0, 0, 5), mult(rotationY(-np.pi / 4), mult(rotationX(np.pi / 2), scaling(10, 0.01, 10))))) left_wall.material = floor.material right_wall = Sphere(transform=mult( translation(0, 0, 5), mult(rotationY(np.pi / 4), mult(rotationX(np.pi / 2), scaling(10, 0.01, 10))))) right_wall.material = floor.material middle = Sphere( transform=mult(translation(1.5, 0.5, -0.5), scaling(0.5, .5, .5))) middle.material = Material(Vector(0.5, 0.9, 0.75), diffuse=0.5)
if __name__ == '__main__': ray_origin = Point(0, 0, -5) wall_z = 10 wall_size = 7 canvas_pixels = 100 pixel_size = wall_size / canvas_pixels half = wall_size / 2 canvas = Canvas(canvas_pixels, canvas_pixels) color = Color(1, 0, 0) shape = Sphere() shape.material = Material() shape.material.color = Color(1, 0.2, 1) light_position = Point(-10, 10, -10) light_color = Color(1, 1, 1) light = PointLight(light_position, light_color) for y in range(canvas_pixels): world_y = half - pixel_size * y for x in range(canvas_pixels): world_x = -half + pixel_size * x position = Point(world_x, world_y, wall_z) r = Ray(ray_origin, Vector.normalize(position - ray_origin))
from sphere import Sphere from material import Material from light import Light from time import time if __name__ == '__main__': t1 = time() pixel_size = 100 WIDTH = 100 HEIGHT = 100 c = Canvas(WIDTH, HEIGHT, Color(0.5, 0.5, 0.5)) sphere = Sphere(Point(0, 0, 0), radius=1) m = Material() m.color = Color(1, 0.2, 1) sphere.material = m light = Light(Point(-10, 10, -10), Color(1, 1, 1)) # sphere.set_transform(rotate_z(pi/6)* scale(1, 0.5, 1)*shear(1, 0, 0, 0, 0, 0)) camera = Point(0, 0, -5) wall_z = 10 wall_size = 7 canvas_pixels = 100 pixel_size = wall_size / canvas_pixels half = wall_size / 2 for j in range(HEIGHT): print(j) y = half - pixel_size * j for i in range(WIDTH): x = -half + pixel_size*i target = Point(x, y, wall_z)
def test_material2(self): s = Sphere() m = s.material m.ambient = 1 s.material = m self.assertTrue(s.material == m)