gecko-dev/servo/tests/html/test_webgl_triangle.html
Imanol Fernandez fa1ea9c973 servo: Merge #13208 - Fix WebGL tests & Implement WebGLRenderingContext::{validateProgram, getProgramInfoLog, disableVertexAttribArray} (from MortimerGoro:programinfolog); r=emilio
<!-- Please describe your changes on the following line: -->
Implement WebGLRenderingContext::{validateProgram, getProgramInfoLog} and improve WebGL testcase to show shader link and validation errors.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix #13199 (github issue number if applicable).

<!-- Either: -->
- [X] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

Source-Repo: https://github.com/servo/servo
Source-Revision: fc251384a76547167c027d69c28deae8c410c626
2016-10-03 16:54:51 -05:00

131 lines
3.4 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>WebGL Triangle Test</title>
</head>
<body>
<div style="text-align: center">
<canvas id="canvas" width="512" height="512"></canvas>
</div>
<script id="vertexshader" type="x-shader">
precision mediump float;
attribute vec2 aVertexPosition;
attribute vec4 aColour;
varying vec4 aVertexColor;
void main() {
aVertexColor = aColour;
gl_Position = vec4(aVertexPosition, 0.0, 1.0);
}
</script>
<script id="fragmentshader" type="x-shader">
precision mediump float;
varying vec4 aVertexColor;
void main() {
gl_FragColor = aVertexColor;
}
</script>
<script type="text/javascript">
(function() {
var canvas;
function initWebGL()
{
canvas = document.getElementById("canvas");
var gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
if (!gl) return null; // can't initialize WebGL
return gl;
}
var gl = initWebGL();
if (!gl) {
alert("No webgl context found!");
return;
}
// Setup Shaders:
var v = document.getElementById("vertexshader").firstChild.nodeValue;
var f = document.getElementById("fragmentshader").firstChild.nodeValue;
console.log("vertex source: ", v);
console.log("fragment source: ", f);
var vs = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vs, v);
gl.compileShader(vs);
if (!gl.getShaderParameter(vs, gl.COMPILE_STATUS)) {
alert("Shader failed to compile. Reason: " + gl.getShaderInfoLog(vs));
return;
}
var fs = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fs, f);
gl.compileShader(fs);
if (!gl.getShaderParameter(fs, gl.COMPILE_STATUS)) {
alert("Shader failed to compile. Reason: " + gl.getShaderInfoLog(fs));
return;
}
program = gl.createProgram();
gl.attachShader(program, vs);
gl.attachShader(program, fs);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
alert("Shader failed to link. Reason: " + gl.getProgramInfoLog(program));
return;
}
gl.validateProgram(program);
if (!gl.getProgramParameter( program, gl.VALIDATE_STATUS)) {
alert("Program failed to validate. Reason: " + gl.getProgramInfoLog(program));
return;
}
// Setup Geometry
var vertices = new Float32Array([
-0.5,-0.5, 0.5,-0.5, 0.0,0.5, // Triangle-Coordinates
1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
]);
vbuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
itemSize = 2; // we have 2 coordinates (x,y)
numItems = vertices.length / itemSize; // number of vertices
// Viewport
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(0, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// Setup Geometry
gl.useProgram(program);
program.aVertexPosition = gl.getAttribLocation(program, "aVertexPosition");
gl.enableVertexAttribArray(program.aVertexPosition);
gl.vertexAttribPointer(program.aVertexPosition, itemSize, gl.FLOAT, false, 0, 0);
program.aColour = gl.getAttribLocation(program, "aColour");
console.log("aColour: " + program.aColour);
gl.enableVertexAttribArray(program.aColour);
gl.vertexAttribPointer(program.aColour, 4, gl.FLOAT, false, 0, 24);
// Draw
gl.drawArrays(gl.TRIANGLES, 0, 3);
})();
</script>
</body>
</html>