3
\$\begingroup\$

The rendering keeps happening, and the debugger shows my camera is getting its position updated properly, but for some reason the camera doesn't appear to have its position updated. I'm not sure what I'm doing wrong.

Can uniforms fail to get set when swapping between different programs/shaders?

\$\endgroup\$
2
\$\begingroup\$

This is what I get for following bad tutorials online where their code works with a single shader, but fails when you do multiple...

A newbie mistake on my part, my code looked like this (assume everything was compressed into one long function):

//...

glGetUniformLocation(prog1, ...);
glUniformMatrix4fv(...);

glUseProgram(prog1);
glDrawElements(...);

//...

glUseProgram(prog2);
glDrawElements(...);

//...

This occurred because glUseProgram() has to be called before updating the uniforms, because otherwise the second glUseProgram() was called, which left it active, and when the uniform setting started again it would be using prog2 instead since that was loaded from the previous cycle, causing the uniforms to never get updated and looking like it froze.

Meaning it should have looked like this:

//...

glUseProgram(prog1);  // Move this before.

glGetUniformLocation(prog1, ...);
glUniformMatrix4fv(...);

glDrawElements(...);

//...

glUseProgram(prog2);
glDrawElements(...);

//...

Upon doing this, everything works great again.

Note: If I checked glGetError() I'd also have noticed an error when trying to get the uniform, which was in line with what is seen in the official documentation.

\$\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.