so I'm working on a game engine. It's partially python and c++.

One part that is currently a pain point is setting the uniforms for shaders. Currently I have to define a behavior for all 50+ methods used for setting the uniforms glUniform{1,2,3,4}{uifd}{v}

Is there a single method that I could just call, give it some memory, and tell it, this is a <inset type here> at this location and it repeats this many times?

My motivation:

These uniforms usually come from the "mesh object", (containing single values, bones, textures, etc.) or they come from "shader object" where they're set per shader, (like light position, camera orientation etc.)

So setting all of it programmatically, instead of having to deal with multiple different methods is preferable.


1 Answer 1


You should use uniform buffer objects instead of individual glUniform calls. Then the uniforms are all stored in one block of memory which you tell OpenGL about. The shader and the code that fills the buffer with data have to agree on what the memory layout is, but the code that selects the UBO while drawing doesn't need to care.

You will likely also be able to have one UBO per “mesh object” and one per “shader object”, so that each combination can be activated just with two bindings.

  • $\begingroup$ I totally agree to @kevinReid, you can additional on c++ define a "setConstant" method for each type (int, float, vec2, ivec2 uvec2, vec3...), so that you don't accidental use the wrong glUniform method. $\endgroup$
    – Thomas
    Commented Jul 6, 2023 at 20:43
  • $\begingroup$ woah i didn't knew you could do this!!! that saves me so much work!! $\endgroup$
    – LemonJumps
    Commented Jul 7, 2023 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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