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 load(self, shader_name): 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') self.shader = Shader(code=code, args=args)
def load(self, shader_name): text = self._loader.load(shader_name, "props.txt") # args = create_args(text) code = self._loader.load(shader_name, "code.py") sample = Sample(0.0, 0.0, 0, 0, 0.0) func_args = [StructArgPtr("sample", sample)] args = [ IntArg("width", self._width), IntArg("height", self._height), IntArg("pass_number", self._pass_number), FloatArg("pixel_size", self._pixel_size), ] tiles = create_tiles(self._width, self._height, self._nthreads) targs = [StructArg("tile", tile) for tile in tiles] targ = ArgList("tile", targs) args.append(targ) curx_args = [IntArg("curx", 0) for tile in tiles] curx_arg = ArgList("curx", curx_args) args.append(curx_arg) cury_args = [IntArg("cury", tile.y) for tile in tiles] cury_arg = ArgList("cury", cury_args) args.append(cury_arg) endx_args = [IntArg("endx", tile.width) for tile in tiles] endx_arg = ArgList("endx", endx_args) args.append(endx_arg) endy_args = [IntArg("endy", tile.y + tile.height) for tile in tiles] endy_arg = ArgList("endy", endy_args) args.append(endy_arg) self.shader = Shader(code=code, args=args, name="generate_sample", func_args=func_args, is_func=True)
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, spectrum): self.bsdf = None self.sampling = None self.emission = None code = self._loader.load(shader_name, 'bsdf.py') #TODO hitpoint, shadepoint function arguments if code is not None: args = self._load_args(shader_name) name = 'bsdf_' + str(id(args)) func_args = self._func_args(spectrum) self.bsdf = Shader(code=code, args=args, name=name, func_args=func_args, is_func=True) code = self._loader.load(shader_name, 'sampling.py') if code is not None: args = self._load_args(shader_name) self.sampling = Shader(code=code, args=args) code = self._loader.load(shader_name, 'emission.py') if code is not None: args = self._load_args(shader_name) self.emission = Shader(code=code, args=args)
def load(self, shader_name): text = self._loader.load(shader_name, 'props.txt') #args = create_args(text) code = self._loader.load(shader_name, 'code.py') sample = Sample(0.0, 0.0, 0, 0, 0.0) func_args = [StructArgPtr('sample', sample)] args = [ IntArg('width', self._width), IntArg('height', self._height), IntArg('pass_number', self._pass_number), FloatArg('pixel_size', self._pixel_size) ] tiles = create_tiles(self._width, self._height, self._nthreads) targs = [StructArg('tile', tile) for tile in tiles] targ = ArgList('tile', targs) args.append(targ) curx_args = [IntArg('curx', 0) for tile in tiles] curx_arg = ArgList('curx', curx_args) args.append(curx_arg) cury_args = [IntArg('cury', tile.y) for tile in tiles] cury_arg = ArgList('cury', cury_args) args.append(cury_arg) endx_args = [IntArg('endx', tile.width) for tile in tiles] endx_arg = ArgList('endx', endx_args) args.append(endx_arg) endy_args = [IntArg('endy', tile.y + tile.height) for tile in tiles] endy_arg = ArgList('endy', endy_args) args.append(endy_arg) self.shader = Shader(code=code, args=args, name='generate_sample', func_args=func_args, is_func=True)
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
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): 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') self.shader = Shader(code=code, args=args) def compile(self, shaders=[]): self.shader.compile(shaders) def prepare(self, runtimes): self.shader.prepare(runtimes) def execute(self): self.shader.execute()
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
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]) 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): #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 compile(self): self.shader.compile() def prepare(self, runtimes): self.shader.prepare(runtimes) def execute_py(self, ray, sample): d = {'camera': self, 'sample': sample, 'ray': ray} exec(self._py_code, d, d)
class Material: def __init__(self): self.bsdf = None self.sampling = None self.emission = None path = os.path.dirname(__file__) path = os.path.join(path, 'mat_shaders') self._loader = Loader([path]) def _load_args(self, name): args = [] props = self._loader.load(name, 'props.txt') if props is not None: args = parse_args(props) return args def _func_args(self, spectrum): wi = Vector3(0.0, 0.0, 0.0) wo = Vector3(0.0, 0.0, 0.0) ref = spectrum.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)] return func_args def load(self, shader_name, spectrum): self.bsdf = None self.sampling = None self.emission = None code = self._loader.load(shader_name, 'bsdf.py') #TODO hitpoint, shadepoint function arguments if code is not None: args = self._load_args(shader_name) name = 'bsdf_' + str(id(args)) func_args = self._func_args(spectrum) self.bsdf = Shader(code=code, args=args, name=name, func_args=func_args, is_func=True) code = self._loader.load(shader_name, 'sampling.py') if code is not None: args = self._load_args(shader_name) self.sampling = Shader(code=code, args=args) code = self._loader.load(shader_name, 'emission.py') if code is not None: args = self._load_args(shader_name) self.emission = Shader(code=code, args=args) def compile(self, shaders=[]): if self.bsdf is not None: self.bsdf.compile(shaders) if self.sampling is not None: self.sampling.compile(shaders) if self.emission is not None: self.emission.compile(shaders) def prepare(self, runtimes): if self.bsdf is not None: self.bsdf.prepare(runtimes) if self.sampling is not None: self.sampling.prepare(runtimes) if self.emission is not None: self.emission.prepare(runtimes)
class Sampler: def __init__(self): path = os.path.dirname(__file__) path = os.path.join(path, 'sam_shaders') self._loader = Loader([path]) self.shader = None #default values self._width = 200 self._height = 200 self._pixel_size = 1.0 self._pass_number = 1 self._nthreads = 1 def set_resolution(self, width, height): self._width = int(width) self._height = int(height) if self.shader is not None: self.shader.set_value('width', self._width) self.shader.set_value('height', self._height) def set_pass(self, n): self._pass_number = int(n) if self.shader is not None: self.shader.set_value('pass_number', self._pass_number) def set_pixel_size(self, size): self._pixel_size = float(size) if self.shader is not None: self.shader.set_value('pixel_size', self._pixel_size) def set_nthreads(self, n): self._nthreads = int(n) def load(self, shader_name): text = self._loader.load(shader_name, 'props.txt') #args = create_args(text) code = self._loader.load(shader_name, 'code.py') sample = Sample(0.0, 0.0, 0, 0, 0.0) func_args = [StructArgPtr('sample', sample)] args = [ IntArg('width', self._width), IntArg('height', self._height), IntArg('pass_number', self._pass_number), FloatArg('pixel_size', self._pixel_size) ] tiles = create_tiles(self._width, self._height, self._nthreads) targs = [StructArg('tile', tile) for tile in tiles] targ = ArgList('tile', targs) args.append(targ) curx_args = [IntArg('curx', 0) for tile in tiles] curx_arg = ArgList('curx', curx_args) args.append(curx_arg) cury_args = [IntArg('cury', tile.y) for tile in tiles] cury_arg = ArgList('cury', cury_args) args.append(cury_arg) endx_args = [IntArg('endx', tile.width) for tile in tiles] endx_arg = ArgList('endx', endx_args) args.append(endx_arg) endy_args = [IntArg('endy', tile.y + tile.height) for tile in tiles] endy_arg = ArgList('endy', endy_args) args.append(endy_arg) self.shader = Shader(code=code, args=args, name='generate_sample', func_args=func_args, is_func=True) def compile(self): self.shader.compile() def prepare(self, runtimes): self.shader.prepare(runtimes)
class Sampler: def __init__(self): path = os.path.dirname(__file__) path = os.path.join(path, "sam_shaders") self._loader = Loader([path]) self.shader = None # default values self._width = 200 self._height = 200 self._pixel_size = 1.0 self._pass_number = 1 self._nthreads = 1 def set_resolution(self, width, height): self._width = int(width) self._height = int(height) if self.shader is not None: self.shader.set_value("width", self._width) self.shader.set_value("height", self._height) def set_pass(self, n): self._pass_number = int(n) if self.shader is not None: self.shader.set_value("pass_number", self._pass_number) def set_pixel_size(self, size): self._pixel_size = float(size) if self.shader is not None: self.shader.set_value("pixel_size", self._pixel_size) def set_nthreads(self, n): self._nthreads = int(n) def load(self, shader_name): text = self._loader.load(shader_name, "props.txt") # args = create_args(text) code = self._loader.load(shader_name, "code.py") sample = Sample(0.0, 0.0, 0, 0, 0.0) func_args = [StructArgPtr("sample", sample)] args = [ IntArg("width", self._width), IntArg("height", self._height), IntArg("pass_number", self._pass_number), FloatArg("pixel_size", self._pixel_size), ] tiles = create_tiles(self._width, self._height, self._nthreads) targs = [StructArg("tile", tile) for tile in tiles] targ = ArgList("tile", targs) args.append(targ) curx_args = [IntArg("curx", 0) for tile in tiles] curx_arg = ArgList("curx", curx_args) args.append(curx_arg) cury_args = [IntArg("cury", tile.y) for tile in tiles] cury_arg = ArgList("cury", cury_args) args.append(cury_arg) endx_args = [IntArg("endx", tile.width) for tile in tiles] endx_arg = ArgList("endx", endx_args) args.append(endx_arg) endy_args = [IntArg("endy", tile.y + tile.height) for tile in tiles] endy_arg = ArgList("endy", endy_args) args.append(endy_arg) self.shader = Shader(code=code, args=args, name="generate_sample", func_args=func_args, is_func=True) def compile(self): self.shader.compile() def prepare(self, runtimes): self.shader.prepare(runtimes)