OpenGL - 索引缓冲区困难 [英] OpenGL - Index buffers difficulties

查看:48
本文介绍了OpenGL - 索引缓冲区困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义文件格式,其中包含 3D 网格所需的所有信息(从 3ds Max 导出).我已经提取了顶点、顶点索引和法线的数据.

I have a custom file format that has all the needed information for a 3D mesh (exported from 3ds Max). I've extracted the data for vertices, vertex indices and normals.

我将顶点数据、顶点索引和法线数据传递给 OpenGL,并通过调用 glDrawElements(GL_TRIANGLES,...)

I pass to OpenGL the vertex data, vertex indices and normals data and I render the mesh with a call to glDrawElements(GL_TRIANGLES,...)

除了法线之外,一切看起来都不错.问题是法线有不同的索引.并且因为 OpenGL 只能使用一个索引缓冲区,所以它对顶点和法线都使用该索引缓冲区.

Everything looks right but the normals. The problem is that the normals have different indices. And because OpenGL can use only one index buffer, it uses that index buffer for both the vertices and the normals.

如果您能建议我如何解决这个问题,我将不胜感激.

I would be really grateful if you could suggest me how to go about that problem.

需要注意的重要一点是顶点/法线数据没有排序",因此我无法使用 glDrawArrays(GL_TRIANGLES,...) 的功能 - 网格没有正确渲染.

Important thing to note is that the vertex/normal data is not "sorted" and therefore I am unable to use the functionality of glDrawArrays(GL_TRIANGLES,...) - the mesh doesn't render correctly.

是否有一种方法/算法可以用来对数据进行排序,以便可以使用 glDrawArrays(GL_TRIANGLES,..) 正确绘制网格?但即使有算法,还有一个问题——我将不得不复制一些顶点(因为我的顶点缓冲区由唯一的顶点组成——例如,如果你有立方体,我的缓冲区将只有 8 个顶点),我不确定怎么做.

Is there a way/algorithm that I can use to sort the data so the mesh can be drawn correctly with glDrawArrays(GL_TRIANGLES,..) ? But even if there is an algorithm, there is one more problem - I will have to duplicate some vertices (because my vertex buffer consists of unique vertices - for example if you have cube my buffer will only have 8 vertices) and I am not sure how to do that.

推荐答案

我设法做到了,而无需使用 glDrawArrays(GL_TRIANGLES,..) 将索引缓冲区传递给 OpenGL我所做的是以下内容:填充顶点数组、顶点索引数组、法线数组和法线索引数组.然后我用排序的数据创建了新的顶点和法线数组,并将它们传递给 OpenGL.

I managed to do it without passing index buffer to OpenGL with glDrawArrays(GL_TRIANGLES,..) What I did is the following: Fill a vertex array, vertex indices array, normals array and normals indices array. Then I created new vertex and normal arrays with sorted data and passed them to OpenGL.

for i = 0; i < vertexIndices.size(); ++i
    newVertexArray[i] = oldVertexArray[vertexIndices[i]];

for i = 0; i < normalsIndices.size(); ++i
    newNormalsArray[i] = oldNormalsArray[normalsIndices[i]];

我对其进行了一些优化,根本没有填充索引数组.但优化取决于程序员读取网格数据的方式.

I optimized it a bit, without filling indices arrays at all. But the optimization depends on the way the programmer is reading the mesh data.

这篇关于OpenGL - 索引缓冲区困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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