.obj 文件转换为 OpenGL 模型头文件 - 索引和 f 参数?

发布于 2024-09-26 21:49:04 字数 630 浏览 4 评论 0原文

我正在使用的 OpenGL 模型头文件包含以下定义:

static const float modelVertices[NUM_OBJECT_VERTEX * 3] = {}

static const float modelTexCoords[NUM_OBJECT_VERTEX * 2] = {}

static const float modelNormals[NUM_OBJECT_VERTEX * 3] = {}

static const unsigned Short modelIndices[NUM_OBJECT_INDEX] = {}

其中有一堆数字(浮点数)和整数,如合适),用括号之间的逗号分隔。

将 .obj 文件的 v, vt, vn 转换为上述格式似乎很简单。我的 .obj 文件还有一堆 f,其中包括由 / 分隔的三元组。 我不确定这些参数到底是什么...

我需要转换哪些参数才能获得第四个参数 - modelIndices?

(我需要提前承认我是 OpenGL 的新手,所以如果这看起来太初级了,我深表歉意!)

The OpenGL model header file I'm working with contains definitions along the following:

static const float modelVertices[NUM_OBJECT_VERTEX * 3] = {}

static const float modelTexCoords[NUM_OBJECT_VERTEX * 2] = {}

static const float modelNormals[NUM_OBJECT_VERTEX * 3] = {}

static const unsigned short modelIndices[NUM_OBJECT_INDEX] = {}

Where there are a bunch of numbers (floats and integers, as fitting) separated by comma's inbetween the brackets.

It seems straight-forward to convert an .obj file's v, vt, vn to the above format. My .obj file also has a bunch of f's which include triplets separated by /. I'm not sure what these parameters are exactly...

Which parameters do I need to convert to get the fourth - the modelIndices?

(I need to admit in advance that I am a newbie to OpenGL, so apologies if this seems too elementary!)

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

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

发布评论

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

评论(1

人事已非 2024-10-03 21:49:04

三胞胎只是一个面部定义。

如果你有
f 1 2 3

这意味着你有一个由索引 1 2 和 3 的顶点组成的三角形。

如果所有条目都是这样,这意味着你可以直接用这些索引填充你的 modelIndices ,并绘制他们使用GL_TRIANGLES

现在,如果它们由 / 分隔,这意味着顶点位置和纹理坐标和/或法线之间有不同的映射。

这是 OpenGL 无法直接处理的< /a> 的方法是将纹理坐标和法线数据分解为与顶点位置数组大小相同的数组。

要做到这一点很简单:这里是伪代码:

read face data (triplets)
for each triplet
   read vertex indice
   read texcoord and normal indices
   fetch texcoord @ texcoord indice from your vt array
   store texcoord @ vertex indice in your modelTexCoords array
   fetch normal @ normal indice from your vn array
   store normal @ vertex indice in your modelTexCoords array

   etc

另请参阅维基百科的文档,它很好地解释了 .obj 格式: http: //en.wikipedia.org/wiki/Obj

Triplets are just a face definition.

If you have
f 1 2 3

This means you have a triangle that is made of vertex of indices 1 2 and 3.

If all entries are like that, that means you can directly fill your modelIndices with those indices, and draw them using GL_TRIANGLES.

Now if they are separated by / this means you have different mapping between vertices position and texture coordinates and/or normals.

This is something OpenGL can't handle directly and the way to go for you is to explode the texcoord and normal data into arrays of the same size than vertice position array.

To do this is trivial: here's pseudo code:

read face data (triplets)
for each triplet
   read vertex indice
   read texcoord and normal indices
   fetch texcoord @ texcoord indice from your vt array
   store texcoord @ vertex indice in your modelTexCoords array
   fetch normal @ normal indice from your vn array
   store normal @ vertex indice in your modelTexCoords array

   etc

See also wikipedia's doc, which explain well .obj format: http://en.wikipedia.org/wiki/Obj

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