summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/rendering/line-loop-tri-fan.html
blob: e024157fc9eb7e85536f0be5fd3854bcd674bf28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
<script id="vshader" type="x-shader/x-vertex">
attribute vec2 pos;

void main()
{
    gl_Position = vec4(pos, 0, 1);
}
</script>

<script id="fshader" type="x-shader/x-fragment">
precision mediump float;

void main()
{
    gl_FragColor = vec4(0, 1, 0, 1);
}
</script>

<script>
"use strict";
var wtu = WebGLTestUtils;

// Check a single 32-bit RGBA pixel.
function checkPixel(buf, index, correct) {
    for (var i = 0; i < 4; ++i) {
        if (buf[index + i] != correct[i]) {
            return false;
        }
    }
    return true;
}

// Check the line loop by reading the pixels and making sure just the edge
// pixels are green and the rest are black.
function checkLineLoop(gl, w) {
    var buf = new Uint8Array(w * w * 4);
    gl.readPixels(0, 0, w, w, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    var green = [0,255,0,255];
    var black = [0,0,0,255];
    var isCorrect = true;
    for (var j = 0; j < w * w * 4; j += 4) {
        var correct = black;
        if (j < w * 4 || j > w * (w - 1) * 4 || j % (w * 4) == 0 || j % (w * 4) == (w - 1) * 4) {
            correct = green;
        }
        // ignore corner pixels
        if ((j == 0) || (j == 4*(w-1)) || (j == 4*w*(w-1)) || (j== 4*(w*w - 1))) {
            continue;
        }
        if (!checkPixel(buf, j, correct)) {
            isCorrect = false;
            break;
        }
    }
    if (isCorrect) {
        testPassed("Line loop was drawn correctly.");
    } else {
        testFailed("Line loop was drawn incorrectly.");
    }
}

// Check the tri fan by reading the pixels and making sure they are all green.
function checkTriFan(gl, w) {
    var buf = new Uint8Array(w * w * 4);
    gl.readPixels(0, 0, w, w, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    var filled = true;
    for (var j = 0; j < w * w * 4; j += 4) {
        if (!checkPixel(buf, j, [0,255,0,255])) {
            filled = false;
            break;
        }
    }
    if (filled) {
        testPassed("Triangle fan was drawn correctly.");
    } else {
        testFailed("Triangle fan was drawn incorrectly.");
    }
}

function runTest() {
    var gl = wtu.create3DContext('testbed', { antialias: false });
    if (!gl) {
        testFailed('could not create context');
        return;
    }
    gl.clearColor(0, 0, 0, 1);
    var program = wtu.setupProgram(gl, ['vshader', 'fshader'], ['pos'])
    var w = document.getElementById('testbed').width;

    gl.enableVertexAttribArray(0);

    //---------- LINE_LOOP----------
    var d = 1/w;
    var vertices = new Float32Array([-1+d, -1+d, 1-d, -1+d, 1-d, 1-d, -1+d, 1-d]);
    var vertBuf = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
    gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
    var indBuf = gl.createBuffer();
    var indices = new Uint16Array([0, 1, 2, 3]);
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);

    debug('Draw a square using a line loop and verify that it draws all four sides and nothing else.');
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.drawArrays(gl.LINE_LOOP, 0, vertices.length / 2);
    checkLineLoop(gl, w);

    debug('Draw a square using an indexed line loop and verify that it draws all four sides and nothing else.');
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.drawElements(gl.LINE_LOOP, indices.length, gl.UNSIGNED_SHORT, 0);
    checkLineLoop(gl, w);

    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]);
    vertBuf = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
    gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
    indBuf = gl.createBuffer();
    indices = new Uint16Array([0, 1, 2, 3, 4, 5, 6]);
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);

    debug('Draw a square using a line loop with a vertex buffer offset and verify that it draws all four sides and nothing else.');
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.drawArrays(gl.LINE_LOOP, 3, vertices.length / 2 - 3);
    checkLineLoop(gl, w);

    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.');
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.drawElements(gl.LINE_LOOP, indices.length - 3, gl.UNSIGNED_SHORT, 3 * 2);
    checkLineLoop(gl, w);

    //---------- LINE_LOOP UBYTE ----------
    var degenVerts = new Array(252 * 2);
    for (var j = 0; j < 252 * 2; ++j) {
        degenVerts[j] = -1+d;
    }
    degenVerts = degenVerts.concat([-1+d, -1+d, 1-d, -1+d, 1-d, 1-d, -1+d, 1-d]);
    vertices = new Float32Array(degenVerts);
    vertBuf = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
    gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
    indBuf = gl.createBuffer();
    var degenInd = new Array(252);
    for (var j = 0; j < 252; ++j) {
        degenInd[j] = j;
    }
    degenInd = degenInd.concat([252, 253, 254, 255]);
    indices = new Uint16Array(degenInd);
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);

    debug('Draw a square using an ubyte indexed line loop with 256 indices and verify that it draws all four sides and nothing else.');
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.drawElements(gl.LINE_LOOP, indices.length, gl.UNSIGNED_SHORT, 0);
    checkLineLoop(gl, w);

    //---------- TRIANGLE_FAN ----------
    vertices = new Float32Array([0, 0, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1]);
    vertBuf = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
    gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
    indices = new Uint16Array([0,1,2,3,4,5]);
    indBuf = gl.createBuffer();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);

    debug('Draw a filled square using a triangle fan and verify that it fills the entire canvas.');
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.drawArrays(gl.TRIANGLE_FAN, 0, vertices.length / 2);
    checkTriFan(gl, w);

    debug('Draw a filled square using an indexed triangle fan and verify that it fills the entire canvas.');
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.drawElements(gl.TRIANGLE_FAN, indices.length, gl.UNSIGNED_SHORT, 0);
    checkTriFan(gl, w);

    vertices = new Float32Array([1, 1, 1, 1, 1, 1, 0, 0, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1]);
    vertBuf = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vertBuf);
    gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
    gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
    indices = new Uint16Array([0,1,2,3,4,5,6,7,8]);
    indBuf = gl.createBuffer();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indBuf);
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);

    debug('Draw a filled square using a triangle fan with a vertex buffer offset and verify that it fills the entire canvas.');
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.drawArrays(gl.TRIANGLE_FAN, 3, vertices.length / 2 - 3);
    checkTriFan(gl, w);

    debug('Draw a filled square using an indexed triangle fan with an index buffer offset and verify that it fills the entire canvas.');
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    gl.drawElements(gl.TRIANGLE_FAN, indices.length - 3, gl.UNSIGNED_SHORT, 3 * 2);
    checkTriFan(gl, w);
}
</script>
</head>
<body>
<canvas id="testbed" width="10" height="10" style="width:50px; height:50px"></canvas>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description('Verify that LINE_LOOP and TRIANGLE_FAN works correctly.');
runTest();
var successfullyParsed = true;
</script>
<script src="../../js/js-test-post.js"></script>

</body>
</html>