-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleWebGL01.html
executable file
·51 lines (47 loc) · 1.65 KB
/
simpleWebGL01.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!DOCTYPE html>
<meta charset="utf-8">
<script src="js/webgl.js"></script>
<style>
body {width: 1000px;}
</style>
<body>
<div id="chart"></div>
</body>
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
void main(void) {
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
void main(void) {
gl_Position = vec4(aVertexPosition, 1.0);
}
</script>
<script>
window.onload = function(){
var width = 500, height = 500, nearFrustum = 0.1, farFrustum = 100;
// intialise webGL
var gl = gl_Context( document.getElementById("chart"), width, height);
if( gl.failed ) {
console.log("Could not initialise WebGL, sorry :-(");
return;
};
// shaderProgram is a kind of job order
var shaderProgram = new gl.GlslProg("shader-vs", "shader-fs");
// vertices is series of triplets(x,y,z)
var vertices = new Float32Array( [-0.5,-0.5,0.0, 0.5,-0.5,0.0, 0.0,0.5,0.0] );
// reserve memory in GPU for vertices, telling these are triplets by first 3, number of triplets by second 3.
var triangleVertexBuffer = new gl.Buffer( vertices, 3, 3);
gl.clearColor(0.0, 0.0, 0.0, 1.0); // draw the canvas in black
gl.enable(gl.DEPTH_TEST); // tell GPU to check z-value
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight); // size of canvas
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); // refresh your brain
shaderProgram.bind(); // tell GPU to follow this job order
// send triangleVertexBuffer to GPU
shaderProgram.setBuffer("aVertexPosition", triangleVertexBuffer);
// order to draw it
triangleVertexBuffer.drawArrays( GL_TRIANGLES );
};
</script>