Techniques for drawing large numbers of objects in OpenGL
I am wondering what techniques or methods I could use to draw many objects on OpenGL.
For example, I may have a class which represents a brick, and maybe another which represents a tree, or something else random which I would like to draw like a lamppost. Then say, I have many bricks to draw, or lampposts or something like that... Calling a draw method for each one would lead to very long code.
At the moment I am using a glut callback function to draw the "stuff" in the world, like a brick or tree.
Using a globally accessible vector of base pointers
One idea I had was to have all the classes inherit from a base class, and put base pointers into a vector - then have the vector global. That way I could do something like:
for(int i = 0; i < vector_of_objects.size(); i ++)
vector_of_objects[i]->draw();
Each object could have a draw function, which draws all the OpenGL primitives.
An example of such a class would be something like this. (This is what I have been experimenting with.)
class box {
public:
void draw() { // Do the drawing of a box }
// Other members etc
}
Alternatives?
This doesn't seem like a good idea, since creating a new class for something like a house, would require the user to remember to add a draw function called 'draw'... Although this is a trivial point, not necessarily a big problem.
Are different techniques used in industry or games development? The advantage I see to this method is once the for loop is put in the draw function, you can forget about it - only having to worry about placing more items (or pointers) in the globally accessible vector.
Does anyone have any suggestions for a better alternative?