-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcolor-interleaved.js
101 lines (72 loc) · 3.75 KB
/
color-interleaved.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//////////////////////////////////////////////////////////////////
//
// This example shows you how to interleave positions and colors into a single VBO
// Then how do you know specify the stride and offset to pass positions and colors into two
// attributes array
//
// Han-Wei Shen (shen.94@osu.edu)
//
var gl;
var shaderProgram;
function initGL(canvas) {
try {
gl = canvas.getContext("experimental-webgl");
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
} catch (e) {
}
if (!gl) {
alert("Could not initialise WebGL, sorry :-(");
}
}
//f ************** Initialize VBO *************** -->
var squareVertexBuffer;
var vertices = [];
function initBuffers() {
squareVertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexBuffer);
// below is how you interleave positions and colors into a single array and then VBO
vertices.push(-.5); vertices.push(-.5); vertices.push(0.0); // v1 positions
vertices.push(1.0); vertices.push(0.0); vertices.push(0.0); // v1 colors
vertices.push(.5); vertices.push(.5); vertices.push(0.0); // v2 positions
vertices.push(0.0); vertices.push(0.0); vertices.push(1.0); // v2 colors
vertices.push(-.5); vertices.push(.5); vertices.push(0.0); // v3 positions
vertices.push(0.0); vertices.push(0.0); vertices.push(0.0); // v3 colors
vertices.push(-.5); vertices.push(-.5); vertices.push(0.0); // v4 positions
vertices.push(1.0); vertices.push(0.0); vertices.push(0.0); // v4 colors
vertices.push(.5); vertices.push(-.5); vertices.push(0.0); // v5 positions
vertices.push(0.0); vertices.push(1.0); vertices.push(0.0); // v5 colors
vertices.push(.5); vertices.push(.5); vertices.push(0.0); // v6 positions
vertices.push(0.0); vertices.push(0.0); vertices.push(1.0); // v6 colors
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); // use 4 bytes float
squareVertexBuffer.numFloats = 3; // three floats per vertex
squareVertexBuffer.numVertices = 6; // number of vertices in the buffer
}
function drawScene() {
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexBuffer);
var stride = 6 * 4; // 4 bytes per float, 6 floats,
var offset = 0;
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexBuffer.numFloats, gl.FLOAT, false, stride, offset); // 6*4 is the stride size, 0 is the offset
offset = 3*4; // skip the positions, which has 3 floats * 4 bytes/float
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, squareVertexBuffer.numFloats, gl.FLOAT, false,stride, offset); // 6*4 is the stride size, 3*4 is the offset
offset = 0;
gl.drawArrays(gl.TRIANGLES, offset, squareVertexBuffer.numVertices); // draw 2 triangles
}
function webGLStart() {
var canvas = document.getElementById("code02-canvas");
initGL(canvas);
initShaders();
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);
initBuffers();
gl.clearColor(1.0, 1.0, 0.0, 1.0);
drawScene();
}
function BG(red, green, blue) {
gl.clearColor(red, green, blue, 1.0);
drawScene();
}