123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <!-- 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>
- </head>
- <body>
- <canvas id="canvas" width="128" height="128"></canvas>
- <script id="shader-fs" type="x-shader/x-fragment">
- precision mediump float;
- uniform vec4 mtrColor;
- void main(void) {
- gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0) * mtrColor;
- }
- </script>
- <script id="shader-vs" type="x-shader/x-vertex">
- attribute vec3 aVertexPosition;
- void main(void) {
- gl_PointSize = 5.0;
- gl_Position = vec4(aVertexPosition, 1.0);
- }
- </script>
- <script type="text/javascript;version=1.8">
- "use strict";
- let canvas, gl, shaderProgram;
- let triangleVertexPositionBuffer, squareVertexPositionBuffer;
- let triangleIndexBuffer;
- let squareIndexBuffer, squareStripIndexBuffer, squareFanIndexBuffer
-
- window.onload = function() {
- canvas = document.querySelector("canvas");
- gl = canvas.getContext("webgl", { preserveDrawingBuffer: true });
- gl.viewportWidth = canvas.width;
- gl.viewportHeight = canvas.height;
-
- initShaders();
- initBuffers();
- gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
- gl.clearColor(0.0, 0.0, 0.0, 1.0);
- gl.disable(gl.DEPTH_TEST);
- drawScene();
- }
- function getShader(gl, id) {
- var shaderScript = document.getElementById(id);
- if (!shaderScript) {
- return null;
- }
- var str = "";
- var k = shaderScript.firstChild;
- while (k) {
- if (k.nodeType == 3) {
- str += k.textContent;
- }
- k = k.nextSibling;
- }
- var shader;
- if (shaderScript.type == "x-shader/x-fragment") {
- shader = gl.createShader(gl.FRAGMENT_SHADER);
- } else if (shaderScript.type == "x-shader/x-vertex") {
- shader = gl.createShader(gl.VERTEX_SHADER);
- } else {
- return null;
- }
- gl.shaderSource(shader, str);
- gl.compileShader(shader);
- if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
- alert(gl.getShaderInfoLog(shader));
- return null;
- }
- return shader;
- }
-
- function initShaders() {
- var fragmentShader = getShader(gl, "shader-fs");
- var vertexShader = getShader(gl, "shader-vs");
- shaderProgram = gl.createProgram();
- gl.attachShader(shaderProgram, vertexShader);
- gl.attachShader(shaderProgram, fragmentShader);
- gl.linkProgram(shaderProgram);
- if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
- alert("Could not initialise shaders");
- }
- gl.useProgram(shaderProgram);
- shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
- shaderProgram.pMaterialColor = gl.getUniformLocation(shaderProgram, "mtrColor");
- gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
- }
- function initBuffers() {
- // Create triangle vertex/index buffer
- triangleVertexPositionBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
- var vertices = [
- 0.0, 0.5, 0.0,
- -0.5, -0.5, 0.0,
- 0.5, -0.5, 0.0
- ];
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
- triangleVertexPositionBuffer.itemSize = 3;
- triangleVertexPositionBuffer.numItems = 3;
- triangleIndexBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleIndexBuffer);
- var indices = [
- 0, 1, 2
- ];
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
- triangleIndexBuffer.itemSize = 1;
- triangleIndexBuffer.numItems = 3;
- // Create square vertex/index buffer
- squareVertexPositionBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
- vertices = [
- 0.8, 0.8, 0.0,
- -0.8, 0.8, 0.0,
- 0.8, -0.8, 0.0,
- -0.8, -0.8, 0.0
- ];
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
- squareVertexPositionBuffer.itemSize = 3;
- squareVertexPositionBuffer.numItems = 4;
- squareIndexBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareIndexBuffer);
- indices = [
- 0, 1, 2,
- 1, 3, 2
- ];
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
- squareIndexBuffer.itemSize = 1;
- squareIndexBuffer.numItems = 6;
- squareStripIndexBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer);
- indices = [
- 0, 1, 2, 3
- ];
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
- squareStripIndexBuffer.itemSize = 1;
- squareStripIndexBuffer.numItems = 4;
- }
- function drawScene() {
- gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
- // DrawElements
- // --------------
- // draw triangle
- gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
- gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
- gl.uniform4f(shaderProgram.pMaterialColor, 1, 1, 1, 1);
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareIndexBuffer);
- gl.drawElements(gl.TRIANGLES, squareIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
- // draw square - triangle strip
- gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
- gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
- gl.uniform4f(shaderProgram.pMaterialColor, 0, 1, 0, 1);
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer);
- gl.drawElements(gl.TRIANGLE_FAN, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
- // draw square - triangle fan
- gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
- gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
- gl.uniform4f(shaderProgram.pMaterialColor, 1, 0, 0, 1);
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleIndexBuffer);
- gl.drawElements(gl.TRIANGLE_FAN, triangleIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
- // draw points
- gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
- gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
- gl.uniform4f(shaderProgram.pMaterialColor, 1, 0, 1, 1);
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer);
- gl.drawElements(gl.POINTS, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
- // draw lines
- gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
- gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
- gl.uniform4f(shaderProgram.pMaterialColor, 0, 0, 1, 1);
- gl.lineWidth(8.0);
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer);
- gl.drawElements(gl.LINES, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
- // draw line strip
- gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
- gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
- gl.uniform4f(shaderProgram.pMaterialColor, 0.9, 0.6, 0, 1);
- gl.lineWidth(3.0);
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, squareStripIndexBuffer);
- gl.drawElements(gl.LINE_STRIP, squareStripIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
- // draw line loop
- gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
- gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
- gl.uniform4f(shaderProgram.pMaterialColor, 0, 1, 1, 1);
- gl.lineWidth(3.0);
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, triangleIndexBuffer);
- gl.drawElements(gl.LINE_LOOP, triangleIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
- window.requestAnimationFrame(drawScene);
- }
- </script>
- </body>
- </html>
|