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 load(self, shader_name): #TODO props props = self._loader.load(shader_name, 'props.txt') code = self._loader.load(shader_name, 'code.py') 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 = [w, u, v, distance, eye, lookat] origin = Vector3(0.0, 0.0, 0.0) direction = Vector3(0.0, 0.0, 0.0) ray = Ray(origin, direction) sample = Sample(0.0, 0.0, 0, 0, 0.0) func_args = [StructArgPtr('ray', ray), StructArgPtr('sample', sample)] self.shader = Shader(code=code, args=args, name='generate_ray', func_args=func_args, is_func=True) codepy = self._loader.load(shader_name, 'codepy.py') self._py_code = compile(codepy, 'codepy.py', 'exec')
def test_isect_b_sph(self): sph_shader = Sphere.isect_b_shader() 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.prepare(runtimes) shader.execute() result = shader.get_value('p1') self.assertEqual(result, 1)
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) isect_sph = Sphere.isect_shader() isect_sph.compile() isect_sph.prepare(runtimes) isect_shader = isector.isect_shader() isect_shader.compile([isect_sph]) isect_shader.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([isect_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_sampled_spec_to_vec(self): mgr = SampledManager() shader = sampled_to_vec_shader(mgr) shader.compile() runtime = Runtime() shader.prepare([runtime]) code = """ rez = spectrum_to_vec(r1) """ vals = [(450, 0.13), (480, 0.45), (620, 0.58)] samples = create_samples(vals, 32, 400, 700) sam_spec = SampledSpectrum(samples) s = SampledArg('r1', sam_spec) rez = Vec3Arg('rez', Vector3(0.0, 0.0, 0.0)) shader2 = Shader(code=code, args=[rez, s]) shader2.compile([shader]) shader2.prepare([runtime]) shader2.execute() val = shader2.get_value('rez') conv = mgr.sampled_to_rgb(sam_spec) self.assertAlmostEqual(val.x, conv.r, places=6) self.assertAlmostEqual(val.y, conv.g, places=6) self.assertAlmostEqual(val.z, conv.b, places=6)
def isect_b_shader(cls): 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 """ args = [] origin = Vector3(0.0, 0.0, 0.0) direction = Vector3(0.0, 0.0, 0.0) ray = Ray(origin, direction) sphere = Sphere(Vector3(0.0, 0.0, 0.0), 0.0, 0) func_args = [ StructArgPtr('ray', ray), StructArgPtr('sphere', sphere), FloatArg('min_dist', 0.0) ] shader = Shader(code=code, args=args, name='isect_b_sphere', func_args=func_args, is_func=True) return shader
def test_isect_sph(self): sph_shader = Sphere.isect_shader() 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.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 test_dot_fun(self): code = """ p3 = dot(p1, p2) """ v1 = Vector3(2.5, 1.8, 2.9) v2 = Vector3(2.2, 1.1, 5.22) p1 = Vec3Arg('p1', v1) p2 = Vec3Arg('p2', v2) p3 = FloatArg('p3', 2.2) shader = Shader(code=code, args=[p1, p2, p3]) shader.compile() shader.prepare([Runtime()]) shader.execute() rez = v1.dot(v2) p = shader.get_value('p3') self.assertAlmostEqual(rez, p)
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])
def test_normalize_fun(self): code = """ p2 = normalize(p1) """ v1 = Vector3(2.5, 1.8, 2.9) v2 = Vector3(6.5, 9.8, 3.9) p1 = Vec3Arg('p1', v1) p2 = Vec3Arg('p2', v2) shader = Shader(code=code, args=[p1, p2]) shader.compile() shader.prepare([Runtime()]) shader.execute() v3 = Vector3(2.5, 1.8, 2.9) v3.normalize() p = shader.get_value('p2') self.assertAlmostEqual(p.x, v3.x) self.assertAlmostEqual(p.y, v3.y) self.assertAlmostEqual(p.z, v3.z)
def test_arithmetic(self): code = """ a1 = 33 a2 = 22 p1 = a1 + a2 a1 = 22.3 a2 = 11.1 p2 = a1 + a2 a1 = 44 a2 = -2.36 p3 = a1 * a2 a1 = (2.3, 4) a2 = 5 p4 = a1 * a2 a1 = (3, 4, 6.6) a2 = (7, 4.3, 2.6) p5 = a1 - a2 a1 = (1, 4.1, 5.5, 9.9) a2 = (0.22, 3.3, 2.6, 6.6) p6 = a1 / a2 """ p1 = IntArg('p1', 33) p2 = FloatArg('p2', 55.5) p3 = FloatArg('p3', 55.5) p4 = Vec2Arg('p4', Vector2(0.0, 0.0)) p5 = Vec3Arg('p5', Vector3(0.0, 0.0, 0.0)) p6 = Vec4Arg('p6', Vector4(0.0, 0.0, 0.0, 0.0)) shader = Shader(code=code, args=[p1, p2, p3, p4, p5, p6]) shader.compile() shader.prepare([Runtime()]) shader.execute() rez = shader.get_value('p1') self.assertEqual(rez, 33 + 22) rez = shader.get_value('p2') self.assertAlmostEqual(rez, 22.3 + 11.1, places=5) rez = shader.get_value('p3') self.assertAlmostEqual(rez, 44 * -2.36, places=5) rez = shader.get_value('p4') self.assertAlmostEqual(rez.x, 5 * 2.3, places=5) self.assertAlmostEqual(rez.y, 5 * 4, places=5) rez = shader.get_value('p5') self.assertAlmostEqual(rez.x, 3 - 7.0, places=5) self.assertAlmostEqual(rez.y, 4 - 4.3, places=5) self.assertAlmostEqual(rez.z, 6.6 - 2.6, places=5) rez = shader.get_value('p6') self.assertAlmostEqual(rez.x, 1 / 0.22, places=5) self.assertAlmostEqual(rez.y, 4.1 / 3.3, places=5) self.assertAlmostEqual(rez.z, 5.5 / 2.6, places=5) self.assertAlmostEqual(rez.w, 9.9 / 6.6, places=5)
def test_corss_fun(self): code = """ p3 = cross(p1, p2) """ v1 = Vector3(2.5, 1.8, 2.9) v2 = Vector3(2.5, 1.8, 3.9) v3 = Vector3(0.0, 0.0, 0.0) p1 = Vec3Arg('p1', v1) p2 = Vec3Arg('p2', v2) p3 = Vec3Arg('p3', v3) shader = Shader(code=code, args=[p1, p2, p3]) shader.compile() shader.prepare([Runtime()]) shader.execute() v4 = v1.cross(v2) p = shader.get_value('p3') self.assertAlmostEqual(p.x, v4.x, places=6) self.assertAlmostEqual(p.y, v4.y, places=6) self.assertAlmostEqual(p.z, v4.z, places=6)
def test_struct(self): code = """ a1 = MyPoint() tmp = 66 a1.x = tmp p1 = a1.x tmp = 4.4 a1.y = tmp p2 = a1.y tmp = (6, 7) a1.k = tmp p3 = a1.k tmp = (3, 4, 6) a1.m = tmp p4 = a1.m tmp = (2, 4, 6, 7) a1.p = tmp p5 = a1.p """ #p1 = IntArg('p1', 44) #TODO implicit conversion int to float p1 = IntArg('p1', 44) p2 = FloatArg('p2', 2.2) p3 = Vec2Arg('p3', Vector2(5.5, 7.7)) p4 = Vec3Arg('p4', Vector3(2.2, 2.2, 4.4)) p5 = Vec4Arg('p5', Vector4(8.8, 5.5, 3.3, 1.1)) shader = Shader(code=code, args=[p1, p2, p3, p4, p5]) shader.compile() shader.prepare([Runtime()]) shader.execute() v = shader.get_value('p1') self.assertEqual(v, 66) v = shader.get_value('p2') self.assertAlmostEqual(v, 4.4, places=6) v = shader.get_value('p3') self.assertAlmostEqual(v.x, 6.0, places=6) self.assertAlmostEqual(v.y, 7.0, places=6) v = shader.get_value('p4') self.assertAlmostEqual(v.x, 3.0, places=6) self.assertAlmostEqual(v.y, 4.0, places=6) self.assertAlmostEqual(v.z, 6.0, places=6) v = shader.get_value('p5') self.assertAlmostEqual(v.x, 2.0, places=6) self.assertAlmostEqual(v.y, 4.0, places=6) self.assertAlmostEqual(v.z, 6.0, places=6) self.assertAlmostEqual(v.w, 7.0, places=6)
def test_float3(self): code = """ temp = 5 temp2 = 9.9 p1 = float3(temp, temp2, 2) tmp = 4.4 p2 = float3(tmp, 33, 2.1) """ p1 = Vec3Arg('p1', Vector3(0.0, 1.0, 0.0)) p2 = Vec3Arg('p2', Vector3(0.0, 1.0, 0.0)) shader = Shader(code=code, args=[p1, p2]) shader.compile() shader.prepare([Runtime()]) shader.execute() p = shader.get_value('p1') self.assertAlmostEqual(p.x, 5.0) self.assertAlmostEqual(p.y, 9.9, places=6) self.assertAlmostEqual(p.z, 2.0) p = shader.get_value('p2') self.assertAlmostEqual(p.x, 4.4, places=6) self.assertAlmostEqual(p.y, 33.0, places=6) self.assertAlmostEqual(p.z, 2.1, places=6)
def arg_from_value(name, value): if type(value) in _struct_desc: arg = StructArg(name, value) elif isinstance(value, int): arg = IntArg(name, value) elif isinstance(value, float): arg = FloatArg(name, value) elif isinstance(value, (list, tuple)): if len(value) == 2: arg = Vec2Arg(name, Vector2.create(*value)) elif len(value) == 3: arg = Vec3Arg(name, Vector3.create(*value)) elif len(value) == 4: arg = Vec4Arg(name, Vector4.create(*value)) else: raise ValueError("List or tuple is two big!", value) else: raise ValueError("Unknown type of value", type(value), value) return arg
def test_rgb_spec_to_vec(self): shader = rgb_to_vec_shader() shader.compile() runtime = Runtime() shader.prepare([runtime]) code = """ rez = spectrum_to_vec(r1) """ s = RGBArg('r1', RGBSpectrum(0.2, 0.3, 0.4)) rez = Vec3Arg('rez', Vector3(0.0, 0.0, 0.0)) shader2 = Shader(code=code, args=[rez, s]) shader2.compile([shader]) shader2.prepare([runtime]) shader2.execute() val = shader2.get_value('rez') self.assertAlmostEqual(val.x, 0.2) self.assertAlmostEqual(val.y, 0.3) self.assertAlmostEqual(val.z, 0.4)
def register_sampled_shadepoint(col_mgr): wi = Vector3(0.0, 0.0, 0.0) wo = Vector3(0.0, 0.0, 0.0) ref = col_mgr.zero() register_struct(ShadePoint, 'ShadePoint', fields=[('wi', Vec3Arg), ('wo', Vec3Arg), ('pdf', FloatArg), ('material_reflectance', SampledArg)], factory=lambda: ShadePoint(wi, wo, 0.0, ref)) wi = Vector3(0.0, 0.0, 0.0) wo = Vector3(0.0, 0.0, 0.0) ref = col_mgr.zero() sh = ShadePoint(wi, wo, 0.0, ref) hp = 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('hitpoint', hp), StructArgPtr('shadepoint', sh)] register_prototype('material_reflectance', func_args=func_args)
def test_assign(self): code = """ k = 55 p1 = k m = (4, 7) p2 = m h = (3.13, 8, 9) p3 = h t = (6.6, 2.2, 9, 1) p4 = t r = 3.3 p5 = r """ p1 = IntArg('p1', 33) p2 = Vec2Arg('p2', Vector2(9, 22)) p3 = Vec3Arg('p3', Vector3(1, 4.4, 29)) p4 = Vec4Arg('p4', Vector4(2, 3, 1.1, 2)) p5 = FloatArg('p5', 87.33) shader = Shader(code=code, args=[p1, p2, p3, p4, p5]) shader.compile() shader.prepare([Runtime()]) shader.execute() self.assertEqual(shader.get_value('p1'), 55) v = shader.get_value('p2') self.assertAlmostEqual(v.x, 4.0) self.assertAlmostEqual(v.y, 7.0) v = shader.get_value('p3') self.assertAlmostEqual(v.x, 3.13, places=6) self.assertAlmostEqual(v.y, 8.0) self.assertAlmostEqual(v.z, 9.0) v = shader.get_value('p4') self.assertAlmostEqual(v.x, 6.6, places=6) self.assertAlmostEqual(v.y, 2.2) self.assertAlmostEqual(v.z, 9.0) self.assertAlmostEqual(v.w, 1.0) v = shader.get_value('p5') self.assertAlmostEqual(v, 3.3)
def _compute_uvw(self): self._w = self._eye - self._lookat # w is in oposite direction of view self._w.normalize() self._u = self._up.cross(self._w) self._u.normalize() self._v = self._w.cross(self._u) #singularity if self._eye.x == self._lookat.x and self._eye.z == self._lookat.z\ and self._eye.y > self._lookat.y: # looking vertically down self._u = Vector3(0.0, 0.0, 1.0) self._v = Vector3(1.0, 0.0, 0.0) self._w = Vector3(0.0, 1.0, 0.0) if self._eye.x == self._lookat.x and self._eye.z == self._lookat.z\ and self._eye.y < self._lookat.y: # looking vertically up self._u = Vector3(1.0, 0.0, 0.0) self._v = Vector3(0.0, 0.0, 1.0) self._w = Vector3(0.0, -1.0, 0.0)
def test_cameras(self): eye = Vector3(0.0, 10.0, 5.0) lookat = Vector3(0.0, 0.0, 0.0) distance = 100.0 cam = Camera(eye, lookat, distance) cam.load('pinhole') cam.compile() runtimes = [Runtime()] cam.prepare(runtimes) code = """ ray = Ray() sample = Sample() sample.x = 2.2 sample.y = 2.5 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)) args = [origin, direction] shader = Shader(code=code, args=args) shader.compile([cam.shader]) shader.prepare(runtimes) shader.execute() ray = Ray(Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0)) sample = Sample(2.2, 2.5, 2, 2, 1.0) cam.execute_py(ray, sample) v = shader.get_value('origin') o = ray.origin self.assertAlmostEqual(v.x, o.x) self.assertAlmostEqual(v.y, o.y) self.assertAlmostEqual(v.z, o.z) v = shader.get_value('direction') d = ray.direction self.assertAlmostEqual(v.x, d.x) self.assertAlmostEqual(v.y, d.y) self.assertAlmostEqual(v.z, d.z)
from renlight.renderer.integrator import Integrator from renlight.renderer.sphere import Sphere from renlight.renderer.linear import LinearIsect from renlight.renderer.shp_mgr import ShapeManager from renlight.image import ImagePRGBA, ImageRGBA from renlight.renderer.blt_floatrgba import blt_prgba_to_rgba from renlight.win import show_image_in_window if __name__ == "__main__": runtimes = [Runtime()] sampler = Sampler() sampler.set_resolution(1024, 768) sampler.load('regular') eye = Vector3(0.0, 10.0, 5.0) lookat = Vector3(0.0, 0.0, 0.0) distance = 100.0 camera = Camera(eye, lookat, distance) camera.load('pinhole') #intersection mgr = ShapeManager() sphere = Sphere(Vector3(0.0, 0.0, 0.0), 2.0, 0) mgr.add('sph1', sphere) isector = LinearIsect(mgr) isector.compile() isector.prepare(runtimes) ###################
def conv_to_obj(cls, val): return Vector3(val[0], val[1], val[2])
from renlight.vector import Vector3 from renlight.sdl import register_struct, IntArg, FloatArg, Vec3Arg class HitPoint: __slots__ = ['t', 'hit', 'normal', 'mat_idx', 'u', 'v'] def __init__(self, t=0.0, hit=None, normal=None, mat_idx=0, u=0.0, v=0.0): self.t = t self.hit = hit self.normal = normal self.mat_idx = mat_idx self.u = u self.v = v register_struct(HitPoint, 'HitPoint', fields=[('t', FloatArg), ('hit', Vec3Arg), ('normal', Vec3Arg), ('mat_idx', IntArg), ('u', FloatArg), ('v', FloatArg)], factory=lambda: HitPoint(0.0, Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0), 0, 0.0, 0.0))
def __init__(self, name, value=Vector3(0.0, 0.0, 0.0)): super(Vec3Arg, self).__init__(name) assert Vector3 is type(value) self._value = value
def isect_shader(cls): 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.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.u = 0.0 hitpoint.v = 0.0 return 1 return 0 """ 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) sphere = Sphere(Vector3(0.0, 0.0, 0.0), 0.0, 0) func_args = [ StructArgPtr('ray', ray), StructArgPtr('sphere', sphere), StructArgPtr('hitpoint', hitpoint), FloatArg('min_dist', 0.0) ] shader = Shader(code=code, args=args, name='isect_sphere', func_args=func_args, is_func=True) return shader
from .loader import Loader from .shader import Shader from .builtins import * from .args import register_struct, Vec3Arg, StructArgPtr,\ StructArg, PointerArg, parse_args from renlight.ray import Ray from renlight.vector import Vector3 from renlight.image import ImageRGBA, ImagePRGBA register_struct( Ray, 'Ray', fields=[('origin', Vec3Arg), ('direction', Vec3Arg)], factory=lambda: Ray(Vector3(0.0, 0.0, 0.0), Vector3(0.0, 0.0, 0.0))) register_struct(ImageRGBA, 'ImageRGBA', fields=[('width', IntArg), ('height', IntArg), ('pitch', IntArg), ('pixels', PointerArg)], factory=lambda: ImageRGBA(1, 1)) register_struct(ImagePRGBA, 'ImagePRGBA', fields=[('width', IntArg), ('height', IntArg), ('pitch', IntArg), ('pixels', PointerArg)], factory=lambda: ImagePRGBA(1, 1))
temp = ray.origin - self.origin a = ray.direction.dot(ray.direction) b = temp.dot(ray.direction) * 2.0 c = temp.dot(temp) - self.radius * self.radius disc = b * b - 4.0 * a * c if disc < 0.0: return False else: e = math.sqrt(disc) denom = 2.0 * a t = (-b - e) / denom # smaller root if t > 0.0005 and t < min_dist: normal = (temp + ray.direction * t) * (1.0 / self.radius) hit_point = ray.origin + ray.direction * t return HitPoint(t, hit_point, normal, self.mat_idx, 0.0, 0.0) t = (-b + e) / denom # larger root if t > 0.0005 and t < min_dist: normal = (temp + ray.direction * t) * (1.0 / self.radius) hit_point = ray.origin + ray.direction * t return HitPoint(t, hit_point, normal, self.mat_idx, 0.0, 0.0) return False register_struct(Sphere, 'Sphere', fields=[('origin', Vec3Arg), ('radius', FloatArg), ('mat_idx', IntArg)], factory=lambda: Sphere(Vector3(0.0, 0.0, 0.0), 0.0, 0))
class MyPoint: def __init__(self, x, y, k, m, p): self.x = x self.y = y self.k = k self.m = m self.p = p register_struct( MyPoint, 'MyPoint', fields=[('x', IntArg), ('y', FloatArg), ('k', Vec2Arg), ('m', Vec3Arg), ('p', Vec4Arg)], factory=lambda: MyPoint(1, 2.0, Vector2(2.2, 3.3), Vector3(3.3, 5.5, 7.7), Vector4(3.3, 6.6, 8.8, 9.9))) class StructTests(unittest.TestCase): def test_struct(self): code = """ a1 = MyPoint() tmp = 66 a1.x = tmp p1 = a1.x tmp = 4.4 a1.y = tmp p2 = a1.y