michael@0: // The ray tracer code in this file is written by Adam Burmister. It michael@0: // is available in its original form from: michael@0: // michael@0: // http://labs.flog.nz.co/raytracer/ michael@0: // michael@0: // It has been modified slightly by Google to work as a standalone michael@0: // benchmark, but the all the computational code remains michael@0: // untouched. This file also contains a copy of parts of the Prototype michael@0: // JavaScript framework which is used by the ray tracer. michael@0: michael@0: var RayTrace = new BenchmarkSuite('RayTrace', 739989, [ michael@0: new Benchmark('RayTrace', renderScene) michael@0: ]); michael@0: michael@0: michael@0: // Variable used to hold a number that can be used to verify that michael@0: // the scene was ray traced correctly. michael@0: var checkNumber; michael@0: michael@0: michael@0: // ------------------------------------------------------------------------ michael@0: // ------------------------------------------------------------------------ michael@0: michael@0: // The following is a copy of parts of the Prototype JavaScript library: michael@0: michael@0: // Prototype JavaScript framework, version 1.5.0 michael@0: // (c) 2005-2007 Sam Stephenson michael@0: // michael@0: // Prototype is freely distributable under the terms of an MIT-style license. michael@0: // For details, see the Prototype web site: http://prototype.conio.net/ michael@0: michael@0: michael@0: var Class = { michael@0: create: function() { michael@0: return function() { michael@0: this.initialize.apply(this, arguments); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: michael@0: Object.extend = function(destination, source) { michael@0: for (var property in source) { michael@0: destination[property] = source[property]; michael@0: } michael@0: return destination; michael@0: }; michael@0: michael@0: michael@0: // ------------------------------------------------------------------------ michael@0: // ------------------------------------------------------------------------ michael@0: michael@0: // The rest of this file is the actual ray tracer written by Adam michael@0: // Burmister. It's a concatenation of the following files: michael@0: // michael@0: // flog/color.js michael@0: // flog/light.js michael@0: // flog/vector.js michael@0: // flog/ray.js michael@0: // flog/scene.js michael@0: // flog/material/basematerial.js michael@0: // flog/material/solid.js michael@0: // flog/material/chessboard.js michael@0: // flog/shape/baseshape.js michael@0: // flog/shape/sphere.js michael@0: // flog/shape/plane.js michael@0: // flog/intersectioninfo.js michael@0: // flog/camera.js michael@0: // flog/background.js michael@0: // flog/engine.js michael@0: michael@0: michael@0: /* Fake a Flog.* namespace */ michael@0: if(typeof(Flog) == 'undefined') var Flog = {}; michael@0: if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {}; michael@0: michael@0: Flog.RayTracer.Color = Class.create(); michael@0: michael@0: Flog.RayTracer.Color.prototype = { michael@0: red : 0.0, michael@0: green : 0.0, michael@0: blue : 0.0, michael@0: michael@0: initialize : function(r, g, b) { michael@0: if(!r) r = 0.0; michael@0: if(!g) g = 0.0; michael@0: if(!b) b = 0.0; michael@0: michael@0: this.red = r; michael@0: this.green = g; michael@0: this.blue = b; michael@0: }, michael@0: michael@0: add : function(c1, c2){ michael@0: var result = new Flog.RayTracer.Color(0,0,0); michael@0: michael@0: result.red = c1.red + c2.red; michael@0: result.green = c1.green + c2.green; michael@0: result.blue = c1.blue + c2.blue; michael@0: michael@0: return result; michael@0: }, michael@0: michael@0: addScalar: function(c1, s){ michael@0: var result = new Flog.RayTracer.Color(0,0,0); michael@0: michael@0: result.red = c1.red + s; michael@0: result.green = c1.green + s; michael@0: result.blue = c1.blue + s; michael@0: michael@0: result.limit(); michael@0: michael@0: return result; michael@0: }, michael@0: michael@0: subtract: function(c1, c2){ michael@0: var result = new Flog.RayTracer.Color(0,0,0); michael@0: michael@0: result.red = c1.red - c2.red; michael@0: result.green = c1.green - c2.green; michael@0: result.blue = c1.blue - c2.blue; michael@0: michael@0: return result; michael@0: }, michael@0: michael@0: multiply : function(c1, c2) { michael@0: var result = new Flog.RayTracer.Color(0,0,0); michael@0: michael@0: result.red = c1.red * c2.red; michael@0: result.green = c1.green * c2.green; michael@0: result.blue = c1.blue * c2.blue; michael@0: michael@0: return result; michael@0: }, michael@0: michael@0: multiplyScalar : function(c1, f) { michael@0: var result = new Flog.RayTracer.Color(0,0,0); michael@0: michael@0: result.red = c1.red * f; michael@0: result.green = c1.green * f; michael@0: result.blue = c1.blue * f; michael@0: michael@0: return result; michael@0: }, michael@0: michael@0: divideFactor : function(c1, f) { michael@0: var result = new Flog.RayTracer.Color(0,0,0); michael@0: michael@0: result.red = c1.red / f; michael@0: result.green = c1.green / f; michael@0: result.blue = c1.blue / f; michael@0: michael@0: return result; michael@0: }, michael@0: michael@0: limit: function(){ michael@0: this.red = (this.red > 0.0) ? ( (this.red > 1.0) ? 1.0 : this.red ) : 0.0; michael@0: this.green = (this.green > 0.0) ? ( (this.green > 1.0) ? 1.0 : this.green ) : 0.0; michael@0: this.blue = (this.blue > 0.0) ? ( (this.blue > 1.0) ? 1.0 : this.blue ) : 0.0; michael@0: }, michael@0: michael@0: distance : function(color) { michael@0: var d = Math.abs(this.red - color.red) + Math.abs(this.green - color.green) + Math.abs(this.blue - color.blue); michael@0: return d; michael@0: }, michael@0: michael@0: blend: function(c1, c2, w){ michael@0: var result = new Flog.RayTracer.Color(0,0,0); michael@0: result = Flog.RayTracer.Color.prototype.add( michael@0: Flog.RayTracer.Color.prototype.multiplyScalar(c1, 1 - w), michael@0: Flog.RayTracer.Color.prototype.multiplyScalar(c2, w) michael@0: ); michael@0: return result; michael@0: }, michael@0: michael@0: brightness : function() { michael@0: var r = Math.floor(this.red*255); michael@0: var g = Math.floor(this.green*255); michael@0: var b = Math.floor(this.blue*255); michael@0: return (r * 77 + g * 150 + b * 29) >> 8; michael@0: }, michael@0: michael@0: toString : function () { michael@0: var r = Math.floor(this.red*255); michael@0: var g = Math.floor(this.green*255); michael@0: var b = Math.floor(this.blue*255); michael@0: michael@0: return "rgb("+ r +","+ g +","+ b +")"; michael@0: } michael@0: } michael@0: /* Fake a Flog.* namespace */ michael@0: if(typeof(Flog) == 'undefined') var Flog = {}; michael@0: if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {}; michael@0: michael@0: Flog.RayTracer.Light = Class.create(); michael@0: michael@0: Flog.RayTracer.Light.prototype = { michael@0: position: null, michael@0: color: null, michael@0: intensity: 10.0, michael@0: michael@0: initialize : function(pos, color, intensity) { michael@0: this.position = pos; michael@0: this.color = color; michael@0: this.intensity = (intensity ? intensity : 10.0); michael@0: }, michael@0: michael@0: toString : function () { michael@0: return 'Light [' + this.position.x + ',' + this.position.y + ',' + this.position.z + ']'; michael@0: } michael@0: } michael@0: /* Fake a Flog.* namespace */ michael@0: if(typeof(Flog) == 'undefined') var Flog = {}; michael@0: if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {}; michael@0: michael@0: Flog.RayTracer.Vector = Class.create(); michael@0: michael@0: Flog.RayTracer.Vector.prototype = { michael@0: x : 0.0, michael@0: y : 0.0, michael@0: z : 0.0, michael@0: michael@0: initialize : function(x, y, z) { michael@0: this.x = (x ? x : 0); michael@0: this.y = (y ? y : 0); michael@0: this.z = (z ? z : 0); michael@0: }, michael@0: michael@0: copy: function(vector){ michael@0: this.x = vector.x; michael@0: this.y = vector.y; michael@0: this.z = vector.z; michael@0: }, michael@0: michael@0: normalize : function() { michael@0: var m = this.magnitude(); michael@0: return new Flog.RayTracer.Vector(this.x / m, this.y / m, this.z / m); michael@0: }, michael@0: michael@0: magnitude : function() { michael@0: return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z)); michael@0: }, michael@0: michael@0: cross : function(w) { michael@0: return new Flog.RayTracer.Vector( michael@0: -this.z * w.y + this.y * w.z, michael@0: this.z * w.x - this.x * w.z, michael@0: -this.y * w.x + this.x * w.y); michael@0: }, michael@0: michael@0: dot : function(w) { michael@0: return this.x * w.x + this.y * w.y + this.z * w.z; michael@0: }, michael@0: michael@0: add : function(v, w) { michael@0: return new Flog.RayTracer.Vector(w.x + v.x, w.y + v.y, w.z + v.z); michael@0: }, michael@0: michael@0: subtract : function(v, w) { michael@0: if(!w || !v) throw 'Vectors must be defined [' + v + ',' + w + ']'; michael@0: return new Flog.RayTracer.Vector(v.x - w.x, v.y - w.y, v.z - w.z); michael@0: }, michael@0: michael@0: multiplyVector : function(v, w) { michael@0: return new Flog.RayTracer.Vector(v.x * w.x, v.y * w.y, v.z * w.z); michael@0: }, michael@0: michael@0: multiplyScalar : function(v, w) { michael@0: return new Flog.RayTracer.Vector(v.x * w, v.y * w, v.z * w); michael@0: }, michael@0: michael@0: toString : function () { michael@0: return 'Vector [' + this.x + ',' + this.y + ',' + this.z + ']'; michael@0: } michael@0: } michael@0: /* Fake a Flog.* namespace */ michael@0: if(typeof(Flog) == 'undefined') var Flog = {}; michael@0: if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {}; michael@0: michael@0: Flog.RayTracer.Ray = Class.create(); michael@0: michael@0: Flog.RayTracer.Ray.prototype = { michael@0: position : null, michael@0: direction : null, michael@0: initialize : function(pos, dir) { michael@0: this.position = pos; michael@0: this.direction = dir; michael@0: }, michael@0: michael@0: toString : function () { michael@0: return 'Ray [' + this.position + ',' + this.direction + ']'; michael@0: } michael@0: } michael@0: /* Fake a Flog.* namespace */ michael@0: if(typeof(Flog) == 'undefined') var Flog = {}; michael@0: if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {}; michael@0: michael@0: Flog.RayTracer.Scene = Class.create(); michael@0: michael@0: Flog.RayTracer.Scene.prototype = { michael@0: camera : null, michael@0: shapes : [], michael@0: lights : [], michael@0: background : null, michael@0: michael@0: initialize : function() { michael@0: this.camera = new Flog.RayTracer.Camera( michael@0: new Flog.RayTracer.Vector(0,0,-5), michael@0: new Flog.RayTracer.Vector(0,0,1), michael@0: new Flog.RayTracer.Vector(0,1,0) michael@0: ); michael@0: this.shapes = new Array(); michael@0: this.lights = new Array(); michael@0: this.background = new Flog.RayTracer.Background(new Flog.RayTracer.Color(0,0,0.5), 0.2); michael@0: } michael@0: } michael@0: /* Fake a Flog.* namespace */ michael@0: if(typeof(Flog) == 'undefined') var Flog = {}; michael@0: if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {}; michael@0: if(typeof(Flog.RayTracer.Material) == 'undefined') Flog.RayTracer.Material = {}; michael@0: michael@0: Flog.RayTracer.Material.BaseMaterial = Class.create(); michael@0: michael@0: Flog.RayTracer.Material.BaseMaterial.prototype = { michael@0: michael@0: gloss: 2.0, // [0...infinity] 0 = matt michael@0: transparency: 0.0, // 0=opaque michael@0: reflection: 0.0, // [0...infinity] 0 = no reflection michael@0: refraction: 0.50, michael@0: hasTexture: false, michael@0: michael@0: initialize : function() { michael@0: michael@0: }, michael@0: michael@0: getColor: function(u, v){ michael@0: michael@0: }, michael@0: michael@0: wrapUp: function(t){ michael@0: t = t % 2.0; michael@0: if(t < -1) t += 2.0; michael@0: if(t >= 1) t -= 2.0; michael@0: return t; michael@0: }, michael@0: michael@0: toString : function () { michael@0: return 'Material [gloss=' + this.gloss + ', transparency=' + this.transparency + ', hasTexture=' + this.hasTexture +']'; michael@0: } michael@0: } michael@0: /* Fake a Flog.* namespace */ michael@0: if(typeof(Flog) == 'undefined') var Flog = {}; michael@0: if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {}; michael@0: michael@0: Flog.RayTracer.Material.Solid = Class.create(); michael@0: michael@0: Flog.RayTracer.Material.Solid.prototype = Object.extend( michael@0: new Flog.RayTracer.Material.BaseMaterial(), { michael@0: initialize : function(color, reflection, refraction, transparency, gloss) { michael@0: this.color = color; michael@0: this.reflection = reflection; michael@0: this.transparency = transparency; michael@0: this.gloss = gloss; michael@0: this.hasTexture = false; michael@0: }, michael@0: michael@0: getColor: function(u, v){ michael@0: return this.color; michael@0: }, michael@0: michael@0: toString : function () { michael@0: return 'SolidMaterial [gloss=' + this.gloss + ', transparency=' + this.transparency + ', hasTexture=' + this.hasTexture +']'; michael@0: } michael@0: } michael@0: ); michael@0: /* Fake a Flog.* namespace */ michael@0: if(typeof(Flog) == 'undefined') var Flog = {}; michael@0: if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {}; michael@0: michael@0: Flog.RayTracer.Material.Chessboard = Class.create(); michael@0: michael@0: Flog.RayTracer.Material.Chessboard.prototype = Object.extend( michael@0: new Flog.RayTracer.Material.BaseMaterial(), { michael@0: colorEven: null, michael@0: colorOdd: null, michael@0: density: 0.5, michael@0: michael@0: initialize : function(colorEven, colorOdd, reflection, transparency, gloss, density) { michael@0: this.colorEven = colorEven; michael@0: this.colorOdd = colorOdd; michael@0: this.reflection = reflection; michael@0: this.transparency = transparency; michael@0: this.gloss = gloss; michael@0: this.density = density; michael@0: this.hasTexture = true; michael@0: }, michael@0: michael@0: getColor: function(u, v){ michael@0: var t = this.wrapUp(u * this.density) * this.wrapUp(v * this.density); michael@0: michael@0: if(t < 0.0) michael@0: return this.colorEven; michael@0: else michael@0: return this.colorOdd; michael@0: }, michael@0: michael@0: toString : function () { michael@0: return 'ChessMaterial [gloss=' + this.gloss + ', transparency=' + this.transparency + ', hasTexture=' + this.hasTexture +']'; michael@0: } michael@0: } michael@0: ); michael@0: /* Fake a Flog.* namespace */ michael@0: if(typeof(Flog) == 'undefined') var Flog = {}; michael@0: if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {}; michael@0: if(typeof(Flog.RayTracer.Shape) == 'undefined') Flog.RayTracer.Shape = {}; michael@0: michael@0: Flog.RayTracer.Shape.Sphere = Class.create(); michael@0: michael@0: Flog.RayTracer.Shape.Sphere.prototype = { michael@0: initialize : function(pos, radius, material) { michael@0: this.radius = radius; michael@0: this.position = pos; michael@0: this.material = material; michael@0: }, michael@0: michael@0: intersect: function(ray){ michael@0: var info = new Flog.RayTracer.IntersectionInfo(); michael@0: info.shape = this; michael@0: michael@0: var dst = Flog.RayTracer.Vector.prototype.subtract(ray.position, this.position); michael@0: michael@0: var B = dst.dot(ray.direction); michael@0: var C = dst.dot(dst) - (this.radius * this.radius); michael@0: var D = (B * B) - C; michael@0: michael@0: if(D > 0){ // intersection! michael@0: info.isHit = true; michael@0: info.distance = (-B) - Math.sqrt(D); michael@0: info.position = Flog.RayTracer.Vector.prototype.add( michael@0: ray.position, michael@0: Flog.RayTracer.Vector.prototype.multiplyScalar( michael@0: ray.direction, michael@0: info.distance michael@0: ) michael@0: ); michael@0: info.normal = Flog.RayTracer.Vector.prototype.subtract( michael@0: info.position, michael@0: this.position michael@0: ).normalize(); michael@0: michael@0: info.color = this.material.getColor(0,0); michael@0: } else { michael@0: info.isHit = false; michael@0: } michael@0: return info; michael@0: }, michael@0: michael@0: toString : function () { michael@0: return 'Sphere [position=' + this.position + ', radius=' + this.radius + ']'; michael@0: } michael@0: } michael@0: /* Fake a Flog.* namespace */ michael@0: if(typeof(Flog) == 'undefined') var Flog = {}; michael@0: if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {}; michael@0: if(typeof(Flog.RayTracer.Shape) == 'undefined') Flog.RayTracer.Shape = {}; michael@0: michael@0: Flog.RayTracer.Shape.Plane = Class.create(); michael@0: michael@0: Flog.RayTracer.Shape.Plane.prototype = { michael@0: d: 0.0, michael@0: michael@0: initialize : function(pos, d, material) { michael@0: this.position = pos; michael@0: this.d = d; michael@0: this.material = material; michael@0: }, michael@0: michael@0: intersect: function(ray){ michael@0: var info = new Flog.RayTracer.IntersectionInfo(); michael@0: michael@0: var Vd = this.position.dot(ray.direction); michael@0: if(Vd == 0) return info; // no intersection michael@0: michael@0: var t = -(this.position.dot(ray.position) + this.d) / Vd; michael@0: if(t <= 0) return info; michael@0: michael@0: info.shape = this; michael@0: info.isHit = true; michael@0: info.position = Flog.RayTracer.Vector.prototype.add( michael@0: ray.position, michael@0: Flog.RayTracer.Vector.prototype.multiplyScalar( michael@0: ray.direction, michael@0: t michael@0: ) michael@0: ); michael@0: info.normal = this.position; michael@0: info.distance = t; michael@0: michael@0: if(this.material.hasTexture){ michael@0: var vU = new Flog.RayTracer.Vector(this.position.y, this.position.z, -this.position.x); michael@0: var vV = vU.cross(this.position); michael@0: var u = info.position.dot(vU); michael@0: var v = info.position.dot(vV); michael@0: info.color = this.material.getColor(u,v); michael@0: } else { michael@0: info.color = this.material.getColor(0,0); michael@0: } michael@0: michael@0: return info; michael@0: }, michael@0: michael@0: toString : function () { michael@0: return 'Plane [' + this.position + ', d=' + this.d + ']'; michael@0: } michael@0: } michael@0: /* Fake a Flog.* namespace */ michael@0: if(typeof(Flog) == 'undefined') var Flog = {}; michael@0: if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {}; michael@0: michael@0: Flog.RayTracer.IntersectionInfo = Class.create(); michael@0: michael@0: Flog.RayTracer.IntersectionInfo.prototype = { michael@0: isHit: false, michael@0: hitCount: 0, michael@0: shape: null, michael@0: position: null, michael@0: normal: null, michael@0: color: null, michael@0: distance: null, michael@0: michael@0: initialize : function() { michael@0: this.color = new Flog.RayTracer.Color(0,0,0); michael@0: }, michael@0: michael@0: toString : function () { michael@0: return 'Intersection [' + this.position + ']'; michael@0: } michael@0: } michael@0: /* Fake a Flog.* namespace */ michael@0: if(typeof(Flog) == 'undefined') var Flog = {}; michael@0: if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {}; michael@0: michael@0: Flog.RayTracer.Camera = Class.create(); michael@0: michael@0: Flog.RayTracer.Camera.prototype = { michael@0: position: null, michael@0: lookAt: null, michael@0: equator: null, michael@0: up: null, michael@0: screen: null, michael@0: michael@0: initialize : function(pos, lookAt, up) { michael@0: this.position = pos; michael@0: this.lookAt = lookAt; michael@0: this.up = up; michael@0: this.equator = lookAt.normalize().cross(this.up); michael@0: this.screen = Flog.RayTracer.Vector.prototype.add(this.position, this.lookAt); michael@0: }, michael@0: michael@0: getRay: function(vx, vy){ michael@0: var pos = Flog.RayTracer.Vector.prototype.subtract( michael@0: this.screen, michael@0: Flog.RayTracer.Vector.prototype.subtract( michael@0: Flog.RayTracer.Vector.prototype.multiplyScalar(this.equator, vx), michael@0: Flog.RayTracer.Vector.prototype.multiplyScalar(this.up, vy) michael@0: ) michael@0: ); michael@0: pos.y = pos.y * -1; michael@0: var dir = Flog.RayTracer.Vector.prototype.subtract( michael@0: pos, michael@0: this.position michael@0: ); michael@0: michael@0: var ray = new Flog.RayTracer.Ray(pos, dir.normalize()); michael@0: michael@0: return ray; michael@0: }, michael@0: michael@0: toString : function () { michael@0: return 'Ray []'; michael@0: } michael@0: } michael@0: /* Fake a Flog.* namespace */ michael@0: if(typeof(Flog) == 'undefined') var Flog = {}; michael@0: if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {}; michael@0: michael@0: Flog.RayTracer.Background = Class.create(); michael@0: michael@0: Flog.RayTracer.Background.prototype = { michael@0: color : null, michael@0: ambience : 0.0, michael@0: michael@0: initialize : function(color, ambience) { michael@0: this.color = color; michael@0: this.ambience = ambience; michael@0: } michael@0: } michael@0: /* Fake a Flog.* namespace */ michael@0: if(typeof(Flog) == 'undefined') var Flog = {}; michael@0: if(typeof(Flog.RayTracer) == 'undefined') Flog.RayTracer = {}; michael@0: michael@0: Flog.RayTracer.Engine = Class.create(); michael@0: michael@0: Flog.RayTracer.Engine.prototype = { michael@0: canvas: null, /* 2d context we can render to */ michael@0: michael@0: initialize: function(options){ michael@0: this.options = Object.extend({ michael@0: canvasHeight: 100, michael@0: canvasWidth: 100, michael@0: pixelWidth: 2, michael@0: pixelHeight: 2, michael@0: renderDiffuse: false, michael@0: renderShadows: false, michael@0: renderHighlights: false, michael@0: renderReflections: false, michael@0: rayDepth: 2 michael@0: }, options || {}); michael@0: michael@0: this.options.canvasHeight /= this.options.pixelHeight; michael@0: this.options.canvasWidth /= this.options.pixelWidth; michael@0: michael@0: /* TODO: dynamically include other scripts */ michael@0: }, michael@0: michael@0: setPixel: function(x, y, color){ michael@0: var pxW, pxH; michael@0: pxW = this.options.pixelWidth; michael@0: pxH = this.options.pixelHeight; michael@0: michael@0: if (this.canvas) { michael@0: this.canvas.fillStyle = color.toString(); michael@0: this.canvas.fillRect (x * pxW, y * pxH, pxW, pxH); michael@0: } else { michael@0: if (x === y) { michael@0: checkNumber += color.brightness(); michael@0: } michael@0: // print(x * pxW, y * pxH, pxW, pxH); michael@0: } michael@0: }, michael@0: michael@0: renderScene: function(scene, canvas){ michael@0: checkNumber = 0; michael@0: /* Get canvas */ michael@0: if (canvas) { michael@0: this.canvas = canvas.getContext("2d"); michael@0: } else { michael@0: this.canvas = null; michael@0: } michael@0: michael@0: var canvasHeight = this.options.canvasHeight; michael@0: var canvasWidth = this.options.canvasWidth; michael@0: michael@0: for(var y=0; y < canvasHeight; y++){ michael@0: for(var x=0; x < canvasWidth; x++){ michael@0: var yp = y * 1.0 / canvasHeight * 2 - 1; michael@0: var xp = x * 1.0 / canvasWidth * 2 - 1; michael@0: michael@0: var ray = scene.camera.getRay(xp, yp); michael@0: michael@0: var color = this.getPixelColor(ray, scene); michael@0: michael@0: this.setPixel(x, y, color); michael@0: } michael@0: } michael@0: if (checkNumber !== 2321) { michael@0: throw new Error("Scene rendered incorrectly"); michael@0: } michael@0: }, michael@0: michael@0: getPixelColor: function(ray, scene){ michael@0: var info = this.testIntersection(ray, scene, null); michael@0: if(info.isHit){ michael@0: var color = this.rayTrace(info, ray, scene, 0); michael@0: return color; michael@0: } michael@0: return scene.background.color; michael@0: }, michael@0: michael@0: testIntersection: function(ray, scene, exclude){ michael@0: var hits = 0; michael@0: var best = new Flog.RayTracer.IntersectionInfo(); michael@0: best.distance = 2000; michael@0: michael@0: for(var i=0; i= 0 && info.distance < best.distance){ michael@0: best = info; michael@0: hits++; michael@0: } michael@0: } michael@0: } michael@0: best.hitCount = hits; michael@0: return best; michael@0: }, michael@0: michael@0: getReflectionRay: function(P,N,V){ michael@0: var c1 = -N.dot(V); michael@0: var R1 = Flog.RayTracer.Vector.prototype.add( michael@0: Flog.RayTracer.Vector.prototype.multiplyScalar(N, 2*c1), michael@0: V michael@0: ); michael@0: return new Flog.RayTracer.Ray(P, R1); michael@0: }, michael@0: michael@0: rayTrace: function(info, ray, scene, depth){ michael@0: // Calc ambient michael@0: var color = Flog.RayTracer.Color.prototype.multiplyScalar(info.color, scene.background.ambience); michael@0: var oldColor = color; michael@0: var shininess = Math.pow(10, info.shape.material.gloss + 1); michael@0: michael@0: for(var i=0; i 0.0){ michael@0: color = Flog.RayTracer.Color.prototype.add( michael@0: color, michael@0: Flog.RayTracer.Color.prototype.multiply( michael@0: info.color, michael@0: Flog.RayTracer.Color.prototype.multiplyScalar( michael@0: light.color, michael@0: L michael@0: ) michael@0: ) michael@0: ); michael@0: } michael@0: } michael@0: michael@0: // The greater the depth the more accurate the colours, but michael@0: // this is exponentially (!) expensive michael@0: if(depth <= this.options.rayDepth){ michael@0: // calculate reflection ray michael@0: if(this.options.renderReflections && info.shape.material.reflection > 0) michael@0: { michael@0: var reflectionRay = this.getReflectionRay(info.position, info.normal, ray.direction); michael@0: var refl = this.testIntersection(reflectionRay, scene, info.shape); michael@0: michael@0: if (refl.isHit && refl.distance > 0){ michael@0: refl.color = this.rayTrace(refl, reflectionRay, scene, depth + 1); michael@0: } else { michael@0: refl.color = scene.background.color; michael@0: } michael@0: michael@0: color = Flog.RayTracer.Color.prototype.blend( michael@0: color, michael@0: refl.color, michael@0: info.shape.material.reflection michael@0: ); michael@0: } michael@0: michael@0: // Refraction michael@0: /* TODO */ michael@0: } michael@0: michael@0: /* Render shadows and highlights */ michael@0: michael@0: var shadowInfo = new Flog.RayTracer.IntersectionInfo(); michael@0: michael@0: if(this.options.renderShadows){ michael@0: var shadowRay = new Flog.RayTracer.Ray(info.position, v); michael@0: michael@0: shadowInfo = this.testIntersection(shadowRay, scene, info.shape); michael@0: if(shadowInfo.isHit && shadowInfo.shape != info.shape /*&& shadowInfo.shape.type != 'PLANE'*/){ michael@0: var vA = Flog.RayTracer.Color.prototype.multiplyScalar(color, 0.5); michael@0: var dB = (0.5 * Math.pow(shadowInfo.shape.material.transparency, 0.5)); michael@0: color = Flog.RayTracer.Color.prototype.addScalar(vA,dB); michael@0: } michael@0: } michael@0: michael@0: // Phong specular highlights michael@0: if(this.options.renderHighlights && !shadowInfo.isHit && info.shape.material.gloss > 0){ michael@0: var Lv = Flog.RayTracer.Vector.prototype.subtract( michael@0: info.shape.position, michael@0: light.position michael@0: ).normalize(); michael@0: michael@0: var E = Flog.RayTracer.Vector.prototype.subtract( michael@0: scene.camera.position, michael@0: info.shape.position michael@0: ).normalize(); michael@0: michael@0: var H = Flog.RayTracer.Vector.prototype.subtract( michael@0: E, michael@0: Lv michael@0: ).normalize(); michael@0: michael@0: var glossWeight = Math.pow(Math.max(info.normal.dot(H), 0), shininess); michael@0: color = Flog.RayTracer.Color.prototype.add( michael@0: Flog.RayTracer.Color.prototype.multiplyScalar(light.color, glossWeight), michael@0: color michael@0: ); michael@0: } michael@0: } michael@0: color.limit(); michael@0: return color; michael@0: } michael@0: }; michael@0: michael@0: michael@0: function renderScene(){ michael@0: var scene = new Flog.RayTracer.Scene(); michael@0: michael@0: scene.camera = new Flog.RayTracer.Camera( michael@0: new Flog.RayTracer.Vector(0, 0, -15), michael@0: new Flog.RayTracer.Vector(-0.2, 0, 5), michael@0: new Flog.RayTracer.Vector(0, 1, 0) michael@0: ); michael@0: michael@0: scene.background = new Flog.RayTracer.Background( michael@0: new Flog.RayTracer.Color(0.5, 0.5, 0.5), michael@0: 0.4 michael@0: ); michael@0: michael@0: var sphere = new Flog.RayTracer.Shape.Sphere( michael@0: new Flog.RayTracer.Vector(-1.5, 1.5, 2), michael@0: 1.5, michael@0: new Flog.RayTracer.Material.Solid( michael@0: new Flog.RayTracer.Color(0,0.5,0.5), michael@0: 0.3, michael@0: 0.0, michael@0: 0.0, michael@0: 2.0 michael@0: ) michael@0: ); michael@0: michael@0: var sphere1 = new Flog.RayTracer.Shape.Sphere( michael@0: new Flog.RayTracer.Vector(1, 0.25, 1), michael@0: 0.5, michael@0: new Flog.RayTracer.Material.Solid( michael@0: new Flog.RayTracer.Color(0.9,0.9,0.9), michael@0: 0.1, michael@0: 0.0, michael@0: 0.0, michael@0: 1.5 michael@0: ) michael@0: ); michael@0: michael@0: var plane = new Flog.RayTracer.Shape.Plane( michael@0: new Flog.RayTracer.Vector(0.1, 0.9, -0.5).normalize(), michael@0: 1.2, michael@0: new Flog.RayTracer.Material.Chessboard( michael@0: new Flog.RayTracer.Color(1,1,1), michael@0: new Flog.RayTracer.Color(0,0,0), michael@0: 0.2, michael@0: 0.0, michael@0: 1.0, michael@0: 0.7 michael@0: ) michael@0: ); michael@0: michael@0: scene.shapes.push(plane); michael@0: scene.shapes.push(sphere); michael@0: scene.shapes.push(sphere1); michael@0: michael@0: var light = new Flog.RayTracer.Light( michael@0: new Flog.RayTracer.Vector(5, 10, -1), michael@0: new Flog.RayTracer.Color(0.8, 0.8, 0.8) michael@0: ); michael@0: michael@0: var light1 = new Flog.RayTracer.Light( michael@0: new Flog.RayTracer.Vector(-3, 5, -15), michael@0: new Flog.RayTracer.Color(0.8, 0.8, 0.8), michael@0: 100 michael@0: ); michael@0: michael@0: scene.lights.push(light); michael@0: scene.lights.push(light1); michael@0: michael@0: var imageWidth = 100; // $F('imageWidth'); michael@0: var imageHeight = 100; // $F('imageHeight'); michael@0: var pixelSize = "5,5".split(','); // $F('pixelSize').split(','); michael@0: var renderDiffuse = true; // $F('renderDiffuse'); michael@0: var renderShadows = true; // $F('renderShadows'); michael@0: var renderHighlights = true; // $F('renderHighlights'); michael@0: var renderReflections = true; // $F('renderReflections'); michael@0: var rayDepth = 2;//$F('rayDepth'); michael@0: michael@0: var raytracer = new Flog.RayTracer.Engine( michael@0: { michael@0: canvasWidth: imageWidth, michael@0: canvasHeight: imageHeight, michael@0: pixelWidth: pixelSize[0], michael@0: pixelHeight: pixelSize[1], michael@0: "renderDiffuse": renderDiffuse, michael@0: "renderHighlights": renderHighlights, michael@0: "renderShadows": renderShadows, michael@0: "renderReflections": renderReflections, michael@0: "rayDepth": rayDepth michael@0: } michael@0: ); michael@0: michael@0: raytracer.renderScene(scene, null, 0); michael@0: }