browser/devtools/shadereditor/test/doc_simple-canvas.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!-- Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ -->
michael@0 3 <!doctype html>
michael@0 4
michael@0 5 <html>
michael@0 6 <head>
michael@0 7 <meta charset="utf-8"/>
michael@0 8 <title>WebGL editor test page</title>
michael@0 9
michael@0 10 <script id="shader-vs" type="x-shader/x-vertex">
michael@0 11 precision lowp float;
michael@0 12 attribute vec3 aVertexPosition;
michael@0 13 attribute vec3 aVertexColor;
michael@0 14 varying vec3 vFragmentColor;
michael@0 15
michael@0 16 void main(void) {
michael@0 17 gl_Position = vec4(aVertexPosition, 1.0);
michael@0 18 vFragmentColor = aVertexColor; // I'm special!
michael@0 19 }
michael@0 20 </script>
michael@0 21
michael@0 22 <script id="shader-fs" type="x-shader/x-fragment">
michael@0 23 precision lowp float;
michael@0 24 varying vec3 vFragmentColor;
michael@0 25
michael@0 26 void main(void) {
michael@0 27 gl_FragColor = vec4(vFragmentColor, 1.0); // I'm also special!
michael@0 28 }
michael@0 29 </script>
michael@0 30 </head>
michael@0 31
michael@0 32 <body>
michael@0 33 <canvas width="512" height="512"></canvas>
michael@0 34
michael@0 35 <script type="text/javascript;version=1.8">
michael@0 36 "use strict";
michael@0 37
michael@0 38 let canvas, gl;
michael@0 39 let program;
michael@0 40 let squareVerticesPositionBuffer;
michael@0 41 let squareVerticesColorBuffer;
michael@0 42 let vertexPositionAttribute;
michael@0 43 let vertexColorAttribute;
michael@0 44
michael@0 45 window.onload = function() {
michael@0 46 canvas = document.querySelector("canvas");
michael@0 47 gl = canvas.getContext("webgl", { preserveDrawingBuffer: true });
michael@0 48 gl.clearColor(0.0, 0.0, 0.0, 1.0);
michael@0 49
michael@0 50 initProgram();
michael@0 51 initBuffers();
michael@0 52 drawScene();
michael@0 53 }
michael@0 54
michael@0 55 function initProgram() {
michael@0 56 let vertexShader = getShader(gl, "shader-vs");
michael@0 57 let fragmentShader = getShader(gl, "shader-fs");
michael@0 58
michael@0 59 program = gl.createProgram();
michael@0 60 gl.attachShader(program, vertexShader);
michael@0 61 gl.attachShader(program, fragmentShader);
michael@0 62 gl.linkProgram(program);
michael@0 63
michael@0 64 vertexPositionAttribute = gl.getAttribLocation(program, "aVertexPosition");
michael@0 65 gl.enableVertexAttribArray(vertexPositionAttribute);
michael@0 66
michael@0 67 vertexColorAttribute = gl.getAttribLocation(program, "aVertexColor");
michael@0 68 gl.enableVertexAttribArray(vertexColorAttribute);
michael@0 69 }
michael@0 70
michael@0 71 function getShader(gl, id) {
michael@0 72 let script = document.getElementById(id);
michael@0 73 let source = script.textContent;
michael@0 74 let shader;
michael@0 75
michael@0 76 if (script.type == "x-shader/x-fragment") {
michael@0 77 shader = gl.createShader(gl.FRAGMENT_SHADER);
michael@0 78 } else if (script.type == "x-shader/x-vertex") {
michael@0 79 shader = gl.createShader(gl.VERTEX_SHADER);
michael@0 80 }
michael@0 81
michael@0 82 gl.shaderSource(shader, source);
michael@0 83 gl.compileShader(shader);
michael@0 84
michael@0 85 return shader;
michael@0 86 }
michael@0 87
michael@0 88 function initBuffers() {
michael@0 89 squareVerticesPositionBuffer = gl.createBuffer();
michael@0 90 gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesPositionBuffer);
michael@0 91 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
michael@0 92 1.0, 1.0, 0.0,
michael@0 93 -1.0, 1.0, 0.0,
michael@0 94 1.0, -1.0, 0.0,
michael@0 95 -1.0, -1.0, 0.0
michael@0 96 ]), gl.STATIC_DRAW);
michael@0 97
michael@0 98 squareVerticesColorBuffer = gl.createBuffer();
michael@0 99 gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesColorBuffer);
michael@0 100 gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
michael@0 101 1.0, 1.0, 1.0, 1.0,
michael@0 102 1.0, 0.0, 0.0, 1.0,
michael@0 103 0.0, 1.0, 0.0, 1.0,
michael@0 104 0.0, 0.0, 1.0, 1.0
michael@0 105 ]), gl.STATIC_DRAW);
michael@0 106 }
michael@0 107
michael@0 108 function drawScene() {
michael@0 109 gl.clear(gl.COLOR_BUFFER_BIT);
michael@0 110
michael@0 111 gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesPositionBuffer);
michael@0 112 gl.vertexAttribPointer(vertexPositionAttribute, 3, gl.FLOAT, false, 0, 0);
michael@0 113
michael@0 114 gl.bindBuffer(gl.ARRAY_BUFFER, squareVerticesColorBuffer);
michael@0 115 gl.vertexAttribPointer(vertexColorAttribute, 4, gl.FLOAT, false, 0, 0);
michael@0 116
michael@0 117 gl.useProgram(program);
michael@0 118 gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
michael@0 119
michael@0 120 window.requestAnimationFrame(drawScene);
michael@0 121 }
michael@0 122 </script>
michael@0 123 </body>
michael@0 124
michael@0 125 </html>

mercurial