content/canvas/test/webgl-conformance/conformance/rendering/line-loop-tri-fan.html

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 <!--
michael@0 2 Copyright (C) 2011 Opera Software ASA. All rights reserved.
michael@0 3
michael@0 4 Redistribution and use in source and binary forms, with or without
michael@0 5 modification, are permitted provided that the following conditions
michael@0 6 are met:
michael@0 7 1. Redistributions of source code must retain the above copyright
michael@0 8 notice, this list of conditions and the following disclaimer.
michael@0 9 2. Redistributions in binary form must reproduce the above copyright
michael@0 10 notice, this list of conditions and the following disclaimer in the
michael@0 11 documentation and/or other materials provided with the distribution.
michael@0 12
michael@0 13 THIS SOFTWARE IS PROVIDED BY OPERA SOFTWARE ASA. ''AS IS'' AND ANY
michael@0 14 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0 15 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
michael@0 16 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
michael@0 17 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0 18 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0 19 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0 20 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
michael@0 21 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 24 -->
michael@0 25 <!DOCTYPE html>
michael@0 26 <html>
michael@0 27 <head>
michael@0 28 <meta charset="utf-8">
michael@0 29 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
michael@0 30 <script src="../../resources/js-test-pre.js"></script>
michael@0 31 <script src="../resources/webgl-test.js"></script>
michael@0 32 <script id="vshader" type="x-shader/x-vertex">
michael@0 33 attribute vec2 pos;
michael@0 34
michael@0 35 void main()
michael@0 36 {
michael@0 37 gl_Position = vec4(pos, 0, 1);
michael@0 38 }
michael@0 39 </script>
michael@0 40
michael@0 41 <script id="fshader" type="x-shader/x-fragment">
michael@0 42 precision mediump float;
michael@0 43
michael@0 44 void main()
michael@0 45 {
michael@0 46 gl_FragColor = vec4(0, 1, 0, 1);
michael@0 47 }
michael@0 48 </script>
michael@0 49
michael@0 50 <script>
michael@0 51 // Check a single 32-bit RGBA pixel.
michael@0 52 function checkPixel(buf, index, correct) {
michael@0 53 for (var i = 0; i < 4; ++i) {
michael@0 54 if (buf[index + i] != correct[i]) {
michael@0 55 return false;
michael@0 56 }
michael@0 57 }
michael@0 58 return true;
michael@0 59 }
michael@0 60
michael@0 61 // Check the line loop by reading the pixels and making sure just the edge
michael@0 62 // pixels are green and the rest are black.
michael@0 63 function checkLineLoop(gl, w) {
michael@0 64 var buf = new Uint8Array(w * w * 4);
michael@0 65 gl.readPixels(0, 0, w, w, gl.RGBA, gl.UNSIGNED_BYTE, buf);
michael@0 66 var green = [0,255,0,255];
michael@0 67 var black = [0,0,0,255];
michael@0 68 var isCorrect = true;
michael@0 69 for (var j = 0; j < w * w * 4; j += 4) {
michael@0 70 var correct = black;
michael@0 71 if (j < w * 4 || j > w * (w - 1) * 4 || j % (w * 4) == 0 || j % (w * 4) == (w - 1) * 4) {
michael@0 72 correct = green;
michael@0 73 }
michael@0 74 // ignore corner pixels
michael@0 75 if ((j == 0) || (j == 4*(w-1)) || (j == 4*w*(w-1)) || (j== 4*(w*w - 1))) {
michael@0 76 continue;
michael@0 77 }
michael@0 78 if (!checkPixel(buf, j, correct)) {
michael@0 79 isCorrect = false;
michael@0 80 break;
michael@0 81 }
michael@0 82 }
michael@0 83 if (isCorrect) {
michael@0 84 testPassed("Line loop was drawn correctly.");
michael@0 85 } else {
michael@0 86 testFailed("Line loop was drawn incorrectly.");
michael@0 87 }
michael@0 88 }
michael@0 89
michael@0 90 // Check the tri fan by reading the pixels and making sure they are all green.
michael@0 91 function checkTriFan(gl, w) {
michael@0 92 buf = new Uint8Array(w * w * 4);
michael@0 93 gl.readPixels(0, 0, w, w, gl.RGBA, gl.UNSIGNED_BYTE, buf);
michael@0 94 var filled = true;
michael@0 95 for (var j = 0; j < w * w * 4; j += 4) {
michael@0 96 if (!checkPixel(buf, j, [0,255,0,255])) {
michael@0 97 filled = false;
michael@0 98 break;
michael@0 99 }
michael@0 100 }
michael@0 101 if (filled) {
michael@0 102 testPassed("Triangle fan was drawn correctly.");
michael@0 103 } else {
michael@0 104 testFailed("Triangle fan was drawn incorrectly.");
michael@0 105 }
michael@0 106 }
michael@0 107
michael@0 108 function runTest()
michael@0 109 {
michael@0 110 var gl = initWebGL('testbed', 'vshader', 'fshader', ['pos'], [0, 0, 0, 1], 1, { antialias: false });
michael@0 111 if (!gl) {
michael@0 112 testFailed('initWebGL(..) failed');
michael@0 113 return;
michael@0 114 }
michael@0 115 var w = document.getElementById('testbed').width;
michael@0 116
michael@0 117 gl.enableVertexAttribArray(0);
michael@0 118
michael@0 119 //---------- LINE_LOOP----------
michael@0 120 var d = 1/w;
michael@0 121 var vertices = new Float32Array([-1+d, -1+d, 1-d, -1+d, 1-d, 1-d, -1+d, 1-d]);
michael@0 122 var vertBuf = gl.createBuffer();
michael@0 123 gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
michael@0 124 gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
michael@0 125 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
michael@0 126 var indBuf = gl.createBuffer();
michael@0 127 var indices = new Uint16Array([0, 1, 2, 3]);
michael@0 128 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
michael@0 129 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
michael@0 130
michael@0 131 debug('Draw a square using a line loop and verify that it draws all four sides and nothing else.');
michael@0 132 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
michael@0 133 gl.drawArrays(gl.LINE_LOOP, 0, vertices.length / 2);
michael@0 134 checkLineLoop(gl, w);
michael@0 135
michael@0 136 debug('Draw a square using an indexed line loop and verify that it draws all four sides and nothing else.');
michael@0 137 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
michael@0 138 gl.drawElements(gl.LINE_LOOP, indices.length, gl.UNSIGNED_SHORT, 0);
michael@0 139 checkLineLoop(gl, w);
michael@0 140
michael@0 141 vertices = new Float32Array([0, 0, 0, 0, 0, 0, -1+d, -1+d, 1-d, -1+d, 1-d, 1-d, -1+d, 1-d]);
michael@0 142 vertBuf = gl.createBuffer();
michael@0 143 gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
michael@0 144 gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
michael@0 145 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
michael@0 146 indBuf = gl.createBuffer();
michael@0 147 indices = new Uint16Array([0, 1, 2, 3, 4, 5, 6]);
michael@0 148 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
michael@0 149 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
michael@0 150
michael@0 151 debug('Draw a square using a line loop with a vertex buffer offset and verify that it draws all four sides and nothing else.');
michael@0 152 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
michael@0 153 gl.drawArrays(gl.LINE_LOOP, 3, vertices.length / 2 - 3);
michael@0 154 checkLineLoop(gl, w);
michael@0 155
michael@0 156 debug('Draw a square using an indexed line loop with an index buffer offset and verify that it draws all four sides and nothing else.');
michael@0 157 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
michael@0 158 gl.drawElements(gl.LINE_LOOP, indices.length - 3, gl.UNSIGNED_SHORT, 3 * 2);
michael@0 159 checkLineLoop(gl, w);
michael@0 160
michael@0 161 //---------- LINE_LOOP UBYTE ----------
michael@0 162 var degenVerts = new Array(252 * 2);
michael@0 163 for (var j = 0; j < 252 * 2; ++j) {
michael@0 164 degenVerts[j] = -1+d;
michael@0 165 }
michael@0 166 degenVerts = degenVerts.concat([-1+d, -1+d, 1-d, -1+d, 1-d, 1-d, -1+d, 1-d]);
michael@0 167 vertices = new Float32Array(degenVerts);
michael@0 168 vertBuf = gl.createBuffer();
michael@0 169 gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
michael@0 170 gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
michael@0 171 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
michael@0 172 indBuf = gl.createBuffer();
michael@0 173 var degenInd = new Array(252);
michael@0 174 for (var j = 0; j < 252; ++j) {
michael@0 175 degenInd[j] = j;
michael@0 176 }
michael@0 177 degenInd = degenInd.concat([252, 253, 254, 255]);
michael@0 178 indices = new Uint8Array(degenInd);
michael@0 179 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
michael@0 180 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
michael@0 181
michael@0 182 debug('Draw a square using an ubyte indexed line loop with 256 indices and verify that it draws all four sides and nothing else.');
michael@0 183 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
michael@0 184 gl.drawElements(gl.LINE_LOOP, indices.length, gl.UNSIGNED_BYTE, 0);
michael@0 185 checkLineLoop(gl, w);
michael@0 186
michael@0 187
michael@0 188 //---------- TRIANGLE_FAN ----------
michael@0 189 vertices = new Float32Array([0, 0, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1]);
michael@0 190 vertBuf = gl.createBuffer();
michael@0 191 gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
michael@0 192 gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
michael@0 193 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
michael@0 194 indices = new Uint16Array([0,1,2,3,4,5]);
michael@0 195 indBuf = gl.createBuffer();
michael@0 196 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
michael@0 197 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
michael@0 198
michael@0 199 debug('Draw a filled square using a triangle fan and verify that it fills the entire canvas.');
michael@0 200 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
michael@0 201 gl.drawArrays(gl.TRIANGLE_FAN, 0, vertices.length / 2);
michael@0 202 checkTriFan(gl, w);
michael@0 203
michael@0 204 debug('Draw a filled square using an indexed triangle fan and verify that it fills the entire canvas.');
michael@0 205 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
michael@0 206 gl.drawElements(gl.TRIANGLE_FAN, indices.length, gl.UNSIGNED_SHORT, 0);
michael@0 207 checkTriFan(gl, w);
michael@0 208
michael@0 209 vertices = new Float32Array([1, 1, 1, 1, 1, 1, 0, 0, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1]);
michael@0 210 vertBuf = gl.createBuffer();
michael@0 211 gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
michael@0 212 gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
michael@0 213 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
michael@0 214 indices = new Uint16Array([0,1,2,3,4,5,6,7,8]);
michael@0 215 indBuf = gl.createBuffer();
michael@0 216 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
michael@0 217 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
michael@0 218
michael@0 219 debug('Draw a filled square using a triangle fan with a vertex buffer offset and verify that it fills the entire canvas.');
michael@0 220 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
michael@0 221 gl.drawArrays(gl.TRIANGLE_FAN, 3, vertices.length / 2 - 3);
michael@0 222 checkTriFan(gl, w);
michael@0 223
michael@0 224 debug('Draw a filled square using an indexed triangle fan with an index buffer offset and verify that it fills the entire canvas.');
michael@0 225 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
michael@0 226 gl.drawElements(gl.TRIANGLE_FAN, indices.length - 3, gl.UNSIGNED_SHORT, 3 * 2);
michael@0 227 checkTriFan(gl, w);
michael@0 228 }
michael@0 229 </script>
michael@0 230 </head>
michael@0 231 <body>
michael@0 232 <canvas id="testbed" width="10px" height="10px" style="width:50px; height:50px"></canvas>
michael@0 233 <div id="description"></div>
michael@0 234 <div id="console"></div>
michael@0 235 <script>
michael@0 236 description('Verify that LINE_LOOP and TRIANGLE_FAN works correctly.');
michael@0 237 runTest();
michael@0 238 successfullyParsed = true;
michael@0 239 </script>
michael@0 240 <script>finishTest();</script>
michael@0 241 </body>
michael@0 242 </html>

mercurial