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