def __init__(self, scene=None): SceneObject.__init__( self, scene, attributes= {'position': (-10.99, 20.0, 20.0), 'focus': (0.0, 0.0, 0.0), 'color': Color(1.0, 1.0, 1.0), 'specular': [Color(1.0, 1.0, 1.0), 5], 'fov': 180.0, 'attenuation': 0.0, 'areaLights': 1, 'areaLightSize': 4.0})
def __init__(self, scene=None): SceneObject.__init__(self, scene, attributes={ 'ambience': Color(0.3, 0.3, 0.3), 'skybox': None })
def trace(self, ray, objects, lights): C = Color(0, 0, 0) for i in range(self.RAY_PER_PIXEL): C = C + (self.recursiveTrace(ray, objects, lights, 0, 0) / self.RAY_PER_PIXEL) return C
def __setattr__(self, attr, value): if hasattr(self, "_attributes") and attr in self._attributes: attrValue = getattr(self, "_" + attr) if isinstance(attrValue, Color): # Ensure Color attributes are of type Color value = Color(value) if (attrValue != value): object.__setattr__(self, "_" + attr, value) self.changed() else: object.__setattr__(self, attr, value)
def load(self, hfile): for attr in self._attributes: # Check if attribute exists in the file by checking # the compatibility of their versions filever = self._scene.filever supported = False for verlim in self._attrver[attr]: if checkVersions((verlim[0], filever, verlim[1])): supported = True break if supported: attrV = pickle.load(hfile) if isinstance(getattr(self, "_" + attr), Color): setattr(self, "_" + attr, Color(attrV)) else: setattr(self, "_" + attr, attrV)
def recursiveTrace(self, ray, objects, lights, depth, distance): if depth > self.MAX_DEPTH: return Color(0, 0, 0) distances = self.distances(objects, ray) if abs(distances[0].distance) < EPSILON: nearest = distances[1] else: nearest = distances[0] if nearest.distance == np.inf: return WHITE intersection = ray.origin + nearest.distance * ray.direction A = nearest.object.getColor() * nearest.object.material.ambient C = nearest.object.getColor() * nearest.object.material.ambient # default lighting for light in lights: C = C + (self.shading(intersection, nearest.object, light) * self.calcShadowFactor(intersection, objects, light)) # recursive reflection computation. if nearest.object.material.specular > 0: N = nearest.object.normalAt(intersection) reflectionRayDirection = ray.direction - 2 * (np.dot( ray.direction, N)) * N reflection = Ray(intersection, reflectionRayDirection) recursiveValue = self.recursiveTrace(reflection, objects, lights, depth + 1, distance + nearest.distance) recursiveValue = recursiveValue * ( nearest.object.material.specular) recursiveValue = recursiveValue * self.lightAttenuation2(distance) C = C + recursiveValue if nearest.object.material.diffuse > 0: for c in range(self.DIFFUSE_REFLECT): N = nearest.object.normalAt(intersection) # calculate random new ray direction from hemisphere. # calculate random hemisphere shit in tangent space: # http://www.keithlantz.net/2013/03/a-basic-path-tracer-with-cuda/ # http://www.rorydriscoll.com/2009/01/07/better-sampling/ #D_tangent_space = self.random_normal_hemisphere() ## get local coordinate system from at normal. #local_coord = self.local_coordinate_system_from(N) ## transform tangent-space shit in this normal space. #new_D = np.dot(local_coord, D_tangent_space) from random import uniform new_D = np.array( [uniform(-1, 1), uniform(-1, 1), uniform(-1, 1)]) Diffuse = Ray(intersection, new_D) recursiveValue = self.recursiveTrace( Diffuse, objects, lights, depth + 1, distance + nearest.distance) recursiveValue = recursiveValue / self.DIFFUSE_REFLECT recursiveValue = recursiveValue * self.lightAttenuation2( distance) C = C + recursiveValue return C
def __init__(self, material): if material is None: material = Material(Color(0, 0, 0), 0, 0) self.material = material
# oben t11 = Triangle(v5, v6, v7, self.material) t12 = Triangle(v5, v7, v8, self.material) self.triangles = [t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12] def intersect(self, ray): lol = map(lambda t: t.intersect(ray), self.triangles) distance = reduce(lambda x, y: min(x, y), lol) return distance def normalAt(self, point): for t in self.triangles: if t.pointIn(point): return t.normalAt(point) return None if __name__ == "__main__": r = Ray([0, 0, 0], [0, 0, 1]) r2 = Ray.fromPoints(p1=[0, 0, 10], p2=[0, 0, 0]) p = Plane([0, 0, 5], [0, 0, -1]) s = Sphere([0, 0, 2], 1) s.setColor(Color(255, 0, 0)) print p.intersect(r) print s.intersect(r) print p.intersect(r2) print s.getColorHex()
#!/usr/bin/python from geometry import Plane, Sphere, Triangle, Cube from scene import Screen, Scene from tracer import SimpleRayTracer, SimpleShadowRayTracer, ShadingShadowRayTracer, RecursiveRayTracer, PathTracer from material import Material, Color from window import Window if __name__ == "__main__": p1 = Plane([0, 5, 0], [0, -1, 0], Material(Color(255, 0, 0), 1, 0, 0.1)) p2 = Plane([0, -5, 0], [0, 1, 0], Material(Color(0, 255, 0), 1, 0, 0.1)) p3 = Plane([5, 0, 0], [-1, 0, 0], Material(Color(0, 0, 255), 1, 0, 0.1)) p4 = Plane([-5, 0, 0], [1, 0, 0], Material(Color(255, 255, 0), 1, 0, 0.1)) p5 = Plane([0, 0, 5], [0, 0, -1], Material(Color(255, 0, 255), 1, 0, 0.1)) p6 = Plane([0, 0, -5], [0, 0, 1], Material(Color(0, 255, 255), 1, 0, 0.1)) s1 = Sphere([0, 3, 2], 2, Material(Color(100, 100, 100), 1, 0, 0.1, refractive=False, n=1.52)) s2 = Sphere([4, 2, 1], 0.5, Material(Color(100, 100, 100), 1, 0, 0.1, refractive=False, n=1.52)) s3 = Sphere([-3, 2, 1], 1,