OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 |
| 3 /* |
| 4 ** Copyright (c) 2012 The Khronos Group Inc. |
| 5 ** |
| 6 ** Permission is hereby granted, free of charge, to any person obtaining a |
| 7 ** copy of this software and/or associated documentation files (the |
| 8 ** "Materials"), to deal in the Materials without restriction, including |
| 9 ** without limitation the rights to use, copy, modify, merge, publish, |
| 10 ** distribute, sublicense, and/or sell copies of the Materials, and to |
| 11 ** permit persons to whom the Materials are furnished to do so, subject to |
| 12 ** the following conditions: |
| 13 ** |
| 14 ** The above copyright notice and this permission notice shall be included |
| 15 ** in all copies or substantial portions of the Materials. |
| 16 ** |
| 17 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 24 */ |
| 25 |
| 26 --> |
| 27 |
| 28 <!DOCTYPE html> |
| 29 <html> |
| 30 <head> |
| 31 <meta charset="utf-8"> |
| 32 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
| 33 <script src="../../resources/js-test-pre.js"></script> |
| 34 <script src="../resources/webgl-test.js"></script> |
| 35 <script id="vshader" type="x-shader/x-vertex"> |
| 36 attribute vec2 pos; |
| 37 |
| 38 void main() |
| 39 { |
| 40 gl_Position = vec4(pos, 0, 1); |
| 41 } |
| 42 </script> |
| 43 |
| 44 <script id="fshader" type="x-shader/x-fragment"> |
| 45 precision mediump float; |
| 46 |
| 47 void main() |
| 48 { |
| 49 gl_FragColor = vec4(0, 1, 0, 1); |
| 50 } |
| 51 </script> |
| 52 |
| 53 <script> |
| 54 // Check a single 32-bit RGBA pixel. |
| 55 function checkPixel(buf, index, correct) { |
| 56 for (var i = 0; i < 4; ++i) { |
| 57 if (buf[index + i] != correct[i]) { |
| 58 return false; |
| 59 } |
| 60 } |
| 61 return true; |
| 62 } |
| 63 |
| 64 // Check the line loop by reading the pixels and making sure just th
e edge |
| 65 // pixels are green and the rest are black. |
| 66 function checkLineLoop(gl, w) { |
| 67 var buf = new Uint8Array(w * w * 4); |
| 68 gl.readPixels(0, 0, w, w, gl.RGBA, gl.UNSIGNED_BYTE, buf); |
| 69 var green = [0,255,0,255]; |
| 70 var black = [0,0,0,255]; |
| 71 var isCorrect = true; |
| 72 for (var j = 0; j < w * w * 4; j += 4) { |
| 73 var correct = black; |
| 74 if (j < w * 4 || j > w * (w - 1) * 4 || j % (w * 4) == 0 ||
j % (w * 4) == (w - 1) * 4) { |
| 75 correct = green; |
| 76 } |
| 77 // ignore corner pixels |
| 78 if ((j == 0) || (j == 4*(w-1)) || (j == 4*w*(w-1)) || (j== 4
*(w*w - 1))) { |
| 79 continue; |
| 80 } |
| 81 if (!checkPixel(buf, j, correct)) { |
| 82 isCorrect = false; |
| 83 break; |
| 84 } |
| 85 } |
| 86 if (isCorrect) { |
| 87 testPassed("Line loop was drawn correctly."); |
| 88 } else { |
| 89 testFailed("Line loop was drawn incorrectly."); |
| 90 } |
| 91 } |
| 92 |
| 93 // Check the tri fan by reading the pixels and making sure they are
all green. |
| 94 function checkTriFan(gl, w) { |
| 95 buf = new Uint8Array(w * w * 4); |
| 96 gl.readPixels(0, 0, w, w, gl.RGBA, gl.UNSIGNED_BYTE, buf); |
| 97 var filled = true; |
| 98 for (var j = 0; j < w * w * 4; j += 4) { |
| 99 if (!checkPixel(buf, j, [0,255,0,255])) { |
| 100 filled = false; |
| 101 break; |
| 102 } |
| 103 } |
| 104 if (filled) { |
| 105 testPassed("Triangle fan was drawn correctly."); |
| 106 } else { |
| 107 testFailed("Triangle fan was drawn incorrectly."); |
| 108 } |
| 109 } |
| 110 |
| 111 function runTest() |
| 112 { |
| 113 var gl = initWebGL('testbed', { antialias: false }); |
| 114 if (!gl) { |
| 115 testFailed('initWebGL(..) failed'); |
| 116 return; |
| 117 } |
| 118 var program = setupProgram(gl, 'vshader', 'fshader', ['pos']) |
| 119 var w = document.getElementById('testbed').width; |
| 120 |
| 121 gl.enableVertexAttribArray(0); |
| 122 |
| 123 //---------- LINE_LOOP---------- |
| 124 var d = 1/w; |
| 125 var vertices = new Float32Array([-1+d, -1+d, 1-d, -1+d, 1-d, 1-d
, -1+d, 1-d]); |
| 126 var vertBuf = gl.createBuffer(); |
| 127 gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf); |
| 128 gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); |
| 129 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); |
| 130 var indBuf = gl.createBuffer(); |
| 131 var indices = new Uint16Array([0, 1, 2, 3]); |
| 132 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf); |
| 133 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW); |
| 134 |
| 135 debug('Draw a square using a line loop and verify that it draws
all four sides and nothing else.'); |
| 136 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 137 gl.drawArrays(gl.LINE_LOOP, 0, vertices.length / 2); |
| 138 checkLineLoop(gl, w); |
| 139 |
| 140 debug('Draw a square using an indexed line loop and verify that
it draws all four sides and nothing else.'); |
| 141 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 142 gl.drawElements(gl.LINE_LOOP, indices.length, gl.UNSIGNED_SHORT,
0); |
| 143 checkLineLoop(gl, w); |
| 144 |
| 145 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]); |
| 146 vertBuf = gl.createBuffer(); |
| 147 gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf); |
| 148 gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); |
| 149 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); |
| 150 indBuf = gl.createBuffer(); |
| 151 indices = new Uint16Array([0, 1, 2, 3, 4, 5, 6]); |
| 152 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf); |
| 153 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW); |
| 154 |
| 155 debug('Draw a square using a line loop with a vertex buffer offs
et and verify that it draws all four sides and nothing else.'); |
| 156 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 157 gl.drawArrays(gl.LINE_LOOP, 3, vertices.length / 2 - 3); |
| 158 checkLineLoop(gl, w); |
| 159 |
| 160 debug('Draw a square using an indexed line loop with an index bu
ffer offset and verify that it draws all four sides and nothing else.'); |
| 161 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 162 gl.drawElements(gl.LINE_LOOP, indices.length - 3, gl.UNSIGNED_SH
ORT, 3 * 2); |
| 163 checkLineLoop(gl, w); |
| 164 |
| 165 //---------- LINE_LOOP UBYTE ---------- |
| 166 var degenVerts = new Array(252 * 2); |
| 167 for (var j = 0; j < 252 * 2; ++j) { |
| 168 degenVerts[j] = -1+d; |
| 169 } |
| 170 degenVerts = degenVerts.concat([-1+d, -1+d, 1-d, -1+d, 1-d, 1-d,
-1+d, 1-d]); |
| 171 vertices = new Float32Array(degenVerts); |
| 172 vertBuf = gl.createBuffer(); |
| 173 gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf); |
| 174 gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); |
| 175 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); |
| 176 indBuf = gl.createBuffer(); |
| 177 var degenInd = new Array(252); |
| 178 for (var j = 0; j < 252; ++j) { |
| 179 degenInd[j] = j; |
| 180 } |
| 181 degenInd = degenInd.concat([252, 253, 254, 255]); |
| 182 indices = new Uint8Array(degenInd); |
| 183 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf); |
| 184 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW); |
| 185 |
| 186 debug('Draw a square using an ubyte indexed line loop with 256 i
ndices and verify that it draws all four sides and nothing else.'); |
| 187 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 188 gl.drawElements(gl.LINE_LOOP, indices.length, gl.UNSIGNED_BYTE,
0); |
| 189 checkLineLoop(gl, w); |
| 190 |
| 191 |
| 192 //---------- TRIANGLE_FAN ---------- |
| 193 vertices = new Float32Array([0, 0, -1, -1, 1, -1, 1, 1, -1, 1, -
1, -1]); |
| 194 vertBuf = gl.createBuffer(); |
| 195 gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf); |
| 196 gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); |
| 197 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); |
| 198 indices = new Uint16Array([0,1,2,3,4,5]); |
| 199 indBuf = gl.createBuffer(); |
| 200 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf); |
| 201 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW); |
| 202 |
| 203 debug('Draw a filled square using a triangle fan and verify that
it fills the entire canvas.'); |
| 204 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 205 gl.drawArrays(gl.TRIANGLE_FAN, 0, vertices.length / 2); |
| 206 checkTriFan(gl, w); |
| 207 |
| 208 debug('Draw a filled square using an indexed triangle fan and ve
rify that it fills the entire canvas.'); |
| 209 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 210 gl.drawElements(gl.TRIANGLE_FAN, indices.length, gl.UNSIGNED_SHO
RT, 0); |
| 211 checkTriFan(gl, w); |
| 212 |
| 213 vertices = new Float32Array([1, 1, 1, 1, 1, 1, 0, 0, -1, -1, 1,
-1, 1, 1, -1, 1, -1, -1]); |
| 214 vertBuf = gl.createBuffer(); |
| 215 gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf); |
| 216 gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); |
| 217 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); |
| 218 indices = new Uint16Array([0,1,2,3,4,5,6,7,8]); |
| 219 indBuf = gl.createBuffer(); |
| 220 gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf); |
| 221 gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW); |
| 222 |
| 223 debug('Draw a filled square using a triangle fan with a vertex b
uffer offset and verify that it fills the entire canvas.'); |
| 224 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 225 gl.drawArrays(gl.TRIANGLE_FAN, 3, vertices.length / 2 - 3); |
| 226 checkTriFan(gl, w); |
| 227 |
| 228 debug('Draw a filled square using an indexed triangle fan with a
n index buffer offset and verify that it fills the entire canvas.'); |
| 229 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 230 gl.drawElements(gl.TRIANGLE_FAN, indices.length - 3, gl.UNSIGNED
_SHORT, 3 * 2); |
| 231 checkTriFan(gl, w); |
| 232 } |
| 233 </script> |
| 234 </head> |
| 235 <body> |
| 236 <canvas id="testbed" width="10px" height="10px" style="width:50px; heigh
t:50px"></canvas> |
| 237 <div id="description"></div> |
| 238 <div id="console"></div> |
| 239 <script> |
| 240 description('Verify that LINE_LOOP and TRIANGLE_FAN works correctly.
'); |
| 241 runTest(); |
| 242 successfullyParsed = true; |
| 243 </script> |
| 244 <script src="../../resources/js-test-post.js"></script> |
| 245 </body> |
| 246 </html> |
OLD | NEW |