4
$\begingroup$

I try to write a simple triangle mesh renderer with OpenGL.

void ShaderProgram::SetUniformMatrix4(std::string name, glm::mat4 matrix)
{
  GLint location = glGetUniformLocation(program_, name.c_str());

  if (location != -1)
  {
     glUniformMatrix4fv(location, 1, GL_FALSE, &matrix[0][0]);
  }
}

Is my uniform related code. I made sure that glUseProgram(program_) is called before this method is executed.
Also the glm::mat4 does indeed contain the correct values and program_ has a value of 3 so it seems valid.
The location comes out as a 0

Here how I call that method:

shader_->BindProgram();
shader_->SetUniformMatrix4("matModel", transformation);

texture_->BindTexture();
mesh_->Draw();
texture_->FreeTexture();

shader_->FreeProgram();

Now I have following simple Vertex Shader

#version 330 core

layout (location = 0) in vec3 vertexPosition_modelspace;
layout (location = 1) in vec3 vertexNormal_modelspace;
layout (location = 2) in vec2 textureCoords_modelspace;

uniform mat4 matModel;

layout(std140) uniform Camera{
  mat4 matView;
  mat4 matProjection;
};

out vec2 textureCoords;

void main(){    
  textureCoords = textureCoords_modelspace;
  gl_Position =  matProjection * matView * matModel * vec4(vertexPosition_modelspace, 1.0);
}

I see nothing on the screen which is the main error.
When Debugging with Nvidia-NSight I find that all values except the matModel are correctly set. The matModel contains all zero which seems to be why the model isn't drawn.

Why is the matModel always zero, even when I try to set the uniform?

$\endgroup$
1
$\begingroup$

It seems that nothing was wrong with the code I posted. I deleted my complete build and rebuild it, and now it seems to work. I don't see anything on screen but i think that error is unrelated.

I can see the matrix in the nsight debugger.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.