In modern OpenGL, you define any matrix that you want to use as part of the shader program that you load. Get used to the idea of writing shader code. It's scary, I know. This is not so bad, I promise.
In particular, you will define them as "uniforms", which are data that will be available for the shader (but will not be associated with a vertex). You can transmit other data in the form of uniforms, including reverse matrices for those cases where you need them …
For example, some of my shader codes start like this:
#version 330 core
uniform resolution vec2;
uniform floating time;
uniform mat4 projection;
uniform inverse projection mat4;
uniform view mat4;
uniform mat4 inverseView;
Here I switch the screen size as
resolution vec2, the flow
time like a
fleet for animation in the GPU and then some
mat4, each a transformation matrix.
So, how do we specify which of these uniforms we put in place?
We will call
glGetUniformLocation, with the
login of our shader program (which you receive from
glCreateProgram in the process of setting up your shaders) and the name of the chain of the uniform you want.
Then you call a
glUniforme function, depending on the type of data you want, passing the location you got
Since we are going to put a
mat4we will call
glUniformMatrix4fv. You will find that you must also specify a
bool if you want to transpose the matrix during its setting (just in case you use a major order of lines, but OpenGL wants a major order of columns).
// Get the location of the matrix you want to define
int modelLocation = glGetUniformLocation (shader_id, "model");
glUniformMatrix4fv (modelLocation, 1, false, model)
// ^ ^ ^^
// | | | |
// | | | pointer to the beginning of the matrix
// | | no, do not transpose my matrix
// | just take a matrix of this pointer, thanks
// the location of the shader program where we are placing ...
// ... which identifies which uniform we are putting in place
Oh, okay, you can have a chart of
mat4 like a
uniform, for example:
// somewhere in the header of the shader
mat4 bones uniform;
Then put it in your code:
int bonesLocation = glGetUniformLocation (shader_id, "bones");
glUniformMatrix4fv (bonesLocation, 10, fake, bones)
// take 10 mat4
So, in a sense
glGetUniformLocation is the replacement of
glMatrixMode. Please tell me do not use
Finally, of course, if you want to mimic the old behavior, the multiplication order of the matrix will be
projection * view * pattern * vector somewhere on your vertex shader.
Know that by writing your own shaders, you can go crazy …
I am writing a launcher and I have a 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)). X Y Z;