In modern OpenGL, you define any matrix you want to use as part of the shader program you load. Get used to the idea that you will write shader code. It is scary, I know. It is not that bad, I promise.
In particular, you will define them as "uniforms", which are data that will be available to the shader (but not associated to a vertex). You could pass other data as uniforms aside, including the inverse of the matrices for those cases where you need it...
For instance, some of my shader code starts like this:
#version 330 core
uniform vec2 resolution;
uniform float time;
uniform mat4 projection;
uniform mat4 inverseProjection;
uniform mat4 view;
uniform mat4 inverseView;
Here I am passing the size of the screen as a
vec2 resolution, the current
time as a
float for animating in the GPU, and then some
mat4, each one a transformation matrix.
So, how do we specify which of these uniforms we are setting?
We, will call
glGetUniformLocation, with the
id of our shader program (which you get from
glCreateProgram as part of the process of setting your shaders) and the string name of the uniform you want.
Afterwards, you call a
glUniform function, according to the datatype you want, passing the location you got from
Since we will setting a
mat4, we will call
glUniformMatrix4fv. You will find that you also need to specify a
bool if you want to transpose the matrix when setting it (just in case you are using row major order, but OpenGL wants column major order).
// Get the location of the matrix you want to set
int modelLocation = glGetUniformLocation(shader_id, "model");
// Set it
glUniformMatrix4fv(modelLocation, 1, false, model);
// ^ ^ ^ ^
// | | | |
// | | | pointer to the start of the matrix
// | | no, do not transpose my matrix
// | only take one matrix from that pointer, thank you
// the location on the shader program where we are setting...
// ... that identifies what uniform we are setting
Oh, right, you can have an array of
mat4 as a
uniform, for example:
// somewhere in the shader header
uniform mat4 bones;
Then set it in your code:
int bonesLocation = glGetUniformLocation(shader_id, "bones");
glUniformMatrix4fv(bonesLocation, 10, false, bones);
// Take 10 mat4
So, in a sense
glGetUniformLocation is the replacement for
glMatrixMode. Please tell me are not using
Finally, of course, if you want to mimic the old behavior, the matrix multiplication order would be
projection * view * model * vector somewhere on your vertex shader.
Know that by writing your own shaders, you can go crazy...
I am currently writing a ray caster, and I have code like this:
gl_Position = vec4(position.xy, 0.0, 1.0);
cameraRayDirection = ((inverseView * inverseProjection) * gl_Position).xyz;
cameraRayPosition = (inverseView * vec4(0.0, 0.0, 0.0, 1.0)).xyz;