Search code examples
c++opengltexturesdrawvbo

Draw different objects with differents textures on the same VBO OpenGL


I'm working with OpenGL and in my program, drawing various geometric shapes (squares, triangles, etc.) each object with different textures.

I tested perform rendering with VBO and shaders and this worked well creating a VBO per object. The problem occurs when a large amount of objects (between 150 and 200) ... this means very many calls to function glDrawElements():

glDrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

I found that the best way is to create a single VBO which contains all vertices to draw (vertices, texture coordinates, indices, etc.).

The problem with this is that I can not use a different texture for each of the objects as the VBO draw all geometry once.

The question is .. what is the best way (most optimal) to perform what I need? without using functions or methods that were already deprecated as glBegin () / glEnd () or glDrawArrays () (I'm working with open GL 3.0 and higher).

PD: I use OpenGL with C++.


Solution

  • If you want to render a VBO first with one texture then with another:

    1. Bind the first texture
    2. Render the VBO
    3. Bind the second texture
    4. Render the VBO again

    If you want to draw part of a VBO with one texture and part of it with another texture you have a couple options:

    • Split up you VBO into two separate VBOs and render them seperately,
    • Combine both textures into one larger texture, and adjust the texture coordinates accordingly, or
    • Use array textures and select the correct layer in your shader. If supported by your target hardware, this is probably your best option. For even more flexibility, consider sparse texture arrays.