mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-07 20:17:37 +00:00
126 lines
3.6 KiB
HTML
126 lines
3.6 KiB
HTML
<!-- Any copyright is dedicated to the Public Domain.
|
|
http://creativecommons.org/publicdomain/zero/1.0/ -->
|
|
<!doctype html>
|
|
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8"/>
|
|
<title>WebGL editor test page</title>
|
|
|
|
<script id="shader-vs" type="x-shader/x-vertex">
|
|
precision lowp float;
|
|
attribute vec3 aVertexPosition;
|
|
attribute vec3 aVertexColor;
|
|
varying vec3 vFragmentColor;
|
|
|
|
void main(void) {
|
|
gl_Position = vec4(aVertexPosition, 1.0);
|
|
vFragmentColor = aVertexColor; // I'm special!
|
|
}
|
|
</script>
|
|
|
|
<script id="shader-fs" type="x-shader/x-fragment">
|
|
precision lowp float;
|
|
varying vec3 vFragmentColor;
|
|
|
|
void main(void) {
|
|
gl_FragColor = vec4(vFragmentColor, 1.0); // I'm also special!
|
|
}
|
|
</script>
|
|
</head>
|
|
|
|
<body>
|
|
<canvas width="512" height="512"></canvas>
|
|
|
|
<script type="text/javascript;version=1.8">
|
|
"use strict";
|
|
|
|
let canvas, gl;
|
|
let program;
|
|
let squareVerticesPositionBuffer;
|
|
let squareVerticesColorBuffer;
|
|
let vertexPositionAttribute;
|
|
let vertexColorAttribute;
|
|
|
|
window.onload = function() {
|
|
canvas = document.querySelector("canvas");
|
|
gl = canvas.getContext("webgl", { preserveDrawingBuffer: true });
|
|
gl.clearColor(0.0, 0.0, 0.0, 1.0);
|
|
|
|
initProgram();
|
|
initBuffers();
|
|
drawScene();
|
|
}
|
|
|
|
function initProgram() {
|
|
let vertexShader = getShader(gl, "shader-vs");
|
|
let fragmentShader = getShader(gl, "shader-fs");
|
|
|
|
program = gl.createProgram();
|
|
gl.attachShader(program, vertexShader);
|
|
gl.attachShader(program, fragmentShader);
|
|
gl.linkProgram(program);
|
|
|
|
vertexPositionAttribute = gl.getAttribLocation(program, "aVertexPosition");
|
|
gl.enableVertexAttribArray(vertexPositionAttribute);
|
|
|
|
vertexColorAttribute = gl.getAttribLocation(program, "aVertexColor");
|
|
gl.enableVertexAttribArray(vertexColorAttribute);
|
|
}
|
|
|
|
function getShader(gl, id) {
|
|
let script = document.getElementById(id);
|
|
let source = script.textContent;
|
|
let shader;
|
|
|
|
if (script.type == "x-shader/x-fragment") {
|
|
shader = gl.createShader(gl.FRAGMENT_SHADER);
|
|
} else if (script.type == "x-shader/x-vertex") {
|
|
shader = gl.createShader(gl.VERTEX_SHADER);
|
|
}
|
|
|
|
gl.shaderSource(shader, source);
|
|
gl.compileShader(shader);
|
|
|
|
return shader;
|
|
}
|
|
|
|
function initBuffers() {
|
|
squareVerticesPositionBuffer = gl.createBuffer();
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesPositionBuffer);
|
|
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
|
|
1.0, 1.0, 0.0,
|
|
-1.0, 1.0, 0.0,
|
|
1.0, -1.0, 0.0,
|
|
-1.0, -1.0, 0.0
|
|
]), gl.STATIC_DRAW);
|
|
|
|
squareVerticesColorBuffer = gl.createBuffer();
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesColorBuffer);
|
|
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
|
|
1.0, 1.0, 1.0, 1.0,
|
|
1.0, 0.0, 0.0, 1.0,
|
|
0.0, 1.0, 0.0, 1.0,
|
|
0.0, 0.0, 1.0, 1.0
|
|
]), gl.STATIC_DRAW);
|
|
}
|
|
|
|
function drawScene() {
|
|
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesPositionBuffer);
|
|
gl.vertexAttribPointer(vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0);
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesColorBuffer);
|
|
gl.vertexAttribPointer(vertexColorAttribute, 4, gl.FLOAT, false, 0, 0);
|
|
|
|
gl.useProgram(program);
|
|
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
|
|
window.requestAnimationFrame(drawScene);
|
|
}
|
|
</script>
|
|
</body>
|
|
|
|
</html>
|