lwjgl 中的多个顶点缓冲区对象 (VBO)

发布于 2024-12-25 06:35:38 字数 1082 浏览 1 评论 0原文

我能够让 VBO 适用于单个网格。 首先,我设置 opengl:

//....
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);

对于每个网格,我生成缓冲区(每个网格两次调用此函数):

vertBuffer = GL15.glGenBuffers( BufferUtils.createIntBuffer(1)); 
//same idea for faceBuffer

我将网格数据放入 FloatBuffers 和 IntBuffers 中。 然后我必须将这些发送给 opengl:

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertBuffer)
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, myVertPositions,GL15.GL_STATIC_DRAW);
//same idea for faces.

注意:上述所有步骤仅完成一次。

最后是渲染:

//...
int stride = 0; //0 = compact arrays.
int offset = 0; //Each mesh would need a different offset?

GL11.glVertexPointer(3, GL11.GL_FLOAT, stride, offset);
GL12.glDrawRangeElements(GL11.GL_TRIANGLES, 0, numVerts, 
        numFaces*3, GL11.GL_UNSIGNED_INT, offset);
//Using the "buffer versions" of these functions throws  
//"Cannot use Buffers when Array Buffer Object is enabled"
//Whether or not glEnableClientState is called.

我添加的任何新网格都会覆盖旧网格。将所有网格物体放入同一个缓冲区听起来效率不高。 我还能如何让多个网格共存?

I was able to get VBO's working for a single mesh.
First I set up opengl:

//....
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);

For each mesh I generate the buffer (two calls to this function per mesh):

vertBuffer = GL15.glGenBuffers( BufferUtils.createIntBuffer(1)); 
//same idea for faceBuffer

I put the mesh data into FloatBuffers and IntBuffers.
Then I have to send these over to opengl:

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertBuffer)
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, myVertPositions,GL15.GL_STATIC_DRAW);
//same idea for faces.

Note: All of the above steps only are done once.

Finally there is rendering:

//...
int stride = 0; //0 = compact arrays.
int offset = 0; //Each mesh would need a different offset?

GL11.glVertexPointer(3, GL11.GL_FLOAT, stride, offset);
GL12.glDrawRangeElements(GL11.GL_TRIANGLES, 0, numVerts, 
        numFaces*3, GL11.GL_UNSIGNED_INT, offset);
//Using the "buffer versions" of these functions throws  
//"Cannot use Buffers when Array Buffer Object is enabled"
//Whether or not glEnableClientState is called.

Any new mesh I add overwrites the old mesh. Putting ALL of my meshes into the same buffer doesn't sound efficient.
How else can I get multiple meshes to coexist?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意中人 2025-01-01 06:35:38

将所有网格物体放入同一个缓冲区听起来效率不高。

事实上是这样的:切换 VBO 比设置一些偏移量更昂贵。特别是对于很多小的(就顶点数量而言)对象,建议将它们放入一个VBO中。

无论如何:您可以使用任意数量的 VBO。 glGenBuffers 为您提供一个 ID,它允许您识别这些缓冲区并使用 glBindBuffer 定位它们。

Putting ALL of my meshes into the same buffer doesn't sound efficient.

Actually it is: Switching VBOs is more expensive, than setting some offset. Especially for a lot of small (in terms of number of vertices) objects, putting them into one VBO is recommended.

Anyway: You can use as many VBOs as you like. glGenBuffers gives you an ID, which allows you to identify those buffers and target them with glBindBuffer.

似梦非梦 2025-01-01 06:35:38

这是一个很简单的问题。

为了使用多个 VBO,您必须保留函数 glGenBuffers 分配的 ID 作为对网格的引用。

然后,当您想要绘制特定的网格时,您需要像这样绑定它(我的引擎代码的摘录):

glBindBuffer(GL_ARRAY_BUFFER, Object_To_Draw->PATRIA_Model->MODEL_VBO_ID);

就好像你有 3 个网格,苹果、梨和橙子,每个网格都有自己的 ID。

如果你想使用苹果,你就绑定苹果的ID(简单来说,你对GPU说,从存储苹果的内存中获取几何数据......:)),如果你想要梨,你就指向那和奥索。

我希望它有帮助。

this is a quite simple issue.

In order to use multiple VBOs, you have to keep the ID assigned by the function glGenBuffers as your reference to the Mesh.

then, when you want to draw that specific mesh, you need to bind it like this (an excerpt of my engine's code):

glBindBuffer(GL_ARRAY_BUFFER, Object_To_Draw->PATRIA_Model->MODEL_VBO_ID);

It's like you have 3 meshes, apple, pear and orange, each one has its own ID.

If you want to use the apple, you bind the ID of the apple (in simple words you say to the GPU, take the geometry data from the memory storing the apple.... :)), if you want the pear you point that and oso on.

I hope it helped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文