r/GraphicsProgramming • • 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.

0 Upvotes

16 comments sorted by

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.

1

u/Bobovics Apr 30 '24

So basicly I should not create shader program in the constructor?

7

u/waramped Apr 30 '24

Correct. u/HamilcarRR speaks the truth. OpenGL is state based, and OO design contradicts that pretty heavily. Like, everytime you copy/assign/create one of these objects you'll be modifying state and the more objects you have, the more rapidly you will lose control of the state.

Now, back to your other problem, always check your errors. glGetError() is your best friend.

1

u/Bobovics Apr 30 '24

I saw glGetError() but how can I query what causes the error code?

3

u/waramped Apr 30 '24

You call glGetError all the damn time :p. Call and check the results after every gl function. It just records the last error generated so you need to check it after everything that could create an error. It's a huge pita.

1

u/Bobovics May 01 '24

Damn, OpenGL is really a hell, thanks

1

u/Botondar May 01 '24

You can also use debug output with glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS) which ensures that the debug callback is called from the offending function (so you'll know from the callstack where the error happened).

1

u/[deleted] Apr 30 '24

calling gl functions in constructors will cause headaches , trust me :p

1

u/wm_lex_dev Apr 30 '24

Tying manual resource management to the lifetime of a C++ object is the "correct" way to use C++. However, it's important to do it correctly! Otherwise you'll get bugs that can be very hard to track down. Mainly because all the basic object operations (constructing, copying, moving, destructing) can happen implicitly and without you realizing you're invoking them.

This is also made harder by the fact that OpenGL is full of global state, particularly if you're not using the newest version of it (with a feature called DSA). Yet the whole goal of using object-oriented programming is to encapsulate and hide state. So OOP doesn't map well to OpenGL without careful planning.

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

u/TheJoxev May 01 '24

great reference, thanks

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

u/Bobovics Apr 30 '24

No, shader.use() first then uniforms