2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 function createVector(x,y,z) {
27 return new Array(x,y,z);
30 function sqrLengthVector(self) {
31 return self[0] * self[0] + self[1] * self[1] + self[2] * self[2];
34 function lengthVector(self) {
35 return Math.sqrt(self[0] * self[0] + self[1] * self[1] + self[2] * self[2]);
38 function addVector(self, v) {
45 function subVector(self, v) {
52 function scaleVector(self, scale) {
59 function normaliseVector(self) {
60 var len = Math.sqrt(self[0] * self[0] + self[1] * self[1] + self[2] * self[2]);
67 function add(v1, v2) {
68 return new Array(v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]);
71 function sub(v1, v2) {
72 return new Array(v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]);
75 function scalev(v1, v2) {
76 return new Array(v1[0] * v2[0], v1[1] * v2[1], v1[2] * v2[2]);
79 function dot(v1, v2) {
80 return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
83 function scale(v, scale) {
84 return [v[0] * scale, v[1] * scale, v[2] * scale];
87 function cross(v1, v2) {
88 return [v1[1] * v2[2] - v1[2] * v2[1],
89 v1[2] * v2[0] - v1[0] * v2[2],
90 v1[0] * v2[1] - v1[1] * v2[0]];
94 function normalise(v) {
95 var len = lengthVector(v);
96 return [v[0] / len, v[1] / len, v[2] / len];
99 function transformMatrix(self, v) {
101 var x = vals[0] * v[0] + vals[1] * v[1] + vals[2] * v[2] + vals[3];
102 var y = vals[4] * v[0] + vals[5] * v[1] + vals[6] * v[2] + vals[7];
103 var z = vals[8] * v[0] + vals[9] * v[1] + vals[10] * v[2] + vals[11];
107 function invertMatrix(self) {
108 var temp = new Array(16);
112 for (h = 0; h < 3; h++)
113 for (v = 0; v < 3; v++)
114 temp[h + v * 4] = self[v + h * 4];
115 for (i = 0; i < 11; i++)
117 self[3] = tx * self[0] + ty * self[1] + tz * self[2];
118 self[7] = tx * self[4] + ty * self[5] + tz * self[6];
119 self[11] = tx * self[8] + ty * self[9] + tz * self[10];
124 // Triangle intersection using barycentric coord method
125 function Triangle(p1, p2, p3) {
126 var edge1 = sub(p3, p1);
127 var edge2 = sub(p2, p1);
128 var normal = cross(edge1, edge2);
129 if (Math.abs(normal[0]) > Math.abs(normal[1]))
130 if (Math.abs(normal[0]) > Math.abs(normal[2]))
135 if (Math.abs(normal[1]) > Math.abs(normal[2]))
139 var u = (this.axis + 1) % 3;
140 var v = (this.axis + 2) % 3;
146 this.normal = normalise(normal);
147 this.nu = normal[u] / normal[this.axis];
148 this.nv = normal[v] / normal[this.axis];
149 this.nd = dot(normal, p1) / normal[this.axis];
150 var det = u1 * v2 - v1 * u2;
154 this.nv1 = -v1 / det;
156 this.nv2 = -u2 / det;
157 this.material = [0.7, 0.7, 0.7];
160 Triangle.prototype.intersect = function(orig, dir, near, far) {
161 var u = (this.axis + 1) % 3;
162 var v = (this.axis + 2) % 3;
163 var d = dir[this.axis] + this.nu * dir[u] + this.nv * dir[v];
164 var t = (this.nd - orig[this.axis] - this.nu * orig[u] - this.nv * orig[v]) / d;
165 if (t < near || t > far)
167 var Pu = orig[u] + t * dir[u] - this.eu;
168 var Pv = orig[v] + t * dir[v] - this.ev;
169 var a2 = Pv * this.nu1 + Pu * this.nv1;
172 var a3 = Pu * this.nu2 + Pv * this.nv2;
181 function Scene(a_triangles) {
182 this.triangles = a_triangles;
184 this.ambient = [0,0,0];
185 this.background = [0.8,0.8,1];
187 var zero = new Array(0,0,0);
189 Scene.prototype.intersect = function(origin, dir, near, far) {
191 for (i = 0; i < this.triangles.length; i++) {
192 var triangle = this.triangles[i];
193 var d = triangle.intersect(origin, dir, near, far);
194 if (d == null || d > far || d < near)
201 return [this.background[0],this.background[1],this.background[2]];
203 var normal = closest.normal;
204 var hit = add(origin, scale(dir, far));
205 if (dot(dir, normal) > 0)
206 normal = [-normal[0], -normal[1], -normal[2]];
209 if (closest.shader) {
210 colour = closest.shader(closest, hit, dir);
212 colour = closest.material;
216 var reflected = null;
217 if (colour.reflection > 0.001) {
218 var reflection = addVector(scale(normal, -2*dot(dir, normal)), dir);
219 reflected = this.intersect(hit, reflection, 0.0001, 1000000);
220 if (colour.reflection >= 0.999999)
224 var l = [this.ambient[0], this.ambient[1], this.ambient[2]];
225 for (var i = 0; i < this.lights.length; i++) {
226 var light = this.lights[i];
227 var toLight = sub(light, hit);
228 var distance = lengthVector(toLight);
229 scaleVector(toLight, 1.0/distance);
231 if (this.blocked(hit, toLight, distance))
233 var nl = dot(normal, toLight);
235 addVector(l, scale(light.colour, nl));
237 l = scalev(l, colour);
239 l = addVector(scaleVector(l, 1 - colour.reflection), scaleVector(reflected, colour.reflection));
244 Scene.prototype.blocked = function(O, D, far) {
247 for (i = 0; i < this.triangles.length; i++) {
248 var triangle = this.triangles[i];
249 var d = triangle.intersect(O, D, near, far);
250 if (d == null || d > far || d < near)
259 // this camera code is from notes i made ages ago, it is from *somewhere* -- i cannot remember where
261 function Camera(origin, lookat, up) {
262 var zaxis = normaliseVector(subVector(lookat, origin));
263 var xaxis = normaliseVector(cross(up, zaxis));
264 var yaxis = normaliseVector(cross(xaxis, subVector([0,0,0], zaxis)));
265 var m = new Array(16);
266 m[0] = xaxis[0]; m[1] = xaxis[1]; m[2] = xaxis[2];
267 m[4] = yaxis[0]; m[5] = yaxis[1]; m[6] = yaxis[2];
268 m[8] = zaxis[0]; m[9] = zaxis[1]; m[10] = zaxis[2];
270 m[3] = 0; m[7] = 0; m[11] = 0;
271 this.origin = origin;
272 this.directions = new Array(4);
273 this.directions[0] = normalise([-0.7, 0.7, 1]);
274 this.directions[1] = normalise([ 0.7, 0.7, 1]);
275 this.directions[2] = normalise([ 0.7, -0.7, 1]);
276 this.directions[3] = normalise([-0.7, -0.7, 1]);
277 this.directions[0] = transformMatrix(m, this.directions[0]);
278 this.directions[1] = transformMatrix(m, this.directions[1]);
279 this.directions[2] = transformMatrix(m, this.directions[2]);
280 this.directions[3] = transformMatrix(m, this.directions[3]);
283 Camera.prototype.generateRayPair = function(y) {
284 rays = new Array(new Object(), new Object());
285 rays[0].origin = this.origin;
286 rays[1].origin = this.origin;
287 rays[0].dir = addVector(scale(this.directions[0], y), scale(this.directions[3], 1 - y));
288 rays[1].dir = addVector(scale(this.directions[1], y), scale(this.directions[2], 1 - y));
292 function renderRows(camera, scene, pixels, width, height, starty, stopy) {
293 for (var y = starty; y < stopy; y++) {
294 var rays = camera.generateRayPair(y / height);
295 for (var x = 0; x < width; x++) {
297 var origin = addVector(scale(rays[0].origin, xp), scale(rays[1].origin, 1 - xp));
298 var dir = normaliseVector(addVector(scale(rays[0].dir, xp), scale(rays[1].dir, 1 - xp)));
299 var l = scene.intersect(origin, dir);
305 Camera.prototype.render = function(scene, pixels, width, height) {
308 renderRows(cam, scene, pixels, width, height, 0, height);
313 var startDate = new Date().getTime();
314 var numTriangles = 2 * 6;
315 var triangles = new Array();//numTriangles);
316 var tfl = createVector(-10, 10, -10);
317 var tfr = createVector( 10, 10, -10);
318 var tbl = createVector(-10, 10, 10);
319 var tbr = createVector( 10, 10, 10);
320 var bfl = createVector(-10, -10, -10);
321 var bfr = createVector( 10, -10, -10);
322 var bbl = createVector(-10, -10, 10);
323 var bbr = createVector( 10, -10, 10);
329 triangles[i++] = new Triangle(tfl, tfr, bfr);
330 triangles[i++] = new Triangle(tfl, bfr, bfl);
332 triangles[i++] = new Triangle(tbl, tbr, bbr);
333 triangles[i++] = new Triangle(tbl, bbr, bbl);
334 // triangles[i-1].material = [0.7,0.2,0.2];
335 // triangles[i-1].material.reflection = 0.8;
337 triangles[i++] = new Triangle(tbl, tfl, bbl);
338 // triangles[i-1].reflection = 0.6;
339 triangles[i++] = new Triangle(tfl, bfl, bbl);
340 // triangles[i-1].reflection = 0.6;
342 triangles[i++] = new Triangle(tbr, tfr, bbr);
343 triangles[i++] = new Triangle(tfr, bfr, bbr);
345 triangles[i++] = new Triangle(tbl, tbr, tfr);
346 triangles[i++] = new Triangle(tbl, tfr, tfl);
348 triangles[i++] = new Triangle(bbl, bbr, bfr);
349 triangles[i++] = new Triangle(bbl, bfr, bfl);
352 var green = createVector(0.0, 0.4, 0.0);
353 var grey = createVector(0.4, 0.4, 0.4);
354 grey.reflection = 1.0;
355 var floorShader = function(tri, pos, view) {
356 var x = ((pos[0]/32) % 2 + 2) % 2;
357 var z = ((pos[2]/32 + 0.3) % 2 + 2) % 2;
358 if (x < 1 != z < 1) {
359 //in the real world we use the fresnel term...
360 // var angle = 1-dot(view, tri.normal);
364 //grey.reflection = angle;
369 var ffl = createVector(-1000, -30, -1000);
370 var ffr = createVector( 1000, -30, -1000);
371 var fbl = createVector(-1000, -30, 1000);
372 var fbr = createVector( 1000, -30, 1000);
373 triangles[i++] = new Triangle(fbl, fbr, ffr);
374 triangles[i-1].shader = floorShader;
375 triangles[i++] = new Triangle(fbl, ffr, ffl);
376 triangles[i-1].shader = floorShader;
378 var _scene = new Scene(triangles);
379 _scene.lights[0] = createVector(20, 38, -22);
380 _scene.lights[0].colour = createVector(0.7, 0.3, 0.3);
381 _scene.lights[1] = createVector(-23, 40, 17);
382 _scene.lights[1].colour = createVector(0.7, 0.3, 0.3);
383 _scene.lights[2] = createVector(23, 20, 17);
384 _scene.lights[2].colour = createVector(0.7, 0.7, 0.7);
385 _scene.ambient = createVector(0.1, 0.1, 0.1);
386 // _scene.background = createVector(0.7, 0.7, 1.0);
389 var pixels = new Array();
390 for (var y = 0; y < size; y++) {
391 pixels[y] = new Array();
392 for (var x = 0; x < size; x++) {
398 var _camera = new Camera(createVector(-40, 40, 40), createVector(0, 0, 0), createVector(0, 1, 0));
399 _camera.render(_scene, pixels, size, size);