I'm starting up on OpenGL, and want to write a well structured and expandable, yet relatively simple 3D graphics engine for fun and personal knowledge. I'm looking for information concerning the best practices and design patterns related to this sort of code.

Specifically, I'm having issues deciding how classes should be organized, how to divide responsibilities, etc. Right now, I'm at the point where everything is done in the rendering method!

Can anyone give me some advice? How are the responsibilities and program flow organized, in a graphics engine?


1 Answer 1


I subdivided my classes into three kinds:

  1. Central renderer class.
  2. Resource class- texture, mesh, font
  3. Instance classes- label, sprite, 3D Object

I kept lists of type 2 and 3 classes in the central Renderer class, and when draw time comes, then I already know all the objects to render. When you come to draw, then efficient drawing does not lend itself to drawing each object sequentially- you will want to do things like sort them in certain orders.

Edit: To be more specific, the user manages the lifetime of the resources. They're shared amongst all the instance classes that are using it, and the user. If the caller of the code wants to cache the resource, then that's their problem.

  • \$\begingroup\$ Thanks for the helpful anwser. Where do you put your individual (per sprite, model, etc) rendering logic? Can your models render themselves to a provided OpenGL context or is the central rendering class rendering everything itself? \$\endgroup\$
    – anthonyvd
    Commented Jun 13, 2011 at 23:30
  • 2
    \$\begingroup\$ @pwny: No, you really want to keep all the Draw logic in the central class. This is because partly, you need to know all the data when you render for maximal efficiency. It'll also make the logic more interchangable between renderers. And I'm on D3D, but the point is the same. \$\endgroup\$
    – DeadMG
    Commented Jun 14, 2011 at 0:04
  • \$\begingroup\$ By what criteria do you sort your objects? By texture? By mesh? By both? Etc \$\endgroup\$ Commented Jun 14, 2011 at 1:14
  • \$\begingroup\$ @Daniel: None, at the moment. But graphics pipeline is definitely one place where you will need it. \$\endgroup\$
    – DeadMG
    Commented Jun 14, 2011 at 8:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .