def __init__(self, color_mgr=RGBManager()): # def __init__(self, color_mgr=SampledManager()): self._color_mgr = color_mgr register_prototypes(color_mgr.zero()) self.sampler = RegularSampler(width=512, height=512, pixelsize=1.0, nthreads=1) self.camera = Camera(eye=Vector3(5.0, 5.0, 5.0), lookat=Vector3(0.0, 0.0, 0.0), distance=200) self.camera.load('pinhole') self.shapes = ShapeManager() self.intersector = LinearIsect(self.shapes) self.materials = MaterialManager() self.lights = LightManager() self.filter = SampleFilter() self.filter.load('box') self.integrator = Integrator() self.integrator.load('test', self._color_mgr) self.tone_mapping = Tmo() self.tone_mapping.load('exp') self._ready = False self._create_hdr_buffer()
def test_isect_b_sph(self): sph_shader = Sphere.isect_b_shader('isect_b_sphere') sph_shader.compile() runtimes = [Runtime()] sph_shader.prepare(runtimes) code = """ min_dist = 99999.0 p1 = isect_b_sphere(ray, sphere, min_dist) """ direction = Vector3(-1.0, -1.0, -1.0) direction.normalize() ray = Ray(Vector3(5.0, 5.0, 5.0), direction) sphere = Sphere(Vector3(0.0, 0.0, 0.0), 2.0, 0) r_arg = StructArg('ray', ray) sph_arg = StructArg('sphere', sphere) p1 = IntArg('p1', 6) args = [r_arg, sph_arg, p1] shader = Shader(code=code, args=args) shader.compile([sph_shader.shader]) shader.prepare(runtimes) shader.execute() result = shader.get_value('p1') self.assertEqual(result, 1)
def _parse_vec3(line): t = line.split('=', maxsplit=1) name = t.pop(0).strip() if len(t) == 0: return Vec3Arg(name, Vector3(0.0, 0.0, 0.0)) v = t.pop(0).strip().split(',') arg = Vec3Arg(name, Vector3(float(v[0]), float(v[1]), float(v[2]))) return arg
def factory(cls, spectrum): wo = Vector3(0.0, 0.0, 0.0) wi = Vector3(0.0, 0.0, 0.0) li = spectrum.zero() lpos = Vector3(0.0, 0.0, 0.0) ref = spectrum.zero() lnormal = Vector3(0.0, 1.0, 0.0) em = spectrum.zero() return ShadePoint(wo, wi, li, lpos, ref, 1.0, lnormal, 1.0, em, 0)
def test_mesh(mesh, nrays=1): dep_shader = type(mesh).isect_shader('ray_flat_mesh_isect') dep_shader.compile() runtimes = [Runtime()] dep_shader.prepare(runtimes) code = """ min_dist = 99999.0 ret = ray_flat_mesh_isect(ray, mesh, hitpoint, min_dist) """ ray = generate_ray(mesh) hitpoint = HitPoint(0.0, Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0), 6, 0.0, 0.0) r_arg = StructArg('ray', ray) mesh_arg = StructArg('mesh', mesh) harg = StructArg('hitpoint', hitpoint) ret = IntArg('ret', 6) args = [r_arg, mesh_arg, harg, ret] shader = Shader(code=code, args=args) shader.compile([dep_shader.shader]) shader.prepare(runtimes) for i in range(nrays): ray = generate_ray(mesh) # origin = Vector3(-0.21099534597992897,-0.02090535280108452,-0.09716709856688976) # direction = Vector3(0.7856996643888073,0.4629769683728137,0.4102783983292736) # ray = Ray(origin, direction) shader.set_value('ray', ray) hp2 = mesh.isect(ray) hp, index = isect_ray_mesh(ray, mesh) shader.execute() # print(hp, shader.get_value('ret')) if hp: ret = shader.get_value('ret') hp_new = shader.get_value('hitpoint') if round(hp.t - hp_new.t, 5) != 0: print(hp.t, hp_new.t, ret, index, hp2.t) print(ray.origin) print(ray.direction) p0, p1, p2 = mesh.get_points(index) print(p0) print(p1) print(p2) print('------------------------------------------')
def bbox(self): epsilon = 0.0001 v0 = self.p0 v1 = self.p1 v2 = self.p2 minx = min(min(v0.x, v1.x), v2.x) - epsilon maxx = max(max(v0.x, v1.x), v2.x) + epsilon miny = min(min(v0.y, v1.y), v2.y) - epsilon maxy = max(max(v0.y, v1.y), v2.y) + epsilon minz = min(min(v0.z, v1.z), v2.z) - epsilon maxz = max(max(v0.z, v1.z), v2.z) + epsilon p0 = Vector3(minx, miny, minz) p1 = Vector3(maxx, maxy, maxz) return BBox(p0, p1)
def bbox(self): epsilon = 0.001 p0X = self.origin.x - self.radius - epsilon p0Y = self.origin.y - self.radius - epsilon p0Z = self.origin.z - self.radius - epsilon p1X = self.origin.x + self.radius + epsilon p1Y = self.origin.y + self.radius + epsilon p1Z = self.origin.z + self.radius + epsilon p0 = Vector3(p0X, p0Y, p0Z) p1 = Vector3(p1X, p1Y, p1Z) return BBox(p0, p1)
def test_sampled_to_rgb(self): runtimes = [Runtime()] color_mgr = SampledManager() s_to_rgb = spectrum_to_rgb_shader(color_mgr) s_to_rgb.compile(color_mgr=color_mgr) s_to_rgb.prepare(runtimes) code = """ value = spectrum_to_rgb(color) """ spec = RGBSpectrum(0.3, 0.5, 0.4) spec = color_mgr.rgb_to_sampled(spec) col = SampledArg('color', spec) val = Vec3Arg('value', Vector3(0.0, 0.0, 0.0)) shader = Shader(code=code, args=[col, val]) shader.compile(shaders=[s_to_rgb], color_mgr=color_mgr) shader.prepare(runtimes) shader.execute() value = shader.get_value('value') vv = color_mgr.sampled_to_rgb(spec) self.assertAlmostEqual(value.x, vv.r) self.assertAlmostEqual(value.y, vv.g) self.assertAlmostEqual(value.z, vv.b, places=6)
def _parse_camera(fobj, renderer): values = _extract_values(fobj) #NOTE keywords are expected to be in lower case!!! Improve this eye = values['eye'] eye = Vector3(float(eye[0]), float(eye[1]), float(eye[2])) lookat = values['lookat'] lookat = Vector3(float(lookat[0]), float(lookat[1]), float(lookat[2])) distance = float(values['distance'][0]) camera = Camera(eye=eye, lookat=lookat, distance=distance) typ = values['type'][0].strip().lower() camera.load(typ) #TODO - shader public parameters renderer.camera = camera
def _rgb_to_rgb_spectrum_shader(rgb_mgr): code = """ return rgb(color[0], color[1], color[2]) """ vec = Vec3Arg('color', Vector3.zero()) shader = Shader(code=code, name='rgb_to_spectrum', func_args=[vec], is_func=True) return shader
def test_isect_b_flat_triangle(self): runtimes = [Runtime()] tri_shader = FlatTriangle.isect_b_shader('isect_b_flat_triangle') tri_shader.compile() tri_shader.prepare(runtimes) p0 = Vector3(2.2, 4.4, 6.6) p1 = Vector3(1.1, 1.1, 1.1) p2 = Vector3(5.1, -1.1, 5.1) origin = Vector3(0.0, 0.0, 0.0) direction = Vector3(3, 3.0, 3.01) direction.normalize() ray = Ray(origin, direction) t = FlatTriangle(p0, p1, p2) code = """ min_dist = 150.0 ret = isect_b_flat_triangle(ray, triangle, min_dist) """ r_arg = StructArg('ray', ray) tri_arg = StructArg('triangle', t) ret = IntArg('ret', 6) args = [r_arg, tri_arg, ret] shader = Shader(code=code, args=args) shader.compile([tri_shader.shader]) shader.prepare(runtimes) shader.execute() min_dist = 150.0 hit = t.isect_b(ray, min_dist) hit2 = shader.get_value('ret') if hit2 == 0: hit2 = False elif hit2 == 1: hit2 = True else: raise ValueError("Unexpected value for isect flat triangle ", hit2) self.assertEqual(hit, hit2)
def test_isect_sph(self): sph_shader = Sphere.isect_shader('isect_sphere') sph_shader.compile() runtimes = [Runtime()] sph_shader.prepare(runtimes) code = """ min_dist = 99999.0 p1 = isect_sphere(ray, sphere, hitpoint, min_dist) """ direction = Vector3(-1.0, -1.0, -1.0) direction.normalize() ray = Ray(Vector3(5.0, 5.0, 5.0), direction) sphere = Sphere(Vector3(0.0, 0.0, 0.0), 2.0, 0) hitpoint = HitPoint(0.0, Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0), 6, 0.0, 0.0) r_arg = StructArg('ray', ray) sph_arg = StructArg('sphere', sphere) harg = StructArg('hitpoint', hitpoint) p1 = IntArg('p1', 6) args = [r_arg, sph_arg, harg, p1] shader = Shader(code=code, args=args) shader.compile([sph_shader.shader]) shader.prepare(runtimes) shader.execute() hp2 = sphere.isect(ray) hitpoint = shader.get_value('hitpoint') self.assertAlmostEqual(hp2.t, hitpoint.t) self.assertEqual(hp2.mat_idx, hitpoint.mat_idx) n1 = hp2.normal n2 = hitpoint.normal self.assertAlmostEqual(n1.x, n2.x) self.assertAlmostEqual(n1.y, n2.y) self.assertAlmostEqual(n1.z, n2.z) self.assertAlmostEqual(hitpoint.hit.x, hp2.hit.x) self.assertAlmostEqual(hitpoint.hit.y, hp2.hit.y) self.assertAlmostEqual(hitpoint.hit.z, hp2.hit.z) result = shader.get_value('p1') self.assertEqual(result, 1)
def bounding_sphere(self): if self._bbox is None: origin = Vector3(0.0, 0.0, 0.0) return Sphere(origin, radius=0.01, mat_idx=0) bbox = self._bbox dx = (bbox.x1 - bbox.x0)**2 dy = (bbox.y1 - bbox.y0)**2 dz = (bbox.z1 - bbox.z0)**2 distance = math.sqrt(dx + dy + dz) radius = distance / 2.0 ox = (bbox.x0 + bbox.x1) * 0.5 oy = (bbox.y0 + bbox.y1) * 0.5 oz = (bbox.z0 + bbox.z1) * 0.5 origin = Vector3(ox, oy, oz) return Sphere(origin, radius=radius, mat_idx=0)
def test_pinhole(self): cam = Camera(eye=Vector3(5.0, 5.0, 5.0), lookat=Vector3(0.0, 0.0, 0.0), distance=200) cam.load('pinhole') cam.prepare_standalone() sample = Sample(x=2.14, y=3.33, px=0.6, py=0.7, ix=2, iy=2, weight=1.0) r1 = cam.generate_ray(sample) r2 = self.generate_pinhole_ray(cam, sample) self.assertAlmostEqual(r1.origin.x, r2.origin.x) self.assertAlmostEqual(r1.origin.y, r2.origin.y) self.assertAlmostEqual(r1.origin.z, r2.origin.z) self.assertAlmostEqual(r1.direction.x, r2.direction.x) self.assertAlmostEqual(r1.direction.y, r2.direction.y) self.assertAlmostEqual(r1.direction.z, r2.direction.z)
def bbox(self): epsilon = 0.001 p = self.point ea = self.edge_a eb = self.edge_b p0X = min(p.x, p.x + ea.x, p.x + eb.x, p.x + ea.x + eb.x) - epsilon p1X = max(p.x, p.x + ea.x, p.x + eb.x, p.x + ea.x + eb.x) + epsilon p0Y = min(p.y, p.y + ea.y, p.y + eb.y, p.y + ea.y + eb.y) - epsilon p1Y = max(p.y, p.y + ea.y, p.y + eb.y, p.y + ea.y + eb.y) + epsilon p0Z = min(p.z, p.z + ea.z, p.z + eb.z, p.z + ea.z + eb.z) - epsilon p1Z = max(p.z, p.z + ea.z, p.z + eb.z, p.z + ea.z + eb.z) + epsilon p0 = Vector3(p0X, p0Y, p0Z) p1 = Vector3(p1X, p1Y, p1Z) return BBox(p0, p1)
def generate_pinhole_ray(self, camera, sample): direction = camera._u * sample.x + camera._v *\ sample.y - camera._w * camera._distance direction.normalize() eye = camera._eye origin = Vector3(eye.x, eye.y, eye.z) return Ray(origin, direction)
def argument_factory(name, cls): if cls == int: return IntArg(name, int()) elif cls == float: return FloatArg(name, float()) elif cls == Vector3: return Vec3Arg(name, Vector3.zero()) else: raise ValueError("Argument factory. Unsuported arugment type", name, cls)
def test_linear(self): sphere = Sphere(Vector3(0.0, 0.0, 0.0), 2.0, 0) mgr = ShapeManager() mgr.add('sph1', sphere) sphere2 = Sphere(Vector3(0.0, 2.0, 0.0), 3.0, 0) mgr.add('sph2', sphere2) isector = LinearIsect(mgr) runtimes = [Runtime()] direction = Vector3(-1.0, -1.0, -1.0) direction.normalize() ray = Ray(Vector3(5.0, 5.0, 5.0), direction) isector.compile() isector.prepare(runtimes) code = """ min_dist = 99999.0 p1 = isect_scene(ray, hitpoint, min_dist) """ direction = Vector3(-1.0, -1.0, -1.0) direction.normalize() ray = Ray(Vector3(5.0, 5.0, 5.0), direction) hitpoint = HitPoint(0.0, Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0), 6, 0.0, 0.0) r_arg = StructArg('ray', ray) harg = StructArg('hitpoint', hitpoint) p1 = IntArg('p1', 6) args = [r_arg, harg, p1] shader = Shader(code=code, args=args) shader.compile([isector.shader]) shader.prepare(runtimes) hp2 = isector.isect(ray) shader.execute() hitpoint = shader.get_value('hitpoint') self.assertAlmostEqual(hp2.t, hitpoint.t, places=5) self.assertEqual(hp2.mat_idx, hitpoint.mat_idx) n1 = hp2.normal n2 = hitpoint.normal self.assertAlmostEqual(n1.x, n2.x) self.assertAlmostEqual(n1.y, n2.y, places=6) self.assertAlmostEqual(n1.z, n2.z) self.assertAlmostEqual(hitpoint.hit.x, hp2.hit.x, places=6) self.assertAlmostEqual(hitpoint.hit.y, hp2.hit.y, places=6) self.assertAlmostEqual(hitpoint.hit.z, hp2.hit.z, places=6) result = shader.get_value('p1') self.assertEqual(result, 1)
def test_light_manager(self): sam_mgr = SampledManager() register_rgb_shadepoint() runtimes = [Runtime(), Runtime()] lgt = GeneralLight() lgt.load('point', sam_mgr, spectral=False) lgt2 = GeneralLight() lgt2.load('point', sam_mgr, spectral=False) lgt2.set_value('intensity', RGBSpectrum(0.3, 0.3, 0.3)) lgt2.set_value('position', Vector3(2.0, 2.0, 2.0)) mgr = LightManager() mgr.add('light1', lgt) mgr.add('light2', lgt2) mgr.compile_shaders(sam_mgr, spectral=False) mgr.prepare_shaders(runtimes) code = """ hp = HitPoint() hp.hit = (4.0, 5, 6) sp = ShadePoint() light_radiance(hp, sp, 1) wi = sp.wi n = number_of_lights() """ wi = Vec3Arg('wi', Vector3(0.0, 0.0, 0.0)) nlights = IntArg('n', 999) shader = Shader(code=code, args=[wi, nlights]) shader.compile(shaders=[mgr.rad_shader, mgr.nlights_shader]) shader.prepare(runtimes) shader.execute() wi = Vector3(2.0, 2.0, 2.0) - Vector3(4.0, 5.0, 6.0) wi.normalize() wi_s = shader.get_value('wi') self.assertAlmostEqual(wi.x, wi_s.x) self.assertAlmostEqual(wi.y, wi_s.y) self.assertAlmostEqual(wi.z, wi_s.z) self.assertEqual(2, shader.get_value('n'))
def test_material_sampling_manager(self): sam_mgr = SampledManager() register_rgb_shadepoint() runtimes = [Runtime(), Runtime()] mat = Material() mat.load('lambertian', sam_mgr, spectral=False) mat.set_value('diffuse', RGBSpectrum(0.2, 0.3, 0.4)) mgr = MaterialManager() mgr.add('material1', mat) mgr.compile_shaders(sam_mgr, spectral=False) mgr.prepare_shaders(runtimes) code = """ hp = HitPoint() hp.normal = (0.1, 0.4, 0.66) hp.normal = normalize(hp.normal) sp = ShadePoint() material_sampling(hp, sp, 0) pdf = sp.pdf wi = sp.wi spec = sp.material_reflectance """ pdf = FloatArg('pdf', 0.0) wi = Vec3Arg('wi', Vector3(0.0, 0.0, 0.0)) spec = RGBArg('spec', RGBSpectrum(0.5, 0.5, 0.5)) shader = Shader(code=code, args=[pdf, wi, spec]) shader.compile(shaders=[mgr.sampling_shader]) shader.prepare(runtimes) shader.execute() s = shader.get_value('pdf') print(s) s = shader.get_value('wi') print(s) s = shader.get_value('spec') print(s) normal = Vector3(0.1, 0.4, 0.66) normal.normalize() print(cos_hemisphere(r1=0.1, r2=0.06, normal=normal, e=1.0))
def __init__(self, eye, lookat, distance): self._eye = eye self._lookat = lookat self._up = Vector3(0.0, 1.0, 0.0) self._distance = float(distance) # distance of image plane form eye self._compute_uvw() path = os.path.dirname(__file__) path = os.path.join(path, 'cam_shaders') self._loader = Loader([path]) self._standalone = None self._shader_name = None
def prepare_standalone(self): runtimes = [Runtime()] self.compile() self.prepare(runtimes) code = """ ray = Ray() generate_ray(ray, sample) origin = ray.origin direction = ray.direction """ origin = Vec3Arg('origin', Vector3(0.0, 0.0, 0.0)) direction = Vec3Arg('direction', Vector3(0.0, 0.0, 0.0)) sample = Sample(0.0, 0.0, 0.0, 0.0, 0, 0, 0.0) sample_arg = StructArg('sample', sample) args = [origin, direction, sample_arg] self._standalone = shader = Shader(code=code, args=args) shader.compile([self.shader]) shader.prepare(runtimes)
def test_isect_b_rect(self): rect_shader = Rectangle.isect_b_shader('isect_b_rectangle') rect_shader.compile() runtimes = [Runtime()] rect_shader.prepare(runtimes) code = """ min_dist = 99999.0 p1 = isect_b_rectangle(ray, rectangle, min_dist) """ origin = Vector3(3.0, 2.5, 0.0) direction = Vector3(0.0, 0.1, 0.88) direction.normalize() ray = Ray(origin, direction) point = Vector3(0.0, 0.0, 55.92) e1 = Vector3(55.28, 0.0, 0.0) e2 = Vector3(0.0, 54.88, 0.0) normal = Vector3(0.0, 0.0, -1.0) rectangle = Rectangle(point, e1, e2, normal) r_arg = StructArg('ray', ray) sph_arg = StructArg('rectangle', rectangle) p1 = IntArg('p1', 6) args = [r_arg, sph_arg, p1] shader = Shader(code=code, args=args) shader.compile([rect_shader.shader]) shader.prepare(runtimes) shader.execute() result = shader.get_value('p1') self.assertEqual(result, 1)
def test_rgb_area_light(self): sam_mgr = SampledManager() register_rgb_shadepoint() point = Vector3(0.0, 0.0, 55.92) e1 = Vector3(55.28, 0.0, 0.0) e2 = Vector3(0.0, 54.88, 0.0) normal = Vector3(0.0, 0.0, -1.0) rectangle = Rectangle(point, e1, e2, normal) material = Material() material.load('lambertian_emiter', sam_mgr) e = RGBSpectrum(0.5, 0.5, 0.5) material.set_value('emission', e) runtimes = [Runtime()] lgt = AreaLight(shape=rectangle, material=material) lgt.load('general', sam_mgr, spectral=False) lgt.compile() lgt.prepare(runtimes) ptrs = lgt.shader.get_ptrs() ptr_func = PointerArg('ptr_func', ptrs[0]) spec = RGBArg('spec', RGBSpectrum(0.5, 0.5, 0.5)) wi = Vec3Arg('wi', Vector3(0.0, 0.0, 0.0)) pos = Vec3Arg('position', Vector3(0.0, 0.0, 0.0)) n = Vec3Arg('normal', Vector3(0.0, 0.0, 0.0)) pdf = FloatArg('pdf', 0.0) emission = RGBArg('emission', RGBSpectrum(0.0, 0.0, 0.0)) code = """ hp = HitPoint() hp.hit = (4.0, 5, 6) sp = ShadePoint() __light_radiance(hp, sp, ptr_func) spec = sp.light_intensity wi = sp.wi position = sp.light_position normal = sp.light_normal pdf = sp.light_pdf emission = sp.material_emission """ shader = Shader(code=code, args=[ptr_func, wi, spec, pos, n, pdf, emission]) shader.compile() shader.prepare(runtimes) shader.execute() print("Position ", shader.get_value('position')) print("Normal ", shader.get_value('normal')) print("Light pdf ", shader.get_value('pdf')) print("Emission ", shader.get_value('emission')) print("Wi ", shader.get_value('wi')) print("Intensity ", shader.get_value('spec'))
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 calculate_origin(mesh): bbox = mesh._grid.bbox wx = bbox.x1 - bbox.x0 wy = bbox.y1 - bbox.y0 wz = bbox.z1 - bbox.z0 cx = (bbox.x1 + bbox.x0) / 2.0 cy = (bbox.y1 + bbox.y0) / 2.0 cz = (bbox.z1 + bbox.z0) / 2.0 ox = cx - wx oy = cy - wy oz = cz - wz return Vector3(ox, oy, oz)
def visiblity_shader(self): func_args = [Vec3Arg('p1', Vector3(0.0, 0.0, 0.0)), Vec3Arg('p2', Vector3(0.0, 0.0, 0.0))] code = """ epsilon = 0.00001 direction = p2 - p1 len_squared = dot(direction, direction) distance = sqrt(len_squared) - epsilon ray = Ray() ray.origin = p1 ray.direction = normalize(direction) """ args = [] for shp_type in self.shp_mgr.shape_types(): code1, args1 = self._get_visibility_code(shp_type) args.extend(args1) code += code1 code += "\nreturn 1\n" shader = Shader(code=code, args=args, name='visibility', func_args=func_args, is_func=True) return shader
def _value_factory(old_val, val, color_mgr, light=False): if isinstance(old_val, Vector3): return Vector3(float(val[0]), float(val[1]), float(val[2])) elif isinstance(old_val, RGBSpectrum): #TODO parsing of spectrum values from file return RGBSpectrum(float(val[0]), float(val[1]), float(val[2])) elif isinstance(old_val, SampledSpectrum): #TODO parsing of spectrum values from file s = RGBSpectrum(float(val[0]), float(val[1]), float(val[2])) return color_mgr.rgb_to_sampled(s, illum=light) elif isinstance(old_val, float): return float(val[0]) else: raise ValueError("Unknown type ", old_val)
def test_isect_flat_triangle1(self): runtimes = [Runtime()] tri_shader = FlatTriangle.isect_shader('isect_flat_triangle') tri_shader.compile() tri_shader.prepare(runtimes) p0 = Vector3(-0.0831229984, 0.0591476, -0.03213749) p1 = Vector3(-0.082775, 0.059025, -0.031787) p2 = Vector3(-0.0831229, 0.0591773, -0.031787) origin = Vector3(-0.21276909825205803, -0.021492251798510553, -0.09822520208358765) direction = Vector3(0.7788769269741005, 0.4843782624535974, 0.3984073687694737) ray = Ray(origin, direction) hitpoint = HitPoint(0.0, Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0), 6, 0.0, 0.0) t = FlatTriangle(p0, p1, p2) code = """ min_dist = 150.0 ret = isect_flat_triangle(ray, triangle, hitpoint, min_dist) """ r_arg = StructArg('ray', ray) tri_arg = StructArg('triangle', t) harg = StructArg('hitpoint', hitpoint) ret = IntArg('ret', 6) args = [r_arg, tri_arg, harg, ret] shader = Shader(code=code, args=args) shader.compile([tri_shader.shader]) shader.prepare(runtimes) shader.execute() min_dist = 150.0 hit = t.isect(ray, min_dist) hp = shader.get_value('hitpoint') self.assertAlmostEqual(hit.t, hp.t, places=6) self._almost_equal_vec3(hit.hit, hp.hit, places=6) self._almost_equal_vec3(hit.normal, hp.normal, places=6) self.assertEqual(hit.mat_idx, hp.mat_idx) self.assertAlmostEqual(hit.u, hp.u) self.assertAlmostEqual(hit.v, hp.v)
def test_isect_flat_triangle(self): runtimes = [Runtime()] tri_shader = FlatTriangle.isect_shader('isect_flat_triangle') tri_shader.compile() tri_shader.prepare(runtimes) p0 = Vector3(2.2, 4.4, 6.6) p1 = Vector3(1.1, 1.1, 1.1) p2 = Vector3(5.1, -1.1, 5.1) origin = Vector3(0.0, 0.0, 0.0) direction = Vector3(3, 3.0, 3.01) direction.normalize() ray = Ray(origin, direction) hitpoint = HitPoint(0.0, Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0), 6, 0.0, 0.0) t = FlatTriangle(p0, p1, p2) code = """ min_dist = 150.0 ret = isect_flat_triangle(ray, triangle, hitpoint, min_dist) """ r_arg = StructArg('ray', ray) tri_arg = StructArg('triangle', t) harg = StructArg('hitpoint', hitpoint) ret = IntArg('ret', 6) args = [r_arg, tri_arg, harg, ret] shader = Shader(code=code, args=args) shader.compile([tri_shader.shader]) shader.prepare(runtimes) shader.execute() min_dist = 150.0 hit = t.isect(ray, min_dist) hp = shader.get_value('hitpoint') self.assertAlmostEqual(hit.t, hp.t, places=6) self._almost_equal_vec3(hit.hit, hp.hit, places=6) self._almost_equal_vec3(hit.normal, hp.normal, places=6) self.assertEqual(hit.mat_idx, hp.mat_idx) self.assertAlmostEqual(hit.u, hp.u) self.assertAlmostEqual(hit.v, hp.v)
def _rgb_to_sampled_shader(sampled_mgr): code = """ # conversion of reflectance r = color[0] g = color[1] b = color[2] tmp = min(r, g) tmp = min(tmp, b) if tmp == r: # Compute illuminant with red as minimum rez = r * spect_white if g > b: rez = rez + (b - r) * spect_cyan rez = rez + (g - b) * spect_green else: rez = rez + (g - r) * spect_cyan rez = rez + (b - g) * spect_blue else: dummy = 999999 # elif is still not supported in shading language if tmp == g: # Compute illuminant with green as minimum rez = g * spect_white if r > b: rez = rez + (b - g) * spect_magenta rez = rez + (r - b) * spect_red else: rez = rez + (r - g) * spect_magenta rez = rez + (b - r) * spect_blue else: # Compute illuminant with blue as minimum rez = b * spect_white if r > g: rez = rez + (g - b) * spect_yellow rez = rez + (r - g) * spect_red else: rez = rez + (r - b) * spect_yellow rez = rez + (g - r) * spect_green rez = rez * 0.94 #rez.clamp() return rez """ vec = Vec3Arg('color', Vector3.zero()) spect_cyan = SampledArg('spect_cyan', sampled_mgr._spect_cyan) spect_blue = SampledArg('spect_blue', sampled_mgr._spect_blue) spect_green = SampledArg('spect_green', sampled_mgr._spect_green) spect_magenta = SampledArg('spect_magenta', sampled_mgr._spect_magenta) spect_red = SampledArg('spect_red', sampled_mgr._spect_red) spect_yellow = SampledArg('spect_yellow', sampled_mgr._spect_yellow) spect_white = SampledArg('spect_white', sampled_mgr._spect_white) args = [spect_cyan, spect_blue, spect_green, spect_magenta, spect_red, spect_yellow, spect_white] shader = Shader(code=code, args=args, name='rgb_to_spectrum', func_args=[vec], is_func=True) return shader