class SampleFilter: def __init__(self, xwidth=1.0, ywidth=1.0): path = os.path.dirname(__file__) path = os.path.join(path, 'flt_shaders') self._loader = Loader([path]) self.xwidth = xwidth self.ywidth = ywidth 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 compile(self, shaders=[]): self.shader.compile(shaders) def prepare(self, runtimes): self.shader.prepare(runtimes)
def __init__(self): path = os.path.dirname(__file__) path = os.path.join(path, 'shaders') self._loader = Loader([path]) self._shader = None self._runtime = None
def __init__(self, xwidth=1.0, ywidth=1.0): path = os.path.dirname(__file__) path = os.path.join(path, 'flt_shaders') self._loader = Loader([path]) self.xwidth = xwidth self.ywidth = ywidth
def __init__(self): path = os.path.dirname(__file__) path = os.path.join(path, 'mat_shaders') self._loader = Loader([path]) self._bsdf_shader = None self._sampling_shader = None self._shader_name = None
def __init__(self, shape, material): path = os.path.dirname(__file__) path = os.path.join(path, 'area_light_shaders') self._loader = Loader([path]) self.shape = shape self.material = material self.shader = None
class Tmo: def __init__(self): path = os.path.dirname(__file__) path = os.path.join(path, 'shaders') self._loader = Loader([path]) self._shader = None self._runtime = None @property def shader(self): return self._shader 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]) def tmo(self, in_img, out_img): """ Perform tone mapping on input image Args: in_img: Input image out_img: Output image """ if self._shader is None: raise ValueError("Shader is not loaded.") if not isinstance(in_img, ImagePRGBA) and not isinstance( out_img, ImagePRGBA): raise ValueError("ImagePRGBA is expected insted of", in_img, out_img) w1, h1 = in_img.size() w2, h2 = out_img.size() if w1 != w2 or h1 != h2: raise ValueError("Input and output image must be same size!") self._shader.set_value('input_image', in_img) self._shader.set_value('output_image', out_img) self._shader.execute()
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
class Tmo: def __init__(self): path = os.path.dirname(__file__) path = os.path.join(path, 'shaders') self._loader = Loader([path]) self._shader = None self._runtime = None @property def shader(self): return self._shader 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]) def tmo(self, in_img, out_img): """ Perform tone mapping on input image Args: in_img: Input image out_img: Output image """ if self._shader is None: raise ValueError("Shader is not loaded.") if not isinstance(in_img, ImagePRGBA) and not isinstance(out_img, ImagePRGBA): raise ValueError("ImagePRGBA is expected insted of", in_img, out_img) w1, h1 = in_img.size() w2, h2 = out_img.size() if w1 != w2 or h1 != h2: raise ValueError("Input and output image must be same size!") self._shader.set_value('input_image', in_img) self._shader.set_value('output_image', out_img) self._shader.execute()
class EnvironmentLight(Light): def __init__(self): path = os.path.dirname(__file__) path = os.path.join(path, 'env_shaders') self._loader = Loader([path]) def _func_args(self, spectrum): func_args = [ StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spectrum)) ] return func_args def load(self, shader_name, color_mgr): self._color_mgr = color_mgr self._shader_name = shader_name args = [] text = self._loader.load(shader_name, 'props.txt') if text is not None: args = parse_args(text, color_mgr) code = self._loader.load(shader_name, 'env.py') if code is None: raise ValueError("env.py in %s shader dont exist!" % shader_name) name = 'env_light_%i' % id(args) func_args = self._func_args(color_mgr.zero()) self.env_shader = Shader(code=code, args=args, name='environment_emission', func_args=func_args, is_func=True) def get_value(self, name): if self.env_shader is None: raise ValueError("Environment shader is not loaded!") return self.env_shader.get_value(name) def set_value(self, name, val): if self.env_shader is None: raise ValueError("Environment shader is not loaded!") if isinstance(val, (RGBSpectrum, SampledSpectrum)): val = self._color_mgr.convert_spectrum(val, illum=True) self.env_shader.set_value(name, val)
class EnvironmentLight(Light): def __init__(self): path = os.path.dirname(__file__) path = os.path.join(path, 'env_shaders') self._loader = Loader([path]) def _func_args(self, spectrum): func_args = [StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spectrum))] return func_args def load(self, shader_name, color_mgr): self._color_mgr = color_mgr self._shader_name = shader_name args = [] text = self._loader.load(shader_name, 'props.txt') if text is not None: args = parse_args(text, color_mgr) code = self._loader.load(shader_name, 'env.py') if code is None: raise ValueError("env.py in %s shader dont exist!" % shader_name) name = 'env_light_%i' % id(args) func_args = self._func_args(color_mgr.zero()) self.env_shader = Shader(code=code, args=args, name='environment_emission', func_args=func_args, is_func=True) def get_value(self, name): if self.env_shader is None: raise ValueError("Environment shader is not loaded!") return self.env_shader.get_value(name) def set_value(self, name, val): if self.env_shader is None: raise ValueError("Environment shader is not loaded!") if isinstance(val, (RGBSpectrum, SampledSpectrum)): val = self._color_mgr.convert_spectrum(val, illum=True) self.env_shader.set_value(name, val)
class Integrator: def __init__(self): path = os.path.dirname(__file__) path = os.path.join(path, 'int_shaders') self._loader = Loader([path]) def load(self, shader_name, color_mgr): text = self._loader.load(shader_name, 'props.txt') args = [] if text is not None: args = parse_args(text) code = self._loader.load(shader_name, 'code.py') if code is None: raise ValueError("Integrator %s code is missing!" % shader_name) #array of shadepoints for debuging purpose sp = ShadePoint.factory(color_mgr.zero()) arr = ObjArray(sp) for i in range(10): arr.append(sp) arg = ArrayArg('path_array', arr) args.append(arg) hdr_buffer = StructArg('hdr_buffer', ImagePRGBA(1, 1)) args.append(hdr_buffer) self.shader = Shader(code=code, args=args) self._color_mgr = color_mgr def compile(self, shaders=[]): self.shader.compile(shaders, color_mgr=self._color_mgr) def prepare(self, runtimes): self.shader.prepare(runtimes) def execute(self, hdr_buffer): self.shader.set_value('hdr_buffer', hdr_buffer) self.shader.execute()
class AreaLight(Light): def __init__(self, shape, material): path = os.path.dirname(__file__) path = os.path.join(path, 'area_light_shaders') self._loader = Loader([path]) self.shape = shape self.material = material self.shader = None def _func_args(self, spectrum): func_args = [StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spectrum))] return func_args def _load_args(self): args = [] text = self._loader.load(self._shader_name, 'props.txt') if text is not None: args = parse_args(text, self._color_mgr) return args 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) def compile(self, shaders=[]): self.shader.compile(shaders, color_mgr=self._color_mgr) self.emission_shader.compile(shaders, color_mgr=self._color_mgr) spec = self._color_mgr.zero() self.light_sample_shader = self.shape.light_sample(spec) self.light_sample_shader.compile(shaders, color_mgr=self._color_mgr) self.light_pdf_shader = self.shape.light_pdf(spec) self.light_pdf_shader.compile(shaders, color_mgr=self._color_mgr) self._emission_shader = self.material.emission_shader() self._emission_shader.compile(shaders, color_mgr=self._color_mgr) def prepare(self, runtimes): self.light_sample_shader.prepare(runtimes) ptrs = self.light_sample_shader.get_ptrs() args = [PointerArg('ptr_light_sample', p) for p in ptrs] lgt_sample = self.shader._get_arg('ptr_light_sample') lgt_sample.resize(args) self.light_pdf_shader.prepare(runtimes) ptrs = self.light_pdf_shader.get_ptrs() args = [PointerArg('ptr_light_pdf', p) for p in ptrs] lgt_pdf = self.emission_shader._get_arg('ptr_light_pdf') lgt_pdf.resize(args) self._emission_shader.prepare(runtimes) self.material.sync_shader_props(self._emission_shader) ptrs = self._emission_shader.get_ptrs() args = [PointerArg('ptr_mat_emission', p) for p in ptrs] ptr_emission = self.shader._get_arg('ptr_mat_emission') ptr_emission.resize(args) args = [PointerArg('ptr_mat_emission', p) for p in ptrs] ptr_emission = self.emission_shader._get_arg('ptr_mat_emission') ptr_emission.resize(args) self.shader.prepare(runtimes) self.emission_shader.prepare(runtimes) def get_value(self, name): if self.shader is None: raise ValueError("Light shader is not loaded!") return self.shader.get_value(name) def set_value(self, name, val): if self.shader is None: raise ValueError("Light shader is not loaded!") if isinstance(val, (RGBSpectrum, SampledSpectrum)): val = self._color_mgr.convert_spectrum(val, illum=True) self.shader.set_value(name, val)
def __init__(self): path = os.path.dirname(__file__) path = os.path.join(path, 'int_shaders') self._loader = Loader([path])
class Material: def __init__(self): path = os.path.dirname(__file__) path = os.path.join(path, 'mat_shaders') self._loader = Loader([path]) self._bsdf_shader = None self._sampling_shader = None self._shader_name = None def is_emissive(self): if self._shader_name is None: return False return self._loader.exist(self._shader_name, 'emission.py') def _func_args(self, spectrum): func_args = [ StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spectrum)) ] return func_args def _load_args(self): args = [] text = self._loader.load(self._shader_name, 'props.txt') if text is not None: args = parse_args(text, self._color_mgr, image_factory=self._image_factory) return args 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) def _default_sampling(self): code = """ r1 = random() r2 = random() e = 1.0 phi = 2.0 * 3.14159 * r1 exponent = 1.0 / (e + 1.0) cos_theta = pow(r2, exponent) tmp = 1.0 - cos_theta * cos_theta sin_theta = sqrt(tmp) sin_phi = sin(phi) cos_phi = cos(phi) pu = sin_theta * cos_phi pv = sin_theta * sin_phi pw = cos_theta w = hitpoint.normal tv = (0.0034, 1.0, 0.0071) v = cross(tv, w) v = normalize(v) u = cross(v, w) ndir = u * pu + v * pv + w * pw shadepoint.wi = normalize(ndir) __material_pdf(hitpoint, shadepoint, ptr_mat_pdf) __material_reflectance(hitpoint, shadepoint, ptr_mat_bsdf) """ return code def _default_pdf(self): code = """ shadepoint.pdf = dot(hitpoint.normal, shadepoint.wi) * 0.318309886 """ return code def compile(self, shaders=[]): self._bsdf_shader.compile(shaders, color_mgr=self._color_mgr) self._sampling_shader.compile(shaders, color_mgr=self._color_mgr) self._pdf_shader.compile(shaders, color_mgr=self._color_mgr) def prepare(self, runtimes): self._bsdf_shader.prepare(runtimes) self._pdf_shader.prepare(runtimes) ptrs = self._bsdf_shader.get_ptrs() args = [PointerArg('ptr_mat_bsdf', p) for p in ptrs] ptr_bsdf = self._sampling_shader._get_arg('ptr_mat_bsdf') ptr_bsdf.resize(args) ptrs = self._pdf_shader.get_ptrs() args = [PointerArg('ptr_mat_pdf', p) for p in ptrs] ptr_pdf = self._sampling_shader._get_arg('ptr_mat_pdf') ptr_pdf.resize(args) self._sampling_shader.prepare(runtimes) def emission_shader(self, shaders=[]): args = self._load_args() code = self._loader.load(self._shader_name, 'emission.py') if code is None: raise ValueError("emission.py in %s dont exist!" % self._shader_name) name = 'material_emission_%i' % id(args) s = self._color_mgr.zero() func_args = self._func_args(s) emission_shader = Shader(code=code, args=args, name=name, func_args=func_args, is_func=True) return emission_shader def sync_shader_props(self, shader): for arg in shader.args: val = self.get_value(arg.name) shader.set_value(arg.name, val) def set_value(self, name, val): if self._bsdf_shader is None: raise ValueError("Material shader is not loaded!") if isinstance(val, (RGBSpectrum, SampledSpectrum)): val = self._color_mgr.convert_spectrum(val) self._bsdf_shader.set_value(name, val) self._sampling_shader.set_value(name, val) self._pdf_shader.set_value(name, val) def get_value(self, name): if self._bsdf_shader is None: raise ValueError("Material shader is not loaded!") return self._bsdf_shader.get_value(name) def output(self, name): txt = 'Material\n' txt += 'type = %s\n' % self._shader_name txt += 'name = %s\n' % name args = self._load_args() for arg in args: value = self.get_value(arg.name) txt += output_arg(arg.name, value) txt += 'End\n' return txt
class Camera: 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 _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 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 compile(self, shaders=[]): self.shader.compile(shaders) def prepare(self, runtimes): self.shader.prepare(runtimes) 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 generate_ray(self, sample): self._standalone.set_value('sample', sample) self._standalone.execute() origin = self._standalone.get_value('origin') direction = self._standalone.get_value('direction') ray = Ray(origin, direction) return ray def output(self): type_name = 'Unknown' if self._shader_name is None else self._shader_name txt = 'Camera\n' txt += 'type = %s\n' % type_name txt += 'eye = %f, %f, %f\n' % (self._eye.x, self._eye.y, self._eye.z) txt += 'lookat = %f, %f, %f\n' % (self._lookat.x, self._lookat.y, self._lookat.z) txt += 'distance = %f\n' % self._distance txt += 'End\n' return txt
class AreaLight(Light): def __init__(self, shape, material): path = os.path.dirname(__file__) path = os.path.join(path, 'area_light_shaders') self._loader = Loader([path]) self.shape = shape self.material = material self.shader = None def _func_args(self, spectrum): func_args = [ StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spectrum)) ] return func_args def _load_args(self): args = [] text = self._loader.load(self._shader_name, 'props.txt') if text is not None: args = parse_args(text, self._color_mgr) return args 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) def compile(self, shaders=[]): self.shader.compile(shaders, color_mgr=self._color_mgr) self.emission_shader.compile(shaders, color_mgr=self._color_mgr) spec = self._color_mgr.zero() self.light_sample_shader = self.shape.light_sample(spec) self.light_sample_shader.compile(shaders, color_mgr=self._color_mgr) self.light_pdf_shader = self.shape.light_pdf(spec) self.light_pdf_shader.compile(shaders, color_mgr=self._color_mgr) self._emission_shader = self.material.emission_shader() self._emission_shader.compile(shaders, color_mgr=self._color_mgr) def prepare(self, runtimes): self.light_sample_shader.prepare(runtimes) ptrs = self.light_sample_shader.get_ptrs() args = [PointerArg('ptr_light_sample', p) for p in ptrs] lgt_sample = self.shader._get_arg('ptr_light_sample') lgt_sample.resize(args) self.light_pdf_shader.prepare(runtimes) ptrs = self.light_pdf_shader.get_ptrs() args = [PointerArg('ptr_light_pdf', p) for p in ptrs] lgt_pdf = self.emission_shader._get_arg('ptr_light_pdf') lgt_pdf.resize(args) self._emission_shader.prepare(runtimes) self.material.sync_shader_props(self._emission_shader) ptrs = self._emission_shader.get_ptrs() args = [PointerArg('ptr_mat_emission', p) for p in ptrs] ptr_emission = self.shader._get_arg('ptr_mat_emission') ptr_emission.resize(args) args = [PointerArg('ptr_mat_emission', p) for p in ptrs] ptr_emission = self.emission_shader._get_arg('ptr_mat_emission') ptr_emission.resize(args) self.shader.prepare(runtimes) self.emission_shader.prepare(runtimes) def get_value(self, name): if self.shader is None: raise ValueError("Light shader is not loaded!") return self.shader.get_value(name) def set_value(self, name, val): if self.shader is None: raise ValueError("Light shader is not loaded!") if isinstance(val, (RGBSpectrum, SampledSpectrum)): val = self._color_mgr.convert_spectrum(val, illum=True) self.shader.set_value(name, val)
class GeneralLight(Light): def __init__(self): path = os.path.dirname(__file__) path = os.path.join(path, 'light_shaders') self._loader = Loader([path]) self.shader = None def _func_args(self, spectrum): func_args = [ StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spectrum)) ] return func_args def load(self, shader_name, color_mgr): args = [] text = self._loader.load(shader_name, 'props.txt') if text is not None: args = parse_args(text, color_mgr) 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) self._color_mgr = color_mgr self._shader_name = shader_name def compile(self, shaders=[]): self.shader.compile(shaders, color_mgr=self._color_mgr) def prepare(self, runtimes): self.shader.prepare(runtimes) def get_value(self, name): if self.shader is None: raise ValueError("Light shader is not loaded!") return self.shader.get_value(name) def set_value(self, name, val): if self.shader is None: raise ValueError("Light shader is not loaded!") if isinstance(val, (RGBSpectrum, SampledSpectrum)): val = self._color_mgr.convert_spectrum(val, illum=True) self.shader.set_value(name, val) def output(self, name): txt = 'Light\n' txt += 'type = %s\n' % self._shader_name txt += 'name = %s\n' % name args = [] text = self._loader.load(self._shader_name, 'props.txt') if text is not None: args = parse_args(text) for arg in args: value = self.get_value(arg.name) txt += output_arg(arg.name, value) txt += 'End\n' return txt
class GeneralLight(Light): def __init__(self): path = os.path.dirname(__file__) path = os.path.join(path, 'light_shaders') self._loader = Loader([path]) self.shader = None def _func_args(self, spectrum): func_args = [StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spectrum))] return func_args def load(self, shader_name, color_mgr): args = [] text = self._loader.load(shader_name, 'props.txt') if text is not None: args = parse_args(text, color_mgr) 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) self._color_mgr = color_mgr self._shader_name = shader_name def compile(self, shaders=[]): self.shader.compile(shaders, color_mgr=self._color_mgr) def prepare(self, runtimes): self.shader.prepare(runtimes) def get_value(self, name): if self.shader is None: raise ValueError("Light shader is not loaded!") return self.shader.get_value(name) def set_value(self, name, val): if self.shader is None: raise ValueError("Light shader is not loaded!") if isinstance(val, (RGBSpectrum, SampledSpectrum)): val = self._color_mgr.convert_spectrum(val, illum=True) self.shader.set_value(name, val) def output(self, name): txt ='Light\n' txt +='type = %s\n' % self._shader_name txt +='name = %s\n' % name args = [] text = self._loader.load(self._shader_name, 'props.txt') if text is not None: args = parse_args(text) for arg in args: value = self.get_value(arg.name) txt += output_arg(arg.name, value) txt +='End\n' return txt
class Material: def __init__(self): path = os.path.dirname(__file__) path = os.path.join(path, 'mat_shaders') self._loader = Loader([path]) self._bsdf_shader = None self._sampling_shader = None self._shader_name = None def is_emissive(self): if self._shader_name is None: return False return self._loader.exist(self._shader_name, 'emission.py') def _func_args(self, spectrum): func_args = [StructArgPtr('hitpoint', HitPoint.factory()), StructArgPtr('shadepoint', ShadePoint.factory(spectrum))] return func_args def _load_args(self): args = [] text = self._loader.load(self._shader_name, 'props.txt') if text is not None: args = parse_args(text, self._color_mgr, image_factory=self._image_factory) return args 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) def _default_sampling(self): code = """ r1 = random() r2 = random() e = 1.0 phi = 2.0 * 3.14159 * r1 exponent = 1.0 / (e + 1.0) cos_theta = pow(r2, exponent) tmp = 1.0 - cos_theta * cos_theta sin_theta = sqrt(tmp) sin_phi = sin(phi) cos_phi = cos(phi) pu = sin_theta * cos_phi pv = sin_theta * sin_phi pw = cos_theta w = hitpoint.normal tv = (0.0034, 1.0, 0.0071) v = cross(tv, w) v = normalize(v) u = cross(v, w) ndir = u * pu + v * pv + w * pw shadepoint.wi = normalize(ndir) __material_pdf(hitpoint, shadepoint, ptr_mat_pdf) __material_reflectance(hitpoint, shadepoint, ptr_mat_bsdf) """ return code def _default_pdf(self): code = """ shadepoint.pdf = dot(hitpoint.normal, shadepoint.wi) * 0.318309886 """ return code def compile(self, shaders=[]): self._bsdf_shader.compile(shaders, color_mgr=self._color_mgr) self._sampling_shader.compile(shaders, color_mgr=self._color_mgr) self._pdf_shader.compile(shaders, color_mgr=self._color_mgr) def prepare(self, runtimes): self._bsdf_shader.prepare(runtimes) self._pdf_shader.prepare(runtimes) ptrs = self._bsdf_shader.get_ptrs() args = [PointerArg('ptr_mat_bsdf', p) for p in ptrs] ptr_bsdf = self._sampling_shader._get_arg('ptr_mat_bsdf') ptr_bsdf.resize(args) ptrs = self._pdf_shader.get_ptrs() args = [PointerArg('ptr_mat_pdf', p) for p in ptrs] ptr_pdf = self._sampling_shader._get_arg('ptr_mat_pdf') ptr_pdf.resize(args) self._sampling_shader.prepare(runtimes) def emission_shader(self, shaders=[]): args = self._load_args() code = self._loader.load(self._shader_name, 'emission.py') if code is None: raise ValueError("emission.py in %s dont exist!" % self._shader_name) name = 'material_emission_%i' % id(args) s = self._color_mgr.zero() func_args = self._func_args(s) emission_shader = Shader(code=code, args=args, name=name, func_args=func_args, is_func=True) return emission_shader def sync_shader_props(self, shader): for arg in shader.args: val = self.get_value(arg.name) shader.set_value(arg.name, val) def set_value(self, name, val): if self._bsdf_shader is None: raise ValueError("Material shader is not loaded!") if isinstance(val, (RGBSpectrum, SampledSpectrum)): val = self._color_mgr.convert_spectrum(val) self._bsdf_shader.set_value(name, val) self._sampling_shader.set_value(name, val) self._pdf_shader.set_value(name, val) def get_value(self, name): if self._bsdf_shader is None: raise ValueError("Material shader is not loaded!") return self._bsdf_shader.get_value(name) def output(self, name): txt = 'Material\n' txt += 'type = %s\n' % self._shader_name txt += 'name = %s\n' % name args = self._load_args() for arg in args: value = self.get_value(arg.name) txt += output_arg(arg.name, value) txt += 'End\n' return txt
class Camera: 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 _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 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 compile(self, shaders=[]): self.shader.compile(shaders) def prepare(self, runtimes): self.shader.prepare(runtimes) 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 generate_ray(self, sample): self._standalone.set_value('sample', sample) self._standalone.execute() origin = self._standalone.get_value('origin') direction = self._standalone.get_value('direction') ray = Ray(origin, direction) return ray def output(self): type_name = 'Unknown' if self._shader_name is None else self._shader_name txt = 'Camera\n' txt += 'type = %s\n' % type_name txt += 'eye = %f, %f, %f\n' % (self._eye.x, self._eye.y, self._eye.z) txt += 'lookat = %f, %f, %f\n' % (self._lookat.x, self._lookat.y, self._lookat.z) txt += 'distance = %f\n' % self._distance txt += 'End\n' return txt