在不同的VBO中存储不同的顶点属性 [英] Storing different vertex attributes in different VBO's

查看:83
本文介绍了在不同的VBO中存储不同的顶点属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不同的顶点缓冲区中存储不同的顶点属性?

Is it possible to store different vertex attributes in different vertex buffers?

到目前为止,我看到的所有示例都执行类似的操作

All the examples I've seen so far do something like this

float data[] = 
{
//position
   v1x, v1y, v1z,
   v2x, v2y, v2z,
   ...
   vnx, vny, vnz,

//color
   c1r, c1g, c1b,
   c2r, c2g, c2b,
   ...
   cnr, cng, cnb,   
};
GLuint buffname;
glGenBuffers(1, &buffname);
glBindBuffer(GL_ARRAY_BUFFER, buffname);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);

绘制完成如下:

glBindBuffer(GL_ARRAY_BUFFER, buffname);
glEnableVertexAttrib(position_location);
glEnableVertexAttrib(color_location);
glVertexAttribPointer(position_location, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(color_location, 3, GL_FLOAT, GL_FALSE, 0, (void*)(3*n));

glDrawArrays(GL_TRIANGLES, 0, n/3);

glDisableVertexAttrib(position_location);
glDisableVertexAttrib(color_location);
glBindBuffer(GL_ARRAY_BUFFER, 0);

是否可以在不同的VBO中存储位置数据和颜色数据?问题是我不知道怎么解决,因为您不能一次绑定两个缓冲区,可以吗?

Isn't it possible to store position data and color data in different VBOs? The problem is I don't understand how this would work out because you can't bind two buffers at once, can you?

如果有一个简单但效率低下的解决方案,相对于一个更复杂但有效的解决方案,我会更喜欢它,因为我处于初级学习状态,并且我不想让事情变得过于复杂.

If there is a simple but inefficient solution, I would prefer it over a more complicated but efficient solution because I am in primary learning state and I don't want to complicate things too much.

此外,如果我要问的是可能的,这是个好主意吗?

Also, if what I'm asking is possible, is it a good idea or not?

澄清一下: 我确实了解如何将不同的属性存储在不同的VBO中.我不明白以后怎么画.

To clarify: I do understand how I could store different attributes in different VBO's. I don't understand how I would later draw them.

推荐答案

属性位置X和提供该属性的缓冲区对象之间的关联是通过glVertexAttribPointer命令进行的.它的工作方式很简单,但并不直观.

The association between attribute location X and the buffer object that provides that attribute is made with the glVertexAttribPointer command. The way it works is simple, but unintuitive.

在调用glVertexAttribPointer 时(这是很多人不了解的部分),当前绑定到GL_ARRAY_BUFFER的任何缓冲区对象都与属性X关联,其中X是glVertexAttribPointer的第一个参数.

At the time glVertexAttribPointer is called (that's the part a lot of people don't get), whatever buffer object is currently bound to GL_ARRAY_BUFFER becomes associated with the attribute X, where X is the first parameter of glVertexAttribPointer.

因此,如果要具有一个缓冲区中的属性和另一个缓冲区中的属性,请执行以下操作:

So if you want to have an attribute that comes from one buffer and an attribute that comes from another, you do this:

glEnableVertexAttrib(position_location);
glEnableVertexAttrib(color_location);
glBindBuffer(GL_ARRAY_BUFFER, buffPosition);
glVertexAttribPointer(position_location, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, buffColor);
glVertexAttribPointer(color_location, 3, GL_FLOAT, GL_FALSE, 0, 0);


关于是否应将属性拆分到不同的缓冲区中……我想说的是,只有在有明确的需要的情况下才应该这样做.


As for whether you should split attributes into different buffers... I would say that you should only do it if you have a demonstrable need.

例如,假设您正在制作动态高度图,也许是出于某种水的效果.每个元素的Z位置都会发生变化,但这也意味着法线会发生变化.但是,XY位置和纹理坐标不会改变.

For example, let's say you're doing a dynamic height-map, perhaps for some kind of water effect. The Z position of each element changes, but this also means that the normals change. However, the XY positions and the texture coordinates do not change.

高效的流传输通常需要双缓冲缓冲区对象或使它们无效(使用glBufferData(NULL)或glMapBufferRange(GL_INVALIDATE_BIT)重新分配它们).这两种方法仅在流数据位于非流数据中的另一个缓冲区对象中时有效.

Efficient streaming often requires either double-buffering buffer objects or invalidating them (reallocating them with a glBufferData(NULL) or glMapBufferRange(GL_INVALIDATE_BIT)). Either way only works if the streamed data is in another buffer object from the non-streamed data.

另一个可以证明的需求示例是,如果内存是一个问题,并且几个对象共享某些属性列表.也许对象具有不同的位置和法线阵列,但是具有相同的颜色和纹理坐标阵列.或类似的东西.

Another example of a demonstrable need is if memory is a concern and several objects share certain attribute lists. Perhaps objects have different position and normal arrays but the same color and texture coordinate arrays. Or something like that.

但是,否则,最好只是将一个对象的所有内容都放入一个缓冲区中.即使您不对数组进行交织.

But otherwise, it's best to just put everything for an object into one buffer. Even if you don't interleave the arrays.

这篇关于在不同的VBO中存储不同的顶点属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆