Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | <!DOCTYPE html> |
michael@0 | 2 | <head> |
michael@0 | 3 | <!-- |
michael@0 | 4 | Copyright (C) 2007 Apple Inc. All rights reserved. |
michael@0 | 5 | |
michael@0 | 6 | Redistribution and use in source and binary forms, with or without |
michael@0 | 7 | modification, are permitted provided that the following conditions |
michael@0 | 8 | are met: |
michael@0 | 9 | 1. Redistributions of source code must retain the above copyright |
michael@0 | 10 | notice, this list of conditions and the following disclaimer. |
michael@0 | 11 | 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 12 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 13 | documentation and/or other materials provided with the distribution. |
michael@0 | 14 | |
michael@0 | 15 | THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
michael@0 | 16 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
michael@0 | 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
michael@0 | 18 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
michael@0 | 19 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 20 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 21 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 22 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
michael@0 | 23 | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 26 | --> |
michael@0 | 27 | |
michael@0 | 28 | <title>SunSpider 3d-raytrace</title> |
michael@0 | 29 | </head> |
michael@0 | 30 | |
michael@0 | 31 | <body> |
michael@0 | 32 | <h3>3d-raytrace</h3> |
michael@0 | 33 | <div id="console"> |
michael@0 | 34 | </div> |
michael@0 | 35 | |
michael@0 | 36 | <script> |
michael@0 | 37 | |
michael@0 | 38 | var _sunSpiderStartDate = new Date(); |
michael@0 | 39 | |
michael@0 | 40 | /* |
michael@0 | 41 | * Copyright (C) 2007 Apple Inc. All rights reserved. |
michael@0 | 42 | * |
michael@0 | 43 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 44 | * modification, are permitted provided that the following conditions |
michael@0 | 45 | * are met: |
michael@0 | 46 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 47 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 48 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 49 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 50 | * documentation and/or other materials provided with the distribution. |
michael@0 | 51 | * |
michael@0 | 52 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
michael@0 | 53 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
michael@0 | 54 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
michael@0 | 55 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
michael@0 | 56 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 57 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 58 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 59 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
michael@0 | 60 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 61 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 62 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 63 | */ |
michael@0 | 64 | |
michael@0 | 65 | function createVector(x,y,z) { |
michael@0 | 66 | return new Array(x,y,z); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | function sqrLengthVector(self) { |
michael@0 | 70 | return self[0] * self[0] + self[1] * self[1] + self[2] * self[2]; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | function lengthVector(self) { |
michael@0 | 74 | return Math.sqrt(self[0] * self[0] + self[1] * self[1] + self[2] * self[2]); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | function addVector(self, v) { |
michael@0 | 78 | self[0] += v[0]; |
michael@0 | 79 | self[1] += v[1]; |
michael@0 | 80 | self[2] += v[2]; |
michael@0 | 81 | return self; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | function subVector(self, v) { |
michael@0 | 85 | self[0] -= v[0]; |
michael@0 | 86 | self[1] -= v[1]; |
michael@0 | 87 | self[2] -= v[2]; |
michael@0 | 88 | return self; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | function scaleVector(self, scale) { |
michael@0 | 92 | self[0] *= scale; |
michael@0 | 93 | self[1] *= scale; |
michael@0 | 94 | self[2] *= scale; |
michael@0 | 95 | return self; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | function normaliseVector(self) { |
michael@0 | 99 | var len = Math.sqrt(self[0] * self[0] + self[1] * self[1] + self[2] * self[2]); |
michael@0 | 100 | self[0] /= len; |
michael@0 | 101 | self[1] /= len; |
michael@0 | 102 | self[2] /= len; |
michael@0 | 103 | return self; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | function add(v1, v2) { |
michael@0 | 107 | return new Array(v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | function sub(v1, v2) { |
michael@0 | 111 | return new Array(v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | function scalev(v1, v2) { |
michael@0 | 115 | return new Array(v1[0] * v2[0], v1[1] * v2[1], v1[2] * v2[2]); |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | function dot(v1, v2) { |
michael@0 | 119 | return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2]; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | function scale(v, scale) { |
michael@0 | 123 | return [v[0] * scale, v[1] * scale, v[2] * scale]; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | function cross(v1, v2) { |
michael@0 | 127 | return [v1[1] * v2[2] - v1[2] * v2[1], |
michael@0 | 128 | v1[2] * v2[0] - v1[0] * v2[2], |
michael@0 | 129 | v1[0] * v2[1] - v1[1] * v2[0]]; |
michael@0 | 130 | |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | function normalise(v) { |
michael@0 | 134 | var len = lengthVector(v); |
michael@0 | 135 | return [v[0] / len, v[1] / len, v[2] / len]; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | function transformMatrix(self, v) { |
michael@0 | 139 | var vals = self; |
michael@0 | 140 | var x = vals[0] * v[0] + vals[1] * v[1] + vals[2] * v[2] + vals[3]; |
michael@0 | 141 | var y = vals[4] * v[0] + vals[5] * v[1] + vals[6] * v[2] + vals[7]; |
michael@0 | 142 | var z = vals[8] * v[0] + vals[9] * v[1] + vals[10] * v[2] + vals[11]; |
michael@0 | 143 | return [x, y, z]; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | function invertMatrix(self) { |
michael@0 | 147 | var temp = new Array(16); |
michael@0 | 148 | var tx = -self[3]; |
michael@0 | 149 | var ty = -self[7]; |
michael@0 | 150 | var tz = -self[11]; |
michael@0 | 151 | for (h = 0; h < 3; h++) |
michael@0 | 152 | for (v = 0; v < 3; v++) |
michael@0 | 153 | temp[h + v * 4] = self[v + h * 4]; |
michael@0 | 154 | for (i = 0; i < 11; i++) |
michael@0 | 155 | self[i] = temp[i]; |
michael@0 | 156 | self[3] = tx * self[0] + ty * self[1] + tz * self[2]; |
michael@0 | 157 | self[7] = tx * self[4] + ty * self[5] + tz * self[6]; |
michael@0 | 158 | self[11] = tx * self[8] + ty * self[9] + tz * self[10]; |
michael@0 | 159 | return self; |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | |
michael@0 | 163 | // Triangle intersection using barycentric coord method |
michael@0 | 164 | function Triangle(p1, p2, p3) { |
michael@0 | 165 | var edge1 = sub(p3, p1); |
michael@0 | 166 | var edge2 = sub(p2, p1); |
michael@0 | 167 | var normal = cross(edge1, edge2); |
michael@0 | 168 | if (Math.abs(normal[0]) > Math.abs(normal[1])) |
michael@0 | 169 | if (Math.abs(normal[0]) > Math.abs(normal[2])) |
michael@0 | 170 | this.axis = 0; |
michael@0 | 171 | else |
michael@0 | 172 | this.axis = 2; |
michael@0 | 173 | else |
michael@0 | 174 | if (Math.abs(normal[1]) > Math.abs(normal[2])) |
michael@0 | 175 | this.axis = 1; |
michael@0 | 176 | else |
michael@0 | 177 | this.axis = 2; |
michael@0 | 178 | var u = (this.axis + 1) % 3; |
michael@0 | 179 | var v = (this.axis + 2) % 3; |
michael@0 | 180 | var u1 = edge1[u]; |
michael@0 | 181 | var v1 = edge1[v]; |
michael@0 | 182 | |
michael@0 | 183 | var u2 = edge2[u]; |
michael@0 | 184 | var v2 = edge2[v]; |
michael@0 | 185 | this.normal = normalise(normal); |
michael@0 | 186 | this.nu = normal[u] / normal[this.axis]; |
michael@0 | 187 | this.nv = normal[v] / normal[this.axis]; |
michael@0 | 188 | this.nd = dot(normal, p1) / normal[this.axis]; |
michael@0 | 189 | var det = u1 * v2 - v1 * u2; |
michael@0 | 190 | this.eu = p1[u]; |
michael@0 | 191 | this.ev = p1[v]; |
michael@0 | 192 | this.nu1 = u1 / det; |
michael@0 | 193 | this.nv1 = -v1 / det; |
michael@0 | 194 | this.nu2 = v2 / det; |
michael@0 | 195 | this.nv2 = -u2 / det; |
michael@0 | 196 | this.material = [0.7, 0.7, 0.7]; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | Triangle.prototype.intersect = function(orig, dir, near, far) { |
michael@0 | 200 | var u = (this.axis + 1) % 3; |
michael@0 | 201 | var v = (this.axis + 2) % 3; |
michael@0 | 202 | var d = dir[this.axis] + this.nu * dir[u] + this.nv * dir[v]; |
michael@0 | 203 | var t = (this.nd - orig[this.axis] - this.nu * orig[u] - this.nv * orig[v]) / d; |
michael@0 | 204 | if (t < near || t > far) |
michael@0 | 205 | return null; |
michael@0 | 206 | var Pu = orig[u] + t * dir[u] - this.eu; |
michael@0 | 207 | var Pv = orig[v] + t * dir[v] - this.ev; |
michael@0 | 208 | var a2 = Pv * this.nu1 + Pu * this.nv1; |
michael@0 | 209 | if (a2 < 0) |
michael@0 | 210 | return null; |
michael@0 | 211 | var a3 = Pu * this.nu2 + Pv * this.nv2; |
michael@0 | 212 | if (a3 < 0) |
michael@0 | 213 | return null; |
michael@0 | 214 | |
michael@0 | 215 | if ((a2 + a3) > 1) |
michael@0 | 216 | return null; |
michael@0 | 217 | return t; |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | function Scene(a_triangles) { |
michael@0 | 221 | this.triangles = a_triangles; |
michael@0 | 222 | this.lights = []; |
michael@0 | 223 | this.ambient = [0,0,0]; |
michael@0 | 224 | this.background = [0.8,0.8,1]; |
michael@0 | 225 | } |
michael@0 | 226 | var zero = new Array(0,0,0); |
michael@0 | 227 | |
michael@0 | 228 | Scene.prototype.intersect = function(origin, dir, near, far) { |
michael@0 | 229 | var closest = null; |
michael@0 | 230 | for (i = 0; i < this.triangles.length; i++) { |
michael@0 | 231 | var triangle = this.triangles[i]; |
michael@0 | 232 | var d = triangle.intersect(origin, dir, near, far); |
michael@0 | 233 | if (d == null || d > far || d < near) |
michael@0 | 234 | continue; |
michael@0 | 235 | far = d; |
michael@0 | 236 | closest = triangle; |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | if (!closest) |
michael@0 | 240 | return [this.background[0],this.background[1],this.background[2]]; |
michael@0 | 241 | |
michael@0 | 242 | var normal = closest.normal; |
michael@0 | 243 | var hit = add(origin, scale(dir, far)); |
michael@0 | 244 | if (dot(dir, normal) > 0) |
michael@0 | 245 | normal = [-normal[0], -normal[1], -normal[2]]; |
michael@0 | 246 | |
michael@0 | 247 | var colour = null; |
michael@0 | 248 | if (closest.shader) { |
michael@0 | 249 | colour = closest.shader(closest, hit, dir); |
michael@0 | 250 | } else { |
michael@0 | 251 | colour = closest.material; |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | // do reflection |
michael@0 | 255 | var reflected = null; |
michael@0 | 256 | if (colour.reflection > 0.001) { |
michael@0 | 257 | var reflection = addVector(scale(normal, -2*dot(dir, normal)), dir); |
michael@0 | 258 | reflected = this.intersect(hit, reflection, 0.0001, 1000000); |
michael@0 | 259 | if (colour.reflection >= 0.999999) |
michael@0 | 260 | return reflected; |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | var l = [this.ambient[0], this.ambient[1], this.ambient[2]]; |
michael@0 | 264 | for (var i = 0; i < this.lights.length; i++) { |
michael@0 | 265 | var light = this.lights[i]; |
michael@0 | 266 | var toLight = sub(light, hit); |
michael@0 | 267 | var distance = lengthVector(toLight); |
michael@0 | 268 | scaleVector(toLight, 1.0/distance); |
michael@0 | 269 | distance -= 0.0001; |
michael@0 | 270 | if (this.blocked(hit, toLight, distance)) |
michael@0 | 271 | continue; |
michael@0 | 272 | var nl = dot(normal, toLight); |
michael@0 | 273 | if (nl > 0) |
michael@0 | 274 | addVector(l, scale(light.colour, nl)); |
michael@0 | 275 | } |
michael@0 | 276 | l = scalev(l, colour); |
michael@0 | 277 | if (reflected) { |
michael@0 | 278 | l = addVector(scaleVector(l, 1 - colour.reflection), scaleVector(reflected, colour.reflection)); |
michael@0 | 279 | } |
michael@0 | 280 | return l; |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | Scene.prototype.blocked = function(O, D, far) { |
michael@0 | 284 | var near = 0.0001; |
michael@0 | 285 | var closest = null; |
michael@0 | 286 | for (i = 0; i < this.triangles.length; i++) { |
michael@0 | 287 | var triangle = this.triangles[i]; |
michael@0 | 288 | var d = triangle.intersect(O, D, near, far); |
michael@0 | 289 | if (d == null || d > far || d < near) |
michael@0 | 290 | continue; |
michael@0 | 291 | return true; |
michael@0 | 292 | } |
michael@0 | 293 | |
michael@0 | 294 | return false; |
michael@0 | 295 | } |
michael@0 | 296 | |
michael@0 | 297 | |
michael@0 | 298 | // this camera code is from notes i made ages ago, it is from *somewhere* -- i cannot remember where |
michael@0 | 299 | // that somewhere is |
michael@0 | 300 | function Camera(origin, lookat, up) { |
michael@0 | 301 | var zaxis = normaliseVector(subVector(lookat, origin)); |
michael@0 | 302 | var xaxis = normaliseVector(cross(up, zaxis)); |
michael@0 | 303 | var yaxis = normaliseVector(cross(xaxis, subVector([0,0,0], zaxis))); |
michael@0 | 304 | var m = new Array(16); |
michael@0 | 305 | m[0] = xaxis[0]; m[1] = xaxis[1]; m[2] = xaxis[2]; |
michael@0 | 306 | m[4] = yaxis[0]; m[5] = yaxis[1]; m[6] = yaxis[2]; |
michael@0 | 307 | m[8] = zaxis[0]; m[9] = zaxis[1]; m[10] = zaxis[2]; |
michael@0 | 308 | invertMatrix(m); |
michael@0 | 309 | m[3] = 0; m[7] = 0; m[11] = 0; |
michael@0 | 310 | this.origin = origin; |
michael@0 | 311 | this.directions = new Array(4); |
michael@0 | 312 | this.directions[0] = normalise([-0.7, 0.7, 1]); |
michael@0 | 313 | this.directions[1] = normalise([ 0.7, 0.7, 1]); |
michael@0 | 314 | this.directions[2] = normalise([ 0.7, -0.7, 1]); |
michael@0 | 315 | this.directions[3] = normalise([-0.7, -0.7, 1]); |
michael@0 | 316 | this.directions[0] = transformMatrix(m, this.directions[0]); |
michael@0 | 317 | this.directions[1] = transformMatrix(m, this.directions[1]); |
michael@0 | 318 | this.directions[2] = transformMatrix(m, this.directions[2]); |
michael@0 | 319 | this.directions[3] = transformMatrix(m, this.directions[3]); |
michael@0 | 320 | } |
michael@0 | 321 | |
michael@0 | 322 | Camera.prototype.generateRayPair = function(y) { |
michael@0 | 323 | rays = new Array(new Object(), new Object()); |
michael@0 | 324 | rays[0].origin = this.origin; |
michael@0 | 325 | rays[1].origin = this.origin; |
michael@0 | 326 | rays[0].dir = addVector(scale(this.directions[0], y), scale(this.directions[3], 1 - y)); |
michael@0 | 327 | rays[1].dir = addVector(scale(this.directions[1], y), scale(this.directions[2], 1 - y)); |
michael@0 | 328 | return rays; |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | function renderRows(camera, scene, pixels, width, height, starty, stopy) { |
michael@0 | 332 | for (var y = starty; y < stopy; y++) { |
michael@0 | 333 | var rays = camera.generateRayPair(y / height); |
michael@0 | 334 | for (var x = 0; x < width; x++) { |
michael@0 | 335 | var xp = x / width; |
michael@0 | 336 | var origin = addVector(scale(rays[0].origin, xp), scale(rays[1].origin, 1 - xp)); |
michael@0 | 337 | var dir = normaliseVector(addVector(scale(rays[0].dir, xp), scale(rays[1].dir, 1 - xp))); |
michael@0 | 338 | var l = scene.intersect(origin, dir); |
michael@0 | 339 | pixels[y][x] = l; |
michael@0 | 340 | } |
michael@0 | 341 | } |
michael@0 | 342 | } |
michael@0 | 343 | |
michael@0 | 344 | Camera.prototype.render = function(scene, pixels, width, height) { |
michael@0 | 345 | var cam = this; |
michael@0 | 346 | var row = 0; |
michael@0 | 347 | renderRows(cam, scene, pixels, width, height, 0, height); |
michael@0 | 348 | } |
michael@0 | 349 | |
michael@0 | 350 | |
michael@0 | 351 | |
michael@0 | 352 | function raytraceScene() |
michael@0 | 353 | { |
michael@0 | 354 | var startDate = new Date().getTime(); |
michael@0 | 355 | var numTriangles = 2 * 6; |
michael@0 | 356 | var triangles = new Array();//numTriangles); |
michael@0 | 357 | var tfl = createVector(-10, 10, -10); |
michael@0 | 358 | var tfr = createVector( 10, 10, -10); |
michael@0 | 359 | var tbl = createVector(-10, 10, 10); |
michael@0 | 360 | var tbr = createVector( 10, 10, 10); |
michael@0 | 361 | var bfl = createVector(-10, -10, -10); |
michael@0 | 362 | var bfr = createVector( 10, -10, -10); |
michael@0 | 363 | var bbl = createVector(-10, -10, 10); |
michael@0 | 364 | var bbr = createVector( 10, -10, 10); |
michael@0 | 365 | |
michael@0 | 366 | // cube!!! |
michael@0 | 367 | // front |
michael@0 | 368 | var i = 0; |
michael@0 | 369 | |
michael@0 | 370 | triangles[i++] = new Triangle(tfl, tfr, bfr); |
michael@0 | 371 | triangles[i++] = new Triangle(tfl, bfr, bfl); |
michael@0 | 372 | // back |
michael@0 | 373 | triangles[i++] = new Triangle(tbl, tbr, bbr); |
michael@0 | 374 | triangles[i++] = new Triangle(tbl, bbr, bbl); |
michael@0 | 375 | // triangles[i-1].material = [0.7,0.2,0.2]; |
michael@0 | 376 | // triangles[i-1].material.reflection = 0.8; |
michael@0 | 377 | // left |
michael@0 | 378 | triangles[i++] = new Triangle(tbl, tfl, bbl); |
michael@0 | 379 | // triangles[i-1].reflection = 0.6; |
michael@0 | 380 | triangles[i++] = new Triangle(tfl, bfl, bbl); |
michael@0 | 381 | // triangles[i-1].reflection = 0.6; |
michael@0 | 382 | // right |
michael@0 | 383 | triangles[i++] = new Triangle(tbr, tfr, bbr); |
michael@0 | 384 | triangles[i++] = new Triangle(tfr, bfr, bbr); |
michael@0 | 385 | // top |
michael@0 | 386 | triangles[i++] = new Triangle(tbl, tbr, tfr); |
michael@0 | 387 | triangles[i++] = new Triangle(tbl, tfr, tfl); |
michael@0 | 388 | // bottom |
michael@0 | 389 | triangles[i++] = new Triangle(bbl, bbr, bfr); |
michael@0 | 390 | triangles[i++] = new Triangle(bbl, bfr, bfl); |
michael@0 | 391 | |
michael@0 | 392 | //Floor!!!! |
michael@0 | 393 | var green = createVector(0.0, 0.4, 0.0); |
michael@0 | 394 | var grey = createVector(0.4, 0.4, 0.4); |
michael@0 | 395 | grey.reflection = 1.0; |
michael@0 | 396 | var floorShader = function(tri, pos, view) { |
michael@0 | 397 | var x = ((pos[0]/32) % 2 + 2) % 2; |
michael@0 | 398 | var z = ((pos[2]/32 + 0.3) % 2 + 2) % 2; |
michael@0 | 399 | if (x < 1 != z < 1) { |
michael@0 | 400 | //in the real world we use the fresnel term... |
michael@0 | 401 | // var angle = 1-dot(view, tri.normal); |
michael@0 | 402 | // angle *= angle; |
michael@0 | 403 | // angle *= angle; |
michael@0 | 404 | // angle *= angle; |
michael@0 | 405 | //grey.reflection = angle; |
michael@0 | 406 | return grey; |
michael@0 | 407 | } else |
michael@0 | 408 | return green; |
michael@0 | 409 | } |
michael@0 | 410 | var ffl = createVector(-1000, -30, -1000); |
michael@0 | 411 | var ffr = createVector( 1000, -30, -1000); |
michael@0 | 412 | var fbl = createVector(-1000, -30, 1000); |
michael@0 | 413 | var fbr = createVector( 1000, -30, 1000); |
michael@0 | 414 | triangles[i++] = new Triangle(fbl, fbr, ffr); |
michael@0 | 415 | triangles[i-1].shader = floorShader; |
michael@0 | 416 | triangles[i++] = new Triangle(fbl, ffr, ffl); |
michael@0 | 417 | triangles[i-1].shader = floorShader; |
michael@0 | 418 | |
michael@0 | 419 | var _scene = new Scene(triangles); |
michael@0 | 420 | _scene.lights[0] = createVector(20, 38, -22); |
michael@0 | 421 | _scene.lights[0].colour = createVector(0.7, 0.3, 0.3); |
michael@0 | 422 | _scene.lights[1] = createVector(-23, 40, 17); |
michael@0 | 423 | _scene.lights[1].colour = createVector(0.7, 0.3, 0.3); |
michael@0 | 424 | _scene.lights[2] = createVector(23, 20, 17); |
michael@0 | 425 | _scene.lights[2].colour = createVector(0.7, 0.7, 0.7); |
michael@0 | 426 | _scene.ambient = createVector(0.1, 0.1, 0.1); |
michael@0 | 427 | // _scene.background = createVector(0.7, 0.7, 1.0); |
michael@0 | 428 | |
michael@0 | 429 | var size = 30; |
michael@0 | 430 | var pixels = new Array(); |
michael@0 | 431 | for (var y = 0; y < size; y++) { |
michael@0 | 432 | pixels[y] = new Array(); |
michael@0 | 433 | for (var x = 0; x < size; x++) { |
michael@0 | 434 | pixels[y][x] = 0; |
michael@0 | 435 | } |
michael@0 | 436 | } |
michael@0 | 437 | |
michael@0 | 438 | var _camera = new Camera(createVector(-40, 40, 40), createVector(0, 0, 0), createVector(0, 1, 0)); |
michael@0 | 439 | _camera.render(_scene, pixels, size, size); |
michael@0 | 440 | |
michael@0 | 441 | return pixels; |
michael@0 | 442 | } |
michael@0 | 443 | |
michael@0 | 444 | function arrayToCanvasCommands(pixels) |
michael@0 | 445 | { |
michael@0 | 446 | var s = '<canvas id="renderCanvas" width="30px" height="30px"></canvas><scr' + 'ipt>\nvar pixels = ['; |
michael@0 | 447 | var size = 30; |
michael@0 | 448 | for (var y = 0; y < size; y++) { |
michael@0 | 449 | s += "["; |
michael@0 | 450 | for (var x = 0; x < size; x++) { |
michael@0 | 451 | s += "[" + pixels[y][x] + "],"; |
michael@0 | 452 | } |
michael@0 | 453 | s+= "],"; |
michael@0 | 454 | } |
michael@0 | 455 | s += '];\n var canvas = document.getElementById("renderCanvas").getContext("2d");\n\ |
michael@0 | 456 | \n\ |
michael@0 | 457 | \n\ |
michael@0 | 458 | var size = 30;\n\ |
michael@0 | 459 | canvas.fillStyle = "red";\n\ |
michael@0 | 460 | canvas.fillRect(0, 0, size, size);\n\ |
michael@0 | 461 | canvas.scale(1, -1);\n\ |
michael@0 | 462 | canvas.translate(0, -size);\n\ |
michael@0 | 463 | \n\ |
michael@0 | 464 | if (!canvas.setFillColor)\n\ |
michael@0 | 465 | canvas.setFillColor = function(r, g, b, a) {\n\ |
michael@0 | 466 | this.fillStyle = "rgb("+[Math.floor(r * 255), Math.floor(g * 255), Math.floor(b * 255)]+")";\n\ |
michael@0 | 467 | }\n\ |
michael@0 | 468 | \n\ |
michael@0 | 469 | for (var y = 0; y < size; y++) {\n\ |
michael@0 | 470 | for (var x = 0; x < size; x++) {\n\ |
michael@0 | 471 | var l = pixels[y][x];\n\ |
michael@0 | 472 | canvas.setFillColor(l[0], l[1], l[2], 1);\n\ |
michael@0 | 473 | canvas.fillRect(x, y, 1, 1);\n\ |
michael@0 | 474 | }\n\ |
michael@0 | 475 | }</scr' + 'ipt>'; |
michael@0 | 476 | |
michael@0 | 477 | return s; |
michael@0 | 478 | } |
michael@0 | 479 | |
michael@0 | 480 | testOutput = arrayToCanvasCommands(raytraceScene()); |
michael@0 | 481 | |
michael@0 | 482 | |
michael@0 | 483 | var _sunSpiderInterval = new Date() - _sunSpiderStartDate; |
michael@0 | 484 | |
michael@0 | 485 | document.getElementById("console").innerHTML = _sunSpiderInterval; |
michael@0 | 486 | </script> |
michael@0 | 487 | |
michael@0 | 488 | |
michael@0 | 489 | </body> |
michael@0 | 490 | </html> |