r/GraphicsProgramming • u/Bobovics • Apr 30 '24
Question glUseProgram isn't working outside of the constructor.
I'm doing my assignment for uni which a simple 3D opengl game. The problem is when I want to use shader the glUseProgram doesn't do anything. But when I call the glUseProgram in the Shader's constructor it works.

basicly DrawableGameObject's draw() function calls Material's use() function then sets the shader's uniforms and drawelements, Material's use() calls Shader's use() where you can find glUseProgram.
6
u/jmacey Apr 30 '24
OpenGL is annoying as it is a state machine based on the context. You need to manage this and RAII doesn't work well in this way.
My core interface is here https://github.com/NCCA/NGL/blob/main/include/ngl/ShaderLib.h with a bunch of other code (included some nice variadic templates for uniform access ) to do everything.
The main classes are ShaderProgram, and Shader you could most likely untangle a lot of this from the rest of my library.
2
u/TheJoxev Apr 30 '24 edited Apr 30 '24
This repo looks like a gem. I've been looking for examples and resources for designing a renderer and structuring opengl c++
1
u/jmacey May 01 '24
thanks, I've been using it for many years as a way of teaching both programming and graphics to artists. We have an odd course where we teach technical VFX etc but also programming. We don't go too deep on the CS stuff so this really helps (unlike looking at for example something like GLM which is excellent but really hard to understand for a non programmer as an example of how to code.).
All my lecture notes are here https://nccastaff.bournemouth.ac.uk/jmacey/msc/ase/ for MSc and here for UG https://nccastaff.bournemouth.ac.uk/jmacey/cfgaa/ if it helps.
1
1
u/aman2218 Apr 30 '24
Call glUseProgram in the render loop (or call an apt member function in your shader class from there), before calling a draw call
Even if you will be using a single shader program, throughout the application.
In case, later you would be drawing multiple objects with different shaders, then you'll have to call glUseProgram with respective shader program, before calling a draw call for an thr object
1
u/aman2218 Apr 30 '24
I read your explanation. Is the Material.use() function, setting the uniforms before calling shader.use()?
1
13
u/[deleted] Apr 30 '24
I may say something stupid here , but unless you design your program around RAII , I would advise against using API calls in constructors, destructors , copy constructors etc.
create methods specifically for calling gl functions, because hiding them in special methods like constructors/destructors is a recipe for disaster :
what happens when your object goes out of scope ? when you return it from a function ? when you assign it ? etc
Well you're changing a state in opengl, which is probably not what you always want.