def __init__(self, film_width, film_height, camera2world, vfov, aperture=0.0, focus_dist=1.0): self.m_film = Film(film_width, film_height) self.m_sv_camera2world = vki.SVMat4x4(camera2world) theta = vfov * math.pi / 180.0 half_height = math.tan(theta * 0.5) * focus_dist size_pix = half_height * 2.0 / film_height lens_radius = aperture * 0.5 self.m_sv_size_pix = vki.SVFloat(size_pix) self.m_sv_lens_radius = vki.SVFloat(lens_radius) self.m_sv_focus_dist = vki.SVFloat(focus_dist) self.m_cptr = SVCombine_Create( { 'film': self.m_film, 'camera2world': self.m_sv_camera2world, 'size_pix': self.m_sv_size_pix, 'lens_radius': self.m_sv_lens_radius, 'focus_dist': self.m_sv_focus_dist }, ''' void generate_ray(in Comb_#hash# camera, float film_x, float film_y, float lens_x, float lens_y, out vec3 origin, out vec3 dir) { origin = vec3(lens_x*camera.lens_radius, lens_y*camera.lens_radius, 0.0); origin = (camera.camera2world*vec4(origin, 1.0)).xyz; vec3 pos_pix = vec3((film_x - float(camera.film.width)*0.5)*camera.size_pix, (float(camera.film.height)*0.5 - film_y)*camera.size_pix, -camera.focus_dist); pos_pix = (camera.camera2world*vec4(pos_pix, 1.0)).xyz; dir = normalize(pos_pix - origin); } ''')
def __init__(self, positions, normals, indices_pos, indices_norm, modelMat=glm.identity(glm.mat4), color=(1.0, 1.0, 1.0)): Geometry.__init__(self, modelMat) self.m_gpuPos = vki.device_vector_from_numpy(positions) self.m_gpuNorm = vki.device_vector_from_numpy(normals) self.m_gpuIndPos = vki.device_vector_from_numpy(indices_pos) self.m_gpuIndNorm = vki.device_vector_from_numpy(indices_norm) self.m_blas = vki.BaseLevelAS(self.m_gpuIndPos, self.m_gpuPos) vert0 = positions[0] self.m_aabb = np.array( [vert0[0], vert0[1], vert0[2], vert0[0], vert0[1], vert0[2]], dtype=np.float32) for vert in positions: self.m_aabb[0] = min(self.m_aabb[0], vert[0]) self.m_aabb[1] = min(self.m_aabb[1], vert[1]) self.m_aabb[2] = min(self.m_aabb[2], vert[2]) self.m_aabb[3] = max(self.m_aabb[3], vert[0]) self.m_aabb[4] = max(self.m_aabb[4], vert[1]) self.m_aabb[5] = max(self.m_aabb[5], vert[2]) self.d_normMat = vki.SVMat4x4(self.m_normMat) self.d_color = Spectrum(color) self.m_cptr = SVCombine_Create( { 'indices': self.m_gpuIndNorm, 'normals': self.m_gpuNorm, 'normalMat': self.d_normMat, 'color': self.d_color }, ''' void closethit(in Comb_#hash# self, int primitiveId, in vec3 barycentrics, inout {HitInfo_Lambert} hitinfo) {{ uint i0 = get_value(self.indices, 3 * primitiveId); uint i1 = get_value(self.indices, 3 * primitiveId + 1); uint i2 = get_value(self.indices, 3 * primitiveId + 2); vec3 norm0 = get_value(self.normals, i0); vec3 norm1 = get_value(self.normals, i1); vec3 norm2 = get_value(self.normals, i2); vec3 normal = norm0 * barycentrics.x + norm1 * barycentrics.y + norm2 * barycentrics.z; normal = normalize((self.normalMat * vec4(normal, 0.0)).xyz); hitinfo.lambert.color = self.color; hitinfo.normal = normal; }} '''.format(HitInfo_Lambert=Name_HitInfo_Lambert))
def __init__(self, modelMat=glm.identity(glm.mat4), color=(1.0, 1.0, 1.0)): Sphere.__init__(self, modelMat) self.d_normMat = vki.SVMat4x4(self.m_normMat) self.d_color = Spectrum(color) self.m_cptr = SVCombine_Create( { 'normalMat': self.d_normMat, 'color': self.d_color }, ''' void closethit(in Comb_#hash# self, in vec3 hitpoint, inout {HitInfo_Lambert} hitinfo) {{ hitinfo.lambert.color = self.color; hitinfo.normal = normalize((self.normalMat * vec4(hitpoint, 0.0)).xyz); }} '''.format(HitInfo_Lambert=Name_HitInfo_Lambert))
def render(self, cube, filename = None): colorBuf = vki.Texture2D(self.width, self.height, VK_FORMAT_R8G8B8A8_SRGB) colorBuf4x = vki.Texture2D(self.width, self.height, VK_FORMAT_R8G8B8A8_SRGB, samples=4) depthBuf4x = vki.Texture2D(self.width, self.height, VK_FORMAT_D32_SFLOAT, isDepth=True, samples=4) gpu_matrix = vki.SVMat4x4(self.matrix) gpu_map = vki.device_vector_from_numpy(cube.map) gpu_dirs = vki.device_vector_from_numpy(cube.dirs) self.rp.launch([6*9*6], [colorBuf4x], depthBuf4x, self.bg_color, self.bg_depth, [gpu_matrix, gpu_map, gpu_dirs], resolveBufs=[colorBuf], cubemaps = [self.skin]) image_out = np.empty((self.height, self.width, 4), dtype=np.uint8) colorBuf.download(image_out) if not filename is None: Image.fromarray(image_out, 'RGBA').save(filename) return image_out
{ outColor = vec4(0.0, 0.0, 0.0, 1.0); vec3 norm = normalize(vNorm); vec3 light_dir = normalize(vec3(0.2, 0.5, -1.0)); light_dir = reflect(light_dir, norm); if (light_dir.z>0.0) { float intensity = pow(light_dir.z, 5.0)*0.8; outColor += vec4(intensity, intensity, intensity, 0.0); } outColor = clamp(outColor, 0.0, 1.0); } ''')) proj = glm.perspective(glm.radians(45.0), width / height, 0.1, 2000.0) modelView = glm.lookAt(glm.vec3(-100.0, 200.0, 200.0), glm.vec3(0.0, 0.0, 0.0), glm.vec3(0.0, 1.0, 0.0)) mat_pos = vki.SVMat4x4(proj * modelView) mat_norm = vki.SVMat4x4(glm.transpose(glm.inverse(modelView))) rp.launch([len(vertex_inds)], [colorBuf], depthBuf, [0.5, 0.5, 0.5, 1.0], 1.0, [gpuPos, gpuNormals, gpuInd_pos, gpuInd_norm, mat_pos, mat_norm]) image_out = np.empty((height, width, 4), dtype=np.uint8) colorBuf.download(image_out) Image.fromarray(image_out, 'RGBA').save('output.png')