简体   繁体   中英

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++.

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.

use tiled textures

meaning in one texture image You have textures for more objects. Sometimes it is also called Texture atlas . So change texture coordinates accordingly.

If you use 4x4 textures in single image you can have 16 different objects in single VBO . Of course you are limited by max size of texture image and texture quality you wanna to use.

3D textures and Texture arrays

as gfx HW progresses now also 3D textures and texture arrays are available so you can have one slice of texture per object now ... without the need to switch between textures ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM