Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 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 responsabilities, etc. (Right now, I'm at the point where everything is done in the rendering method!)

Can anyone give me some advice or point me to some appropriate litterature?

Thank you very much.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
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? –  pwny Jun 13 '11 at 23:30
2  
@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. –  DeadMG Jun 14 '11 at 0:04
    
Thank you very much for your advice –  pwny Jun 14 '11 at 0:14
    
By what criteria do you sort your objects? By texture? By mesh? By both? Etc –  Daniel Jun 14 '11 at 1:14
    
@Daniel: None, at the moment. But graphics pipeline is definitely one place where you will need it. –  DeadMG Jun 14 '11 at 8:19

Your Answer

 
discard

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.