-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model.js
37 lines (30 loc) · 1.06 KB
/
Model.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
class Model {
constructor() {
this.positionMatrix = new Matrix4();
this.scaleMatrix = new Matrix4();
this.rotationMatrix = new Matrix4();
this.vertices = [];
this.textureNum = -1;
this.textureWeight = 1;
this.hasLighting = 1;
}
render() {
let modelMatrix = new Matrix4();
modelMatrix.set(this.positionMatrix);
modelMatrix.multiply(this.scaleMatrix);
modelMatrix.multiply(this.rotationMatrix);
// Put model into buffer
gl.bufferData(gl.ARRAY_BUFFER, this.vertices, gl.STATIC_DRAW);
// Computer normal matrix
let normalMatrix = new Matrix4();
normalMatrix.setInverseOf(modelMatrix);
normalMatrix.transpose();
// Set all uniforms
gl.uniformMatrix4fv(u_NormalMatrix, false, normalMatrix.elements);
gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements);
gl.uniform1f(u_TextureWeight, this.textureWeight);
gl.uniform1i(u_TextureNum, this.textureNum);
gl.uniform1i(u_LightingOn, this.hasLighting);
gl.drawArrays(gl.TRIANGLES, 0, this.vertices.length / g_dataPerVertex);
}
}