If you want to translate the following code:
shader.setMat4("model", model);
shader.setMat4("view", view);
shader.setMat4("projection", projection);
into using explicit OpenGL functions, you need to follow a similar process for each matrix (model, view, and projection).
Here's the step-by-step code for setting the "model", "view", and "projection" matrices:
- Get the uniform location for each matrix using
glGetUniformLocation.
- Set each matrix using
glUniformMatrix4fv.
Here's what the complete code would look like:
// Assumes you have a valid shader program ID and it is active
GLuint program = shaderProgramID; // Replace with your actual shader program ID
glm::mat4 model = glm::mat4(1.0f); // Replace this with your actual model matrix
glm::mat4 view = glm::mat4(1.0f); // Replace this with your actual view matrix
glm::mat4 projection = glm::mat4(1.0f); // Replace this with your actual projection matrix
// Make sure the shader program is active
glUseProgram(program);
// Get the location of the uniform variables in the shader
GLint modelMatrixLoc = glGetUniformLocation(program, "model");
GLint viewMatrixLoc = glGetUniformLocation(program, "view");
GLint projectionMatrixLoc = glGetUniformLocation(program, "projection");
// Set each uniform matrix
glUniformMatrix4fv(modelMatrixLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewMatrixLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projectionMatrixLoc, 1, GL_FALSE, glm::value_ptr(projection));
Breaking it down:
shaderProgramID is the ID of the compiled and linked shader program.
model, view, and projection are your 4x4 transformation matrices.
glUseProgram(program) activates the shader program.
glGetUniformLocation retrieves the location of the uniform variables named "model", "view", and "projection".
glUniformMatrix4fv uploads the matrices to the shader. The parameters are:
- The location of the uniform variable.
- The number of matrices to send (always 1 in this context).
GL_FALSE to specify that the matrix should not be transposed.
- A pointer to the matrix data, which can be obtained using
glm::value_ptr.
Make sure you include the necessary headers and use glm for matrix operations:
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
This explicit method ensures that each uniform variable in your shader program receives the correct matrix data, achieving the same effect as using the shader.setMat4 method if the shader object encapsulates these OpenGL calls internally.