r/GraphicsProgramming • u/nutellis • Oct 23 '17
How about using shaders as singletons ?
I want to have a getActiveShader() method so it will return the active shader(custom class) and use it with my playerComponent or anything that needs to be rendered. I was thinking to use singletons to do that so that the shader can be accessed on the fly from everywhere. I am a bit confused though and i am not really sure if i am on the right track here. Any thoughts on the subject? Using Opengl 3.3 .
0
u/nutellis Oct 23 '17
I think i found it. I will create a "static" class that will "manage" the shaders. So everytime i need to change the shader i'll just do:
Manager::activate("shadername");
//draw and stuff1
Oct 25 '17
FWIW you can have more than one OpenGL context in a single program, which may not be able to share shaders, so it may become logically unsound to treat shaders (or any OpenGL GPU commands) as a global operation.
4
u/Rusky Oct 23 '17
OpenGL already has the concept of an "active shader"-
glUseProgrampersists across draw calls. What would you be using your custom shader class for?Further, it's generally not a great idea to run a lot of custom rendering code per entity anyway. You want everything in the form of a "drawable" type, as pure data that you can sort and filter. This lets you more easily minimize state changes, run multiple rendering passes, etc.