在OpenGL中使用VBO的偏移量 [英] Using an offset with VBOs in OpenGL

查看:318
本文介绍了在OpenGL中使用VBO的偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是用相同的vbo但不同的偏移量多次渲染网格。示例:

What I want to do is to render a mesh multiple times with the same vbo but with different offset. Example:

//Load VBO
glGenBuffers(2, &bufferObjects[0]);
glBindBuffer(GL_ARRAY_BUFFER, bufferObjects[VERTEX_DATA]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*size(vertices)*3, &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects[INDEX_DATA]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*size(indices), &indices[0], GL_STATIC_DRAW);

//Render VBO
glBindBuffer(GL_ARRAY_BUFFER, bufferObjects[VERTEX_DATA]);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferObjects[INDEX_DATA]);
glDrawElements(renderFlag, nrIndices, GL_UNSIGNED_INT, 0);

如果我在绘制孔网格的同时没有问题,具有不同开始索引的相同网格,如下所示:

If I draw the hole mesh at the same time there is no problem, but is it possible to draw the same mesh with a different start index, like this:

glDrawElements(renderFlag, 20, GL_UNSIGNED_INT, "WHAT TO WRITE HERE"?);


推荐答案

start index是什么意思?您可以指以下两种情况之一:

What do you mean by "start index"? You could mean one of two things:

去做。 glDrawElements 将缓冲区对象的偏移量开始拉动索引。所以添加一个值。

Well, just do that. glDrawElements takes an offset into the buffer object for where it starts to pull indices from. So add a value to that.

glDrawElements(renderFlag, 20, GL_UNSIGNED_INT, (void*)(ixStart * sizeof(GLuint)));



偏移从缓冲区获取的索引



这意味着您要绘制相同范围的索引,但是要对这些索引值本身应用偏移量。所以如果你的索引缓冲区看起来像这样:(1,4,2,0,5,...),你应用偏移量20,然后它将获取这些索引:(21,24,22,20,25 ,...)。

Offset the indices you fetch from the buffer

This means that you want to draw the same range of indices, but you want to apply an offset to those index values themselves. So if your index buffer looks like this: (1, 4, 2, 0, 5, ...), and you apply an offset of 20, then it will fetch these indices: (21, 24, 22, 20, 25, ...).

这是通过 glDrawElementsBaseVertex 。它看起来像这样:

This is done with glDrawElementsBaseVertex. It looks something like this:

glDrawElementsBaseVertex(renderFlag, 20, GL_UNSIGNED_INT, 0, offset);

这篇关于在OpenGL中使用VBO的偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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