1
votes

I've been following a tutorial on modern OpenGL with the GLM library I'm on a segment where we introduce matrices for transforming models, positioning the camera, and adding perspective.

I've got a triangle:

const GLfloat vertexBufferData[] = {
 -1.0f, -1.0f,  0.0f,
  1.0f, -1.0f,  0.0f,
  0.0f,  1.0f,  0.0f,
};

I've got my shaders:

GLuint programID = loadShaders("testVertexShader.glsl",
                               "testFragmentShader.glsl");

I've got a model matrix that does no transformations:

glm::mat4 modelMatrix = glm::mat4(1.0f);  /* Identity matrix */

I've got a camera matrix:

glm::mat4 cameraMatrix = glm::lookAt(
  glm::vec3(4.0f, 4.0f, 3.0f),  /*Camera position*/
  glm::vec3(0.0f, 0.0f, 0.0f),  /*Camera target*/
  glm::vec3(0.0f, 1.0f, 0.0f)   /*Up vector*/
 );

And I've got a projection matrix:

glm::mat4 projectionMatrix = glm::perspective(
 90.0f,        /*FOV in degrees*/
 4.0f / 3.0f,  /*Aspect ratio*/
 0.1f,         /*Near clipping distance*/
 100.0f        /*Far clipping distance*/
);

Then I multiply all the matrices together to get the final matrix for the triangle I want to draw:

glm::mat4 finalMatrix = projectionMatrix
                      * cameraMatrix
                      * modelMatrix;

Then I send the matrix to GLSL (I think?):

GLuint matrixID = glGetUniformLocation(programID, "MVP");
glUniformMatrix4fv(matrixID, 1, GL_FALSE, &finalMatrix[0][0]);

Then I do shader stuff I don't understand very well:

/*vertex shader*/
#version 330 core

in vec3 vertexPosition_modelspace;
uniform mat4 MVP;

void main(){
 vec4 v = vec4(vertexPosition_modelspace, 1);
 gl_Position = MVP * v;
}

/*fragment shader*/
#version 330 core
out vec3 color;

void main(){
 color = vec3(1, 1, 0);
}

Everything compiles and runs, but I see no triangle. I've moved the triangle and camera around, thinking maybe the camera was pointed the wrong way, but with no success. I was able to successfully get a triangle on the screen before we introduced matrices, but now, no triangle. The triangle should be at origin, and the camera is a few units away from origin, looking at origin.

2

2 Answers

4
votes

Turns out, you need to send the matrix to the shader after you've bound the shader. In other words, you call glUniformMatrix4fv() after glUseProgram()

2
votes

Lots of things could be your problem - try outputting a vec4 color instead, with alpha explicitly set to 1. One thing I often do as a sanity check is to have the vertex shader ignore all inputs, and to just output vertices directly, e.g. something like:

void main(){
  if (gl_VertexID == 0) {
    gl_Position = vec4(-1, -1, 0, 1);
  } else if (gl_VertexID == 1) {
    gl_Position = vec4(1, -1, 0, 1);
  } else if (gl_VertexID == 2) {
    gl_Position = vec4(0, 1, 0, 1);
  }
}

If that works, then you can try adding your vertex position input back in. If that works, you can add your camera or projection matrices back in, etc.

More generally, remove things until something works, and you understand why it works, and then add parts back in until you stop understanding why things don't work. Quite often I've been off by a sign, or in the order of multiplication.