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

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

问题描述

我有了一个三维网格所有需要的信息(从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.

推荐答案

我设法做到这一点没有通过索引缓冲区到OpenGL与(4,8,GL_TRIANGLES,..) 我所做的是以下几点: 填写一个顶点数组,顶点索引数组,数组法线和法线索引数组。 然后,我创造了新的顶点和正常阵列排序的数据,并将其传递给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天全站免登陆