0
votes

So, I am struggling with shaders right now in my program. Basically this is the main loop:

heightmap_shader.use();
nanosuit_shader.use();

glm::mat4 nanosuitProj, nanosuitModel, nanosuitView;
nanosuitProj = glm::perspective(glm::radians(45.0f), float(800/600), 0.1f, 1000.0f);
nanosuitView = camera.getViewMatrix();
glUniformMatrix4fv(glGetUniformLocation(nanosuit_shader.getProgramID(), "projection"), 1, GL_FALSE, glm::value_ptr(nanosuitProj));
glUniformMatrix4fv(glGetUniformLocation(nanosuit_shader.getProgramID(), "view"), 1, GL_FALSE, glm::value_ptr(nanosuitView));

// Draw the loaded model
nanosuitModel = glm::translate(nanosuitModel, glm::vec3(0.0f, 2.0f, 0.0f)); // Translate it down a bit so it's at the center of the scene
nanosuitModel = glm::scale(nanosuitModel, glm::vec3(0.05f, 0.05f, 0.05f));  // It's a bit too big for our scene, so scale it down
glUniformMatrix4fv(glGetUniformLocation(nanosuit_shader.getProgramID(), "model"), 1, GL_FALSE, glm::value_ptr(nanosuitModel));

nanosuit_Model.Draw(nanosuit_shader);

glfwSwapBuffers(window);

So basically I use a shader first (heightmap) but do nothing with it. After that I change to the actual necessary shader, and after that I draw the object. This does not work. The screen is empty.

If I comment out

heightmap_shader.use()

the program works fine. Can someone explain this? Am I doing something wrong?

In my head the program should work even with the first shader, since I am switching it everytime. But it seems my program doesn't understand it.

Note: The method use() is:

glUseProgram(shaderId)

where shaderId is the id of the shader after it is compiled.

EDIT:

Just want to say that this SHOULD work, and it was not working because there was a bug in my Shader class.

1
"bug in shader class" was in #5-10 on my list. It was the "etc". :) - badweasel

1 Answers

1
votes

Chances are it is switching shaders and something else is wrong. Having a blank screen could mean one (or more) of like 10 different things. Such as:

  1. Your object is off screen. Most common for me is the z axis being wrong.
  2. You are not passing attributes or uniforms correctly.
  3. The texture didn't get loaded correctly.
  4. There is a bug in your shader code that you're not catching.
  5. (5-10) Other things in your code that I'm not privy too. Like how you're managing objects, how you're loading 3d objects, how you're handling transformations, your view or projection matrix, etc.

My first test is always setting the fragment color to a specific thing. Like setting:

gl_FragColor = vec4(1.0,0.0,0.0,1.0); // red

To see which shader you are using, if you think that maybe it's not switching shaders when you ask it to put the above in one and:

gl_FragColor = vec4(0.0,1.0,0.0,1.0); // green

in the other and see which color the object is.

When I start running in to problems with a shader I simplify as much as possible. Comment out code or simply hard code your gl_FragColor. First to a solid color and then when you get that working hard code it to a variable that you're wanting to see.

If you can't get gl_FragColor = red to show your object on screen as solid red, you've got other problems.