Exemple #1
0
    def __init__(self):

        # setup programs
        use_texture = False
        self.closest_hit_program = Program(
            "phong.cu", "closest_hit_radiance_textured"
            if use_texture else "closest_hit_radiance")
        self.any_hit_program = Program("phong.cu", "any_hit_shadow")
        pgram_bounding_box = Program('triangle_mesh.cu', 'mesh_bounds')
        pgram_intersection = Program('triangle_mesh.cu', 'mesh_intersect')

        # setup material and geometry
        self.material = Material(closest_hit={0: self.closest_hit_program},
                                 any_hit={1: self.any_hit_program})
        self.geometry = Geometry(bounding_box_program=pgram_bounding_box,
                                 intersection_program=pgram_intersection)

        # init buffers
        self.n_vertices = 0
        self.n_triangles = 0
        self.positions_buffer = Buffer.from_array([],
                                                  dtype=np.dtype('f4, f4, f4'),
                                                  buffer_type='i')
        self.tri_indices = Buffer.from_array([],
                                             dtype=np.dtype('i4, i4, i4'),
                                             buffer_type='i')
        self.normals_buffer = Buffer.from_array([],
                                                dtype=np.dtype('f4, f4, f4'),
                                                buffer_type='i')
        self.texcoord_buffer = Buffer.from_array([],
                                                 dtype=np.dtype('i4, i4'),
                                                 buffer_type='i')
        self.material_buffer = Buffer.from_array([],
                                                 dtype=np.dtype('i4'),
                                                 buffer_type='i')
Exemple #2
0
def create_context():
    context = Context()

    context.set_ray_type_count(2)
    context.set_entry_point_count(1)
    context.set_stack_size(4640)

    context['max_depth'] = np.array(100, dtype=np.int32)
    context['radiance_ray_type'] = np.array(0, dtype=np.uint32)
    context['shadow_ray_type'] = np.array(1, dtype=np.uint32)
    context['scene_epsilon'] = np.array(1e-4, dtype=np.float32)
    context['importance_cutoff'] = np.array(0.01, dtype=np.float32)
    context['ambient_light_color'] = np.array([0.31, 0.33, 0.28], dtype=np.float32)

    context['output_buffer'] = Buffer.empty((height, width, 4), dtype=np.uint8, buffer_type='o', drop_last_dim=True)

    # Ray generation program
    camera_name = "env_camera" if tutorial_number >= 11 else "pinhole_camera"
    ray_generation_program = Program(tutorial_file, camera_name)

    # Exception program
    exception_program = Program(tutorial_file, "exception")

    # Miss program
    miss_name = "envmap_miss" if tutorial_number >= 5 else "miss"
    miss_program = Program(tutorial_file, miss_name)

    entry_point = EntryPoint(ray_generation_program, exception_program, miss_program)
    context['sqrt_num_samples'] = np.array(2, dtype=np.uint32)
    context['bad_color'] = np.array([1., 1., 1.], dtype=np.float32)
    context['bg_color'] = np.array([0.34, 0.55, 0.85], dtype=np.float32)

    hdr_image = load_hdr("../data/CedarCity.hdr")
    hdr_image = np.flip(hdr_image, axis=0)
    texture = np.zeros((hdr_image.shape[0], hdr_image.shape[1], 4), np.float32)
    texture[:, :, :3] = hdr_image
    tex_buffer = Buffer.from_array(texture, buffer_type='i', drop_last_dim=True)
    tex_sampler = TextureSampler(tex_buffer, wrap_mode='repeat', indexing_mode='normalized_coordinates',
                                 read_mode='normalized_float', filter_mode='linear')
    context['envmap'] = tex_sampler

    noise = np.random.uniform(0, 1, 64*64*64).astype(np.float32)
    tex_buffer = Buffer.from_array(noise.reshape(64, 64, 64), buffer_type='i', drop_last_dim=False)
    noise_sampler = TextureSampler(tex_buffer, wrap_mode='repeat', indexing_mode='normalized_coordinates',
                                   read_mode='normalized_float', filter_mode='linear')
    context["noise_texture"] = noise_sampler

    return context, entry_point
Exemple #3
0
    def load_from_file(self, filename):
        mesh = trimesh.load(filename)
        self.bbox_min = mesh.bounding_box.bounds[0]
        self.bbox_max = mesh.bounding_box.bounds[1]
        self.n_triangles = mesh.faces.shape[0]
        self.n_vertices = mesh.vertices.shape[0]
        self.positions_buffer = Buffer.from_array(
            [x.tobytes() for x in mesh.vertices.astype(np.float32)],
            buffer_type='i')
        self.tri_indices = Buffer.from_array(
            [x.tobytes() for x in mesh.faces.astype(np.int32)],
            buffer_type='i')
        self.normals_buffer = Buffer.from_array(
            [x.tobytes() for x in mesh.vertex_normals.astype(np.float32)],
            buffer_type='i')
        if "vertex_texture" in mesh.metadata:
            self.texcoord_buffer = Buffer.from_array([
                x.tobytes()
                for x in mesh.metadata["vertex_texture"].astype(np.int32)
            ],
                                                     buffer_type='i')

        # setup material
        self.Kd = np.array([0.7, 0.7, 0.7], np.float32)
        self.Ks = np.array([0, 0, 0], np.float32)
        self.Kr = np.array([0, 0, 0], np.float32)
        self.Ka = np.array([0, 0, 0], np.float32)
        self.exp = np.zeros(1, np.float32)

        self.material["Kd_mapped"] = np.zeros(1, np.int32)
        self.material["Kd"] = self.Kd
        self.material["Ks"] = self.Ks
        self.material["Kr"] = self.Kr
        self.material["Ka"] = self.Ka
        self.material["exp"] = self.exp

        self.material_indices = np.zeros(self.n_triangles, np.int32)
        self.material_buffer = Buffer.from_array(self.material_indices,
                                                 dtype=np.dtype('i4'),
                                                 buffer_type='i')

        self.geometry.set_primitive_count(self.n_triangles)
        self.geometry["vertex_buffer"] = self.positions_buffer
        self.geometry["index_buffer"] = self.tri_indices
        self.geometry["normal_buffer"] = self.normals_buffer
        self.geometry["texcoord_buffer"] = self.texcoord_buffer
        self.geometry["material_buffer"] = self.material_buffer
        self.geometry_instance = GeometryInstance(self.geometry, self.material)
Exemple #4
0
def create_context():
    context = Context()

    context.set_ray_type_count(1)
    context['radiance_ray_type'] = np.array(0, dtype=np.uint32)
    context['scene_epsilon'] = np.array(1e-4, dtype=np.float32)
    context['output_buffer'] = Buffer.empty((height, width, 4),
                                            dtype=np.uint8,
                                            buffer_type='o',
                                            drop_last_dim=True)
    entry_point = EntryPoint(Program('pinhole_camera.cu', 'pinhole_camera'),
                             Program('pinhole_camera.cu', 'exception'))

    cam_eye = [0.0, 0.0, 5.0]
    lookat = [0.0, 0.0, 0.0]
    up = [0.0, 1.0, 0.0]
    hfov = 60.0
    aspect_ratio = width / height
    camera_u, camera_v, camera_w = calculate_camera_variables(
        cam_eye, lookat, up, hfov, aspect_ratio, True)

    context['eye'] = np.array(cam_eye, dtype=np.float32)
    context['U'] = np.array(camera_u, dtype=np.float32)
    context['V'] = np.array(camera_v, dtype=np.float32)
    context['W'] = np.array(camera_w, dtype=np.float32)

    context['bad_color'] = np.array([1.0, 0.0, 1.0], dtype=np.float32)
    context.set_miss_program(0, Program('constantbg.cu', 'miss'))
    context['bg_color'] = np.array([0.2, 0.1, 0.3], dtype=np.float32)

    return context, entry_point
Exemple #5
0
def create_context():
    context = Context()

    context.set_ray_type_count(2)
    context.set_entry_point_count(1)
    context.set_stack_size(1800)

    context['scene_epsilon'] = np.array(1e-3, dtype=np.float32)
    context['pathtrace_ray_type'] = np.array(0, dtype=np.uint32)
    context['pathtrace_shadow_ray_type'] = np.array(1, dtype=np.uint32)
    context['rr_begin_depth'] = np.array(1, dtype=np.uint32)

    context['output_buffer'] = Buffer.empty((height, width, 4),
                                            dtype=np.float32,
                                            buffer_type='o',
                                            drop_last_dim=True)
    Program('optixPathTracer.cu', 'pathtrace_camera')
    entry_point = EntryPoint(Program('optixPathTracer.cu', 'pathtrace_camera'),
                             Program('optixPathTracer.cu', 'exception'),
                             Program('optixPathTracer.cu', 'miss'))
    context['sqrt_num_samples'] = np.array(2, dtype=np.uint32)
    context['bad_color'] = np.array([1000000., 0., 1000000.], dtype=np.float32)
    context['bg_color'] = np.zeros(3, dtype=np.float32)

    return context, entry_point
Exemple #6
0
def create_context():
    context = Context()

    context.set_ray_type_count(1)
    context['radiance_ray_type'] = np.array(0, dtype=np.uint32)
    context['scene_epsilon'] = np.array(1e-4, dtype=np.float32)
    context['output_buffer'] = Buffer.empty((height, width, 4), dtype=np.uint8, buffer_type='o', drop_last_dim=True)
    entry_point = EntryPoint(Program('pinhole_camera.cu', 'pinhole_camera'),
                             Program('pinhole_camera.cu', 'exception'))

    cam_eye = [0.0, 0.0, 5.0]
    lookat = [0.0, 0.0, 0.0]
    up = [0.0, 1.0, 0.0]
    hfov = 60.0
    aspect_ratio = width / height
    camera_u, camera_v, camera_w = calculate_camera_variables(cam_eye, lookat, up, hfov, aspect_ratio, True)

    context['eye'] = np.array(cam_eye, dtype=np.float32)
    context['U'] = np.array(camera_u, dtype=np.float32)
    context['V'] = np.array(camera_v, dtype=np.float32)
    context['W'] = np.array(camera_w, dtype=np.float32)

    context['bad_color'] = np.array([1.0, 0.0, 1.0], dtype=np.float32)
    context.set_miss_program(0, Program('constantbg.cu', 'miss'))
    context['bg_color'] = np.array([0.2, 0.1, 0.3], dtype=np.float32)

    return context, entry_point
Exemple #7
0
def create_light(context):
    basic_lights = np.zeros(1,
                            dtype=[('pos', ('<f4', 3)), ('color', ('<f4', 3)), ('cast_shadow', '<u4'), ('padd', '<u4')])
    basic_lights["pos"] = [[-5., 60., -16.0]]
    basic_lights["color"] = [[1., 1., 1.]]
    basic_lights["cast_shadow"] = [1]

    light_buffer = Buffer.from_array([x.tobytes() for x in basic_lights], buffer_type='i')
    context["lights"] = light_buffer
Exemple #8
0
def main():
    tex_width = 64
    tex_height = 64

    trace_width = 512
    trace_height = 384

    context = Context()

    tex_data = []
    for j in range(tex_height):
        tex_data.append([])
        for i in range(tex_width):
            tex_data[j].append([(i + j) / (tex_width + tex_height) * 255,
                                i / tex_width * 255, j / tex_height * 255,
                                255])

    tex_buffer = Buffer.from_array(np.array(tex_data, dtype=np.uint8),
                                   buffer_type='i',
                                   drop_last_dim=True)
    tex_sampler = TextureSampler(tex_buffer,
                                 wrap_mode='clamp_to_edge',
                                 indexing_mode='normalized_coordinates',
                                 read_mode='normalized_float',
                                 filter_mode='linear')

    context['input_texture'] = tex_sampler

    context['result_buffer'] = Buffer.empty((trace_height, trace_width, 4),
                                            dtype=np.float32,
                                            buffer_type='o',
                                            drop_last_dim=True)

    entry_point = EntryPoint(Program('draw_texture.cu', 'draw_texture'),
                             Program('draw_texture.cu', 'exception'))

    entry_point.launch((trace_width, trace_height))

    result_array = context['result_buffer'].to_array()
    result_array *= 255
    result_array = result_array.astype(np.uint8)
    result_image = Image.fromarray(result_array)
    ImageWindow(result_image)
Exemple #9
0
def main():
    tex_width = 64
    tex_height = 64

    trace_width = 512
    trace_height = 384

    context = Context()

    tex_data = []
    for j in range(tex_height):
        tex_data.append([])
        for i in range(tex_width):
            tex_data[j].append([
                (i + j) / (tex_width + tex_height) * 255,
                i / tex_width * 255,
                j / tex_height * 255,
                255
            ])

    tex_buffer = Buffer.from_array(np.array(tex_data, dtype=np.uint8), buffer_type='i', drop_last_dim=True)
    tex_sampler = TextureSampler(tex_buffer, wrap_mode='clamp_to_edge', indexing_mode='normalized_coordinates',
                                 read_mode='normalized_float', filter_mode='linear')

    context['input_texture'] = tex_sampler

    context['result_buffer'] = Buffer.empty((trace_height, trace_width, 4), dtype=np.float32,
                                            buffer_type='o', drop_last_dim=True)

    entry_point = EntryPoint(Program('draw_texture.cu', 'draw_texture'),
                             Program('draw_texture.cu', 'exception'))

    entry_point.launch((trace_width, trace_height))

    result_array = context['result_buffer'].to_array()
    result_array *= 255
    result_array = result_array.astype(np.uint8)
    result_image = Image.fromarray(result_array)
    ImageWindow(result_image)
Exemple #10
0
def create_random_buffer(max_width, max_height):
    scale = randf()
    w = int(max(max_width * scale, 1))
    h = int(max(max_height * scale, 1))

    arr = []
    red, green, blue = randf(), randf(), randf()

    for y in range(h):
        arr.append([])
        for x in range(w):
            if randf() < 0.1:
                arr[y].append([red * 255.0, green * 255.0, blue * 255.0, 255])
            else:
                arr[y].append([255, 255, 255, 0])

    return Buffer.from_array(np.array(arr, dtype=np.uint8), buffer_type='i', drop_last_dim=True)
Exemple #11
0
def create_context():
    context = Context()

    context.set_ray_type_count(2)
    context.set_stack_size(1200)
    context.set_print_enabled(True)
    context.set_all_exceptions_enabled(True)

    # here pyoptix won't be able to deduce types of these variables,
    # so we must put them inside numpy arrays with proper dtypes
    context['max_depth'] = np.array(5, dtype=np.int32)
    context['radiance_ray_type'] = np.array(0, dtype=np.uint32)
    context['shadow_ray_type'] = np.array(1, dtype=np.uint32)
    context['scene_epsilon'] = np.array(1e-4, dtype=np.float32)

    context['output_buffer'] = Buffer.empty((height, width, 4),
                                            dtype=np.uint8,
                                            buffer_type='o',
                                            drop_last_dim=True)

    cam_eye = [2.0, 1.5, -2.0]
    lookat = [0.0, 1.2, 0.0]
    up = [0.0, 1.0, 0.0]
    hfov = 60.0
    aspect_ratio = width / height
    camera_u, camera_v, camera_w = calculate_camera_variables(
        cam_eye, lookat, up, hfov, aspect_ratio)

    context['eye'] = np.array(cam_eye, dtype=np.float32)
    context['U'] = np.array(camera_u, dtype=np.float32)
    context['V'] = np.array(camera_v, dtype=np.float32)
    context['W'] = np.array(camera_w, dtype=np.float32)

    ray_gen_program = Program('pinhole_camera.cu', 'pinhole_camera')
    exception_program = Program('pinhole_camera.cu', 'exception')
    entry_point = EntryPoint(ray_gen_program, exception_program)

    context['bad_color'] = np.array([0, 1, 1], dtype=np.float32)

    context.set_miss_program(0, Program('constantbg.cu', 'miss'))
    context['bg_color'] = np.array([0.4, 0.33, 0.21], dtype=np.float32)

    return context, entry_point
Exemple #12
0
def create_context():
    context = Context()

    context.set_ray_type_count(2)
    context.set_entry_point_count(1)
    context.set_stack_size(1800)

    context['scene_epsilon'] = np.array(1e-4, dtype=np.float32)
    context['radiance_ray_type'] = np.array(0, dtype=np.uint32)
    context['shadow_ray_type'] = np.array(1, dtype=np.uint32)

    context['output_buffer'] = Buffer.empty((height, width, 4), dtype=np.uint8, buffer_type='o', drop_last_dim=True)
    entry_point = EntryPoint(Program('pinhole_camera.cu', 'pinhole_camera'),
                             Program('pinhole_camera.cu', 'exception'),
                             Program('constantbg.cu', 'miss'))
    context['bad_color'] = np.array([1., 0., 1.], dtype=np.float32)
    context['bg_color'] = np.array([0.34, 0.55, 0.85], dtype=np.float32)

    return context, entry_point
Exemple #13
0
def create_geometry(context):
    global mesh_g
    mesh_g = OptixMesh()
    mesh_g.load_from_file("../data/cow.obj")
    basic_lights = np.zeros(3,
                            dtype=[('pos', ('<f4', 3)), ('color', ('<f4', 3)), ('cast_shadow', '<u4'), ('padd', '<u4')])
    basic_lights["pos"] = [[-0.5, 0.25, -1.0],
                           [-0.5, 0.0, 1.0],
                           [0.5, 0.5, 0.5]]
    basic_lights["color"] = [[0.2, 0.2, 0.25],
                             [0.1, 0.1, 0.1],
                             [0.7, 0.7, 0.65]]
    basic_lights["cast_shadow"] = [0, 0, 1]

    light_buffer = Buffer.from_array([x.tobytes() for x in basic_lights], buffer_type='i')
    context["lights"] = light_buffer

    group = GeometryGroup(children=([mesh_g.geometry_instance]))
    group.set_acceleration(Acceleration("Trbvh"))
    context['top_object'] = group
    context['top_shadower'] = group
Exemple #14
0
def main():
    width = 512
    height = 384

    context = Context()

    context.set_ray_type_count(1)

    context['result_buffer'] = Buffer.empty((height, width, 4), buffer_type='o', dtype=np.float32, drop_last_dim=True)

    ray_gen_program = Program('draw_color.cu', 'draw_solid_color')

    ray_gen_program['draw_color'] = np.array([0.462, 0.725, 0.0], dtype=np.float32)

    entry_point = EntryPoint(ray_gen_program)
    entry_point.launch(size=(width, height))

    result_array = context['result_buffer'].to_array()
    result_array *= 255
    result_image = Image.fromarray(result_array.astype(np.uint8)[:, :, :3])

    ImageWindow(result_image)
Exemple #15
0
def create_context():
    context = Context()

    context.set_ray_type_count(2)
    context.set_stack_size(1200)
    context.set_print_enabled(True)
    context.set_all_exceptions_enabled(True)

    # here pyoptix won't be able to deduce types of these variables,
    # so we must put them inside numpy arrays with proper dtypes
    context['max_depth'] = np.array(5, dtype=np.int32)
    context['radiance_ray_type'] = np.array(0, dtype=np.uint32)
    context['shadow_ray_type'] = np.array(1, dtype=np.uint32)
    context['scene_epsilon'] = np.array(1e-4, dtype=np.float32)

    context['output_buffer'] = Buffer.empty((height, width, 4), dtype=np.uint8, buffer_type='o', drop_last_dim=True)

    cam_eye = [2.0, 1.5, -2.0]
    lookat = [0.0, 1.2, 0.0]
    up = [0.0, 1.0, 0.0]
    hfov = 60.0
    aspect_ratio = width / height
    camera_u, camera_v, camera_w = calculate_camera_variables(cam_eye, lookat, up, hfov, aspect_ratio)

    context['eye'] = np.array(cam_eye, dtype=np.float32)
    context['U'] = np.array(camera_u, dtype=np.float32)
    context['V'] = np.array(camera_v, dtype=np.float32)
    context['W'] = np.array(camera_w, dtype=np.float32)

    ray_gen_program = Program('pinhole_camera.cu', 'pinhole_camera')
    exception_program = Program('pinhole_camera.cu', 'exception')
    entry_point = EntryPoint(ray_gen_program, exception_program)

    context['bad_color'] = np.array([0, 1, 1], dtype=np.float32)

    context.set_miss_program(0, Program('constantbg.cu', 'miss'))
    context['bg_color'] = np.array([0.4, 0.33, 0.21], dtype=np.float32)

    return context, entry_point
Exemple #16
0
def main():
    width = 512
    height = 384

    context = Context()

    context.set_ray_type_count(1)

    context['output_buffer'] = Buffer.empty((height, width, 4),
                                            buffer_type='o',
                                            dtype=np.float32,
                                            drop_last_dim=True)

    ray_gen_program = Program('draw_color.cu', 'draw_solid_color')

    ray_gen_program['draw_color'] = np.array([0.462, 0.725, 0.0],
                                             dtype=np.float32)

    entry_point = EntryPoint(ray_gen_program)
    entry_point.launch(size=(width, height))

    window = ImageWindowBase(context, width, height)
    window.run()
Exemple #17
0
def create_scene(context, num_buffers):
    # Sphere
    sphere = Geometry(Program('sphere_texcoord.cu', 'bounds'),
                      Program('sphere_texcoord.cu', 'intersect'))
    sphere.set_primitive_count(1)
    sphere['sphere'] = np.array([0.0, 1.2, 0.0, 1.0], dtype=np.float32)
    sphere['matrix_row_0'] = np.array([1.0, 0.0, 0.0], dtype=np.float32)
    sphere['matrix_row_1'] = np.array([0.0, 1.0, 0.0], dtype=np.float32)
    sphere['matrix_row_2'] = np.array([0.0, 0.0, 1.0], dtype=np.float32)

    # Floor
    parallelogram = Geometry(Program('parallelogram.cu', 'bounds'),
                             Program('parallelogram.cu', 'intersect'))
    parallelogram.set_primitive_count(1)
    anchor = np.array([-20.0, 0.01, 20.0], dtype=np.float32)
    v1 = np.array([40.0, 0.0, 0.0], dtype=np.float32)
    v2 = np.array([0.0, 0.0, -40.0], dtype=np.float32)
    normal = np.cross(v1, v2)
    normal /= np.linalg.norm(normal)
    d = np.dot(normal, anchor)
    v1 *= 1 / np.dot(v1, v1)
    v2 *= 1 / np.dot(v2, v2)
    plane = np.append(normal, d)
    parallelogram['plane'] = plane
    parallelogram['v1'] = v1
    parallelogram['v2'] = v2
    parallelogram['anchor'] = anchor

    # Sphere material
    sphere_matl = Material(
        closest_hit={
            0: Program('optixBuffersOfBuffers.cu', 'closest_hit_radiance')
        },
        any_hit={1: Program('optixBuffersOfBuffers.cu', 'any_hit_shadow')})

    buffers = [
        create_random_buffer(MAX_BUFFER_WIDTH, MAX_BUFFER_HEIGHT)
        for _ in range(num_buffers)
    ]

    # mark buffers as bindless so that they won't be destroyed when `buffers` list is garbage-collected
    for buffer in buffers:
        buffer.bindless = True

    sphere_matl['Kd_layers'] = Buffer.from_array(
        [buf.get_id() for buf in buffers], dtype=np.int32, buffer_type='i')

    # Floor material
    floor_matl = Material(
        closest_hit={0: Program('phong.cu', 'closest_hit_radiance')},
        any_hit={1: Program('phong.cu', 'any_hit_shadow')})
    floor_matl['Kd'] = np.array([0.7, 0.7, 0.7], dtype=np.float32)
    floor_matl['Ka'] = np.array([1.0, 1.0, 1.0], dtype=np.float32)
    floor_matl['Kr'] = np.array([0.0, 0.0, 0.0], dtype=np.float32)
    floor_matl['phong_exp'] = np.array(1.0, dtype=np.float32)

    # Place geometry into hierarchy
    geometrygroup = GeometryGroup()
    geometrygroup.add_child(GeometryInstance(sphere, sphere_matl))
    geometrygroup.add_child(GeometryInstance(parallelogram, floor_matl))
    acc = Acceleration('Sbvh', 'Bvh')
    geometrygroup.set_acceleration(acc)

    context['top_object'] = geometrygroup
    context['top_shadower'] = geometrygroup

    # Lights
    context['ambient_light_color'] = np.array([0.1, 0.1, 0.1],
                                              dtype=np.float32)
    lights = [
        np.array(BasicLight([0.0, 8.0, -5.0], [0.4, 0.4, 0.4], True)),
        np.array(BasicLight([5.0, 8.0, 0.0], [0.4, 0.4, 0.4], True)),
    ]
    context["lights"] = Buffer.from_array(np.array(lights),
                                          buffer_type='i',
                                          drop_last_dim=True)
Exemple #18
0
def create_geometry(context):
    light = ParallelogramLight()
    light.set_corner(343., 548.6, 227.)
    light.set_v1_v2(np.array([-130., 0., 0.]), np.array([0., 0., 105.]))
    light.set_emission(15, 15, 5)
    light_buffer = Buffer.from_array([light.buffer_numpy.tobytes()],
                                     buffer_type='i')
    context["lights"] = light_buffer

    diffuse = Material(
        closest_hit={0: Program('optixPathTracer.cu', 'diffuse')},
        any_hit={1: Program('optixPathTracer.cu', 'shadow')})

    diffuse_light = Material(
        closest_hit={0: Program('optixPathTracer.cu', 'diffuseEmitter')})

    pgram_bounding_box = Program('parallelogram.cu', 'bounds')
    pgram_intersection = Program('parallelogram.cu', 'intersect')

    # colors
    white = np.array([0.8, 0.8, 0.8], dtype=np.float32)
    green = np.array([0.05, 0.8, 0.05], dtype=np.float32)
    red = np.array([0.8, 0.05, 0.05], dtype=np.float32)

    geometry_instances = []

    # floor
    floor_geometry = create_parallelogram(
        np.array([0., 0.0, 0.], dtype=np.float32),
        np.array([0., 0.0, 559.2], dtype=np.float32),
        np.array([556., 0, 0.], dtype=np.float32), pgram_intersection,
        pgram_bounding_box)
    geometry_instances.append(
        create_geometry_instance(floor_geometry, diffuse, "diffuse_color",
                                 white))

    # ceiling
    ceiling_geometry = create_parallelogram(
        np.array([0., 548.8, 0.], dtype=np.float32),
        np.array([556., 0.0, 0.], dtype=np.float32),
        np.array([0., 0, 559.2], dtype=np.float32), pgram_intersection,
        pgram_bounding_box)
    geometry_instances.append(
        create_geometry_instance(ceiling_geometry, diffuse, "diffuse_color",
                                 white))

    # back wall
    bwall_geometry = create_parallelogram(
        np.array([0., 0., 559.2], dtype=np.float32),
        np.array([0., 548.8, 0.], dtype=np.float32),
        np.array([556., 0, 0.], dtype=np.float32), pgram_intersection,
        pgram_bounding_box)
    geometry_instances.append(
        create_geometry_instance(bwall_geometry, diffuse, "diffuse_color",
                                 white))

    # right wall
    rwall_geometry = create_parallelogram(
        np.array([0., 0., 0.], dtype=np.float32),
        np.array([0., 548.8, 0.], dtype=np.float32),
        np.array([0., 0, 559.2], dtype=np.float32), pgram_intersection,
        pgram_bounding_box)
    geometry_instances.append(
        create_geometry_instance(rwall_geometry, diffuse, "diffuse_color",
                                 green))

    # left wall
    lwall_geometry = create_parallelogram(
        np.array([556., 0., 0.], dtype=np.float32),
        np.array([0., 0., 559.2], dtype=np.float32),
        np.array([0., 548.8, 0.], dtype=np.float32), pgram_intersection,
        pgram_bounding_box)
    geometry_instances.append(
        create_geometry_instance(lwall_geometry, diffuse, "diffuse_color",
                                 red))

    # short block
    short_a = create_parallelogram(
        np.array([130.0, 165.0, 65.0], dtype=np.float32),
        np.array([-48., 0., 160], dtype=np.float32),
        np.array([160., 0.0, 49.], dtype=np.float32), pgram_intersection,
        pgram_bounding_box)
    short_b = create_parallelogram(
        np.array([290., 0., 114.], dtype=np.float32),
        np.array([0., 165., 0.0], dtype=np.float32),
        np.array([-50., 0.0, 158.], dtype=np.float32), pgram_intersection,
        pgram_bounding_box)
    short_c = create_parallelogram(np.array([130., 0., 65.], dtype=np.float32),
                                   np.array([0., 165., 0.], dtype=np.float32),
                                   np.array([160., 0., 49.], dtype=np.float32),
                                   pgram_intersection, pgram_bounding_box)
    short_d = create_parallelogram(
        np.array([82., 0., 225.], dtype=np.float32),
        np.array([0., 165., 0.], dtype=np.float32),
        np.array([48., 0., -160.], dtype=np.float32), pgram_intersection,
        pgram_bounding_box)
    short_e = create_parallelogram(
        np.array([240., 0., 272.], dtype=np.float32),
        np.array([0., 165., 0.], dtype=np.float32),
        np.array([-158., 0., -47.], dtype=np.float32), pgram_intersection,
        pgram_bounding_box)

    geometry_instances.append(
        create_geometry_instance(short_a, diffuse, "diffuse_color", white))
    geometry_instances.append(
        create_geometry_instance(short_b, diffuse, "diffuse_color", white))
    geometry_instances.append(
        create_geometry_instance(short_c, diffuse, "diffuse_color", white))
    geometry_instances.append(
        create_geometry_instance(short_d, diffuse, "diffuse_color", white))
    geometry_instances.append(
        create_geometry_instance(short_e, diffuse, "diffuse_color", white))

    # short block
    tall_a = create_parallelogram(
        np.array([426.0, 330.0, 247.0], dtype=np.float32),
        np.array([-158., 0., 49], dtype=np.float32),
        np.array([49., 0.0, 159.], dtype=np.float32), pgram_intersection,
        pgram_bounding_box)
    tall_b = create_parallelogram(np.array([423., 0., 247.], dtype=np.float32),
                                  np.array([0., 330., 0.0], dtype=np.float32),
                                  np.array([49., 0.0, 159.], dtype=np.float32),
                                  pgram_intersection, pgram_bounding_box)
    tall_c = create_parallelogram(np.array([472., 0., 406.], dtype=np.float32),
                                  np.array([0., 330., 0.], dtype=np.float32),
                                  np.array([-158., 0., 50.], dtype=np.float32),
                                  pgram_intersection, pgram_bounding_box)
    tall_d = create_parallelogram(
        np.array([314., 0., 456.], dtype=np.float32),
        np.array([0., 330., 0.], dtype=np.float32),
        np.array([-49., 0., -160.], dtype=np.float32), pgram_intersection,
        pgram_bounding_box)
    tall_e = create_parallelogram(np.array([265., 0., 296.], dtype=np.float32),
                                  np.array([0., 330., 0.], dtype=np.float32),
                                  np.array([158., 0., -49.], dtype=np.float32),
                                  pgram_intersection, pgram_bounding_box)

    geometry_instances.append(
        create_geometry_instance(tall_a, diffuse, "diffuse_color", white))
    geometry_instances.append(
        create_geometry_instance(tall_b, diffuse, "diffuse_color", white))
    geometry_instances.append(
        create_geometry_instance(tall_c, diffuse, "diffuse_color", white))
    geometry_instances.append(
        create_geometry_instance(tall_d, diffuse, "diffuse_color", white))
    geometry_instances.append(
        create_geometry_instance(tall_e, diffuse, "diffuse_color", white))

    shadow_group = GeometryGroup(children=geometry_instances)
    shadow_group.set_acceleration(Acceleration("Trbvh"))
    context['top_shadower'] = shadow_group

    # light
    light_geometry = create_parallelogram(
        np.array([343., 548.6, 227.], dtype=np.float32),
        np.array([-130., 0.0, 0.], dtype=np.float32),
        np.array([0., 0, 105.], dtype=np.float32), pgram_intersection,
        pgram_bounding_box)
    light_instance = create_geometry_instance(
        light_geometry, diffuse_light, "emission_color",
        np.array([15., 15., 5.], dtype=np.float32))

    group = GeometryGroup(children=(geometry_instances + [light_instance]))
    group.set_acceleration(Acceleration("Trbvh"))
    context['top_object'] = group
Exemple #19
0
def create_scene(context, num_buffers):
    # Sphere
    sphere = Geometry(Program('sphere_texcoord.cu', 'bounds'), Program('sphere_texcoord.cu', 'intersect'))
    sphere.set_primitive_count(1)
    sphere['sphere'] = np.array([0.0, 1.2, 0.0, 1.0], dtype=np.float32)
    sphere['matrix_row_0'] = np.array([1.0, 0.0, 0.0], dtype=np.float32)
    sphere['matrix_row_1'] = np.array([0.0, 1.0, 0.0], dtype=np.float32)
    sphere['matrix_row_2'] = np.array([0.0, 0.0, 1.0], dtype=np.float32)

    # Floor
    parallelogram = Geometry(Program('parallelogram.cu', 'bounds'), Program('parallelogram.cu', 'intersect'))
    parallelogram.set_primitive_count(1)
    anchor = np.array([-20.0, 0.01, 20.0], dtype=np.float32)
    v1 = np.array([40.0, 0.0, 0.0], dtype=np.float32)
    v2 = np.array([0.0, 0.0, -40.0], dtype=np.float32)
    normal = np.cross(v1, v2)
    normal /= np.linalg.norm(normal)
    d = np.dot(normal, anchor)
    v1 *= 1 / np.dot(v1, v1)
    v2 *= 1 / np.dot(v2, v2)
    plane = np.append(normal, d)
    parallelogram['plane'] = plane
    parallelogram['v1'] = v1
    parallelogram['v2'] = v2
    parallelogram['anchor'] = anchor

    # Sphere material
    sphere_matl = Material(
        closest_hit={
            0: Program('optixBuffersOfBuffers.cu', 'closest_hit_radiance')
        },
        any_hit={
            1: Program('optixBuffersOfBuffers.cu', 'any_hit_shadow')
        }
    )

    buffers = [create_random_buffer(MAX_BUFFER_WIDTH, MAX_BUFFER_HEIGHT) for _ in range(num_buffers)]

    # mark buffers as bindless so that they won't be destroyed when `buffers` list is garbage-collected
    for buffer in buffers:
        buffer.bindless = True

    sphere_matl['Kd_layers'] = Buffer.from_array([buf.get_id() for buf in buffers], dtype=np.int32, buffer_type='i')

    # Floor material
    floor_matl = Material(
        closest_hit={
            0: Program('phong.cu', 'closest_hit_radiance')
        },
        any_hit={
            1: Program('phong.cu', 'any_hit_shadow')
        }
    )
    floor_matl['Kd'] = np.array([0.7, 0.7, 0.7], dtype=np.float32)
    floor_matl['Ka'] = np.array([1.0, 1.0, 1.0], dtype=np.float32)
    floor_matl['Kr'] = np.array([0.0, 0.0, 0.0], dtype=np.float32)
    floor_matl['phong_exp'] = np.array(1.0, dtype=np.float32)

    # Place geometry into hierarchy
    geometrygroup = GeometryGroup()
    geometrygroup.add_child(GeometryInstance(sphere, sphere_matl))
    geometrygroup.add_child(GeometryInstance(parallelogram, floor_matl))
    acc = Acceleration('Sbvh', 'Bvh')
    geometrygroup.set_acceleration(acc)

    context['top_object'] = geometrygroup
    context['top_shadower'] = geometrygroup

    # Lights
    context['ambient_light_color'] = np.array([0.1, 0.1, 0.1], dtype=np.float32)
    lights = [
        np.array(BasicLight([0.0, 8.0, -5.0], [0.4, 0.4, 0.4], True)),
        np.array(BasicLight([5.0, 8.0, 0.0], [0.4, 0.4, 0.4], True)),
    ]
    context["lights"] = Buffer.from_array(np.array(lights), buffer_type='i', drop_last_dim=True)
Exemple #20
0
def create_geometry(context):
    pgram_bounding_box = Program('box.cu', 'box_bounds')
    pgram_intersection = Program('box.cu', 'box_intersect')

    # Create box
    box = Geometry(bounding_box_program=pgram_bounding_box, intersection_program=pgram_intersection)
    box.set_primitive_count(1)
    box["boxmin"] = np.array([-2., 0., -2.], np.float32)
    box["boxmax"] = np.array([2., 7., 2.], np.float32)

    # Create chull
    if tutorial_number >= 9:
        chull = Geometry(bounding_box_program=Program(tutorial_file, 'chull_bounds'),
                         intersection_program=Program(tutorial_file, 'chull_intersect'))
        chull.set_primitive_count(1)
        nsides = 6
        chplane = []
        radius = 1
        xlate = np.array([-1.4, 0, -3.7], dtype=np.float32)
        for i in range(nsides):
            angle = float(i)/float(nsides) * math.pi * 2.
            x = math.cos(angle)
            y = math.sin(angle)
            chplane.append(make_plane(np.array([x, 0, y], dtype=np.float32),
                                      np.array([x*radius, 0, y*radius], dtype=np.float32) + xlate))
        min = 0.02
        max = 3.5
        chplane.append(make_plane(np.array([0, -1, 0], dtype=np.float32),
                                  np.array([0, min, 0], dtype=np.float32) + xlate))
        angle = 5/nsides * math.pi * 2
        chplane.append(make_plane(np.array([math.cos(angle), 0.7, math.sin(angle)], dtype=np.float32),
                                  np.array([0, max, 0], dtype=np.float32) + xlate))
        plane_buffer = Buffer.from_array([x.tobytes() for x in chplane], buffer_type='i')
        chull["planes"] = plane_buffer
        chull["chull_bbmin"] = np.array([-radius + xlate[0], min + xlate[1], -radius + xlate[2]], dtype=np.float32)
        chull["chull_bbmax"] = np.array([radius + xlate[0], max + xlate[1], radius + xlate[2]], dtype=np.float32)

    # Floor geometry
    floor_geometry = create_parallelogram(np.array([-64., 0.01, -64.], dtype=np.float32),
                                          np.array([128., 0., 0.], dtype=np.float32),
                                          np.array([0., 0, 128.], dtype=np.float32),
                                          Program('parallelogram.cu', 'intersect'),
                                          Program('parallelogram.cu', 'bounds'))

    # Materials
    box_chname = "closest_hit_radiance0"
    if tutorial_number >= 8:
        box_chname = "box_closest_hit_radiance"
    elif tutorial_number >= 3:
        box_chname = "closest_hit_radiance3"
    elif tutorial_number >= 2:
        box_chname = "closest_hit_radiance2"
    elif tutorial_number >= 1:
        box_chname = "closest_hit_radiance1"

    closest_hit = {0: Program(tutorial_file, box_chname)}
    any_hit = None
    if tutorial_number >= 3:
        any_hit = {1: Program(tutorial_file, "any_hit_shadow")}
    box_matl = Material(closest_hit=closest_hit, any_hit=any_hit)
    box_matl["Ka"] = np.array([0.3, 0.3, 0.3], dtype=np.float32)
    box_matl["Kd"] = np.array([0.6, 0.7, 0.8], dtype=np.float32)
    box_matl["Ks"] = np.array([0.8, 0.9, 0.8], dtype=np.float32)
    box_matl["phong_exp"] = np.array(88, dtype=np.float32)
    box_matl["reflectivity_n"] = np.array([0.2, 0.2, 0.2], dtype=np.float32)

    floor_name = "closest_hit_radiance0"
    if tutorial_number >= 7:
        floor_name = "floor_closest_hit_radiance"
    elif tutorial_number >= 6:
        floor_name = "floor_closest_hit_radiance5"
    elif tutorial_number >= 4:
        floor_name = "floor_closest_hit_radiance4"
    elif tutorial_number >= 3:
        floor_name = "closest_hit_radiance3"
    elif tutorial_number >= 2:
        floor_name = "closest_hit_radiance2"
    elif tutorial_number >= 1:
        floor_name = "closest_hit_radiance1"

    closest_hit = {0: Program(tutorial_file, floor_name)}
    any_hit = None
    if tutorial_number >= 3:
        any_hit = {1: Program(tutorial_file, "any_hit_shadow")}
    floor_matl = Material(closest_hit=closest_hit, any_hit=any_hit)
    floor_matl["Ka"] = np.array([0.3, 0.3, 0.1], dtype=np.float32)
    floor_matl["Kd"] = np.array([194./255.*0.6, 186./255.*0.6, 151./255.*0.6], dtype=np.float32)
    floor_matl["Ks"] = np.array([0.4, 0.4, 0.4], dtype=np.float32)
    floor_matl["reflectivity"] = np.array([0.1, 0.1, 0.1], dtype=np.float32)
    floor_matl["reflectivity_n"] = np.array([0.05, 0.05, 0.05], dtype=np.float32)
    floor_matl["phong_exp"] = np.array(88, dtype=np.float32)
    floor_matl["tile_v0"] = np.array([0.25, 0., 0.15], dtype=np.float32)
    floor_matl["tile_v1"] = np.array([-0.15, 0., 0.25], dtype=np.float32)
    floor_matl["crack_color"] = np.array([0.1, 0.1, 0.1], dtype=np.float32)
    floor_matl["crack_width"] = np.array(0.02, dtype=np.float32)

    # Glass material
    if tutorial_number >= 9:
        closest_hit = {0: Program(tutorial_file, "glass_closest_hit_radiance")}
        any_hit = {1: Program(tutorial_file, "glass_any_hit_shadow")} if tutorial_number >= 10 else {1: Program(tutorial_file, "any_hit_shadow")}
        glass_matl = Material(closest_hit=closest_hit, any_hit=any_hit)

        glass_matl["importance_cutoff"] = np.array(1e-2, dtype=np.float32)
        glass_matl["cutoff_color"] = np.array([0.34, 0.55, 0.85], dtype=np.float32)
        glass_matl["fresnel_exponent"] = np.array(3., dtype=np.float32)
        glass_matl["fresnel_minimum"] = np.array(0.1, dtype=np.float32)
        glass_matl["fresnel_maximum"] = np.array(1.0, dtype=np.float32)
        glass_matl["refraction_index"] = np.array(1.4, dtype=np.float32)
        glass_matl["refraction_color"] = np.array([1., 1., 1.], dtype=np.float32)
        glass_matl["reflection_color"] = np.array([1., 1., 1.], dtype=np.float32)
        glass_matl["refraction_maxdepth"] = np.array(100, dtype=np.int32)
        glass_matl["reflection_maxdepth"] = np.array(100, dtype=np.int32)
        extinction = np.array([0.8, 0.89, 0.75], dtype=np.float32)
        glass_matl["extinction_constant"] = np.log(extinction)
        glass_matl["shadow_attenuation"] = np.array([0.4, 0.7, 0.4], dtype=np.float32)

    # GI
    geometry_instances = [GeometryInstance(box, box_matl),
                          GeometryInstance(floor_geometry, floor_matl)]

    if tutorial_number >= 9:
        geometry_instances.append(GeometryInstance(chull, glass_matl))

    geometry_group = GeometryGroup(children=geometry_instances)
    geometry_group.set_acceleration(Acceleration("NoAccel"))
    context['top_object'] = geometry_group
    context['top_shadower'] = geometry_group