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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/canvas/test/webgl-conformance/conformance/rendering/line-loop-tri-fan.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,242 @@
     1.4 +<!--
     1.5 +Copyright (C) 2011 Opera Software ASA.  All rights reserved.
     1.6 +
     1.7 +Redistribution and use in source and binary forms, with or without
     1.8 +modification, are permitted provided that the following conditions
     1.9 +are met:
    1.10 +1. Redistributions of source code must retain the above copyright
    1.11 +   notice, this list of conditions and the following disclaimer.
    1.12 +2. Redistributions in binary form must reproduce the above copyright
    1.13 +   notice, this list of conditions and the following disclaimer in the
    1.14 +   documentation and/or other materials provided with the distribution.
    1.15 +
    1.16 +THIS SOFTWARE IS PROVIDED BY OPERA SOFTWARE ASA. ''AS IS'' AND ANY
    1.17 +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.18 +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.19 +PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
    1.20 +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.21 +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.22 +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.23 +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
    1.24 +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.25 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    1.26 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.27 + -->
    1.28 +<!DOCTYPE html>
    1.29 +<html>
    1.30 +    <head>
    1.31 +        <meta charset="utf-8">
    1.32 +        <link rel="stylesheet" href="../../resources/js-test-style.css"/>
    1.33 +        <script src="../../resources/js-test-pre.js"></script>
    1.34 +        <script src="../resources/webgl-test.js"></script>
    1.35 +        <script id="vshader" type="x-shader/x-vertex">
    1.36 +            attribute vec2 pos;
    1.37 +
    1.38 +            void main()
    1.39 +            {
    1.40 +                gl_Position = vec4(pos, 0, 1);
    1.41 +            }
    1.42 +        </script>
    1.43 +
    1.44 +        <script id="fshader" type="x-shader/x-fragment">
    1.45 +            precision mediump float;
    1.46 +
    1.47 +            void main()
    1.48 +            {
    1.49 +                gl_FragColor = vec4(0, 1, 0, 1);
    1.50 +            }
    1.51 +        </script>
    1.52 +
    1.53 +        <script>
    1.54 +            // Check a single 32-bit RGBA pixel.
    1.55 +            function checkPixel(buf, index, correct) {
    1.56 +                for (var i = 0; i < 4; ++i) {
    1.57 +                    if (buf[index + i] != correct[i]) {
    1.58 +                        return false;
    1.59 +                    }
    1.60 +                }
    1.61 +                return true;
    1.62 +            }
    1.63 +
    1.64 +            // Check the line loop by reading the pixels and making sure just the edge
    1.65 +            // pixels are green and the rest are black.
    1.66 +            function checkLineLoop(gl, w) {
    1.67 +                var buf = new Uint8Array(w * w * 4);
    1.68 +                gl.readPixels(0, 0, w, w, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    1.69 +                var green = [0,255,0,255];
    1.70 +                var black = [0,0,0,255];
    1.71 +                var isCorrect = true;
    1.72 +                for (var j = 0; j < w * w * 4; j += 4) {
    1.73 +                    var correct = black;
    1.74 +                    if (j < w * 4 || j > w * (w - 1) * 4 || j % (w * 4) == 0 || j % (w * 4) == (w - 1) * 4) {
    1.75 +                        correct = green;
    1.76 +                    }
    1.77 +                    // ignore corner pixels
    1.78 +                    if ((j == 0) || (j == 4*(w-1)) || (j == 4*w*(w-1)) || (j== 4*(w*w - 1))) {
    1.79 +                        continue;
    1.80 +                    }
    1.81 +                    if (!checkPixel(buf, j, correct)) {
    1.82 +                        isCorrect = false;
    1.83 +                        break;
    1.84 +                    }
    1.85 +                }
    1.86 +                if (isCorrect) {
    1.87 +                    testPassed("Line loop was drawn correctly.");
    1.88 +                } else {
    1.89 +                    testFailed("Line loop was drawn incorrectly.");
    1.90 +                }
    1.91 +            }
    1.92 +
    1.93 +            // Check the tri fan by reading the pixels and making sure they are all green.
    1.94 +            function checkTriFan(gl, w) {
    1.95 +                buf = new Uint8Array(w * w * 4);
    1.96 +                gl.readPixels(0, 0, w, w, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    1.97 +                var filled = true;
    1.98 +                for (var j = 0; j < w * w * 4; j += 4) {
    1.99 +                    if (!checkPixel(buf, j, [0,255,0,255])) {
   1.100 +                        filled = false;
   1.101 +                        break;
   1.102 +                    }
   1.103 +                }
   1.104 +                if (filled) {
   1.105 +                    testPassed("Triangle fan was drawn correctly.");
   1.106 +                } else {
   1.107 +                    testFailed("Triangle fan was drawn incorrectly.");
   1.108 +                }                
   1.109 +            }
   1.110 +
   1.111 +            function runTest()
   1.112 +            {
   1.113 +                var gl = initWebGL('testbed', 'vshader', 'fshader', ['pos'], [0, 0, 0, 1], 1, { antialias: false });
   1.114 +                if (!gl) {
   1.115 +                    testFailed('initWebGL(..) failed');
   1.116 +                    return;
   1.117 +                }
   1.118 +                var w = document.getElementById('testbed').width;
   1.119 +
   1.120 +                gl.enableVertexAttribArray(0);
   1.121 +
   1.122 +                //---------- LINE_LOOP----------
   1.123 +                var d = 1/w;
   1.124 +                var vertices = new Float32Array([-1+d, -1+d, 1-d, -1+d, 1-d, 1-d, -1+d, 1-d]);
   1.125 +                var vertBuf = gl.createBuffer();
   1.126 +                gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
   1.127 +                gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
   1.128 +                gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
   1.129 +                var indBuf = gl.createBuffer();
   1.130 +                var indices = new Uint16Array([0, 1, 2, 3]);
   1.131 +                gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
   1.132 +                gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
   1.133 +
   1.134 +                debug('Draw a square using a line loop and verify that it draws all four sides and nothing else.');
   1.135 +                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
   1.136 +                gl.drawArrays(gl.LINE_LOOP, 0, vertices.length / 2);
   1.137 +                checkLineLoop(gl, w);
   1.138 +
   1.139 +                debug('Draw a square using an indexed line loop and verify that it draws all four sides and nothing else.');
   1.140 +                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
   1.141 +                gl.drawElements(gl.LINE_LOOP, indices.length, gl.UNSIGNED_SHORT, 0);
   1.142 +                checkLineLoop(gl, w);
   1.143 +
   1.144 +                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]);
   1.145 +                vertBuf = gl.createBuffer();
   1.146 +                gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
   1.147 +                gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
   1.148 +                gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
   1.149 +                indBuf = gl.createBuffer();
   1.150 +                indices = new Uint16Array([0, 1, 2, 3, 4, 5, 6]);
   1.151 +                gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
   1.152 +                gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
   1.153 +
   1.154 +                debug('Draw a square using a line loop with a vertex buffer offset and verify that it draws all four sides and nothing else.');
   1.155 +                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
   1.156 +                gl.drawArrays(gl.LINE_LOOP, 3, vertices.length / 2 - 3);
   1.157 +                checkLineLoop(gl, w);
   1.158 +
   1.159 +                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.');
   1.160 +                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
   1.161 +                gl.drawElements(gl.LINE_LOOP, indices.length - 3, gl.UNSIGNED_SHORT, 3 * 2);
   1.162 +                checkLineLoop(gl, w);
   1.163 +
   1.164 +                //---------- LINE_LOOP UBYTE ----------
   1.165 +                var degenVerts = new Array(252 * 2);
   1.166 +                for (var j = 0; j < 252 * 2; ++j) {
   1.167 +                    degenVerts[j] = -1+d;
   1.168 +                }
   1.169 +                degenVerts = degenVerts.concat([-1+d, -1+d, 1-d, -1+d, 1-d, 1-d, -1+d, 1-d]);
   1.170 +                vertices = new Float32Array(degenVerts);
   1.171 +                vertBuf = gl.createBuffer();
   1.172 +                gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
   1.173 +                gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
   1.174 +                gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
   1.175 +                indBuf = gl.createBuffer();
   1.176 +                var degenInd = new Array(252);
   1.177 +                for (var j = 0; j < 252; ++j) {
   1.178 +                    degenInd[j] = j;
   1.179 +                }
   1.180 +                degenInd = degenInd.concat([252, 253, 254, 255]);
   1.181 +                indices = new Uint8Array(degenInd);
   1.182 +                gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
   1.183 +                gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
   1.184 +
   1.185 +                debug('Draw a square using an ubyte indexed line loop with 256 indices and verify that it draws all four sides and nothing else.');
   1.186 +                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
   1.187 +                gl.drawElements(gl.LINE_LOOP, indices.length, gl.UNSIGNED_BYTE, 0);
   1.188 +                checkLineLoop(gl, w);
   1.189 +
   1.190 +
   1.191 +                //---------- TRIANGLE_FAN ----------
   1.192 +                vertices = new Float32Array([0, 0, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1]);
   1.193 +                vertBuf = gl.createBuffer();
   1.194 +                gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
   1.195 +                gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
   1.196 +                gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
   1.197 +                indices = new Uint16Array([0,1,2,3,4,5]);
   1.198 +                indBuf = gl.createBuffer();
   1.199 +                gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
   1.200 +                gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
   1.201 +
   1.202 +                debug('Draw a filled square using a triangle fan and verify that it fills the entire canvas.');
   1.203 +                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
   1.204 +                gl.drawArrays(gl.TRIANGLE_FAN, 0, vertices.length / 2);
   1.205 +                checkTriFan(gl, w);
   1.206 +
   1.207 +                debug('Draw a filled square using an indexed triangle fan and verify that it fills the entire canvas.');
   1.208 +                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
   1.209 +                gl.drawElements(gl.TRIANGLE_FAN, indices.length, gl.UNSIGNED_SHORT, 0);
   1.210 +                checkTriFan(gl, w);
   1.211 +
   1.212 +                vertices = new Float32Array([1, 1, 1, 1, 1, 1, 0, 0, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1]);
   1.213 +                vertBuf = gl.createBuffer();
   1.214 +                gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
   1.215 +                gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
   1.216 +                gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
   1.217 +                indices = new Uint16Array([0,1,2,3,4,5,6,7,8]);
   1.218 +                indBuf = gl.createBuffer();
   1.219 +                gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
   1.220 +                gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
   1.221 +
   1.222 +                debug('Draw a filled square using a triangle fan with a vertex buffer offset and verify that it fills the entire canvas.');
   1.223 +                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
   1.224 +                gl.drawArrays(gl.TRIANGLE_FAN, 3, vertices.length / 2 - 3);
   1.225 +                checkTriFan(gl, w);
   1.226 +
   1.227 +                debug('Draw a filled square using an indexed triangle fan with an index buffer offset and verify that it fills the entire canvas.');
   1.228 +                gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
   1.229 +                gl.drawElements(gl.TRIANGLE_FAN, indices.length - 3, gl.UNSIGNED_SHORT, 3 * 2);
   1.230 +                checkTriFan(gl, w);
   1.231 +            }
   1.232 +        </script>
   1.233 +    </head>
   1.234 +    <body>
   1.235 +        <canvas id="testbed" width="10px" height="10px" style="width:50px; height:50px"></canvas>
   1.236 +        <div id="description"></div>
   1.237 +        <div id="console"></div>
   1.238 +        <script>
   1.239 +            description('Verify that LINE_LOOP and TRIANGLE_FAN works correctly.');
   1.240 +            runTest();
   1.241 +            successfullyParsed = true;
   1.242 +        </script>
   1.243 +        <script>finishTest();</script>
   1.244 +    </body>
   1.245 +</html>

mercurial