michael@0: /*
michael@0: * Copyright (C) 2007 Apple Inc. All rights reserved.
michael@0: *
michael@0: * Redistribution and use in source and binary forms, with or without
michael@0: * modification, are permitted provided that the following conditions
michael@0: * are met:
michael@0: * 1. Redistributions of source code must retain the above copyright
michael@0: * notice, this list of conditions and the following disclaimer.
michael@0: * 2. Redistributions in binary form must reproduce the above copyright
michael@0: * notice, this list of conditions and the following disclaimer in the
michael@0: * documentation and/or other materials provided with the distribution.
michael@0: *
michael@0: * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
michael@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
michael@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
michael@0: * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0: */
michael@0:
michael@0: function createVector(x,y,z) {
michael@0: return new Array(x,y,z);
michael@0: }
michael@0:
michael@0: function sqrLengthVector(self) {
michael@0: return self[0] * self[0] + self[1] * self[1] + self[2] * self[2];
michael@0: }
michael@0:
michael@0: function lengthVector(self) {
michael@0: return Math.sqrt(self[0] * self[0] + self[1] * self[1] + self[2] * self[2]);
michael@0: }
michael@0:
michael@0: function addVector(self, v) {
michael@0: self[0] += v[0];
michael@0: self[1] += v[1];
michael@0: self[2] += v[2];
michael@0: return self;
michael@0: }
michael@0:
michael@0: function subVector(self, v) {
michael@0: self[0] -= v[0];
michael@0: self[1] -= v[1];
michael@0: self[2] -= v[2];
michael@0: return self;
michael@0: }
michael@0:
michael@0: function scaleVector(self, scale) {
michael@0: self[0] *= scale;
michael@0: self[1] *= scale;
michael@0: self[2] *= scale;
michael@0: return self;
michael@0: }
michael@0:
michael@0: function normaliseVector(self) {
michael@0: var len = Math.sqrt(self[0] * self[0] + self[1] * self[1] + self[2] * self[2]);
michael@0: self[0] /= len;
michael@0: self[1] /= len;
michael@0: self[2] /= len;
michael@0: return self;
michael@0: }
michael@0:
michael@0: function add(v1, v2) {
michael@0: return new Array(v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]);
michael@0: }
michael@0:
michael@0: function sub(v1, v2) {
michael@0: return new Array(v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]);
michael@0: }
michael@0:
michael@0: function scalev(v1, v2) {
michael@0: return new Array(v1[0] * v2[0], v1[1] * v2[1], v1[2] * v2[2]);
michael@0: }
michael@0:
michael@0: function dot(v1, v2) {
michael@0: return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
michael@0: }
michael@0:
michael@0: function scale(v, scale) {
michael@0: return [v[0] * scale, v[1] * scale, v[2] * scale];
michael@0: }
michael@0:
michael@0: function cross(v1, v2) {
michael@0: return [v1[1] * v2[2] - v1[2] * v2[1],
michael@0: v1[2] * v2[0] - v1[0] * v2[2],
michael@0: v1[0] * v2[1] - v1[1] * v2[0]];
michael@0:
michael@0: }
michael@0:
michael@0: function normalise(v) {
michael@0: var len = lengthVector(v);
michael@0: return [v[0] / len, v[1] / len, v[2] / len];
michael@0: }
michael@0:
michael@0: function transformMatrix(self, v) {
michael@0: var vals = self;
michael@0: var x = vals[0] * v[0] + vals[1] * v[1] + vals[2] * v[2] + vals[3];
michael@0: var y = vals[4] * v[0] + vals[5] * v[1] + vals[6] * v[2] + vals[7];
michael@0: var z = vals[8] * v[0] + vals[9] * v[1] + vals[10] * v[2] + vals[11];
michael@0: return [x, y, z];
michael@0: }
michael@0:
michael@0: function invertMatrix(self) {
michael@0: var temp = new Array(16);
michael@0: var tx = -self[3];
michael@0: var ty = -self[7];
michael@0: var tz = -self[11];
michael@0: for (h = 0; h < 3; h++)
michael@0: for (v = 0; v < 3; v++)
michael@0: temp[h + v * 4] = self[v + h * 4];
michael@0: for (i = 0; i < 11; i++)
michael@0: self[i] = temp[i];
michael@0: self[3] = tx * self[0] + ty * self[1] + tz * self[2];
michael@0: self[7] = tx * self[4] + ty * self[5] + tz * self[6];
michael@0: self[11] = tx * self[8] + ty * self[9] + tz * self[10];
michael@0: return self;
michael@0: }
michael@0:
michael@0:
michael@0: // Triangle intersection using barycentric coord method
michael@0: function Triangle(p1, p2, p3) {
michael@0: var edge1 = sub(p3, p1);
michael@0: var edge2 = sub(p2, p1);
michael@0: var normal = cross(edge1, edge2);
michael@0: if (Math.abs(normal[0]) > Math.abs(normal[1]))
michael@0: if (Math.abs(normal[0]) > Math.abs(normal[2]))
michael@0: this.axis = 0;
michael@0: else
michael@0: this.axis = 2;
michael@0: else
michael@0: if (Math.abs(normal[1]) > Math.abs(normal[2]))
michael@0: this.axis = 1;
michael@0: else
michael@0: this.axis = 2;
michael@0: var u = (this.axis + 1) % 3;
michael@0: var v = (this.axis + 2) % 3;
michael@0: var u1 = edge1[u];
michael@0: var v1 = edge1[v];
michael@0:
michael@0: var u2 = edge2[u];
michael@0: var v2 = edge2[v];
michael@0: this.normal = normalise(normal);
michael@0: this.nu = normal[u] / normal[this.axis];
michael@0: this.nv = normal[v] / normal[this.axis];
michael@0: this.nd = dot(normal, p1) / normal[this.axis];
michael@0: var det = u1 * v2 - v1 * u2;
michael@0: this.eu = p1[u];
michael@0: this.ev = p1[v];
michael@0: this.nu1 = u1 / det;
michael@0: this.nv1 = -v1 / det;
michael@0: this.nu2 = v2 / det;
michael@0: this.nv2 = -u2 / det;
michael@0: this.material = [0.7, 0.7, 0.7];
michael@0: }
michael@0:
michael@0: Triangle.prototype.intersect = function(orig, dir, near, far) {
michael@0: var u = (this.axis + 1) % 3;
michael@0: var v = (this.axis + 2) % 3;
michael@0: var d = dir[this.axis] + this.nu * dir[u] + this.nv * dir[v];
michael@0: var t = (this.nd - orig[this.axis] - this.nu * orig[u] - this.nv * orig[v]) / d;
michael@0: if (t < near || t > far)
michael@0: return null;
michael@0: var Pu = orig[u] + t * dir[u] - this.eu;
michael@0: var Pv = orig[v] + t * dir[v] - this.ev;
michael@0: var a2 = Pv * this.nu1 + Pu * this.nv1;
michael@0: if (a2 < 0)
michael@0: return null;
michael@0: var a3 = Pu * this.nu2 + Pv * this.nv2;
michael@0: if (a3 < 0)
michael@0: return null;
michael@0:
michael@0: if ((a2 + a3) > 1)
michael@0: return null;
michael@0: return t;
michael@0: }
michael@0:
michael@0: function Scene(a_triangles) {
michael@0: this.triangles = a_triangles;
michael@0: this.lights = [];
michael@0: this.ambient = [0,0,0];
michael@0: this.background = [0.8,0.8,1];
michael@0: }
michael@0: var zero = new Array(0,0,0);
michael@0:
michael@0: Scene.prototype.intersect = function(origin, dir, near, far) {
michael@0: var closest = null;
michael@0: for (i = 0; i < this.triangles.length; i++) {
michael@0: var triangle = this.triangles[i];
michael@0: var d = triangle.intersect(origin, dir, near, far);
michael@0: if (d == null || d > far || d < near)
michael@0: continue;
michael@0: far = d;
michael@0: closest = triangle;
michael@0: }
michael@0:
michael@0: if (!closest)
michael@0: return [this.background[0],this.background[1],this.background[2]];
michael@0:
michael@0: var normal = closest.normal;
michael@0: var hit = add(origin, scale(dir, far));
michael@0: if (dot(dir, normal) > 0)
michael@0: normal = [-normal[0], -normal[1], -normal[2]];
michael@0:
michael@0: var colour = null;
michael@0: if (closest.shader) {
michael@0: colour = closest.shader(closest, hit, dir);
michael@0: } else {
michael@0: colour = closest.material;
michael@0: }
michael@0:
michael@0: // do reflection
michael@0: var reflected = null;
michael@0: if (colour.reflection > 0.001) {
michael@0: var reflection = addVector(scale(normal, -2*dot(dir, normal)), dir);
michael@0: reflected = this.intersect(hit, reflection, 0.0001, 1000000);
michael@0: if (colour.reflection >= 0.999999)
michael@0: return reflected;
michael@0: }
michael@0:
michael@0: var l = [this.ambient[0], this.ambient[1], this.ambient[2]];
michael@0: for (var i = 0; i < this.lights.length; i++) {
michael@0: var light = this.lights[i];
michael@0: var toLight = sub(light, hit);
michael@0: var distance = lengthVector(toLight);
michael@0: scaleVector(toLight, 1.0/distance);
michael@0: distance -= 0.0001;
michael@0: if (this.blocked(hit, toLight, distance))
michael@0: continue;
michael@0: var nl = dot(normal, toLight);
michael@0: if (nl > 0)
michael@0: addVector(l, scale(light.colour, nl));
michael@0: }
michael@0: l = scalev(l, colour);
michael@0: if (reflected) {
michael@0: l = addVector(scaleVector(l, 1 - colour.reflection), scaleVector(reflected, colour.reflection));
michael@0: }
michael@0: return l;
michael@0: }
michael@0:
michael@0: Scene.prototype.blocked = function(O, D, far) {
michael@0: var near = 0.0001;
michael@0: var closest = null;
michael@0: for (i = 0; i < this.triangles.length; i++) {
michael@0: var triangle = this.triangles[i];
michael@0: var d = triangle.intersect(O, D, near, far);
michael@0: if (d == null || d > far || d < near)
michael@0: continue;
michael@0: return true;
michael@0: }
michael@0:
michael@0: return false;
michael@0: }
michael@0:
michael@0:
michael@0: // this camera code is from notes i made ages ago, it is from *somewhere* -- i cannot remember where
michael@0: // that somewhere is
michael@0: function Camera(origin, lookat, up) {
michael@0: var zaxis = normaliseVector(subVector(lookat, origin));
michael@0: var xaxis = normaliseVector(cross(up, zaxis));
michael@0: var yaxis = normaliseVector(cross(xaxis, subVector([0,0,0], zaxis)));
michael@0: var m = new Array(16);
michael@0: m[0] = xaxis[0]; m[1] = xaxis[1]; m[2] = xaxis[2];
michael@0: m[4] = yaxis[0]; m[5] = yaxis[1]; m[6] = yaxis[2];
michael@0: m[8] = zaxis[0]; m[9] = zaxis[1]; m[10] = zaxis[2];
michael@0: invertMatrix(m);
michael@0: m[3] = 0; m[7] = 0; m[11] = 0;
michael@0: this.origin = origin;
michael@0: this.directions = new Array(4);
michael@0: this.directions[0] = normalise([-0.7, 0.7, 1]);
michael@0: this.directions[1] = normalise([ 0.7, 0.7, 1]);
michael@0: this.directions[2] = normalise([ 0.7, -0.7, 1]);
michael@0: this.directions[3] = normalise([-0.7, -0.7, 1]);
michael@0: this.directions[0] = transformMatrix(m, this.directions[0]);
michael@0: this.directions[1] = transformMatrix(m, this.directions[1]);
michael@0: this.directions[2] = transformMatrix(m, this.directions[2]);
michael@0: this.directions[3] = transformMatrix(m, this.directions[3]);
michael@0: }
michael@0:
michael@0: Camera.prototype.generateRayPair = function(y) {
michael@0: rays = new Array(new Object(), new Object());
michael@0: rays[0].origin = this.origin;
michael@0: rays[1].origin = this.origin;
michael@0: rays[0].dir = addVector(scale(this.directions[0], y), scale(this.directions[3], 1 - y));
michael@0: rays[1].dir = addVector(scale(this.directions[1], y), scale(this.directions[2], 1 - y));
michael@0: return rays;
michael@0: }
michael@0:
michael@0: function renderRows(camera, scene, pixels, width, height, starty, stopy) {
michael@0: for (var y = starty; y < stopy; y++) {
michael@0: var rays = camera.generateRayPair(y / height);
michael@0: for (var x = 0; x < width; x++) {
michael@0: var xp = x / width;
michael@0: var origin = addVector(scale(rays[0].origin, xp), scale(rays[1].origin, 1 - xp));
michael@0: var dir = normaliseVector(addVector(scale(rays[0].dir, xp), scale(rays[1].dir, 1 - xp)));
michael@0: var l = scene.intersect(origin, dir);
michael@0: pixels[y][x] = l;
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: Camera.prototype.render = function(scene, pixels, width, height) {
michael@0: var cam = this;
michael@0: var row = 0;
michael@0: renderRows(cam, scene, pixels, width, height, 0, height);
michael@0: }
michael@0:
michael@0:
michael@0:
michael@0: function raytraceScene()
michael@0: {
michael@0: var numTriangles = 2 * 6;
michael@0: var triangles = new Array();//numTriangles);
michael@0: var tfl = createVector(-10, 10, -10);
michael@0: var tfr = createVector( 10, 10, -10);
michael@0: var tbl = createVector(-10, 10, 10);
michael@0: var tbr = createVector( 10, 10, 10);
michael@0: var bfl = createVector(-10, -10, -10);
michael@0: var bfr = createVector( 10, -10, -10);
michael@0: var bbl = createVector(-10, -10, 10);
michael@0: var bbr = createVector( 10, -10, 10);
michael@0:
michael@0: // cube!!!
michael@0: // front
michael@0: var i = 0;
michael@0:
michael@0: triangles[i++] = new Triangle(tfl, tfr, bfr);
michael@0: triangles[i++] = new Triangle(tfl, bfr, bfl);
michael@0: // back
michael@0: triangles[i++] = new Triangle(tbl, tbr, bbr);
michael@0: triangles[i++] = new Triangle(tbl, bbr, bbl);
michael@0: // triangles[i-1].material = [0.7,0.2,0.2];
michael@0: // triangles[i-1].material.reflection = 0.8;
michael@0: // left
michael@0: triangles[i++] = new Triangle(tbl, tfl, bbl);
michael@0: // triangles[i-1].reflection = 0.6;
michael@0: triangles[i++] = new Triangle(tfl, bfl, bbl);
michael@0: // triangles[i-1].reflection = 0.6;
michael@0: // right
michael@0: triangles[i++] = new Triangle(tbr, tfr, bbr);
michael@0: triangles[i++] = new Triangle(tfr, bfr, bbr);
michael@0: // top
michael@0: triangles[i++] = new Triangle(tbl, tbr, tfr);
michael@0: triangles[i++] = new Triangle(tbl, tfr, tfl);
michael@0: // bottom
michael@0: triangles[i++] = new Triangle(bbl, bbr, bfr);
michael@0: triangles[i++] = new Triangle(bbl, bfr, bfl);
michael@0:
michael@0: //Floor!!!!
michael@0: var green = createVector(0.0, 0.4, 0.0);
michael@0: var grey = createVector(0.4, 0.4, 0.4);
michael@0: grey.reflection = 1.0;
michael@0: var floorShader = function(tri, pos, view) {
michael@0: var x = ((pos[0]/32) % 2 + 2) % 2;
michael@0: var z = ((pos[2]/32 + 0.3) % 2 + 2) % 2;
michael@0: if (x < 1 != z < 1) {
michael@0: //in the real world we use the fresnel term...
michael@0: // var angle = 1-dot(view, tri.normal);
michael@0: // angle *= angle;
michael@0: // angle *= angle;
michael@0: // angle *= angle;
michael@0: //grey.reflection = angle;
michael@0: return grey;
michael@0: } else
michael@0: return green;
michael@0: }
michael@0: var ffl = createVector(-1000, -30, -1000);
michael@0: var ffr = createVector( 1000, -30, -1000);
michael@0: var fbl = createVector(-1000, -30, 1000);
michael@0: var fbr = createVector( 1000, -30, 1000);
michael@0: triangles[i++] = new Triangle(fbl, fbr, ffr);
michael@0: triangles[i-1].shader = floorShader;
michael@0: triangles[i++] = new Triangle(fbl, ffr, ffl);
michael@0: triangles[i-1].shader = floorShader;
michael@0:
michael@0: var _scene = new Scene(triangles);
michael@0: _scene.lights[0] = createVector(20, 38, -22);
michael@0: _scene.lights[0].colour = createVector(0.7, 0.3, 0.3);
michael@0: _scene.lights[1] = createVector(-23, 40, 17);
michael@0: _scene.lights[1].colour = createVector(0.7, 0.3, 0.3);
michael@0: _scene.lights[2] = createVector(23, 20, 17);
michael@0: _scene.lights[2].colour = createVector(0.7, 0.7, 0.7);
michael@0: _scene.ambient = createVector(0.1, 0.1, 0.1);
michael@0: // _scene.background = createVector(0.7, 0.7, 1.0);
michael@0:
michael@0: var size = 30;
michael@0: var pixels = new Array();
michael@0: for (var y = 0; y < size; y++) {
michael@0: pixels[y] = new Array();
michael@0: for (var x = 0; x < size; x++) {
michael@0: pixels[y][x] = 0;
michael@0: }
michael@0: }
michael@0:
michael@0: var _camera = new Camera(createVector(-40, 40, 40), createVector(0, 0, 0), createVector(0, 1, 0));
michael@0: _camera.render(_scene, pixels, size, size);
michael@0:
michael@0: return pixels;
michael@0: }
michael@0:
michael@0: function arrayToCanvasCommands(pixels)
michael@0: {
michael@0: var s = '\nvar pixels = [';
michael@0: var size = 30;
michael@0: for (var y = 0; y < size; y++) {
michael@0: s += "[";
michael@0: for (var x = 0; x < size; x++) {
michael@0: s += "[" + pixels[y][x] + "],";
michael@0: }
michael@0: s+= "],";
michael@0: }
michael@0: s += '];\n var canvas = document.getElementById("renderCanvas").getContext("2d");\n\
michael@0: \n\
michael@0: \n\
michael@0: var size = 30;\n\
michael@0: canvas.fillStyle = "red";\n\
michael@0: canvas.fillRect(0, 0, size, size);\n\
michael@0: canvas.scale(1, -1);\n\
michael@0: canvas.translate(0, -size);\n\
michael@0: \n\
michael@0: if (!canvas.setFillColor)\n\
michael@0: canvas.setFillColor = function(r, g, b, a) {\n\
michael@0: this.fillStyle = "rgb("+[Math.floor(r * 255), Math.floor(g * 255), Math.floor(b * 255)]+")";\n\
michael@0: }\n\
michael@0: \n\
michael@0: for (var y = 0; y < size; y++) {\n\
michael@0: for (var x = 0; x < size; x++) {\n\
michael@0: var l = pixels[y][x];\n\
michael@0: canvas.setFillColor(l[0], l[1], l[2], 1);\n\
michael@0: canvas.fillRect(x, y, 1, 1);\n\
michael@0: }\n\
michael@0: }';
michael@0:
michael@0: return s;
michael@0: }
michael@0:
michael@0: testOutput = arrayToCanvasCommands(raytraceScene());
michael@0: expected = '';
michael@0: assertEq(testOutput, expected)