Source code

Revision control

Copy as Markdown

Other Tools

<!--
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>