OLD | NEW |
(Empty) | |
| 1 /* |
| 2 ** Copyright (c) 2012 The Khronos Group Inc. |
| 3 ** |
| 4 ** Permission is hereby granted, free of charge, to any person obtaining a |
| 5 ** copy of this software and/or associated documentation files (the |
| 6 ** "Materials"), to deal in the Materials without restriction, including |
| 7 ** without limitation the rights to use, copy, modify, merge, publish, |
| 8 ** distribute, sublicense, and/or sell copies of the Materials, and to |
| 9 ** permit persons to whom the Materials are furnished to do so, subject to |
| 10 ** the following conditions: |
| 11 ** |
| 12 ** The above copyright notice and this permission notice shall be included |
| 13 ** in all copies or substantial portions of the Materials. |
| 14 ** |
| 15 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 22 */ |
| 23 |
| 24 function webglTestLog(msg) { |
| 25 if (window.console && window.console.log) { |
| 26 window.console.log(msg); |
| 27 } |
| 28 if (document.getElementById("console")) { |
| 29 var log = document.getElementById("console"); |
| 30 log.innerHTML += msg + "<br>"; |
| 31 } |
| 32 } |
| 33 |
| 34 // |
| 35 // create3DContext |
| 36 // |
| 37 // Returns the WebGLRenderingContext for any known implementation. |
| 38 // |
| 39 function create3DContext(canvas, attributes) |
| 40 { |
| 41 if (!canvas) |
| 42 canvas = document.createElement("canvas"); |
| 43 var names = ["webgl", "experimental-webgl"]; |
| 44 var context = null; |
| 45 for (var i = 0; i < names.length; ++i) { |
| 46 try { |
| 47 context = canvas.getContext(names[i], attributes); |
| 48 } catch (e) { |
| 49 } |
| 50 if (context) { |
| 51 break; |
| 52 } |
| 53 } |
| 54 if (!context) { |
| 55 throw "Unable to fetch WebGL rendering context for Canvas"; |
| 56 } |
| 57 return context; |
| 58 } |
| 59 |
| 60 function createGLErrorWrapper(context, fname) { |
| 61 return function() { |
| 62 var rv = context[fname].apply(context, arguments); |
| 63 var err = context.getError(); |
| 64 if (err != 0) |
| 65 throw "GL error " + err + " in " + fname; |
| 66 return rv; |
| 67 }; |
| 68 } |
| 69 |
| 70 function create3DContextWithWrapperThatThrowsOnGLError(canvas, attributes) { |
| 71 var context = create3DContext(canvas, attributes); |
| 72 // Thanks to Ilmari Heikkinen for the idea on how to implement this so elegant
ly. |
| 73 var wrap = {}; |
| 74 for (var i in context) { |
| 75 try { |
| 76 if (typeof context[i] == 'function') { |
| 77 wrap[i] = createGLErrorWrapper(context, i); |
| 78 } else { |
| 79 wrap[i] = context[i]; |
| 80 } |
| 81 } catch (e) { |
| 82 webglTestLog("createContextWrapperThatThrowsOnGLError: Error accessing " +
i); |
| 83 } |
| 84 } |
| 85 wrap.getError = function() { |
| 86 return context.getError(); |
| 87 }; |
| 88 return wrap; |
| 89 } |
| 90 |
| 91 function getGLErrorAsString(ctx, err) { |
| 92 if (err === ctx.NO_ERROR) { |
| 93 return "NO_ERROR"; |
| 94 } |
| 95 for (var name in ctx) { |
| 96 if (ctx[name] === err) { |
| 97 return name; |
| 98 } |
| 99 } |
| 100 return "0x" + err.toString(16); |
| 101 } |
| 102 |
| 103 // Pass undefined for glError to test that it at least throws some error |
| 104 function shouldGenerateGLError(ctx, glErrors, evalStr) { |
| 105 if (!glErrors.length) { |
| 106 glErrors = [glErrors]; |
| 107 } |
| 108 var exception; |
| 109 try { |
| 110 eval(evalStr); |
| 111 } catch (e) { |
| 112 exception = e; |
| 113 } |
| 114 if (exception) { |
| 115 testFailed(evalStr + " threw exception " + exception); |
| 116 } else { |
| 117 var err = ctx.getError(); |
| 118 if (glErrors.indexOf(err) < 0) { |
| 119 var errStrs = []; |
| 120 for (var ii = 0; ii < glErrors.length; ++ii) { |
| 121 errStrs.push(getGLErrorAsString(ctx, glErrors[ii])); |
| 122 } |
| 123 testFailed(evalStr + " expected: " + errStrs.join(" or ") + ". Was " + get
GLErrorAsString(ctx, err) + "."); |
| 124 } else { |
| 125 testPassed(evalStr + " generated expected GL error: " + getGLErrorAsString
(ctx, err) + "."); |
| 126 } |
| 127 } |
| 128 } |
| 129 |
| 130 /** |
| 131 * Tests that the first error GL returns is the specified error. |
| 132 * @param {!WebGLContext} gl The WebGLContext to use. |
| 133 * @param {number|!Array.<number>} glError The expected gl |
| 134 * error. Multiple errors can be passed in using an |
| 135 * array. |
| 136 * @param {string} opt_msg Optional additional message. |
| 137 */ |
| 138 function glErrorShouldBe(gl, glErrors, opt_msg) { |
| 139 if (!glErrors.length) { |
| 140 glErrors = [glErrors]; |
| 141 } |
| 142 opt_msg = opt_msg || ""; |
| 143 var err = gl.getError(); |
| 144 var ndx = glErrors.indexOf(err); |
| 145 if (ndx < 0) { |
| 146 if (glErrors.length == 1) { |
| 147 testFailed("getError expected: " + getGLErrorAsString(gl, glErrors[0]) + |
| 148 ". Was " + getGLErrorAsString(gl, err) + " : " + opt_msg); |
| 149 } else { |
| 150 var errs = []; |
| 151 for (var ii = 0; ii < glErrors.length; ++ii) { |
| 152 errs.push(getGLErrorAsString(gl, glErrors[ii])); |
| 153 } |
| 154 testFailed("getError expected one of: [" + errs.join(", ") + |
| 155 "]. Was " + getGLErrorAsString(gl, err) + " : " + opt_msg); |
| 156 } |
| 157 } else { |
| 158 testPassed("getError was expected value: " + |
| 159 getGLErrorAsString(gl, err) + " : " + opt_msg); |
| 160 } |
| 161 }; |
| 162 |
| 163 // |
| 164 // createProgram |
| 165 // |
| 166 // Create and return a program object, attaching each of the given shaders. |
| 167 // |
| 168 // If attribs are given, bind an attrib with that name at that index. |
| 169 // |
| 170 function createProgram(gl, vshaders, fshaders, attribs) |
| 171 { |
| 172 if (typeof(vshaders) == "string") |
| 173 vshaders = [vshaders]; |
| 174 if (typeof(fshaders) == "string") |
| 175 fshaders = [fshaders]; |
| 176 |
| 177 var shaders = []; |
| 178 var i; |
| 179 |
| 180 for (i = 0; i < vshaders.length; ++i) { |
| 181 var shader = loadShader(gl, vshaders[i], gl.VERTEX_SHADER); |
| 182 if (!shader) |
| 183 return null; |
| 184 shaders.push(shader); |
| 185 } |
| 186 |
| 187 for (i = 0; i < fshaders.length; ++i) { |
| 188 var shader = loadShader(gl, fshaders[i], gl.FRAGMENT_SHADER); |
| 189 if (!shader) |
| 190 return null; |
| 191 shaders.push(shader); |
| 192 } |
| 193 |
| 194 var prog = gl.createProgram(); |
| 195 for (i = 0; i < shaders.length; ++i) { |
| 196 gl.attachShader(prog, shaders[i]); |
| 197 } |
| 198 |
| 199 if (attribs) { |
| 200 for (var i = 0; i < attribs.length; ++i) { |
| 201 gl.bindAttribLocation(prog, i, attribs[i]); |
| 202 } |
| 203 } |
| 204 |
| 205 gl.linkProgram(prog); |
| 206 |
| 207 // Check the link status |
| 208 var linked = gl.getProgramParameter(prog, gl.LINK_STATUS); |
| 209 if (!linked) { |
| 210 // something went wrong with the link |
| 211 var error = gl.getProgramInfoLog(prog); |
| 212 webglTestLog("Error in program linking:" + error); |
| 213 |
| 214 gl.deleteProgram(prog); |
| 215 for (i = 0; i < shaders.length; ++i) |
| 216 gl.deleteShader(shaders[i]); |
| 217 return null; |
| 218 } |
| 219 |
| 220 return prog; |
| 221 } |
| 222 |
| 223 // |
| 224 // initWebGL |
| 225 // |
| 226 // Initialize the Canvas element with the passed name as a WebGL object and retu
rn the |
| 227 // WebGLRenderingContext. |
| 228 // |
| 229 // Set the clear color to [0,0,0,1] and the depth to 1. |
| 230 // Enable depth testing and blending with a blend func of (SRC_ALPHA, ONE_MINUS_
SRC_ALPHA) |
| 231 // |
| 232 function initWebGL(canvasName, contextAttribs) |
| 233 { |
| 234 var canvas = document.getElementById(canvasName); |
| 235 var gl = create3DContext(canvas, contextAttribs); |
| 236 if (!gl) { |
| 237 alert("No WebGL context found"); |
| 238 return null; |
| 239 } |
| 240 |
| 241 gl.clearColor(0, 0, 0, 1); |
| 242 gl.clearDepth(1); |
| 243 |
| 244 gl.enable(gl.DEPTH_TEST); |
| 245 gl.enable(gl.BLEND); |
| 246 gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); |
| 247 |
| 248 return gl; |
| 249 } |
| 250 |
| 251 // |
| 252 // setupProgram |
| 253 // |
| 254 // Load shaders with the passed names and create a program with them. |
| 255 // |
| 256 // For each string in the passed attribs array, bind an attrib with that name at
that index. |
| 257 // Once the attribs are bound, link the program and then use it. |
| 258 function setupProgram(gl, vshader, fshader, attribs) |
| 259 { |
| 260 // Create the program object |
| 261 var program = createProgram(gl, vshader, fshader, attribs); |
| 262 if (!program) |
| 263 return null; |
| 264 |
| 265 gl.useProgram(program); |
| 266 return program; |
| 267 } |
| 268 |
| 269 |
| 270 // |
| 271 // getShaderSource |
| 272 // |
| 273 // Load the source from the passed shader file. |
| 274 // |
| 275 function getShaderSource(file) |
| 276 { |
| 277 var xhr = new XMLHttpRequest(); |
| 278 xhr.open("GET", file, false); |
| 279 xhr.send(); |
| 280 return xhr.responseText; |
| 281 } |
| 282 |
| 283 |
| 284 // |
| 285 // loadShader |
| 286 // |
| 287 // 'shader' is either the id of a <script> element containing the shader source |
| 288 // string, the shader string itself, or the URL of a file containing the shader |
| 289 // source. Load this shader and return the WebGLShader object corresponding to |
| 290 // it. |
| 291 // |
| 292 function loadShader(ctx, shaderId, shaderType, isFile) |
| 293 { |
| 294 var shaderSource = ""; |
| 295 |
| 296 if (isFile) |
| 297 shaderSource = getShaderSource(shaderId); |
| 298 else { |
| 299 var shaderScript = document.getElementById(shaderId); |
| 300 if (!shaderScript) { |
| 301 shaderSource = shaderId; |
| 302 } else { |
| 303 if (shaderScript.type == "x-shader/x-vertex") { |
| 304 shaderType = ctx.VERTEX_SHADER; |
| 305 } else if (shaderScript.type == "x-shader/x-fragment") { |
| 306 shaderType = ctx.FRAGMENT_SHADER; |
| 307 } else if (shaderType != ctx.VERTEX_SHADER && shaderType != ctx.FRAG
MENT_SHADER) { |
| 308 webglTestLog("*** Error: unknown shader type"); |
| 309 return null; |
| 310 } |
| 311 |
| 312 shaderSource = shaderScript.text; |
| 313 } |
| 314 } |
| 315 |
| 316 // Create the shader object |
| 317 var shader = ctx.createShader(shaderType); |
| 318 if (shader == null) { |
| 319 webglTestLog("*** Error: unable to create shader '"+shaderId+"'"); |
| 320 return null; |
| 321 } |
| 322 |
| 323 // Load the shader source |
| 324 ctx.shaderSource(shader, shaderSource); |
| 325 |
| 326 // Compile the shader |
| 327 ctx.compileShader(shader); |
| 328 |
| 329 // Check the compile status |
| 330 var compiled = ctx.getShaderParameter(shader, ctx.COMPILE_STATUS); |
| 331 if (!compiled) { |
| 332 // Something went wrong during compilation; get the error |
| 333 var error = ctx.getShaderInfoLog(shader); |
| 334 webglTestLog("*** Error compiling shader '"+shader+"':"+error); |
| 335 ctx.deleteShader(shader); |
| 336 return null; |
| 337 } |
| 338 |
| 339 return shader; |
| 340 } |
| 341 |
| 342 function loadShaderFromFile(ctx, file, type) |
| 343 { |
| 344 return loadShader(ctx, file, type, true); |
| 345 } |
| 346 |
| 347 function loadShaderFromScript(ctx, script) |
| 348 { |
| 349 return loadShader(ctx, script, 0, false); |
| 350 } |
| 351 |
| 352 function loadStandardProgram(context) { |
| 353 var program = context.createProgram(); |
| 354 context.attachShader(program, loadStandardVertexShader(context)); |
| 355 context.attachShader(program, loadStandardFragmentShader(context)); |
| 356 context.linkProgram(program); |
| 357 return program; |
| 358 } |
| 359 |
| 360 function loadProgram(context, vertexShaderPath, fragmentShaderPath, isFile) { |
| 361 isFile = (isFile === undefined) ? true : isFile; |
| 362 var program = context.createProgram(); |
| 363 context.attachShader(program, loadShader(context, vertexShaderPath, context.
VERTEX_SHADER, isFile)); |
| 364 context.attachShader(program, loadShader(context, fragmentShaderPath, contex
t.FRAGMENT_SHADER, isFile)); |
| 365 context.linkProgram(program); |
| 366 return program; |
| 367 } |
| 368 |
| 369 var getBasePathForResources = function() { |
| 370 var expectedBase = "webgl-test.js"; |
| 371 var scripts = document.getElementsByTagName('script'); |
| 372 for (var script, i = 0; script = scripts[i]; i++) { |
| 373 var src = script.src; |
| 374 var l = src.length; |
| 375 if (src.substr(l - expectedBase.length) == expectedBase) { |
| 376 return src.substr(0, l - expectedBase.length); |
| 377 } |
| 378 } |
| 379 throw 'oops'; |
| 380 }; |
| 381 |
| 382 |
| 383 function loadStandardVertexShader(context) { |
| 384 return loadShader( |
| 385 context, |
| 386 getBasePathForResources() + "vertexShader.vert", |
| 387 context.VERTEX_SHADER, |
| 388 true); |
| 389 } |
| 390 |
| 391 function loadStandardFragmentShader(context) { |
| 392 return loadShader( |
| 393 context, |
| 394 getBasePathForResources() + "fragmentShader.frag", |
| 395 context.FRAGMENT_SHADER, |
| 396 true); |
| 397 } |
| 398 |
| 399 // |
| 400 // makeBox |
| 401 // |
| 402 // Create a box with vertices, normals and texCoords. Create VBOs for each as we
ll as the index array. |
| 403 // Return an object with the following properties: |
| 404 // |
| 405 // normalObject WebGLBuffer object for normals |
| 406 // texCoordObject WebGLBuffer object for texCoords |
| 407 // vertexObject WebGLBuffer object for vertices |
| 408 // indexObject WebGLBuffer object for indices |
| 409 // numIndices The number of indices in the indexObject |
| 410 // |
| 411 function makeBox(ctx) |
| 412 { |
| 413 // box |
| 414 // v6----- v5 |
| 415 // /| /| |
| 416 // v1------v0| |
| 417 // | | | | |
| 418 // | |v7---|-|v4 |
| 419 // |/ |/ |
| 420 // v2------v3 |
| 421 // |
| 422 // vertex coords array |
| 423 var vertices = new Float32Array( |
| 424 [ 1, 1, 1, -1, 1, 1, -1,-1, 1, 1,-1, 1, // v0-v1-v2-v3 front |
| 425 1, 1, 1, 1,-1, 1, 1,-1,-1, 1, 1,-1, // v0-v3-v4-v5 right |
| 426 1, 1, 1, 1, 1,-1, -1, 1,-1, -1, 1, 1, // v0-v5-v6-v1 top |
| 427 -1, 1, 1, -1, 1,-1, -1,-1,-1, -1,-1, 1, // v1-v6-v7-v2 left |
| 428 -1,-1,-1, 1,-1,-1, 1,-1, 1, -1,-1, 1, // v7-v4-v3-v2 bottom |
| 429 1,-1,-1, -1,-1,-1, -1, 1,-1, 1, 1,-1 ] // v4-v7-v6-v5 back |
| 430 ); |
| 431 |
| 432 // normal array |
| 433 var normals = new Float32Array( |
| 434 [ 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // v0-v1-v2-v3 front |
| 435 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v3-v4-v5 right |
| 436 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // v0-v5-v6-v1 top |
| 437 -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, // v1-v6-v7-v2 left |
| 438 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, // v7-v4-v3-v2 bottom |
| 439 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1 ] // v4-v7-v6-v5 back |
| 440 ); |
| 441 |
| 442 |
| 443 // texCoord array |
| 444 var texCoords = new Float32Array( |
| 445 [ 1, 1, 0, 1, 0, 0, 1, 0, // v0-v1-v2-v3 front |
| 446 0, 1, 0, 0, 1, 0, 1, 1, // v0-v3-v4-v5 right |
| 447 1, 0, 1, 1, 0, 1, 0, 0, // v0-v5-v6-v1 top |
| 448 1, 1, 0, 1, 0, 0, 1, 0, // v1-v6-v7-v2 left |
| 449 0, 0, 1, 0, 1, 1, 0, 1, // v7-v4-v3-v2 bottom |
| 450 0, 0, 1, 0, 1, 1, 0, 1 ] // v4-v7-v6-v5 back |
| 451 ); |
| 452 |
| 453 // index array |
| 454 var indices = new Uint8Array( |
| 455 [ 0, 1, 2, 0, 2, 3, // front |
| 456 4, 5, 6, 4, 6, 7, // right |
| 457 8, 9,10, 8,10,11, // top |
| 458 12,13,14, 12,14,15, // left |
| 459 16,17,18, 16,18,19, // bottom |
| 460 20,21,22, 20,22,23 ] // back |
| 461 ); |
| 462 |
| 463 var retval = { }; |
| 464 |
| 465 retval.normalObject = ctx.createBuffer(); |
| 466 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.normalObject); |
| 467 ctx.bufferData(ctx.ARRAY_BUFFER, normals, ctx.STATIC_DRAW); |
| 468 |
| 469 retval.texCoordObject = ctx.createBuffer(); |
| 470 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.texCoordObject); |
| 471 ctx.bufferData(ctx.ARRAY_BUFFER, texCoords, ctx.STATIC_DRAW); |
| 472 |
| 473 retval.vertexObject = ctx.createBuffer(); |
| 474 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.vertexObject); |
| 475 ctx.bufferData(ctx.ARRAY_BUFFER, vertices, ctx.STATIC_DRAW); |
| 476 |
| 477 ctx.bindBuffer(ctx.ARRAY_BUFFER, 0); |
| 478 |
| 479 retval.indexObject = ctx.createBuffer(); |
| 480 ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, retval.indexObject); |
| 481 ctx.bufferData(ctx.ELEMENT_ARRAY_BUFFER, indices, ctx.STATIC_DRAW); |
| 482 ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, 0); |
| 483 |
| 484 retval.numIndices = indices.length; |
| 485 |
| 486 return retval; |
| 487 } |
| 488 |
| 489 // |
| 490 // makeSphere |
| 491 // |
| 492 // Create a sphere with the passed number of latitude and longitude bands and th
e passed radius. |
| 493 // Sphere has vertices, normals and texCoords. Create VBOs for each as well as t
he index array. |
| 494 // Return an object with the following properties: |
| 495 // |
| 496 // normalObject WebGLBuffer object for normals |
| 497 // texCoordObject WebGLBuffer object for texCoords |
| 498 // vertexObject WebGLBuffer object for vertices |
| 499 // indexObject WebGLBuffer object for indices |
| 500 // numIndices The number of indices in the indexObject |
| 501 // |
| 502 function makeSphere(ctx, radius, lats, longs) |
| 503 { |
| 504 var geometryData = [ ]; |
| 505 var normalData = [ ]; |
| 506 var texCoordData = [ ]; |
| 507 var indexData = [ ]; |
| 508 |
| 509 for (var latNumber = 0; latNumber <= lats; ++latNumber) { |
| 510 for (var longNumber = 0; longNumber <= longs; ++longNumber) { |
| 511 var theta = latNumber * Math.PI / lats; |
| 512 var phi = longNumber * 2 * Math.PI / longs; |
| 513 var sinTheta = Math.sin(theta); |
| 514 var sinPhi = Math.sin(phi); |
| 515 var cosTheta = Math.cos(theta); |
| 516 var cosPhi = Math.cos(phi); |
| 517 |
| 518 var x = cosPhi * sinTheta; |
| 519 var y = cosTheta; |
| 520 var z = sinPhi * sinTheta; |
| 521 var u = 1-(longNumber/longs); |
| 522 var v = latNumber/lats; |
| 523 |
| 524 normalData.push(x); |
| 525 normalData.push(y); |
| 526 normalData.push(z); |
| 527 texCoordData.push(u); |
| 528 texCoordData.push(v); |
| 529 geometryData.push(radius * x); |
| 530 geometryData.push(radius * y); |
| 531 geometryData.push(radius * z); |
| 532 } |
| 533 } |
| 534 |
| 535 longs += 1; |
| 536 for (var latNumber = 0; latNumber < lats; ++latNumber) { |
| 537 for (var longNumber = 0; longNumber < longs; ++longNumber) { |
| 538 var first = (latNumber * longs) + (longNumber % longs); |
| 539 var second = first + longs; |
| 540 indexData.push(first); |
| 541 indexData.push(second); |
| 542 indexData.push(first+1); |
| 543 |
| 544 indexData.push(second); |
| 545 indexData.push(second+1); |
| 546 indexData.push(first+1); |
| 547 } |
| 548 } |
| 549 |
| 550 var retval = { }; |
| 551 |
| 552 retval.normalObject = ctx.createBuffer(); |
| 553 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.normalObject); |
| 554 ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(normalData), ctx.STATIC_DR
AW); |
| 555 |
| 556 retval.texCoordObject = ctx.createBuffer(); |
| 557 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.texCoordObject); |
| 558 ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(texCoordData), ctx.STATIC_
DRAW); |
| 559 |
| 560 retval.vertexObject = ctx.createBuffer(); |
| 561 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.vertexObject); |
| 562 ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(geometryData), ctx.STATIC_
DRAW); |
| 563 |
| 564 retval.numIndices = indexData.length; |
| 565 retval.indexObject = ctx.createBuffer(); |
| 566 ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, retval.indexObject); |
| 567 ctx.bufferData(ctx.ELEMENT_ARRAY_BUFFER, new Uint16Array(indexData), ctx.STR
EAM_DRAW); |
| 568 |
| 569 return retval; |
| 570 } |
| 571 |
| 572 // |
| 573 // loadObj |
| 574 // |
| 575 // Load a .obj file from the passed URL. Return an object with a 'loaded' proper
ty set to false. |
| 576 // When the object load is complete, the 'loaded' property becomes true and the
following |
| 577 // properties are set: |
| 578 // |
| 579 // normalObject WebGLBuffer object for normals |
| 580 // texCoordObject WebGLBuffer object for texCoords |
| 581 // vertexObject WebGLBuffer object for vertices |
| 582 // indexObject WebGLBuffer object for indices |
| 583 // numIndices The number of indices in the indexObject |
| 584 // |
| 585 function loadObj(ctx, url) |
| 586 { |
| 587 var obj = { loaded : false }; |
| 588 obj.ctx = ctx; |
| 589 var req = new XMLHttpRequest(); |
| 590 req.obj = obj; |
| 591 req.onreadystatechange = function () { processLoadObj(req) }; |
| 592 req.open("GET", url, true); |
| 593 req.send(null); |
| 594 return obj; |
| 595 } |
| 596 |
| 597 function processLoadObj(req) |
| 598 { |
| 599 webglTestLog("req="+req) |
| 600 // only if req shows "complete" |
| 601 if (req.readyState == 4) { |
| 602 doLoadObj(req.obj, req.responseText); |
| 603 } |
| 604 } |
| 605 |
| 606 function doLoadObj(obj, text) |
| 607 { |
| 608 vertexArray = [ ]; |
| 609 normalArray = [ ]; |
| 610 textureArray = [ ]; |
| 611 indexArray = [ ]; |
| 612 |
| 613 var vertex = [ ]; |
| 614 var normal = [ ]; |
| 615 var texture = [ ]; |
| 616 var facemap = { }; |
| 617 var index = 0; |
| 618 |
| 619 var lines = text.split("\n"); |
| 620 for (var lineIndex in lines) { |
| 621 var line = lines[lineIndex].replace(/[ \t]+/g, " ").replace(/\s\s*$/, ""
); |
| 622 |
| 623 // ignore comments |
| 624 if (line[0] == "#") |
| 625 continue; |
| 626 |
| 627 var array = line.split(" "); |
| 628 if (array[0] == "v") { |
| 629 // vertex |
| 630 vertex.push(parseFloat(array[1])); |
| 631 vertex.push(parseFloat(array[2])); |
| 632 vertex.push(parseFloat(array[3])); |
| 633 } |
| 634 else if (array[0] == "vt") { |
| 635 // normal |
| 636 texture.push(parseFloat(array[1])); |
| 637 texture.push(parseFloat(array[2])); |
| 638 } |
| 639 else if (array[0] == "vn") { |
| 640 // normal |
| 641 normal.push(parseFloat(array[1])); |
| 642 normal.push(parseFloat(array[2])); |
| 643 normal.push(parseFloat(array[3])); |
| 644 } |
| 645 else if (array[0] == "f") { |
| 646 // face |
| 647 if (array.length != 4) { |
| 648 webglTestLog("*** Error: face '"+line+"' not handled"); |
| 649 continue; |
| 650 } |
| 651 |
| 652 for (var i = 1; i < 4; ++i) { |
| 653 if (!(array[i] in facemap)) { |
| 654 // add a new entry to the map and arrays |
| 655 var f = array[i].split("/"); |
| 656 var vtx, nor, tex; |
| 657 |
| 658 if (f.length == 1) { |
| 659 vtx = parseInt(f[0]) - 1; |
| 660 nor = vtx; |
| 661 tex = vtx; |
| 662 } |
| 663 else if (f.length = 3) { |
| 664 vtx = parseInt(f[0]) - 1; |
| 665 tex = parseInt(f[1]) - 1; |
| 666 nor = parseInt(f[2]) - 1; |
| 667 } |
| 668 else { |
| 669 webglTestLog("*** Error: did not understand face '"+arra
y[i]+"'"); |
| 670 return null; |
| 671 } |
| 672 |
| 673 // do the vertices |
| 674 var x = 0; |
| 675 var y = 0; |
| 676 var z = 0; |
| 677 if (vtx * 3 + 2 < vertex.length) { |
| 678 x = vertex[vtx*3]; |
| 679 y = vertex[vtx*3+1]; |
| 680 z = vertex[vtx*3+2]; |
| 681 } |
| 682 vertexArray.push(x); |
| 683 vertexArray.push(y); |
| 684 vertexArray.push(z); |
| 685 |
| 686 // do the textures |
| 687 x = 0; |
| 688 y = 0; |
| 689 if (tex * 2 + 1 < texture.length) { |
| 690 x = texture[tex*2]; |
| 691 y = texture[tex*2+1]; |
| 692 } |
| 693 textureArray.push(x); |
| 694 textureArray.push(y); |
| 695 |
| 696 // do the normals |
| 697 x = 0; |
| 698 y = 0; |
| 699 z = 1; |
| 700 if (nor * 3 + 2 < normal.length) { |
| 701 x = normal[nor*3]; |
| 702 y = normal[nor*3+1]; |
| 703 z = normal[nor*3+2]; |
| 704 } |
| 705 normalArray.push(x); |
| 706 normalArray.push(y); |
| 707 normalArray.push(z); |
| 708 |
| 709 facemap[array[i]] = index++; |
| 710 } |
| 711 |
| 712 indexArray.push(facemap[array[i]]); |
| 713 } |
| 714 } |
| 715 } |
| 716 |
| 717 // set the VBOs |
| 718 obj.normalObject = obj.ctx.createBuffer(); |
| 719 obj.ctx.bindBuffer(obj.ctx.ARRAY_BUFFER, obj.normalObject); |
| 720 obj.ctx.bufferData(obj.ctx.ARRAY_BUFFER, new Float32Array(normalArray), obj.
ctx.STATIC_DRAW); |
| 721 |
| 722 obj.texCoordObject = obj.ctx.createBuffer(); |
| 723 obj.ctx.bindBuffer(obj.ctx.ARRAY_BUFFER, obj.texCoordObject); |
| 724 obj.ctx.bufferData(obj.ctx.ARRAY_BUFFER, new Float32Array(textureArray), obj
.ctx.STATIC_DRAW); |
| 725 |
| 726 obj.vertexObject = obj.ctx.createBuffer(); |
| 727 obj.ctx.bindBuffer(obj.ctx.ARRAY_BUFFER, obj.vertexObject); |
| 728 obj.ctx.bufferData(obj.ctx.ARRAY_BUFFER, new Float32Array(vertexArray), obj.
ctx.STATIC_DRAW); |
| 729 |
| 730 obj.numIndices = indexArray.length; |
| 731 obj.indexObject = obj.ctx.createBuffer(); |
| 732 obj.ctx.bindBuffer(obj.ctx.ELEMENT_ARRAY_BUFFER, obj.indexObject); |
| 733 obj.ctx.bufferData(obj.ctx.ELEMENT_ARRAY_BUFFER, new Uint16Array(indexArray)
, obj.ctx.STREAM_DRAW); |
| 734 |
| 735 obj.loaded = true; |
| 736 } |
| 737 |
| 738 // |
| 739 // loadImageTexture |
| 740 // |
| 741 // Load the image at the passed url, place it in a new WebGLTexture object and r
eturn the WebGLTexture. |
| 742 // |
| 743 function loadImageTexture(ctx, url) |
| 744 { |
| 745 var texture = ctx.createTexture(); |
| 746 texture.image = new Image(); |
| 747 texture.image.onload = function() { doLoadImageTexture(ctx, texture.image, t
exture) } |
| 748 texture.image.src = url; |
| 749 return texture; |
| 750 } |
| 751 |
| 752 function doLoadImageTexture(ctx, image, texture) |
| 753 { |
| 754 ctx.enable(ctx.TEXTURE_2D); |
| 755 ctx.bindTexture(ctx.TEXTURE_2D, texture); |
| 756 ctx.texImage2D(ctx.TEXTURE_2D, 0, ctx.RGBA, ctx.RGBA, ctx.UNSIGNED_BYTE, ima
ge); |
| 757 ctx.texParameteri(ctx.TEXTURE_2D, ctx.TEXTURE_MAG_FILTER, ctx.LINEAR); |
| 758 ctx.texParameteri(ctx.TEXTURE_2D, ctx.TEXTURE_MIN_FILTER, ctx.LINEAR_MIPMAP_
LINEAR); |
| 759 ctx.texParameteri(ctx.TEXTURE_2D, ctx.TEXTURE_WRAP_S, ctx.CLAMP_TO_EDGE); |
| 760 ctx.texParameteri(ctx.TEXTURE_2D, ctx.TEXTURE_WRAP_T, ctx.CLAMP_TO_EDGE); |
| 761 ctx.generateMipmap(ctx.TEXTURE_2D) |
| 762 ctx.bindTexture(ctx.TEXTURE_2D, 0); |
| 763 } |
| 764 |
| 765 // |
| 766 // Framerate object |
| 767 // |
| 768 // This object keeps track of framerate and displays it as the innerHTML text of
the |
| 769 // HTML element with the passed id. Once created you call snapshot at the end |
| 770 // of every rendering cycle. Every 500ms the framerate is updated in the HTML el
ement. |
| 771 // |
| 772 Framerate = function(id) |
| 773 { |
| 774 this.numFramerates = 10; |
| 775 this.framerateUpdateInterval = 500; |
| 776 this.id = id; |
| 777 |
| 778 this.renderTime = -1; |
| 779 this.framerates = [ ]; |
| 780 self = this; |
| 781 var fr = function() { self.updateFramerate() } |
| 782 setInterval(fr, this.framerateUpdateInterval); |
| 783 } |
| 784 |
| 785 Framerate.prototype.updateFramerate = function() |
| 786 { |
| 787 var tot = 0; |
| 788 for (var i = 0; i < this.framerates.length; ++i) |
| 789 tot += this.framerates[i]; |
| 790 |
| 791 var framerate = tot / this.framerates.length; |
| 792 framerate = Math.round(framerate); |
| 793 document.getElementById(this.id).innerHTML = "Framerate:"+framerate+"fps"; |
| 794 } |
| 795 |
| 796 Framerate.prototype.snapshot = function() |
| 797 { |
| 798 if (this.renderTime < 0) |
| 799 this.renderTime = new Date().getTime(); |
| 800 else { |
| 801 var newTime = new Date().getTime(); |
| 802 var t = newTime - this.renderTime; |
| 803 var framerate = 1000/t; |
| 804 this.framerates.push(framerate); |
| 805 while (this.framerates.length > this.numFramerates) |
| 806 this.framerates.shift(); |
| 807 this.renderTime = newTime; |
| 808 } |
| 809 } |
OLD | NEW |