OpenGL/LWJGL program structure

Howdy,

I’ve been learning the OpenGL ropes via LWJGL (programmable pipeline), and have got to the point that I’d like to create a more complex scene than just the odd rotating cube or two. However, I’ve become aware that there aren’t many resources out there that actually focus on OpenGL program organisation/structure, particularly in terms of OOP (not that I can find anyway!). I’ve had a good googling, but the results are limited or vague; It could’ve been that I wasn’t using the right terminology in my search. I’ve got some general ideas about how I’ll go about it, but want to run it past the JGO community for opinions & suggestion.

I gather that the most important thing is to try and minimise state changes and pushing data to the GPU; as such, it’s best to render similar objects consecutively (other fancy words like ‘geometry batching’ also popped out at me). So as an example, I was thinking of doing the following:

  • Lets say I created a farming program, with a Sheep, Pig and Cow class. Each of these classes holds a static list/reference to every instance of the class (i.e. instances are added or removed from the list when they are created or destroyed).

  • The class statically holds all OpenGL state common to each instance (i.e. VAO’s, VBO’s, shaders, textures).

  • When it comes time to render, I call a static render() method on each class (i.e. Sheep.render(), Pig.render(), Cow.render()). This method sets the common OpenGL state once, then runs through each instance and renders it, perhaps by calling a non-static render() method on each instance. The instances themselves are responsible for setting uniforms particular to that instance, such as transformations or texture coords, and making the final draw call.

So in general, my questions are:

  • Does this sounds like a decent approach, and do you guys do things simillar or differently?
  • Is there a better, or more accepted way to do this?
  • Will this scale well as my program increases in size &/or complexity? (Keeping in mind at this stage I’ll just be messing around whilst learning, but with a view to creating a full-blown program in the future).
  • Are there any good resources out there on the net that focus on OpenGL program organisation, that I should be looking at?

Cheers muchly,
nerb.

Your design sounds good, but static lists and OpenGL resources aren’t very “clean” in object oriented programs. You could instead store your VBOs, shaders etc in a Mesh object (or something similar) that is shared by a certain animal type, and keep lists of them in the main game class (as normal fields, not static fields). Such a design should scale to a few hundred if not a thousand meshes. If you’re interested in rendering an extreme number of meshes you should look up geometry instancing.

Thanks for the reply theagentd. A more general mesh class makes more sense than what I was planning, and as you mentioned, cleaner. (Not to mention more usable, flexible, extensible, and all those other lovely OOP words). I think I could have run into some strife with my inital idea; if not immediately, certainly down the track.

I’ve touched on instanced drawing whilst learning OpenGL and intend to have a play with it in the next couple of months.

Much appreciated.