Search code examples
openglmodelvboinstances

How to load a model once and reuse it in OpenGL


I've loaded a wavefront model file (.OBJ and .MTL) and stored the vertices/indices in a VBO for rendering. Now I only populate the VBO once when the model is loaded. My question is... What is the best way to reuse this vertex data to redraw multiple instances of the model? At the moment, I'm just storing a transform matrix (rotation, scaling and translation) for each instance of the model and calling the draw() function for each instance with the relevant transform matrix.

Is this a completely incorrect way to do this? What are some better/alternative ways to achieve the same outcome?

Obviously, this will only work for very basic models where the object is stationary with respect to itself (i.e. that no parts of the model are moving regardless of it's world orientation). This is what leads me to believe that there are indeed much better ways to do this.

Hope my ramblings make sense...


Solution

  • If you just want to change the position of the model, changing the transformation matrix is a reasonable way to do it. In later OpenGL versions, you can use instanced drawing to render the model multiple times with different transformations in a single call, and maybe get a little extra performance that way, but a loop that just changes the transformation matrix each time around is a good starting point.

    If you were actually animating the model, that's different, since the vertex coordinates themselves have to change. For keyframe-based animation, you could load each keyframe into a separate buffer, and have the vertex shader do interpolation using a time value that you provide as a uniform. But this sort of thing is independent of just transforming the whole model around.