def light_sample(self, spectrum): area = (self.p1 - self.p0).cross(self.p2 - self.p0).length() * 0.5 inv_area = 1.0 / area code = """ r1 = random() tmp = 1.0 - r1 tmp = sqrt(tmp) beta = 1.0 - tmp gamma = tmp * random() shadepoint.light_position = (1.0 - beta - gamma) * p0 + beta * p1 + gamma * p2 shadepoint.light_pdf = inv_area shadepoint.light_normal = normal """ inv_area = FloatArg('inv_area', inv_area) normal = Vec3Arg('normal', self.normal) p0 = Vec3Arg('p0', self.p0) p1 = Vec3Arg('p1', self.p1) p2 = Vec3Arg('p2', self.p2) args = [inv_area, normal, p0, p1, p2] func_args = [StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spectrum))] name = 'triangle_sample_%i' % id(self) return Shader(code=code, args=args, name=name, func_args=func_args, is_func=True)
def light_sample(self, spectrum): area = self.edge_a.length() * self.edge_b.length() inv_area = 1.0 / area code = """ rnd = random2() shadepoint.light_pdf = inv_area shadepoint.light_normal = normal shadepoint.light_position = point + edge_a * rnd[0] + edge_b * rnd[1] """ inv_area = FloatArg('inv_area', inv_area) normal = Vec3Arg('normal', self.normal) point = Vec3Arg('point', self.point) eda = Vec3Arg('edge_a', self.edge_a) edb = Vec3Arg('edge_b', self.edge_b) args = [inv_area, normal, point, eda, edb] func_args = [ StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spectrum)) ] name = 'rect_sample_%i' % id(self) return Shader(code=code, args=args, name=name, func_args=func_args, is_func=True)
def _func_args(self, spectrum): func_args = [ StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spectrum)), IntArg('mat_idx', 0) ] return func_args
def load(self, shader_name): args = [] text = self._loader.load(shader_name, 'props.txt') if text is not None: args = parse_args(text) w = Vec3Arg('w', self._w) u = Vec3Arg('u', self._u) v = Vec3Arg('v', self._v) distance = FloatArg('distance', self._distance) eye = Vec3Arg('eye', self._eye) lookat = Vec3Arg('lookat', self._lookat) args.extend([w, u, v, distance, eye, lookat]) code = self._loader.load(shader_name, 'code.py') if code is None: raise ValueError("code.py in %s shader dont exist!" % shader_name) func_args = [ StructArgPtr('ray', Ray.factory()), StructArgPtr('sample', Sample.factory()) ] self._shader_name = shader_name self.shader = Shader(code=code, args=args, name='generate_ray', func_args=func_args, is_func=True)
def isect_shader(cls, shader_name): code = """ temp = ray.origin - sphere.origin r_dir = ray.direction a = dot(r_dir, r_dir) b = dot(temp, r_dir) * 2.0 c = dot(temp, temp) - sphere.radius * sphere.radius disc = b * b - 4.0 * a * c if disc < 0.0: return 0 e = sqrt(disc) denom = 2.0 * a t = (-1.0 * b - e) / denom if t > 0.0005: if t < min_dist: normal = (temp + r_dir * t) * (1.0 / sphere.radius) hit = ray.origin + r_dir * t hitpoint.t = t hitpoint.normal = normal hitpoint.hit = hit hitpoint.mat_idx = sphere.mat_idx hitpoint.light_id = sphere.light_id hitpoint.u = 0.0 hitpoint.v = 0.0 return 1 t = (-1.0 * b + e) / denom if t > 0.0005: if t < min_dist: normal = (temp + r_dir * t) * (1.0 / sphere.radius) hit = ray.origin + r_dir * t hitpoint.t = t hitpoint.normal = normal hitpoint.hit = hit hitpoint.mat_idx = sphere.mat_idx hitpoint.light_id = sphere.light_id hitpoint.u = 0.0 hitpoint.v = 0.0 return 1 return 0 """ func_args = [ StructArgPtr('ray', Ray.factory()), StructArgPtr('sphere', Sphere.factory()), StructArgPtr('hitpoint', HitPoint.factory()), FloatArg('min_dist', 0.0) ] shader = Shader(code=code, args=[], name=shader_name, func_args=func_args, is_func=True) return DependencyShader(shader)
def isect_shader(cls, shader_name): code = """ temp1 = dot(ray.direction, rectangle.normal) if temp1 == 0.0: return 0 tmp = rectangle.point - ray.origin t = dot(tmp, rectangle.normal) / temp1 if t < 0.00001: return 0 if t > min_dist: return 0 p = ray.origin + ray.direction * t d = p - rectangle.point ddota = dot(d, rectangle.edge_a) if ddota < 0.0: return 0 if ddota > rectangle.edge_a_squared: return 0 ddotb = dot(d, rectangle.edge_b) if ddotb < 0.0: return 0 if ddotb > rectangle.edge_b_squared: return 0 hitpoint.t = t hitpoint.normal = rectangle.normal hitpoint.hit = p hitpoint.mat_idx = rectangle.mat_idx hitpoint.light_id = rectangle.light_id hitpoint.u = 0.0 hitpoint.v = 0.0 return 1 """ func_args = [ StructArgPtr('ray', Ray.factory()), StructArgPtr('rectangle', Rectangle.factory()), StructArgPtr('hitpoint', HitPoint.factory()), FloatArg('min_dist', 0.0) ] shader = Shader(code=code, args=[], name=shader_name, func_args=func_args, is_func=True) return DependencyShader(shader)
def light_pdf(self, spectrum): area = (self.p1 - self.p0).cross(self.p2 - self.p0).length() * 0.5 inv_area = 1.0 / area code = """ shadepoint.light_pdf = inv_area """ inv_area = FloatArg('inv_area', inv_area) args = [inv_area] func_args = [StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spectrum))] name = 'triangle_light_pdf_%i' % id(self) return Shader(code=code, args=args, name=name, func_args=func_args, is_func=True)
def create_shader(self): code = """ if cury == endy: return 0 rnds = random2() n = float(nsamples) n = sqrt(n) px = (subx + rnds[0]) / n py = (suby + rnds[1]) / n tmp = curx - width * 0.5 + px tmp2 = cury - height * 0.5 + py sample.x = pixelsize * tmp sample.y = pixelsize * tmp2 sample.px = px sample.py = py sample.ix = curx sample.iy = cury sample.weight = 1.0 curx = curx + 1 if curx == endx: curx = tile.x cury = cury + 1 return 1 """ args = self.args() func_args = [StructArgPtr('sample', Sample.factory())] self.shader = Shader(code=code, args=args, name='generate_sample', func_args=func_args, is_func=True) return self.shader
def create_shader(self): code = """ if cury == endy: return 0 tmp = curx - width * 0.5 + 0.5 tmp2 = cury - height * 0.5 + 0.5 sample.x = pixelsize * tmp sample.y = pixelsize * tmp2 sample.px = 0.5 sample.py = 0.5 sample.ix = curx sample.iy = cury sample.weight = 1.0 curx = curx + 1 if curx == endx: curx = tile.x cury = cury + 1 return 1 """ args = self.args() func_args = [StructArgPtr('sample', Sample.factory())] self.shader = Shader(code=code, args=args, name='generate_sample', func_args=func_args, is_func=True) return self.shader
def isect_b_shader(cls, shader_name): label = 'ray_triangle_isect_b_%s' % id(cls) tri_isect = ray_triangle_isect_shader(label, isect_bool=True) code = """ return %s(ray, triangle.p0, triangle.p1, triangle.p2, min_dist) """ % label args = [] func_args = [StructArgPtr('ray', Ray.factory()), StructArgPtr('triangle', FlatTriangle.factory()), FloatArg('min_dist', 0.0)] shader = Shader(code=code, args=args, name=shader_name, func_args=func_args, is_func=True) isect_shader = DependencyShader(shader, [tri_isect]) return isect_shader
def light_pdf(self, spectrum): area = self.edge_a.length() * self.edge_b.length() inv_area = 1.0 / area code = """ shadepoint.light_pdf = inv_area """ inv_area = FloatArg('inv_area', inv_area) args = [inv_area] func_args = [ StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spectrum)) ] name = 'rect_light_pdf_%i' % id(self) return Shader(code=code, args=args, name=name, func_args=func_args, is_func=True)
def _compile_environment(self, color_mgr, shaders=[]): if self._env_light is not None: self.env_shader = self._env_light.env_shader self.env_shader.compile(shaders, color_mgr=color_mgr) return # We create dummy shader for environment emission code = ''' shadepoint.light_intensity = Spectrum(0.0) shadepoint.light_pdf = 1.0 ''' spec = color_mgr.zero() func_args = [ StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spec)) ] self.env_shader = Shader(code=code, name='environment_emission', func_args=func_args, is_func=True) self.env_shader.compile(shaders, color_mgr=color_mgr)
def isect_shader(self): origin = Vector3(0.0, 0.0, 0.0) direction = Vector3(0.0, 0.0, 0.0) ray = Ray(origin, direction) hitpoint = HitPoint(0.0, Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0), 0, 0.0, 0.0) func_args = [StructArgPtr('ray', ray), StructArgPtr('hitpoint', hitpoint), FloatArg('min_dist', 99999.0)] args = [] code = "hit_happend = 0\n" for shp_type in self.shp_mgr.shape_types(): code1, args1 = self._get_shape_code(shp_type) args.extend(args1) code += code1 code += "\nreturn hit_happend\n" shader = Shader(code=code, args=args, name='isect_scene', func_args=func_args, is_func=True) return shader
def isect_b_shader(cls, shader_name): code = """ temp = ray.origin - sphere.origin r_dir = ray.direction a = dot(r_dir, r_dir) b = dot(temp, r_dir) * 2.0 c = dot(temp, temp) - sphere.radius * sphere.radius disc = b * b - 4.0 * a * c if disc < 0.0: return 0 e = sqrt(disc) denom = 2.0 * a t = (-1.0 * b - e) / denom if t > 0.0005: if t < min_dist: return 1 t = (-1.0 * b + e) / denom if t > 0.0005: if t < min_dist: return 1 return 0 """ func_args = [ StructArgPtr('ray', Ray.factory()), StructArgPtr('sphere', Sphere.factory()), FloatArg('min_dist', 0.0) ] shader = Shader(code=code, args=[], name=shader_name, func_args=func_args, is_func=True) return DependencyShader(shader)
def _lgt_emission(self, color_mgr): code = """ if light_id < 0: shadepoint.light_intensity = Spectrum(0.0) shadepoint.light_pdf = 1.0 else: ptr_func = lgt_ptrs[light_id] __light_emission(hitpoint, shadepoint, ptr_func) """ lgt_ptrs = ArrayArg('lgt_ptrs', PtrsArray()) al = ArgList('lgt_ptrs', [lgt_ptrs]) spec = color_mgr.zero() func_args = [ StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spec)), IntArg('light_id', -1) ] args = [al] self.emission_shader = Shader(code=code, args=args, name='light_emission', func_args=func_args, is_func=True)
def isect_shader(cls, shader_name): label = 'ray_triangle_isect_%s' % id(cls) tri_isect = ray_triangle_isect_shader(label, isect_bool=False) code = """ ret = %s(ray, triangle.p0, triangle.p1, triangle.p2, min_dist, hitpoint) if ret: hitpoint.normal = triangle.normal hitpoint.u = 0.0 hitpoint.v = 0.0 hitpoint.mat_idx = triangle.mat_idx hitpoint.light_id = triangle.light_id return 1 else: return 0 """ % label args = [] origin = Vector3(0.0, 0.0, 0.0) direction = Vector3(0.0, 0.0, 0.0) ray = Ray(origin, direction) hitpoint = HitPoint(0.0, Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0), 0, 0.0, 0.0) triangle = FlatTriangle(Vector3(1.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0), Vector3(0.0, 0.0, 1.0), 0) func_args = [StructArgPtr('ray', ray), StructArgPtr('triangle', triangle), StructArgPtr('hitpoint', hitpoint), FloatArg('min_dist', 0.0)] shader = Shader(code=code, args=args, name=shader_name, func_args=func_args, is_func=True) isect_shader = DependencyShader(shader, [tri_isect]) return isect_shader
def load(self, shader_name): args = [] text = self._loader.load(shader_name, 'props.txt') if text is not None: args = parse_args(text) args.append(FloatArg('xwidth', self.xwidth)) args.append(FloatArg('ywidth', self.ywidth)) code = self._loader.load(shader_name, 'code.py') if code is None: raise ValueError("code.py in %s shader dont exist!" % shader_name) func_args = [StructArgPtr('sample', Sample.factory())] self.shader = Shader(code=code, args=args, name='filter_sample', func_args=func_args, is_func=True)
def _register_prototype(spectrum, name): func_args = [StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spectrum))] register_prototype(name, func_args=func_args)
def ray_triangle_isect_shader(name, isect_bool=False): code = """ origin = ray.origin direction = ray.direction a = p0[0] - p1[0] b = p0[0] - p2[0] c = direction[0] d = p0[0] - origin[0] e = p0[1] - p1[1] f = p0[1] - p2[1] g = direction[1] h = p0[1]- origin[1] i = p0[2] - p1[2] j = p0[2] - p2[2] k = direction[2] l = p0[2] - origin[2] m = f * k - g * j n = h * k - g * l p = f * l - h * j q = g * i - e * k s = e * j - f * i temp3 = a * m + b * q + c * s if temp3 == 0.0: return 0 inv_denom = 1.0 / temp3 e1 = d * m - b * n - c * p beta = e1 * inv_denom if beta < 0.0: return 0 r = e * l - h * i e2 = a * n + d * q + c * r gamma = e2 * inv_denom if gamma < 0.0: return 0 betagamma = beta + gamma if betagamma > 1.0: return 0 e3 = a * p - b * r + d * s t = e3 * inv_denom epsilon = 0.00001 if t < 0.00001: return 0 if t > min_dist: return 0 """ if isect_bool: code += """ return 1 """ else: code += """ hitpoint.t = t hitpoint.hit = direction * t + origin hitpoint.u = beta hitpoint.v = gamma return 1 """ ray_a = StructArgPtr('ray', Ray.factory()) p0 = Vec3Arg('p0', Vector3(0.0, 0.0, 0.0)) p1 = Vec3Arg('p1', Vector3(0.0, 0.0, 0.0)) p2 = Vec3Arg('p2', Vector3(0.0, 0.0, 0.0)) dist = FloatArg('min_dist', 0.0) hit_a = StructArgPtr('hitpoint', HitPoint.factory()) if isect_bool: func_args = [ray_a, p0, p1, p2, dist] else: func_args = [ray_a, p0, p1, p2, dist, hit_a] shader = Shader(code=code, args=[], name=name, func_args=func_args, is_func=True) return shader