-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrendering-engine.js
120 lines (89 loc) · 4.27 KB
/
rendering-engine.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
class RenderingEngine {
constructor(canvasID) {
this._gl = RenderUtils.initializeCanvas(canvasID);
this._gl.enable(this._gl.DEPTH_TEST);
var vertexShaderSource = document.getElementById("vertex-shader").text;
var fragmentShaderSource = document.getElementById("fragment-shader").text;
var vertexShader = RenderUtils.createShader(this._gl, this._gl.VERTEX_SHADER, vertexShaderSource);
var fragmentShader = RenderUtils.createShader(this._gl, this._gl.FRAGMENT_SHADER, fragmentShaderSource);
this._program = RenderUtils.createProgram(this._gl, vertexShader, fragmentShader);
this._root = new GameObject();
this.getShaderVariableLocations();
}
getShaderVariableLocations() {
this._a_position = this._gl.getAttribLocation(this._program, "a_position");
this._u_modelMatrix = this._gl.getUniformLocation(this._program, "u_modelMatrix");
this._u_viewProjectionMatrix = this._gl.getUniformLocation(this._program, "u_viewProjectionMatrix");
this._u_color = this._gl.getUniformLocation(this._program, "u_color");
this._u_lampLightPosition = this._gl.getUniformLocation(this._program, "u_lampLightPosiiton");
this._a_normal = this._gl.getAttribLocation(this._program, "a_normal");
}
setUniforms(meshRenderer) {
this._gl.uniform3f(this._u_color, meshRenderer.color[0], meshRenderer.color[1], meshRenderer.color[2]);
var modelMatrix = meshRenderer.gameObject.transform.transformation;
var viewProjectionMatrix = this._camera._components[0].viewProjection;
this._gl.uniformMatrix4fv(this._u_modelMatrix, false, modelMatrix);
this._gl.uniformMatrix4fv(this._u_viewProjectionMatrix, false, viewProjectionMatrix);
var lampLightPosition = this._centerLight.transform.transformedPos;
// console.log(lampLightPosition);
this._gl.uniform3f(this._u_lampLightPosition, lampLightPosition[0], lampLightPosition[1], lampLightPosition[2]);
}
setAttributes(meshRenderer) {
var size = 3;
var type = this._gl.FLOAT;
var normalize = false;
var stride = 0;
var offset = 0;
// vertex buffer
var positionBuffer = this._gl.createBuffer();
this._gl.bindBuffer(this._gl.ARRAY_BUFFER, positionBuffer);
this._gl.bufferData(this._gl.ARRAY_BUFFER, meshRenderer.mesh.vertexPositions, this._gl.STATIC_DRAW);
this._gl.vertexAttribPointer(this._a_position, size, type, normalize, stride, offset);
this._gl.enableVertexAttribArray(this._a_position);
// normal buffer
var normalBuffer = this._gl.createBuffer();
this._gl.bindBuffer(this._gl.ARRAY_BUFFER, normalBuffer);
this._gl.bufferData(this._gl.ARRAY_BUFFER, meshRenderer.mesh.vertexNormals, this._gl.STATIC_DRAW);
this._gl.vertexAttribPointer(this._a_normal, size, type, normalize, stride, offset);
this._gl.enableVertexAttribArray(this._a_normal);
// index buffer
var indexBuffer = this._gl.createBuffer();
this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
this._gl.bufferData(this._gl.ELEMENT_ARRAY_BUFFER, meshRenderer.mesh.indices, this._gl.STATIC_DRAW);
this._gl.bindBuffer(this._gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
}
addCamera(cameraGameObject) {
this._camera = cameraGameObject;
}
addToSceneGraph(gameObject) {
this._root.addChild(gameObject);
}
render() {
this._gl.useProgram(this._program);
RenderUtils.clearCanvas(this._gl);
this._root.renderAll(this);
}
draw(meshRenderer) {
var primitiveType = this._gl.TRIANGLES;
var offset = 0;
var count = meshRenderer.mesh.indices.length;
var indexType = this._gl.UNSIGNED_SHORT;
this._gl.drawElements(primitiveType, count, indexType, offset);
}
update() {
this._root.updateAll();
}
// Not best practice, but this should work
addSunLight(gameObject) {
this._sunLight = gameObject;
}
addLeftHeadLight(gameObject) {
this._leftHeadLight = gameObject;
}
addRightHeadLight(gameObject) {
this._rightHeadLight = gameObject;
}
addCenterLight(gameObject) {
this._centerLight = gameObject;
}
}