5
\$\begingroup\$

I have an OpenGL renderer which has a Scene member variable. The Scene object can contain N SceneObjects. I use these SceneObjects for storing the vertex position and any transforms. My question is, where should shaders be stored in this arrangement? I guess they need to be in a central location because multiple objects can use the same shader. But then each object needs access to the shader because it needs to set attributes into the shader. Does anyone have any advice?

\$\endgroup\$
9
\$\begingroup\$

You should have a shader pool where all shaders are stored. Once the pool is filled, objects are bound with these shaders (think "bound" as object-references-shader.) Many SceneObjects could share the same shader object (be it OOP or whatever) and only access it by referencing the shader in the pool.

Then, for each SceneObject, you just set the attribs/uniforms, bind the shader in OpenGL and draw. You should consider batch-rendering the objects which share the same shader to avoid changing OpenGL state (or at least accounting for it.)

Whenever you load a new object, just bind it with the shader already found in the pool. If you're not preloading shaders and it's the first time you're using the shader, add it into the pool. You should also delete shaders from the pool if you're not going to use them anymore (i.e. counting references.)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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