-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShaderProgram.js
37 lines (29 loc) · 1.22 KB
/
ShaderProgram.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 ShaderProgram {
constructor() {
}
// Create shader program using the provided vertex and fragment shader ids
async load(vertexId, fragmentId, shaderInfo) {
this.program = await createShaderProgram(vertexId, fragmentId);
gl.useProgram(this.program);
this.attributes = {}
this.uniforms = {};
// Extract attribute and uniform information from the shaderInfo object
// and look up their locations
Object.entries(shaderInfo.attributes).forEach(([key, value]) => {
this.attributes[key] = gl.getAttribLocation(this.program, value);
})
Object.entries(shaderInfo.uniforms).forEach(([key, value]) => {
this.uniforms[key] = gl.getUniformLocation(this.program, value);
})
// Send projection matrix
// If this changes, you have to send the new matrix to all shader programs again!
gl.uniformMatrix4fv(this.uniforms.projectionMatrix, gl.FALSE, matrices.projectionMatrix);
}
enable() {
currentShaderProgram = this;
gl.useProgram(this.program);
}
update(projectionMatrix) {
gl.uniformMatrix4fv(this.uniforms.projectionMatrix, gl.FALSE, projectionMatrix);
}
}