def demo(width, height, angle_deg, orthogonal, pfm_output, png_output): image = HdrImage(width, height) # Create a world and populate it with a few shapes world = World() for x in [-0.5, 0.5]: for y in [-0.5, 0.5]: for z in [-0.5, 0.5]: world.add( Sphere(transformation=translation(Vec(x, y, z)) * scaling(Vec(0.1, 0.1, 0.1)))) # Place two other balls in the bottom/left part of the cube, so # that we can check if there are issues with the orientation of # the image world.add( Sphere(transformation=translation(Vec(0.0, 0.0, -0.5)) * scaling(Vec(0.1, 0.1, 0.1)))) world.add( Sphere(transformation=translation(Vec(0.0, 0.5, 0.0)) * scaling(Vec(0.1, 0.1, 0.1)))) # Initialize a camera camera_tr = rotation_z(angle_deg=angle_deg) * translation( Vec(-1.0, 0.0, 0.0)) if orthogonal: camera = OrthogonalCamera(aspect_ratio=width / height, transformation=camera_tr) else: camera = PerspectiveCamera(aspect_ratio=width / height, transformation=camera_tr) # Run the ray-tracer tracer = ImageTracer(image=image, camera=camera) def compute_color(ray: Ray) -> Color: if world.ray_intersection(ray): return WHITE else: return BLACK tracer.fire_all_rays(compute_color) # Save the HDR image with open(pfm_output, "wb") as outf: image.write_pfm(outf) print(f"HDR demo image written to {pfm_output}") # Apply tone-mapping to the image image.normalize_image(factor=1.0) image.clamp_image() # Save the LDR image with open(png_output, "wb") as outf: image.write_ldr_image(outf, "PNG") print(f"PNG demo image written to {png_output}")
def test_clamp_image(self): img = HdrImage(2, 1) img.set_pixel(0, 0, Color(0.5e1, 1.0e1, 1.5e1)) img.set_pixel(1, 0, Color(0.5e3, 1.0e3, 1.5e3)) img.clamp_image() for cur_pixel in img.pixels: assert (cur_pixel.r >= 0) and (cur_pixel.r <= 1) assert (cur_pixel.g >= 0) and (cur_pixel.g <= 1) assert (cur_pixel.b >= 0) and (cur_pixel.b <= 1)
def demo(width, height, algorithm, pfm_output, png_output, num_of_rays, max_depth, init_state, init_seq, samples_per_pixel, declare_float, input_scene_name): samples_per_side = int(sqrt(samples_per_pixel)) if samples_per_side**2 != samples_per_pixel: print( f"Error, the number of samples per pixel ({samples_per_pixel}) must be a perfect square" ) return variables = build_variable_table(declare_float) with open(input_scene_name, "rt") as f: try: scene = parse_scene(input_file=InputStream( stream=f, file_name=input_scene_name), variables=variables) except GrammarError as e: loc = e.location print(f"{loc.file_name}:{loc.line_num}:{loc.col_num}: {e.message}") sys.exit(1) image = HdrImage(width, height) print(f"Generating a {width}×{height} image") # Run the ray-tracer tracer = ImageTracer(image=image, camera=scene.camera, samples_per_side=samples_per_side) if algorithm == "onoff": print("Using on/off renderer") renderer = OnOffRenderer(world=scene.world, background_color=BLACK) elif algorithm == "flat": print("Using flat renderer") renderer = FlatRenderer(world=scene.world, background_color=BLACK) elif algorithm == "pathtracing": print("Using a path tracer") renderer = PathTracer( world=scene.world, pcg=PCG(init_state=init_state, init_seq=init_seq), num_of_rays=num_of_rays, max_depth=max_depth, ) elif algorithm == "pointlight": print("Using a point-light tracer") renderer = PointLightRenderer(world=scene.world, background_color=BLACK) else: print(f"Unknown renderer: {algorithm}") sys.exit(1) def print_progress(row, col): print(f"Rendering row {row + 1}/{image.height}\r", end="") start_time = process_time() tracer.fire_all_rays(renderer, callback=print_progress) elapsed_time = process_time() - start_time print(f"Rendering completed in {elapsed_time:.1f} s") # Save the HDR image with open(pfm_output, "wb") as outf: image.write_pfm(outf) print(f"HDR demo image written to {pfm_output}") # Apply tone-mapping to the image image.normalize_image(factor=1.0) image.clamp_image() # Save the LDR image with open(png_output, "wb") as outf: image.write_ldr_image(outf, "PNG") print(f"PNG demo image written to {png_output}")