I’m learning OpenGL, and am having trouble passing uniform matrices to a vertex shader. I was loosely following the https://learnopengl.com/ tutorial.
Shader Code:
#version 430 core
layout (location = 0) in vec3 position;
layout (location = 1) uniform mat4 model;
layout (location = 2) uniform mat4 view;
layout (location = 3) uniform mat4 perspective;
void main() {
gl_Position = perspective * view * model * vec4(position, 1.0);
//gl_Position = mat4(1.0) * vec4(position, 1.0);
//gl_Position = vec4(position,1.0f);
}
C++ Code
glm::mat4 viewMatrix,projectionMatrix,modelMatrix;
viewMatrix = glm::mat4(1.0f);
projectionMatrix = glm::mat4(1.0f);
modelMatrix = glm::mat4(1.0f);
std::cout << glm::to_string(projectionMatrix*viewMatrix*modelMatrix) << std::endl;
glUniformMatrix4fv(glGetUniformLocation(shaderProgram,"view"),1,GL_FALSE,&viewMatrix[0][0]);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram,"projection"),1,GL_FALSE,&projectionMatrix[0][0]);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram,"model"),1,GL_FALSE,&modelMatrix[0][0]);
std::cout << glGetError() << std::endl;
while (window.isOpen()) {
sf::Event event{};
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
} else if (event.type == sf::Event::Resized) {
glViewport(0, 0, static_cast<int>(event.size.width), static_cast<int>(event.size.height));
}
}
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(VAO);
glDrawArrays(GL_QUADS,0,4);
window.display();
}
When running the code, I get two different outcomes for the three different lines of code in the shader’s main() function:
Output when using either of the lines that are commented out
Output when using the uncommented matrix multiplication line
This leads me to believe that the issue is with passing the matrices into the shader.
Though I’m not entirely sure where specifically.
Running glGetError() returns 0, so I am at a loss.
Any help would be appreciated.
PossibleMaybe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.