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 uniform vec4 col; |
| 47 |
| 48 void main() |
| 49 { |
| 50 gl_FragColor = col; |
| 51 } |
| 52 </script> |
| 53 |
| 54 <script> |
| 55 function draw(gl, arr, colLoc, col) { |
| 56 var vertices = new Float32Array(arr); |
| 57 var vertBuf = gl.createBuffer(); |
| 58 gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf); |
| 59 gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); |
| 60 gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0); |
| 61 gl.uniform4fv(colLoc, col); |
| 62 gl.drawArrays(gl.TRIANGLE_STRIP, 0, vertices.length / 2); |
| 63 } |
| 64 |
| 65 function clear(gl, col) { |
| 66 gl.clearColor(col[0], col[1], col[2], col[3]); |
| 67 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 68 } |
| 69 |
| 70 function check(gl, winding, shoulddraw) { |
| 71 buf = new Uint8Array(4); |
| 72 gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf); |
| 73 if (buf[0] == 0 && buf[1] == 255 && buf[2] == 0 && buf[3] == 255
) |
| 74 testPassed(winding + ' face was ' + (shoulddraw ? '' : 'not
') + 'drawn.'); |
| 75 else |
| 76 testFailed(winding + ' face was ' + (shoulddraw ? 'not ' : '
') + 'drawn.'); |
| 77 } |
| 78 |
| 79 function runTest() |
| 80 { |
| 81 var cwVertices = [-1, -1, -1, 1, 1, -1, 1, 1]; |
| 82 var ccwVertices = [-1, 1, -1, -1, 1, 1, 1, -1]; |
| 83 var red = [1, 0, 0, 1]; |
| 84 var green = [0, 1, 0, 1]; |
| 85 var ok; |
| 86 |
| 87 var gl = initWebGL('testbed', { antialias: false }); |
| 88 if (!gl) { |
| 89 testFailed('initWebGL(..) failed'); |
| 90 return; |
| 91 } |
| 92 var program = setupProgram(gl, 'vshader', 'fshader', ['pos']); |
| 93 var colLoc = gl.getUniformLocation(program, 'col'); |
| 94 |
| 95 gl.enableVertexAttribArray(0); |
| 96 |
| 97 debug('CULL_FACE should be off by default'); |
| 98 clear(gl, red); |
| 99 draw(gl, ccwVertices, colLoc, green); |
| 100 check(gl, 'CCW', true); |
| 101 clear(gl, red); |
| 102 draw(gl, cwVertices, colLoc, green); |
| 103 check(gl, 'CW', true); |
| 104 |
| 105 debug('Enabling CULL_FACE'); |
| 106 gl.enable(gl.CULL_FACE); |
| 107 |
| 108 debug('BACK and CCW should be set by default'); |
| 109 clear(gl, red); |
| 110 draw(gl, ccwVertices, colLoc, green); |
| 111 check(gl, 'CCW', true); |
| 112 clear(gl, green); |
| 113 draw(gl, cwVertices, colLoc, red); |
| 114 check(gl, 'CW', false); |
| 115 |
| 116 var tests = [{ cullFace : 'BACK', frontFace : 'CCW', drawCCW : t
rue, drawCW : false}, |
| 117 { cullFace : 'BACK', frontFace : 'CW', drawCCW : fa
lse, drawCW : true}, |
| 118 { cullFace : 'FRONT', frontFace : 'CCW', drawCCW :
false, drawCW : true }, |
| 119 { cullFace : 'FRONT', frontFace : 'CW', drawCCW : t
rue, drawCW : false}, |
| 120 { cullFace : 'FRONT_AND_BACK', frontFace : 'CCW', d
rawCCW : false, drawCW : false}, |
| 121 { cullFace : 'FRONT_AND_BACK', frontFace : 'CW', dr
awCCW : false, drawCW : false}]; |
| 122 |
| 123 for (var i = 0; i < tests.length; ++i) { |
| 124 var t = tests[i]; |
| 125 debug('Setting ' + t.cullFace + ' and ' + t.frontFace); |
| 126 gl.cullFace(gl[t.cullFace]); |
| 127 gl.frontFace(gl[t.frontFace]); |
| 128 clear(gl, t.drawCCW ? red : green); |
| 129 draw(gl, ccwVertices, colLoc, t.drawCCW ? green : red); |
| 130 check(gl, 'CCW', t.drawCCW); |
| 131 clear(gl, t.drawCW ? red : green); |
| 132 draw(gl, cwVertices, colLoc, t.drawCW ? green : red); |
| 133 check(gl, 'CW', t.drawCW); |
| 134 } |
| 135 |
| 136 } |
| 137 </script> |
| 138 </head> |
| 139 <body> |
| 140 <canvas id="testbed" width="1px" height="1px" style="width:50px; height:
50px"></canvas> |
| 141 <div id="description"></div> |
| 142 <div id="console"></div> |
| 143 <script> |
| 144 description('Verify that culling works'); |
| 145 runTest(); |
| 146 successfullyParsed = true; |
| 147 </script> |
| 148 <script src="../../resources/js-test-post.js"></script> |
| 149 </body> |
| 150 </html> |
OLD | NEW |