def render(self): fov = int(pi / 2) # field of view for y in range(self.height): for x in range(self.width): i = (2 * (x + 0.5) / self.width - 1) * self.width / self.height * tan(fov / 2) j = (2 * (y + 0.5) / self.height - 1) * tan(fov / 2) direction = norm(V3(i, j, -1)) self.pixels[y][x] = self.castRay(V3(0, 0, 0), direction)
def __init__(self, position, size, material): self.position = position self.size = size self.material = material self.planes = [] halfSize = size / 2 self.planes.append( Plane(sum(position, V3(halfSize, 0, 0)), V3(1, 0, 0), material)) self.planes.append( Plane(sum(position, V3(-halfSize, 0, 0)), V3(-1, 0, 0), material)) self.planes.append( Plane(sum(position, V3(0, halfSize, 0)), V3(0, 1, 0), material)) self.planes.append( Plane(sum(position, V3(0, -halfSize, 0)), V3(0, -1, 0), material)) self.planes.append( Plane(sum(position, V3(0, 0, halfSize)), V3(0, 0, 1), material)) self.planes.append( Plane(sum(position, V3(0, 0, -halfSize)), V3(0, 0, -1), material))
bitmap.point(X, Y) def glLine(x0, y0, x1, y1): x0 = bitmap.getCordX(x0) y0 = bitmap.getCordY(y0) x1 = bitmap.getCordX(x1) y1 = bitmap.getCordY(y1) bitmap.line(x0, y0, x1, y1) def glLoad(filename, translate, scale): bitmap.load(filename, translate, scale) def glFinish(filename='out.bmp'): bitmap.write(filename) glCreateWindow(1000, 1000) glClear() # Olaf # glLoad('./olaf.obj', V3(-250, 600, 1), V3(4, 4, 5)) # glFinish('olafGris.bmp') # glFinish('zolaf.bmp') # Sid glLoad('./sid.obj', V3(500, 10, 0), V3(30, 30, 100)) glFinish('sidGris.bmp') # glFinish('zsid.bmp')
return self.currentColor def render(self): fov = int(pi / 2) # field of view for y in range(self.height): for x in range(self.width): i = (2 * (x + 0.5) / self.width - 1) * self.width / self.height * tan(fov / 2) j = (1 - 2 * (y + 0.5) / self.height) * tan(fov / 2) direction = norm(V3(i, j, -1)) self.pixels[y][x] = self.castRay(V3(0, 0, 0), direction) r = Raytracer(800, 800) r.scene = [ Sphere(V3(0.7, -5.05, -15), 0.25, button), Sphere(V3(-0.8, -5.05, -15), 0.25, button), Sphere(V3(0.75, -5, -15), 0.45, eye), Sphere(V3(-0.75, -5, -15), 0.45, eye), Sphere(V3(0, -4.3, -15), 0.35, nose), Sphere(V3(-1, -3.8, -15), 0.25, button), Sphere(V3(-0.4, -3.3, -15), 0.25, button), Sphere(V3(0.4, -3.3, -15), 0.25, button), Sphere(V3(1, -3.8, -15), 0.25, button), Sphere(V3(0, -2.0, -15), 0.45, button), Sphere(V3(0, 0.45, -15), 0.7, button), Sphere(V3(0, 3.2, -15), 0.95, button), Sphere(V3(0, -3.5, -12), 1.5, body), Sphere(V3(0, -1, -12), 2, body), Sphere(V3(0, 2.5, -12), 2.5, body), ]
def makePattern(self, levels=64): self.pattern = [[ Color(random.randint(0, levels - 1), random.randint(0, levels - 1), random.randint(0, levels - 1)) for x in range(self.width) ] for y in range(self.height)] def makeAutostereogram(self, shiftAmplitude=0.1): self.autostereogram = self.pixels for r in range(self.height): for c in range(self.width): if c < self.width: self.autostereogram[r][c] = self.pattern[r % self.height][c] else: shift = int(self.pixels[r][c] * shiftAmplitude * self.width) self.autostereogram[r][c] = self.autostereogram[r][ c - self.width + shift] r = Raytracer(450, 800) r.light = Light(position=V3(0, 0, 20), intensity=1.5) r.scene = [ Sphere(V3(0, 0, -10), 1.5, adorno2), ] r.render() r.makePattern() r.makeAutostereogram() r.write()
direction = norm(V3(i, j, -1)) self.pixels[y][x] = self.castRay(V3(0, 0, 0), direction) def gradientBackground(self): for x in range(self.width): for y in range(self.height): r = int((x / self.width) * 255) if x / self.width < 1 else 1 g = int((y / self.height) * 255) if y / self.height < 1 else 1 b = 0 self.pixels[y][x] = Color(r, g, b) r = Raytracer(1000, 1000) r.light = Light( position = V3(0, 0, 20), intensity = 1.5 ) r.scene = [ Sphere(V3(2.5, -1, -10), 1.75, adorno2), Sphere(V3(-2.5, -1, -10), 1.75, adorno1), Sphere(V3(2.5, 2, -10), 1.5, oso2), Sphere(V3(-2.5, 2, -10), 1.5, oso1), Sphere(V3(2.5, 1.7, -9), 0.65, tono2), Sphere(V3(-2.5, 1.7, -9), 0.65, oso1), Sphere(V3(4, 0, -10), 0.6, oso2), Sphere(V3(-4, 0, -10), 0.6, oso1), Sphere(V3(1, 0, -10), 0.6, oso2), Sphere(V3(-1, 0, -10), 0.6, oso1), Sphere(V3(4, -2.5, -10), 0.75, oso2), Sphere(V3(-4, -2.5, -10), 0.75, oso1),
j = (1 - 2 * (y + 0.5) / self.height) * tan(fov / 2) direction = norm(V3(i, j, -1)) self.pixels[y][x] = self.castRay(V3(0, 0, 0), direction) def gradientBackground(self): for x in range(self.width): for y in range(self.height): r = int((x / self.width) * 255) if x / self.width < 1 else 1 g = int((y / self.height) * 255) if y / self.height < 1 else 1 b = 0 self.pixels[y][x] = color(r, g, b) r = Raytracer(1000, 1000) r.scene = [ Sphere(V3(0.7, -5.05, -15), 0.15, button), Sphere(V3(-0.8, -5.05, -15), 0.15, button), Sphere(V3(0.75, -5, -15), 0.3, eye), Sphere(V3(-0.75, -5, -15), 0.3, eye), Sphere(V3(0, -4.5, -15), 0.4, nose), Sphere(V3(-1, -4, -15), 0.2, button), Sphere(V3(-0.4, -3.5, -15), 0.2, button), Sphere(V3(0.4, -3.5, -15), 0.2, button), Sphere(V3(1, -4, -15), 0.2, button), Sphere(V3(0, -2, -15), 0.25, button), Sphere(V3(0, 0.25, -15), 0.5, button), Sphere(V3(0, 3, -15), 0.75, button), Sphere(V3(0, -3.5, -12), 1.5, body), Sphere(V3(0, -1, -12), 2, body), Sphere(V3(0, 2.5, -12), 2.5, body), Sphere(V3(0, 0, -11), 5, lightblue),
def glChangeLight(light): bitmap.light = light def glFinish(filename='out.bmp'): bitmap.write(filename) glCreateWindow(1000, 1000) glClear() glCustomClear() glColor(0.37, 0.21, 0.06) glLookAt(V3(0, 0, 1), V3(0, 0, 0), V3(0, 1, 0)) glLoad('coral.obj', V3(-0.4, -0.80, 0), V3(0.04, 0.04, 0.04), rotate=(0, 0, -0.2)) glDrawFigure('LINES') glLookAt(V3(0, 0, 1), V3(0, 0, 0), V3(0, 1, 0)) glLoad('coral.obj', V3(0.4, -0.85, 0), V3(0.03, 0.03, 0.03), rotate=(0, 0, -0.2)) glDrawFigure('LINES') glColor(0.78, 0.73, 0.65) glLookAt(V3(0, 0, 1), V3(0, 0, 0), V3(0, 1, 0))
self.pixels[y][x] = self.castRay(V3(0, 0, 0), direction) def gradientBackground(self): for x in range(self.width): for y in range(self.height): r = int((x / self.width) * 255) if x / self.width < 1 else 1 g = int((y / self.height) * 255) if y / self.height < 1 else 1 b = 0 self.pixels[y][x] = Color(r, g, b) r = Raytracer(400, 400) #r.envMap = Envmap('fondo.bmp') r.light = Light( position = V3(0, 20, 20), intensity = 1.5 ) r.ambientLight = AmbientLight(strength = 0.1) r.scene = [ Pyramid([V3(-1, 0, -10), V3(-3, 2, -10), V3(-5, 0, -10), V3(-1, 0, -10)], blue3), Pyramid([V3(1, 0, -10), V3(3, 2, -10), V3(5, 0, -10), V3(1, 0, -10)], blue3), Pyramid([V3(-2, 0, -10), V3(-4, 2, -10), V3(-6, 0, -10), V3(-2, 0, -10)], blue2), Pyramid([V3(2, 0, -10), V3(4, 2, -10), V3(6, 0, -10), V3(2, 0, -10)], blue2), Pyramid([V3(0, 0, -10), V3(-2, 2, -10), V3(-4, 0, -10), V3(0, 0, -10)], blue4), Pyramid([V3(0, 0, -10), V3(2, 2, -10), V3(4, 0, -10), V3(0, 0, -10)], blue4), Pyramid([V3(2, 0, -10), V3(0, 3, -10), V3(-2, 0, -10), V3(2, 0, -10)], blue1), Cube(V3(0, -2, -2), 2, beige), Pyramid([V3(-1, 0, -10), V3(3, 0, -10), V3(-1, 0, -5), V3(-1, -1, -10)], brown), Cube(V3(0.75, -0.75, -2.5), 0.5, blue5), Sphere(V3(0.75, -0.40, -2.5), 0.20, white),
X = bitmap.getCordX(x) Y = bitmap.getCordY(y) bitmap.point(X, Y) def glLine(x0, y0, x1, y1): x0 = bitmap.getCordX(x0) y0 = bitmap.getCordY(y0) x1 = bitmap.getCordX(x1) y1 = bitmap.getCordY(y1) bitmap.line(x0, y0, x1, y1) def glLoad(filename, translate, scale): bitmap.load(filename, translate, scale) def glFinish(filename='out.bmp'): bitmap.write(filename) glCreateWindow(800, 800) glClear() glLoad('./sphere.obj', V3(200, 400, 0), V3(250, 250, 200)) bitmap.activeShader = 'LUNA' glLoad('./sphere.obj', V3(500, 425, 0), V3(125, 100, 200)) glLoad('./sphere.obj', V3(650, 450, 0), V3(40, 25, 200)) glFinish('tercer.bmp')