Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: third_party/webgl/sdk/tests/conformance/resources/webgl-test.js

Issue 9373009: Check in webgl conformance tests r16844 from khronos. (Closed) Base URL: svn://chrome-svn/chrome/trunk/deps/
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 /*
2 Copyright (C) 2011 Apple Computer, Inc. All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
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.
12
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.
24 */
25
26 function webglTestLog(msg) {
27 if (window.console && window.console.log) {
28 window.console.log(msg);
29 }
30 if (document.getElementById("console")) {
31 var log = document.getElementById("console");
32 log.innerHTML += msg + "<br>";
33 }
34 }
35
36 //
37 // create3DContext
38 //
39 // Returns the WebGLRenderingContext for any known implementation.
40 //
41 function create3DContext(canvas, attributes)
42 {
43 if (!canvas)
44 canvas = document.createElement("canvas");
45 var context = null;
46 try {
47 context = canvas.getContext("webgl", attributes);
48 } catch(e) {}
49 if (!context) {
50 try {
51 context = canvas.getContext("experimental-webgl", attributes);
52 } catch(e) {}
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 // Load shaders with the passed names and create a program with them. Return thi s program
230 // in the 'program' property of the returned context.
231 //
232 // For each string in the passed attribs array, bind an attrib with that name at that index.
233 // Once the attribs are bound, link the program and then use it.
234 //
235 // Set the clear color to the passed array (4 values) and set the clear depth to the passed value.
236 // Enable depth testing and blending with a blend func of (SRC_ALPHA, ONE_MINUS_ SRC_ALPHA)
237 //
238 function initWebGL(canvasName, vshader, fshader, attribs, clearColor, clearDepth , contextAttribs)
239 {
240 var canvas = document.getElementById(canvasName);
241 var gl = create3DContext(canvas, contextAttribs);
242 if (!gl) {
243 alert("No WebGL context found");
244 return null;
245 }
246
247 // Create the program object
248 gl.program = createProgram(gl, vshader, fshader, attribs);
249 if (!gl.program)
250 return null;
251
252 gl.useProgram(gl.program);
253
254 gl.clearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
255 gl.clearDepth(clearDepth);
256
257 gl.enable(gl.DEPTH_TEST);
258 gl.enable(gl.BLEND);
259 gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
260
261 return gl;
262 }
263
264 //
265 // getShaderSource
266 //
267 // Load the source from the passed shader file.
268 //
269 function getShaderSource(file)
270 {
271 var xhr = new XMLHttpRequest();
272 xhr.open("GET", file, false);
273 xhr.send();
274 return xhr.responseText;
275 }
276
277
278 //
279 // loadShader
280 //
281 // 'shader' is either the id of a <script> element containing the shader source
282 // string, the shader string itself, or the URL of a file containing the shader
283 // source. Load this shader and return the WebGLShader object corresponding to
284 // it.
285 //
286 function loadShader(ctx, shaderId, shaderType, isFile)
287 {
288 var shaderSource = "";
289
290 if (isFile)
291 shaderSource = getShaderSource(shaderId);
292 else {
293 var shaderScript = document.getElementById(shaderId);
294 if (!shaderScript) {
295 shaderSource = shaderId;
296 } else {
297 if (shaderScript.type == "x-shader/x-vertex") {
298 shaderType = ctx.VERTEX_SHADER;
299 } else if (shaderScript.type == "x-shader/x-fragment") {
300 shaderType = ctx.FRAGMENT_SHADER;
301 } else if (shaderType != ctx.VERTEX_SHADER && shaderType != ctx.FRAG MENT_SHADER) {
302 webglTestLog("*** Error: unknown shader type");
303 return null;
304 }
305
306 shaderSource = shaderScript.text;
307 }
308 }
309
310 // Create the shader object
311 var shader = ctx.createShader(shaderType);
312 if (shader == null) {
313 webglTestLog("*** Error: unable to create shader '"+shaderId+"'");
314 return null;
315 }
316
317 // Load the shader source
318 ctx.shaderSource(shader, shaderSource);
319
320 // Compile the shader
321 ctx.compileShader(shader);
322
323 // Check the compile status
324 var compiled = ctx.getShaderParameter(shader, ctx.COMPILE_STATUS);
325 if (!compiled) {
326 // Something went wrong during compilation; get the error
327 var error = ctx.getShaderInfoLog(shader);
328 webglTestLog("*** Error compiling shader '"+shader+"':"+error);
329 ctx.deleteShader(shader);
330 return null;
331 }
332
333 return shader;
334 }
335
336 function loadShaderFromFile(ctx, file, type)
337 {
338 return loadShader(ctx, file, type, true);
339 }
340
341 function loadShaderFromScript(ctx, script)
342 {
343 return loadShader(ctx, script, 0, false);
344 }
345
346 function loadStandardProgram(context) {
347 var program = context.createProgram();
348 context.attachShader(program, loadStandardVertexShader(context));
349 context.attachShader(program, loadStandardFragmentShader(context));
350 context.linkProgram(program);
351 return program;
352 }
353
354 function loadProgram(context, vertexShaderPath, fragmentShaderPath, isFile) {
355 isFile = (isFile === undefined) ? true : isFile;
356 var program = context.createProgram();
357 context.attachShader(program, loadShader(context, vertexShaderPath, context. VERTEX_SHADER, isFile));
358 context.attachShader(program, loadShader(context, fragmentShaderPath, contex t.FRAGMENT_SHADER, isFile));
359 context.linkProgram(program);
360 return program;
361 }
362
363 var getBasePathForResources = function() {
364 var expectedBase = "webgl-test.js";
365 var scripts = document.getElementsByTagName('script');
366 for (var script, i = 0; script = scripts[i]; i++) {
367 var src = script.src;
368 var l = src.length;
369 if (src.substr(l - expectedBase.length) == expectedBase) {
370 return src.substr(0, l - expectedBase.length);
371 }
372 }
373 throw 'oops';
374 };
375
376
377 function loadStandardVertexShader(context) {
378 return loadShader(
379 context,
380 getBasePathForResources() + "vertexShader.vert",
381 context.VERTEX_SHADER,
382 true);
383 }
384
385 function loadStandardFragmentShader(context) {
386 return loadShader(
387 context,
388 getBasePathForResources() + "fragmentShader.frag",
389 context.FRAGMENT_SHADER,
390 true);
391 }
392
393 //
394 // makeBox
395 //
396 // Create a box with vertices, normals and texCoords. Create VBOs for each as we ll as the index array.
397 // Return an object with the following properties:
398 //
399 // normalObject WebGLBuffer object for normals
400 // texCoordObject WebGLBuffer object for texCoords
401 // vertexObject WebGLBuffer object for vertices
402 // indexObject WebGLBuffer object for indices
403 // numIndices The number of indices in the indexObject
404 //
405 function makeBox(ctx)
406 {
407 // box
408 // v6----- v5
409 // /| /|
410 // v1------v0|
411 // | | | |
412 // | |v7---|-|v4
413 // |/ |/
414 // v2------v3
415 //
416 // vertex coords array
417 var vertices = new Float32Array(
418 [ 1, 1, 1, -1, 1, 1, -1,-1, 1, 1,-1, 1, // v0-v1-v2-v3 front
419 1, 1, 1, 1,-1, 1, 1,-1,-1, 1, 1,-1, // v0-v3-v4-v5 right
420 1, 1, 1, 1, 1,-1, -1, 1,-1, -1, 1, 1, // v0-v5-v6-v1 top
421 -1, 1, 1, -1, 1,-1, -1,-1,-1, -1,-1, 1, // v1-v6-v7-v2 left
422 -1,-1,-1, 1,-1,-1, 1,-1, 1, -1,-1, 1, // v7-v4-v3-v2 bottom
423 1,-1,-1, -1,-1,-1, -1, 1,-1, 1, 1,-1 ] // v4-v7-v6-v5 back
424 );
425
426 // normal array
427 var normals = new Float32Array(
428 [ 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // v0-v1-v2-v3 front
429 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v3-v4-v5 right
430 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // v0-v5-v6-v1 top
431 -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, // v1-v6-v7-v2 left
432 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, // v7-v4-v3-v2 bottom
433 0, 0,-1, 0, 0,-1, 0, 0,-1, 0, 0,-1 ] // v4-v7-v6-v5 back
434 );
435
436
437 // texCoord array
438 var texCoords = new Float32Array(
439 [ 1, 1, 0, 1, 0, 0, 1, 0, // v0-v1-v2-v3 front
440 0, 1, 0, 0, 1, 0, 1, 1, // v0-v3-v4-v5 right
441 1, 0, 1, 1, 0, 1, 0, 0, // v0-v5-v6-v1 top
442 1, 1, 0, 1, 0, 0, 1, 0, // v1-v6-v7-v2 left
443 0, 0, 1, 0, 1, 1, 0, 1, // v7-v4-v3-v2 bottom
444 0, 0, 1, 0, 1, 1, 0, 1 ] // v4-v7-v6-v5 back
445 );
446
447 // index array
448 var indices = new Uint8Array(
449 [ 0, 1, 2, 0, 2, 3, // front
450 4, 5, 6, 4, 6, 7, // right
451 8, 9,10, 8,10,11, // top
452 12,13,14, 12,14,15, // left
453 16,17,18, 16,18,19, // bottom
454 20,21,22, 20,22,23 ] // back
455 );
456
457 var retval = { };
458
459 retval.normalObject = ctx.createBuffer();
460 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.normalObject);
461 ctx.bufferData(ctx.ARRAY_BUFFER, normals, ctx.STATIC_DRAW);
462
463 retval.texCoordObject = ctx.createBuffer();
464 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.texCoordObject);
465 ctx.bufferData(ctx.ARRAY_BUFFER, texCoords, ctx.STATIC_DRAW);
466
467 retval.vertexObject = ctx.createBuffer();
468 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.vertexObject);
469 ctx.bufferData(ctx.ARRAY_BUFFER, vertices, ctx.STATIC_DRAW);
470
471 ctx.bindBuffer(ctx.ARRAY_BUFFER, 0);
472
473 retval.indexObject = ctx.createBuffer();
474 ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, retval.indexObject);
475 ctx.bufferData(ctx.ELEMENT_ARRAY_BUFFER, indices, ctx.STATIC_DRAW);
476 ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, 0);
477
478 retval.numIndices = indices.length;
479
480 return retval;
481 }
482
483 //
484 // makeSphere
485 //
486 // Create a sphere with the passed number of latitude and longitude bands and th e passed radius.
487 // Sphere has vertices, normals and texCoords. Create VBOs for each as well as t he index array.
488 // Return an object with the following properties:
489 //
490 // normalObject WebGLBuffer object for normals
491 // texCoordObject WebGLBuffer object for texCoords
492 // vertexObject WebGLBuffer object for vertices
493 // indexObject WebGLBuffer object for indices
494 // numIndices The number of indices in the indexObject
495 //
496 function makeSphere(ctx, radius, lats, longs)
497 {
498 var geometryData = [ ];
499 var normalData = [ ];
500 var texCoordData = [ ];
501 var indexData = [ ];
502
503 for (var latNumber = 0; latNumber <= lats; ++latNumber) {
504 for (var longNumber = 0; longNumber <= longs; ++longNumber) {
505 var theta = latNumber * Math.PI / lats;
506 var phi = longNumber * 2 * Math.PI / longs;
507 var sinTheta = Math.sin(theta);
508 var sinPhi = Math.sin(phi);
509 var cosTheta = Math.cos(theta);
510 var cosPhi = Math.cos(phi);
511
512 var x = cosPhi * sinTheta;
513 var y = cosTheta;
514 var z = sinPhi * sinTheta;
515 var u = 1-(longNumber/longs);
516 var v = latNumber/lats;
517
518 normalData.push(x);
519 normalData.push(y);
520 normalData.push(z);
521 texCoordData.push(u);
522 texCoordData.push(v);
523 geometryData.push(radius * x);
524 geometryData.push(radius * y);
525 geometryData.push(radius * z);
526 }
527 }
528
529 longs += 1;
530 for (var latNumber = 0; latNumber < lats; ++latNumber) {
531 for (var longNumber = 0; longNumber < longs; ++longNumber) {
532 var first = (latNumber * longs) + (longNumber % longs);
533 var second = first + longs;
534 indexData.push(first);
535 indexData.push(second);
536 indexData.push(first+1);
537
538 indexData.push(second);
539 indexData.push(second+1);
540 indexData.push(first+1);
541 }
542 }
543
544 var retval = { };
545
546 retval.normalObject = ctx.createBuffer();
547 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.normalObject);
548 ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(normalData), ctx.STATIC_DR AW);
549
550 retval.texCoordObject = ctx.createBuffer();
551 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.texCoordObject);
552 ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(texCoordData), ctx.STATIC_ DRAW);
553
554 retval.vertexObject = ctx.createBuffer();
555 ctx.bindBuffer(ctx.ARRAY_BUFFER, retval.vertexObject);
556 ctx.bufferData(ctx.ARRAY_BUFFER, new Float32Array(geometryData), ctx.STATIC_ DRAW);
557
558 retval.numIndices = indexData.length;
559 retval.indexObject = ctx.createBuffer();
560 ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, retval.indexObject);
561 ctx.bufferData(ctx.ELEMENT_ARRAY_BUFFER, new Uint16Array(indexData), ctx.STR EAM_DRAW);
562
563 return retval;
564 }
565
566 //
567 // loadObj
568 //
569 // Load a .obj file from the passed URL. Return an object with a 'loaded' proper ty set to false.
570 // When the object load is complete, the 'loaded' property becomes true and the following
571 // properties are set:
572 //
573 // normalObject WebGLBuffer object for normals
574 // texCoordObject WebGLBuffer object for texCoords
575 // vertexObject WebGLBuffer object for vertices
576 // indexObject WebGLBuffer object for indices
577 // numIndices The number of indices in the indexObject
578 //
579 function loadObj(ctx, url)
580 {
581 var obj = { loaded : false };
582 obj.ctx = ctx;
583 var req = new XMLHttpRequest();
584 req.obj = obj;
585 req.onreadystatechange = function () { processLoadObj(req) };
586 req.open("GET", url, true);
587 req.send(null);
588 return obj;
589 }
590
591 function processLoadObj(req)
592 {
593 webglTestLog("req="+req)
594 // only if req shows "complete"
595 if (req.readyState == 4) {
596 doLoadObj(req.obj, req.responseText);
597 }
598 }
599
600 function doLoadObj(obj, text)
601 {
602 vertexArray = [ ];
603 normalArray = [ ];
604 textureArray = [ ];
605 indexArray = [ ];
606
607 var vertex = [ ];
608 var normal = [ ];
609 var texture = [ ];
610 var facemap = { };
611 var index = 0;
612
613 var lines = text.split("\n");
614 for (var lineIndex in lines) {
615 var line = lines[lineIndex].replace(/[ \t]+/g, " ").replace(/\s\s*$/, "" );
616
617 // ignore comments
618 if (line[0] == "#")
619 continue;
620
621 var array = line.split(" ");
622 if (array[0] == "v") {
623 // vertex
624 vertex.push(parseFloat(array[1]));
625 vertex.push(parseFloat(array[2]));
626 vertex.push(parseFloat(array[3]));
627 }
628 else if (array[0] == "vt") {
629 // normal
630 texture.push(parseFloat(array[1]));
631 texture.push(parseFloat(array[2]));
632 }
633 else if (array[0] == "vn") {
634 // normal
635 normal.push(parseFloat(array[1]));
636 normal.push(parseFloat(array[2]));
637 normal.push(parseFloat(array[3]));
638 }
639 else if (array[0] == "f") {
640 // face
641 if (array.length != 4) {
642 webglTestLog("*** Error: face '"+line+"' not handled");
643 continue;
644 }
645
646 for (var i = 1; i < 4; ++i) {
647 if (!(array[i] in facemap)) {
648 // add a new entry to the map and arrays
649 var f = array[i].split("/");
650 var vtx, nor, tex;
651
652 if (f.length == 1) {
653 vtx = parseInt(f[0]) - 1;
654 nor = vtx;
655 tex = vtx;
656 }
657 else if (f.length = 3) {
658 vtx = parseInt(f[0]) - 1;
659 tex = parseInt(f[1]) - 1;
660 nor = parseInt(f[2]) - 1;
661 }
662 else {
663 webglTestLog("*** Error: did not understand face '"+arra y[i]+"'");
664 return null;
665 }
666
667 // do the vertices
668 var x = 0;
669 var y = 0;
670 var z = 0;
671 if (vtx * 3 + 2 < vertex.length) {
672 x = vertex[vtx*3];
673 y = vertex[vtx*3+1];
674 z = vertex[vtx*3+2];
675 }
676 vertexArray.push(x);
677 vertexArray.push(y);
678 vertexArray.push(z);
679
680 // do the textures
681 x = 0;
682 y = 0;
683 if (tex * 2 + 1 < texture.length) {
684 x = texture[tex*2];
685 y = texture[tex*2+1];
686 }
687 textureArray.push(x);
688 textureArray.push(y);
689
690 // do the normals
691 x = 0;
692 y = 0;
693 z = 1;
694 if (nor * 3 + 2 < normal.length) {
695 x = normal[nor*3];
696 y = normal[nor*3+1];
697 z = normal[nor*3+2];
698 }
699 normalArray.push(x);
700 normalArray.push(y);
701 normalArray.push(z);
702
703 facemap[array[i]] = index++;
704 }
705
706 indexArray.push(facemap[array[i]]);
707 }
708 }
709 }
710
711 // set the VBOs
712 obj.normalObject = obj.ctx.createBuffer();
713 obj.ctx.bindBuffer(obj.ctx.ARRAY_BUFFER, obj.normalObject);
714 obj.ctx.bufferData(obj.ctx.ARRAY_BUFFER, new Float32Array(normalArray), obj. ctx.STATIC_DRAW);
715
716 obj.texCoordObject = obj.ctx.createBuffer();
717 obj.ctx.bindBuffer(obj.ctx.ARRAY_BUFFER, obj.texCoordObject);
718 obj.ctx.bufferData(obj.ctx.ARRAY_BUFFER, new Float32Array(textureArray), obj .ctx.STATIC_DRAW);
719
720 obj.vertexObject = obj.ctx.createBuffer();
721 obj.ctx.bindBuffer(obj.ctx.ARRAY_BUFFER, obj.vertexObject);
722 obj.ctx.bufferData(obj.ctx.ARRAY_BUFFER, new Float32Array(vertexArray), obj. ctx.STATIC_DRAW);
723
724 obj.numIndices = indexArray.length;
725 obj.indexObject = obj.ctx.createBuffer();
726 obj.ctx.bindBuffer(obj.ctx.ELEMENT_ARRAY_BUFFER, obj.indexObject);
727 obj.ctx.bufferData(obj.ctx.ELEMENT_ARRAY_BUFFER, new Uint16Array(indexArray) , obj.ctx.STREAM_DRAW);
728
729 obj.loaded = true;
730 }
731
732 //
733 // loadImageTexture
734 //
735 // Load the image at the passed url, place it in a new WebGLTexture object and r eturn the WebGLTexture.
736 //
737 function loadImageTexture(ctx, url)
738 {
739 var texture = ctx.createTexture();
740 texture.image = new Image();
741 texture.image.onload = function() { doLoadImageTexture(ctx, texture.image, t exture) }
742 texture.image.src = url;
743 return texture;
744 }
745
746 function doLoadImageTexture(ctx, image, texture)
747 {
748 ctx.enable(ctx.TEXTURE_2D);
749 ctx.bindTexture(ctx.TEXTURE_2D, texture);
750 ctx.texImage2D(ctx.TEXTURE_2D, 0, ctx.RGBA, ctx.RGBA, ctx.UNSIGNED_BYTE, ima ge);
751 ctx.texParameteri(ctx.TEXTURE_2D, ctx.TEXTURE_MAG_FILTER, ctx.LINEAR);
752 ctx.texParameteri(ctx.TEXTURE_2D, ctx.TEXTURE_MIN_FILTER, ctx.LINEAR_MIPMAP_ LINEAR);
753 ctx.texParameteri(ctx.TEXTURE_2D, ctx.TEXTURE_WRAP_S, ctx.CLAMP_TO_EDGE);
754 ctx.texParameteri(ctx.TEXTURE_2D, ctx.TEXTURE_WRAP_T, ctx.CLAMP_TO_EDGE);
755 ctx.generateMipmap(ctx.TEXTURE_2D)
756 ctx.bindTexture(ctx.TEXTURE_2D, 0);
757 }
758
759 //
760 // Framerate object
761 //
762 // This object keeps track of framerate and displays it as the innerHTML text of the
763 // HTML element with the passed id. Once created you call snapshot at the end
764 // of every rendering cycle. Every 500ms the framerate is updated in the HTML el ement.
765 //
766 Framerate = function(id)
767 {
768 this.numFramerates = 10;
769 this.framerateUpdateInterval = 500;
770 this.id = id;
771
772 this.renderTime = -1;
773 this.framerates = [ ];
774 self = this;
775 var fr = function() { self.updateFramerate() }
776 setInterval(fr, this.framerateUpdateInterval);
777 }
778
779 Framerate.prototype.updateFramerate = function()
780 {
781 var tot = 0;
782 for (var i = 0; i < this.framerates.length; ++i)
783 tot += this.framerates[i];
784
785 var framerate = tot / this.framerates.length;
786 framerate = Math.round(framerate);
787 document.getElementById(this.id).innerHTML = "Framerate:"+framerate+"fps";
788 }
789
790 Framerate.prototype.snapshot = function()
791 {
792 if (this.renderTime < 0)
793 this.renderTime = new Date().getTime();
794 else {
795 var newTime = new Date().getTime();
796 var t = newTime - this.renderTime;
797 var framerate = 1000/t;
798 this.framerates.push(framerate);
799 while (this.framerates.length > this.numFramerates)
800 this.framerates.shift();
801 this.renderTime = newTime;
802 }
803 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698