-
Notifications
You must be signed in to change notification settings - Fork 0
/
Step3.js
70 lines (50 loc) · 1.62 KB
/
Step3.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var canvas;
var gl;
var program;
var points = [];
var indices = [];
var nFaces;
window.onload = function init()
{
initGL();
rectangle();
render();
}
function initGL()
{
canvas = document.getElementById("gl-canvas");
gl = WebGLUtils.setupWebGL(canvas);
if (!gl) { alert("WebGL isn't available"); }
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(1.0, 1.0, 1.0, 1.0);
program = initShaders(gl, "vertex-shader", "fragment-shader");
gl.useProgram(program);
}
function rectangle()
{
points.push(vec3(0., 0., 0.));
points.push(vec3(1., 0., 0.));
points.push(vec3(1., 1., 0.));
points.push(vec3(0., 1., 0.));
indices.push([0, 1, 2]);
indices.push([0, 2, 3]);
nFaces = indices.length;
//Create buffer to store the vertex coordinates
var vBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vBuffer);
gl.bufferData(gl.ARRAY_BUFFER, flatten(points), gl.STATIC_DRAW);
//Link data to vertex shader input
var vPosition = gl.getAttribLocation(program, "vPosition");
gl.vertexAttribPointer(vPosition, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(vPosition);
//Create buffer to store the triangle elements
var tBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, tBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(flatten(indices)), gl.STATIC_DRAW);
}
function render()
{
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawElements(gl.TRIANGLES, nFaces * 3, gl.UNSIGNED_SHORT, 0);
requestAnimationFrame(render);
}