简体   繁体   English

在同一个VBO OpenGL上使用不同纹理绘制不同的对象

[英]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. 我正在使用OpenGL,在我的程序中,使用不同的纹理绘制每个对象的各种几何形状(正方形,三角形等)。

I tested perform rendering with VBO and shaders and this worked well creating a VBO per object. 我测试了使用VBO和着色器执行渲染,这很好地创建了每个对象的VBO。 The problem occurs when a large amount of objects (between 150 and 200) ... this means very many calls to function glDrawElements() : 当大量对象(150到200之间)出现问题时......这意味着对函数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.). 我发现最好的方法是创建一个包含要绘制的所有顶点的VBO(顶点,纹理坐标,索引等)。

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. 这个问题是我不能为每个对象使用不同的纹理,因为VBO一次绘制所有几何体。

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). 不使用已经弃用的函数或方法glBegin () / glEnd ()glDrawArrays () (我正在使用open GL 3.0及更高版本)。

PD: I use OpenGL with C++. PD:我使用OpenGL和C ++。

If you want to render a VBO first with one texture then with another: 如果要首先使用一个纹理渲染VBO,然后使用另一个纹理渲染VBO:

  1. Bind the first texture 绑定第一个纹理
  2. Render the VBO 渲染VBO
  3. Bind the second texture 绑定第二个纹理
  4. Render the VBO again 再次渲染VBO

If you want to draw part of a VBO with one texture and part of it with another texture you have a couple options: 如果你想用一个纹理绘制VBO的一部分,而用另一个纹理绘制它的一部分你有几个选项:

  • Split up you VBO into two separate VBOs and render them seperately, 将VBO拆分为两个独立的VBO并单独渲染它们,
  • 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 . 如果在单个图像中使用4x4纹理,则可以在单个VBO中使用16个不同的对象。 Of course you are limited by max size of texture image and texture quality you wanna to use. 当然,您受限于您想要使用的纹理图像的最大尺寸和纹理质量。

3D textures and Texture arrays 3D纹理和纹理数组

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 ... 随着gfx HW的进步,3D纹理和纹理数组也可用,因此你现在每个对象可以有一片纹理......无需在纹理之间切换......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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