1
votes

Let's suppose i have an uniform variable in a GLSL shader which value is set at program startup. The value never change during the program execution.

What i want to do is to set this uniform variable from my main C++ program.

My problem is that that uniform variable seems to be cleared each time i call glUseProgram. I have to call again glUniformXX() API

Is there a way to tell OpenGL not to clear uniform variable between glUseProgram?

2
OpenGL does not clear uniform values on its own. You have to show your actual code, since it is toally unclear what exactly is going on.derhass

2 Answers

2
votes

Uniform state is preserved till the next link operation. Indeed glUseProgram do not reset the uniform state.

You can check in Do uniform values remain in GLSL shader if unbound?

-1
votes

You may want to separate logic & storage of data in this situation. In this type of situation you may want to have your GL data that is initialized by the C++ application to be passed off to OpenGL & GLSL through their API calls separate from your logical calls. Then having a database type structure or class that keeps track of all instances of your programs, shaders, uniforms and variables becomes very handy.

Your main engine class or structure would be responsible for setting up your windows, your basic I/O, setting up an OpenGL context, creating your programs & shaders and the calls to pass them off to the renderer or video card. Your application logic should be separate from this as well. Another nice feature to this approach of having your data structures separate would be to have them as a part of your engine that relies on received input data from the specific application through its initialization process.

With a manager type class to maintain and keep track of all of your instances for each program, shader & its data you can keep a low cost single instance of all of your uniforms as const types in containers and variable types remain for their lifetime and then are discarded after use where new ones will be created when needed. This way when you enable & disable your programs that allocate & destroy their internal datasets, you still have access to existing data that you can retrieve and reinitialize with ease so as long as you make this type of managing class in a cache friendly way. This also relies on knowing which type of data to have as stack variables, or which type to have as dynamic memory. The use of templates can be very handy here along with smart pointers such as std::shared_ptr<T> & std::unique_ptr<T>. The reason I mentioned smart pointers here is that it does make clean up easier, it helps to manage ownership, and you can easily store, create, dispatch and remove both uniforms & variable types from a single container. However this type of construct is fairly complex and beyond the scope of trying to fully answer this question as there are too many integrated parts of a game or 3D graphics engine to display here.