def main_activity(w, h, obj_link, tga_link): faces_textures, vertex_normals, faces_vec_normals, vertices, texture_vertices, faces_coordinates = \ parsing_obj(obj_link) textures = np.array(Pi.open(tga_link)) image = np.zeros(shape=(h + 1, w + 1, 3)).astype(np.uint8) n_pass = scaling(np.array([[k[0], k[1], k[2]] for k in vertices]), w, h) z_array = [np.iinfo(np.int32).min for _ in range(h * w)] # vec_p - triangles index # vec_p1 - texture index # vec_p2 - norm vector index k = 0 for vec_p, vec_p1, vec_p2 in zip(faces_coordinates, faces_textures, faces_vec_normals): vec3_int = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] vec3_norm = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] vec_uv = [[0, 0], [0, 0], [0, 0]] for i in range(3): vec3_int[i] = [ h - 1 - n_pass[vec_p[i] - 1, 1], n_pass[vec_p[i] - 1, 0], n_pass[vec_p[i] - 1, 2] ] vec_uv[i] = [ int(texture_vertices[vec_p1[i] - 1][0] * len(textures)), int(texture_vertices[vec_p1[i] - 1][1] * len(textures)) ] vec3_norm[i] = [ vertex_normals[vec_p2[i] - 1][0], vertex_normals[vec_p2[i] - 1][1], vertex_normals[vec_p2[i] - 1][2] ] triangle_color( vec3_int[0], vec3_int[1], vec3_int[2], # triangles vec_uv[0], vec_uv[1], vec_uv[2], image, z_array, textures, vec3_norm[0], vec3_norm[1], vec3_norm[2], w, h) # textures k += 1 return np.uint8(image)
def default(cls): world = cls() world.add_light(PointLight(point(-10, 10, -10), color(1, 1, 1))) sphere1 = Sphere() mat = Material() mat.color = color(0.8, 1.0, 0.6) mat.diffuse = 0.7 mat.specular = 0.2 sphere1.material = mat sphere2 = Sphere() sphere2.set_transform(scaling(0.5, 0.5, 0.5)) world.objects.append(sphere1) world.objects.append(sphere2) return world
def main(): c = Canvas(500, 500) p = point(0, 0, 1) translate = translation(250, 0, 250) scale = scaling(100, 0, 100) for h in range(12): r = rotation_y(h * pi / 6) transform = multiply_matrix(translate, multiply_matrix(scale, r)) p2 = multiply_tuple(transform, p) print(f"position ({p2[0]}, {p2[1]}, {p2[2]})") c.set_pixel(round(p2[0]), c.height - round(p2[2]), color(0.0, 1.0, 0.0)) with open('clock.ppm', 'w') as out_file: out_file.write(c.to_ppm())
def step_impl(context, x, y, z): context.transform = scaling(x, y, z)
def step_assert_a_equals_scaling_matrix(context): expected = scaling(-1, 1, -1) assert context.a == expected, f"{context.a} is not {expected}"
def step_assert_transformation_matrix_of_object_at_index( context, index, tx, ty, tz, sx, sy, sz): assert_matrix( context.w.objects[index].transform(), multiply_matrix(scaling(sx, sy, sz), translation(tx, ty, tz)))
def step_set_transform_of_shape_to_scaling_matrix(context, shape_var, x, y, z): shape = getattr(context, shape_var, None) shape.set_transform(scaling(x, y, z))
def step_create_scaling_rotation_z_matrix_m(context): context.m = multiply_matrix(scaling(1, 0.5, 1), rotation_z(pi / 5))
from behave import given, then, when # pylint: disable=no-name-in-module from asserts import assert_float, assert_tuple from core import color, point, scaling, translation from scene import World from shapes import Plane, Sphere TRANSFORMATIONS = { 'scaling(0.5, 0.5, 0.5)': scaling(0.5, 0.5, 0.5), 'translation(0, 0, 10)': translation(0, 0, 10), 'translation(0, 0, 1)': translation(0, 0, 1), 'translation(0, -1, 0)': translation(0, -1, 0), 'translation(0, 1, 0)': translation(0, 1, 0), 'translation(0, -3.5, -0.5)': translation(0, -3.5, -0.5), 'translation(0, 0, -3)': translation(0, 0, -3), 'translation(5, 0, 0)': translation(5, 0, 0) } def set_shape_attributes(shape, table): for row in table: if row['variable'] == 'material.color' and row['value'] == '(1, 0, 0)': shape.material.color = color(1, 0, 0) if row['variable'] == 'material.ambient': shape.material.ambient = float(row['value']) if row['variable'] == 'material.transparency': shape.material.transparency = float(row['value']) if row['variable'] == 'material.reflective':
def render_scene_with_plane(): floor = Plane() floor.set_transform(scaling(10, 0.01, 10)) # floor.set_transform( # multiply_matrix(translation(0, 0.33, 0), scaling(10, 0.01, 10))) floor.material = Material() floor.material.color = color(1, 0.9, 0.9) floor.material.specular = 0 # left_wall = Plane() # left_wall.set_transform( # multiply_matrix( # translation(0, 0, 5), # multiply_matrix(rotation_y(-pi / 4), # multiply_matrix(rotation_x(pi / 2), scaling(10, 0.01, 10))))) # left_wall.material = floor.material # right_wall = Plane() # right_wall.set_transform( # multiply_matrix( # translation(0, 0, 5), # multiply_matrix(rotation_y(pi / 4), # multiply_matrix(rotation_x(pi / 2), scaling(10, 0.01, 10))))) middle = Sphere() middle.set_transform(translation(-0.5, 1, 0.5)) middle.material = Material() middle.material.pattern = StripePattern(color(0.6, 1.0, 0.6), color(0.3, 0.6, 0.3)) middle.material.color = color(0.1, 1, 0.5) middle.material.diffuse = 0.7 middle.material.specular = 0.3 middle.material.pattern.set_transform( multiply_matrix(scaling(0.2, 0.2, 0.2), rotation_y(pi / 4))) right = Sphere() right.set_transform( multiply_matrix(translation(1.5, 0.5, -0.5), scaling(0.5, 0.5, 0.5))) right.material = Material() right.material.color = color(0.5, 1, 0.1) # right.material.diffuse = 0.7 right.material.diffuse = 0.2 right.material.specular = 0.3 right.material.reflective = 0.7 left = Sphere() left.set_transform( multiply_matrix(translation(-1.5, 0.33, -0.75), scaling(0.33, 0.33, 0.33))) left.material = Material() left.material.color = color(1, 0.8, 0.1) left.material.diffuse = 0.7 left.material.specular = 0.3 world = World() world.add_light(PointLight(point(-10, 10, -10), color(1, 1, 1))) world.objects.append(floor) # world.objects.append(left_wall) # world.objects.append(right_wall) world.objects.append(middle) world.objects.append(right) world.objects.append(left) camera = Camera(600, 500, pi / 3) camera.set_transform( view_transform(point(0, 1.5, -5), point(0, 1, 0), vector(0, 1, 0))) # camera.set_transform( # view_transform(point(0, 4, -1), point(0, 1, 0), vector(0, 1, 0))) canvas = RayTracer().render(camera, world) ppm = canvas.to_ppm() outf = open('render_scene_with_plane.ppm', 'w') outf.write(ppm)
def step_set_transform_of_pattern_to_scaling_matrix(context, x, y, z): context.pattern.set_transform(scaling(x, y, z))
def step_set_transform_of_s_to_scaling_matrix(context, x, y, z): context.s.set_transform(scaling(x, y, z))
def step_assert_w_contains_s2(context): actual = context.w.objects[1] assert actual.transform() == scaling(0.5, 0.5, 0.5)
def step_impl(context, x, y, z): context.B = scaling(x, y, z)
from math import sqrt from behave import given, when, then # pylint: disable=no-name-in-module from asserts import assert_float, assert_tuple from core import point, vector from shapes import Material, Sphere, Shape from core import identity_matrix, multiply from core import translation, scaling, rotation_z from patterns_steps import TestPattern TRANSFORMATIONS = { 'scaling(2, 2, 2)': scaling(2, 2, 2), 'translation(0, 0, 0.25)': translation(0, 0, 0.25), 'translation(0, 0, -0.25)': translation(0, 0, -0.25), 'translation(0, 0, 1)': translation(0, 0, 1), } def glass_sphere(): sphere = Sphere() sphere.material.transparency = 1.0 sphere.material.refractive_index = 1.5 return sphere def set_sphere_attributes(sphere, table): for row in table: if row['variable'] == 'material.ambient': sphere.material.ambient = float(row['value']) print(f'ambient={sphere.material.ambient}') if row['variable'] == 'material.transparency':
def step_impl(context): context.transform = inverse(scaling(2, 3, 4))
def render_simple_scene(): floor = Sphere() floor.set_transform(scaling(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.set_transform( multiply_matrix( translation(0, 0, 5), multiply_matrix( rotation_y(-pi / 4), multiply_matrix(rotation_x(pi / 2), scaling(10, 0.01, 10))))) left_wall.material = floor.material right_wall = Sphere() right_wall.set_transform( multiply_matrix( translation(0, 0, 5), multiply_matrix( rotation_y(pi / 4), multiply_matrix(rotation_x(pi / 2), scaling(10, 0.01, 10))))) right_wall.material = floor.material middle = Sphere() middle.set_transform(translation(-0.5, 1, 0.5)) middle.material = Material() middle.material.color = color(0.1, 1, 0.5) middle.material.diffuse = 0.7 middle.material.specular = 0.3 right = Sphere() right.set_transform( multiply_matrix(translation(1.5, 0.5, -0.5), scaling(0.5, 0.5, 0.5))) right.material = Material() right.material.color = color(0.5, 1, 0.1) right.material.diffuse = 0.7 right.material.specular = 0.3 left = Sphere() left.set_transform( multiply_matrix(translation(-1.5, 0.33, -0.75), scaling(0.33, 0.33, 0.33))) left.material = Material() left.material.color = color(1, 0.8, 0.1) left.material.diffuse = 0.7 left.material.specular = 0.3 world = World() world.add_light(PointLight(point(-10, 10, -10), color(1, 1, 1))) world.objects.append(floor) world.objects.append(left_wall) world.objects.append(right_wall) world.objects.append(middle) world.objects.append(right) world.objects.append(left) camera = Camera(600, 500, pi / 3) camera.set_transform( view_transform(point(0, 1.5, -5), point(0, 1, 0), vector(0, 1, 0))) canvas = RayTracer().render(camera, world) with open('render_simple_scene.ppm', 'w') as out_file: out_file.write(canvas.to_ppm())