Example #1
0
    def load(self, shader_name, color_mgr, image_factory=None):
        self._color_mgr = color_mgr
        self._shader_name = shader_name
        self._image_factory = image_factory

        code = self._loader.load(shader_name, 'bsdf.py')
        if code is None:
            raise ValueError("bsdf.py in %s doesnt exist!" % shader_name)

        args = self._load_args()
        name = 'material_%i' % id(args)
        s = color_mgr.zero()
        func_args = self._func_args(s)
        self._bsdf_shader = Shader(code=code,
                                   args=args,
                                   name=name,
                                   func_args=func_args,
                                   is_func=True)

        #Sampling shader
        code = self._loader.load(self._shader_name, 'sample.py')
        if code is None:
            code = self._default_sampling()

        args = self._load_args()
        ptr_mat_bsdf = PointerArg('ptr_mat_bsdf', 0)
        ptr_mat_pdf = PointerArg('ptr_mat_pdf', 0)
        ptr_bsdf = ArgList('ptr_mat_bsdf', [ptr_mat_bsdf])
        ptr_pdf = ArgList('ptr_mat_pdf', [ptr_mat_pdf])
        args.append(ptr_bsdf)
        args.append(ptr_pdf)

        name = 'material_sampling_%i' % id(args)
        func_args = self._func_args(s)
        self._sampling_shader = Shader(code=code,
                                       args=args,
                                       name=name,
                                       func_args=func_args,
                                       is_func=True)

        #material pdf
        code = self._loader.load(self._shader_name, 'pdf.py')
        if code is None:
            code = self._default_pdf()
        args = self._load_args()
        name = 'material_pdf_%i' % id(args)
        func_args = self._func_args(s)
        self._pdf_shader = Shader(code=code,
                                  args=args,
                                  name=name,
                                  func_args=func_args,
                                  is_func=True)
Example #2
0
def test_mesh_b(mesh, nrays=1):
    dep_shader = type(mesh).isect_b_shader('ray_flat_mesh_b_isect')
    dep_shader.compile()
    runtimes = [Runtime()]
    dep_shader.prepare(runtimes)

    code = """
min_dist = 99999.0
ret = ray_flat_mesh_b_isect(ray, mesh, min_dist)
    """

    origin = calculate_origin(mesh)
    rpoint = random_in_bbox(mesh._grid.bbox)
    direction = rpoint - origin
    direction.normalize()

    ray = Ray(origin, direction)
    r_arg = StructArg('ray', ray)
    mesh_arg = StructArg('mesh', mesh)
    ret = IntArg('ret', 6)

    args = [r_arg, mesh_arg, ret]
    shader = Shader(code=code, args=args)
    shader.compile([dep_shader.shader])

    shader.prepare(runtimes)

    hp = mesh.isect_b(ray)
    shader.execute()
    print("Bool isect", hp, shader.get_value('ret'))
Example #3
0
    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)
Example #4
0
    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)
Example #5
0
def _rgb_luminance_shader(rgb_mgr):
    code = """
return rgb[0] * 0.212671 + rgb[1] * 0.715160 + rgb[2] * 0.072169
    """
    rgb = RGBArg('rgb', RGBSpectrum(0.0, 0.0, 0.0))
    shader = Shader(code=code, name='luminance', func_args=[rgb], is_func=True)
    return shader
Example #6
0
    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)
Example #7
0
def _sampled_to_rgb_shader(sampled_mgr):
    code = """
x = cie_x * spec
y = cie_y * spec
z = cie_z * spec

X = sum_samples(x) * scale
Y = sum_samples(y) * scale
Z = sum_samples(z) * scale

r = 3.240479 * X - 1.537150 * Y - 0.498535 * Z
g = -0.969256 * X + 1.875991 * Y + 0.041556 * Z
b = 0.055648 * X - 0.204043 * Y + 1.057311 * Z

return float3(r, g, b)
    """

    spec = sampled_mgr.zero()
    spec_arg = SampledArgPtr('spec', 0, spec)

    scale = (sampled_mgr.end - sampled_mgr.start) / (sampled_mgr.yint *
                                                     sampled_mgr.nsamples)
    p1 = FloatArg('scale', scale)
    cie_x = SampledArg('cie_x', sampled_mgr._cie_x)
    cie_y = SampledArg('cie_y', sampled_mgr._cie_y)
    cie_z = SampledArg('cie_z', sampled_mgr._cie_z)

    shader = Shader(code=code,
                    args=[p1, cie_x, cie_y, cie_z],
                    name='spectrum_to_rgb',
                    func_args=[spec_arg],
                    is_func=True)
    return shader
Example #8
0
    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
Example #9
0
    def atest_material_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()
sp = ShadePoint()
material_reflectance(hp, sp, 0)
spec = sp.material_reflectance
        """
        spec = RGBArg('spec', RGBSpectrum(0.5, 0.5, 0.5))
        shader = Shader(code=code, args=[spec])
        shader.compile(shaders=[mgr.ref_shader])
        shader.prepare(runtimes)
        shader.execute()

        s = shader.get_value('spec')
        ls = RGBSpectrum(0.2, 0.3, 0.4) * (1.0 / math.pi)

        self.assertAlmostEqual(s.r, ls.r)
        self.assertAlmostEqual(s.g, ls.g)
        self.assertAlmostEqual(s.b, ls.b)
Example #10
0
    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)
Example #11
0
    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
Example #12
0
    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)
Example #13
0
    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)
Example #14
0
    def load(self, shader_name, color_mgr):
        self._color_mgr = color_mgr
        self._shader_name = shader_name

        args = self._load_args()

        ptr_lgt_sample = PointerArg('ptr_light_sample', 0)
        lgt_sample = ArgList('ptr_light_sample', [ptr_lgt_sample])
        args.append(lgt_sample)
        ptr_mat_emission = PointerArg('ptr_mat_emission', 0)
        mat_emission = ArgList('ptr_mat_emission', [ptr_mat_emission])
        args.append(mat_emission)

        code = self._loader.load(shader_name, 'code.py')
        if code is None:
            raise ValueError("code.py in %s shader dont exist!" % shader_name)

        name = 'light_%i' % id(args)
        func_args = self._func_args(color_mgr.zero())
        self.shader = Shader(code=code,
                             args=args,
                             name=name,
                             func_args=func_args,
                             is_func=True)

        # area light emission shader
        name = 'light_emission%i' % id(args)
        func_args = self._func_args(color_mgr.zero())
        args = []
        ptr_lgt_pdf = PointerArg('ptr_light_pdf', 0)
        lgt_pdf = ArgList('ptr_light_pdf', [ptr_lgt_pdf])
        args.append(lgt_pdf)
        ptr_mat_emission = PointerArg('ptr_mat_emission', 0)
        mat_emission = ArgList('ptr_mat_emission', [ptr_mat_emission])
        args.append(mat_emission)
        code = """
__light_pdf(hitpoint, shadepoint, ptr_light_pdf)
__material_emission(hitpoint, shadepoint, ptr_mat_emission)
        """
        self.emission_shader = Shader(code=code,
                                      args=args,
                                      name=name,
                                      func_args=func_args,
                                      is_func=True)
Example #15
0
    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)
Example #16
0
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
Example #17
0
def _rgb_to_rgb_shader(rgb_mgr):
    code = """
return float3(rgb[0], rgb[1], rgb[2])
    """
    rgb = RGBArg('rgb', RGBSpectrum(0.0, 0.0, 0.0))
    shader = Shader(code=code,
                    name='spectrum_to_rgb',
                    func_args=[rgb],
                    is_func=True)
    return shader
Example #18
0
    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'))
Example #19
0
    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)
Example #20
0
    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)
Example #21
0
    def test_isect_rect(self):
        rect_shader = Rectangle.isect_shader('isect_rectangle')
        rect_shader.compile()
        runtimes = [Runtime()]
        rect_shader.prepare(runtimes)

        code = """
min_dist = 99999.0
p1 = isect_rectangle(ray, rectangle, hitpoint, 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)

        hitpoint = HitPoint.factory()
        hitpoint.mat_idx = 5

        r_arg = StructArg('ray', ray)
        sph_arg = StructArg('rectangle', rectangle)
        harg = StructArg('hitpoint', hitpoint)
        p1 = IntArg('p1', 6)

        args = [r_arg, sph_arg, harg, p1]
        shader = Shader(code=code, args=args)
        shader.compile([rect_shader.shader])
        shader.prepare(runtimes)

        shader.execute()
        hp2 = rectangle.isect(ray)

        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)
        self.assertAlmostEqual(n1.z, n2.z)
        self.assertAlmostEqual(hitpoint.hit.x, hp2.hit.x, places=5)
        self.assertAlmostEqual(hitpoint.hit.y, hp2.hit.y, places=5)
        self.assertAlmostEqual(hitpoint.hit.z, hp2.hit.z, places=5)

        result = shader.get_value('p1')
        self.assertEqual(result, 1)
Example #22
0
    def prepare_standalone(self):

        runtimes = [Runtime()]
        self.compile()
        self.prepare(runtimes)

        code = """
ret = generate_sample(sample)
        """
        args = [StructArg('sample', Sample.factory()), IntArg('ret', 11)]
        self._standalone = Shader(code=code, args=args)
        self._standalone.compile([self.shader])
        self._standalone.prepare(runtimes)
Example #23
0
 def wrapped_f(*args, **kwargs):
     arguments = []
     for arg in func_args:
         name, cls = arg
         a = argument_factory(name, cls)
         arguments.append(a)
     code = func(*args, **kwargs)
     name = func.__name__
     return Shader(code=code,
                   args=[],
                   name=name,
                   func_args=arguments,
                   is_func=True)
Example #24
0
    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)
Example #25
0
    def _lgt_radiance(self, color_mgr):
        code = """
ptr_func = lgt_ptrs[mat_idx]
__light_radiance(hitpoint, shadepoint, ptr_func)
        """
        lgt_ptrs = ArrayArg('lgt_ptrs', PtrsArray())
        al = ArgList('lgt_ptrs', [lgt_ptrs])
        func_args = self._func_args(color_mgr.zero())
        args = [al]
        self.rad_shader = Shader(code=code,
                                 args=args,
                                 name='light_radiance',
                                 func_args=func_args,
                                 is_func=True)
Example #26
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('------------------------------------------')
Example #27
0
    def test_sampled_point_light(self):
        sam_mgr = SampledManager()
        register_sampled_shadepoint(sam_mgr)

        runtimes = [Runtime()]
        lgt = GeneralLight()
        lgt.load('point', sam_mgr, spectral=True)
        lgt.set_value('intensity', RGBSpectrum(0.3, 0.3, 0.3))
        lgt.set_value('position', Vector3(2.0, 2.0, 2.0))
        lgt.compile()
        lgt.prepare(runtimes)
        ptrs = lgt.shader.get_ptrs()

        ptr_func = PointerArg('ptr_func', ptrs[0])
        spec = SampledArg('spec', sam_mgr.zero())
        wi = Vec3Arg('wi', Vector3(0.0, 0.0, 0.0))
        pos = Vec3Arg('position', Vector3(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
        """
        shader = Shader(code=code, args=[ptr_func, wi, spec, pos])
        shader.compile()
        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)

        p = Vector3(2.0, 2.0, 2.0)
        p_s = shader.get_value('position')
        self.assertAlmostEqual(p.x, p_s.x)
        self.assertAlmostEqual(p.y, p_s.y)
        self.assertAlmostEqual(p.z, p_s.z)

        s = sam_mgr.rgb_to_sampled(RGBSpectrum(0.3, 0.3, 0.3), illum=True)
        s_s = shader.get_value('spec')

        for i in range(len(s.samples)):
            self.assertAlmostEqual(s.samples[i], s_s.samples[i])
Example #28
0
    def _mtl_pdf(self, color_mgr):
        code = """
ptr_func = mtl_pdf_ptrs[mat_idx]
__material_pdf(hitpoint, shadepoint, ptr_func)
        """
        pdf_ptrs = ArrayArg('mtl_pdf_ptrs', PtrsArray())
        al = ArgList('mtl_pdf_ptrs', [pdf_ptrs])
        func_args = self._func_args(color_mgr.zero())
        args = [al]
        self.pdf_shader = Shader(code=code,
                                 args=args,
                                 name='material_pdf',
                                 func_args=func_args,
                                 is_func=True)
Example #29
0
    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)
Example #30
0
    def load(self, name):
        props = self._loader.load(name, 'props.txt')
        args = []
        if props is not None:
            args = parse_args(props)

        in_img = StructArg('input_image', ImagePRGBA(1, 1))
        out_img = StructArg('output_image', ImagePRGBA(1, 1))
        args.extend([in_img, out_img])

        code = self._loader.load(name, 'code.py')
        self._shader = Shader(code=code, args=args)
        self._shader.compile()
        self._runtime = Runtime()
        self._shader.prepare([self._runtime])