Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 9 down vote accepted

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.)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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