rendering multiple objects in one pass


i have several objects that need to be displayed. Lets say i have a racing car and a track drawn with pure opengl commands, some houses on the track loaded from STL files and maybe some more stuff. Now i’m looking for a viable and efficient way to display all these objects. If each object is an instance of a class (not necessarily the same class), i thought of using such a function called from my main loop:

void RenderWidget::paintGL()

where each object has a function called render() that draws it on its current location (the objects know their position in 3D space and transform themselves accordingly).
Is this approach viable? also, this solution works only if all objects are known at compile-time. What should i do if the number of objects may vary during runtime (more cars and houses for example)? Should i use function pointers in a loop?

thanks in advance.

Using C++ right ? Well make all your drawable classes implement a “Drawable” interface with a render() method.

Beside that, if your scene is large enough consider using some kind of space partitioning.

The easiest solution is to use some hierarchical scene graph.

Use the “Drawable” interface suggested by ZbuffeR and introduce another class that acts as a container for other sub-objects.

Usually with this simple abstraction you can do whatever you wish and it is also usually efficient enough.


thank you for you replies. However I have some problems coming up with a solution to the actual implementation. I can have all my classes have a render() method (I’m using visual c++ with qt btw) but they are all derived from different base classes, so I have problems with coming up with a way to store all my drawable objects in a container. I know this is more a C++ specific question, but maybe you can help me on this one too. The only solution I can come up with so far is to change my code so all drawable classes share a common base class that virtually implements a render() method. Is there a way to store different classes that all implement a render() method but are not derived from the same base class in a single container and to call their render method in a loop?