-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.js
More file actions
67 lines (47 loc) · 2.41 KB
/
object.js
File metadata and controls
67 lines (47 loc) · 2.41 KB
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
function Object() {
this.cubeVertexPositionBuffer = 0;
this.cubeVertexTextureCoordBuffer = 0;
this.cubeVertexIndexBuffer = 0;
this.xRot = 0;
this.yRot = 0;
this.zRot = 0;
this.posX = 0;
this.posY = 0;
this.posZ = 0;
this.scale = 1.0;
this.texture = 0;
}
Object.prototype.initBuffers = function (vertices, textureCoords, vertexIndices) {
this.cubeVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.cubeVertexPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
this.cubeVertexPositionBuffer.itemSize = 3;
this.cubeVertexPositionBuffer.numItems = vertices.length;
this.cubeVertexTextureCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.cubeVertexTextureCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);
this.cubeVertexTextureCoordBuffer.itemSize = 2;
this.cubeVertexTextureCoordBuffer.numItems = length;
this.cubeVertexIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.cubeVertexIndexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(vertexIndices), gl.STATIC_DRAW);
this.cubeVertexIndexBuffer.itemSize = 1;
this.cubeVertexIndexBuffer.numItems = vertexIndices.length;
}
Object.prototype.Draw = function(matrix) {
mat4.translate(matrix, matrix, [this.posX, this.posY, this.posZ]);
mat4.rotate(matrix, matrix, degToRad(this.xRot), [1, 0, 0]);
mat4.rotate(matrix, matrix, degToRad(this.yRot), [0, 1, 0]);
mat4.rotate(matrix, matrix, degToRad(this.zRot), [0, 0, 1]);
mat4.scale(matrix, matrix, [this.scale, this.scale, this.scale]);
gl.bindBuffer(gl.ARRAY_BUFFER, this.cubeVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, this.cubeVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, this.cubeVertexTextureCoordBuffer);
gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, this.cubeVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, this.texture.texture);
gl.uniform1i(shaderProgram.samplerUniform, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.cubeVertexIndexBuffer);
setMatrixUniforms(matrix);
gl.drawElements(gl.TRIANGLES, this.cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
}