def get_normal(self, i): if i.face == 0: x, y, z = i.opos n = (x, -y, z) elif i.face == 1: n = (0.0, 1.0, 0.0) return normalize(self.transform.transform_normal(n))
def __init__(self, pos, at, c, cutoff, exp): self.pos = pos self.at = at self.d = normalize(sub(self.at, self.pos)) self.color = c self.cutoff = cutoff self.coscutoff = math.cos(math.radians(self.cutoff)) self.exp = exp
def get_normal(self, i): if i.face == 0: n = (i.opos[0], 0.0, i.opos[2]) elif i.face == 1: n = (0.0, 1.0, 0.0) elif i.face == 2: n = (0.0, -1.0, 0.0) return normalize(self.transform.transform_normal(n))
def render(amb, lights, obj, depth, fov, w, h, filename): print "Rendering", filename pixels = [] raypos = (0.0, 0.0, -1.0) w_world = 2.0 * math.tan(0.5 * math.radians(fov)) h_world = h * w_world / w c_x = -0.5 * w_world c_y = 0.5 * h_world pw = w_world / w for y in range(h): for x in range(w): raydir = normalize( (c_x + (x + 0.5) * pw, c_y - (y + 0.5) * pw, -raypos[2])) p = trace(amb, lights, obj, depth, raypos, raydir) pixels.append(p) print "Writing", filename write_ppm(pixels, w, h, filename)
def trace(amb, lights, scene, depth, raypos, raydir): i = scene.intersect(raypos, raydir) if i: #for ic in i: # print ic #print isect = i[0] if isect.t == Intersection.EXIT: return (0.0, 0.0, 0.0) sc, kd, ks, n = isect.primitive.get_surface(isect) c = get_ambient(sc, amb, kd) diffuse = (0.0, 0.0, 0.0) specular = (0.0, 0.0, 0.0) pos = isect.wpos normal = isect.normal for light in lights: lightdir, lightdistance = light.get_direction(pos) df = dot(normal, lightdir) if df > 0.0: poseps = add(pos, mul(lightdir, 1e-7)) i = scene.intersect(poseps, lightdir) if not i or (lightdistance and (lightdistance < i[0].distance)): ic = cmul(sc, light.get_intensity(pos)) if kd > 0.0: diffuse = add(diffuse, mul(ic, df)) if ks > 0.0: specular = add( specular, get_specular(ic, lightdir, normal, pos, raypos, n)) c = add(c, add(mul(diffuse, kd), mul(specular, ks))) if ks > 0.0 and depth > 0: refl_raydir = normalize( sub(raydir, mul(normal, 2 * dot(raydir, normal)))) poseps = add(pos, mul(refl_raydir, 1e-7)) rc = trace(amb, lights, scene, depth - 1, poseps, refl_raydir) return add(c, mul(cmul(rc, sc), ks)) else: return c else: return (0.0, 0.0, 0.0)
def get_specular(ic, lightdir, sn, pos, raypos, n): halfway = normalize(add(lightdir, normalize(sub(raypos, pos)))) sp = dot(sn, halfway) if sp > 0.0: return mul(ic, pow(sp, n)) return (0.0, 0.0, 0.0)
def __init__(self, d, c): self.direction = normalize(neg(d)) self.color = c
def get_normal(self, i): return normalize(self.transform.transform_normal(self.np))
def get_normal(self, i): return normalize(self.transform.transform_normal(self.normals[i.face]))
def get_normal(self, i): return normalize(self.transform.transform_normal(i.opos))