Matrix4
The time has come to implement our unified transformation machine: Matrix4
. Create the file lib/matrix.ts
and populate it with the code from the following exercises.
Test that you can transform a model with a matrix by completing the following steps:
Define a new translation, scale, or rotation matrix in
render
near where you call bind
on the shader program.
Once the shader program is bound, upload the matrix as a uniform with this code:
shaderProgram.setUniformMatrix4fv("transform", matrix.elements);
shaderProgram.setUniformMatrix4fv("transform", matrix.elements);
Modify the vertex shader to receive and apply this matrix:
uniform mat4 transform;
// ...
void main() {
gl_Position = transform * vec4(position, 1.0);
// ...
}
uniform mat4 transform; // ... void main() { gl_Position = transform * vec4(position, 1.0); // ... }
This class will play a major role as we arrange scenes, shade surfaces, and form a camera system. While we're at it, let's add one more transformation machine to our repertoire.