I'm building a framework where each object has an associated program and each object has a 'draw' method.

What is the best choice, to have a single modelview stack handled by a Renderer class or to have a modelview stack for each program? In the preferred case how can I handle optimally a "world" transformation that affects all the objects independently by the program (i.e. moving the camera)


You shouldn't need a "stack" at all.

You should have a single "drawable" type (you generally do not need more than one; variations on a request to draw are data and individual game entities should not draw themselves). The Drawable type contains:

  • A reference to the geometry being drawn.
  • A reference to the shader program used to draw.
  • References to any textures needed by the prior two items.
  • A matrix storing the object's world transformation.

(The references are key to avoid data duplication and to allow for sorting by these objects prior to rendering when possible, since that helps minimize state changes and improve rendering performance.)

To handle a transformation like the camera, store the camera's viewing transformation in a Camera object that represents the camera.

To handle actual world transformations applied to multiple objects, such as supporting the bulk selection and moving of many entities, you'll probably want a higher-level system. This is generally not the domain of the renderer itself. Something that can maintain the collection of grouped objects and apply a requested transformation to their existing transformations (for example, a Selection type if all you're doing is selection-related stuff).

A scene graph is occasionally useful for that kind of thing, though you should proceed with caution there as scene graphs tend to create black holes of over-engineering and design paralysis.

  • \$\begingroup\$ Thanks! I have indeed only one drawable type, just forgot to mention. I still have everything not as reference but as value, but I will move to reference asap. I was silly not thinking at a Camera class, in my full inexperience I forgot that I could decouple the view matrix from the model one. Thank you very much! \$\endgroup\$ – JTulip Jun 2 '14 at 23:33

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.