def test_add_example(self): c0 = Color(r=0, g=1, b=2) c1 = Color(r=3, g=4, b=5) actual = c0 + c1 assert actual.r == 3 assert actual.g == 5 assert actual.b == 7
def test_not_hit_example(self): color = Color(r=0, g=0, b=0) ray = Ray(origin=Vector3(x=0, y=0, z=0), direction=Vector3(x=1, y=1, z=0)) sphere = Sphere(center=Vector3(x=2, y=0, z=0), radius=1, color=color) result = sphere.hit(ray) assert result is None
def test_hit_example(self): color = Color(r=0, g=0, b=0) ray = Ray(origin=Vector3(x=0, y=0, z=0), direction=Vector3(x=1, y=0, z=0)) sphere = Sphere(center=Vector3(x=2, y=0, z=0), radius=1, color=color) result = sphere.hit(ray) assert result is not None assert result.shade_record.hit_point == Vector3(x=1, y=0, z=0) assert result.tmin == 1
def render(self) -> List[Color]: hres = self._view_plane.horizontal_resolution vres = self._view_plane.vertical_resolution pixel_size = self._view_plane.pixel_size colors = [None] * (hres * vres) # type: List[Color] num_samples = self._view_plane.sampler.num_samples for r in range(vres): for c in range(hres): color = Color(r=0, g=0, b=0) samples = self._view_plane.sampler.sample_unit_square(0) for i in range(num_samples): x = pixel_size * (c - 0.5 * hres + samples[i].x) y = pixel_size * (r - 0.5 * vres + samples[i].y) ray = Ray(origin=Vector3(x=x, y=y, z=100), direction=Vector3(x=0, y=0, z=-1)) color += self._tracer.trace_ray(ray, self) colors[r * hres + c] = color.div(num_samples) return colors
def main() -> None: print("Rendering...") width = 160 height = 120 world = World( view_plane=ViewPlane( hres=width, vres=height, pixel_size=0.5, gamma=1.0, # sampler=StandardSampler()), sampler=JitteredSampler(samples_per_axis=3, num_sets=1, random=random)), background_color=Color(r=0, g=0, b=0), tracer=Tracer(), scene_objects=[ Sphere(center=Vector3(x=-100, y=0, z=0), radius=70.0, color=Color(r=1, g=0, b=0)), Sphere(center=Vector3(x=00, y=0, z=0), radius=25.0, color=Color(r=0, g=1, b=0)), Sphere(center=Vector3(x=100, y=0, z=0), radius=70.0, color=Color(r=0, g=0, b=1)) ]) colors = world.render() bitmap = Bitmap(width=width, height=height, colors=colors) with open("out.bmp", "wb") as file: bitmap.write_to(file) print("Done")
def empty_image_view(self): stream = BytesIO() bitmap = Bitmap(width=1, height=1, colors=[Color(1, 0, 0)]) bitmap.write_to(stream) return stream.getbuffer()
def test_clamp_example(self): actual = Color(r=-0.0001, g=0.5, b=1.0001).clamp() assert actual.r == 0.0 assert actual.g == 0.5 assert actual.b == 1.0
def test_div_example(self): c = Color(r=1, g=2, b=4) actual = c.div(2) assert actual.r == 0.5 assert actual.g == 1 assert actual.b == 2