从 OBJ 文件导入时如何在 glTexCoordPointer 中设置/计算纹理缓冲区
我正在 Android 中解析 OBJ 文件,我的目标是渲染和渲染显示对象。除了正确的纹理映射(将资源/图像导入 opengl 等工作正常)之外,一切都工作正常。
我不知道如何将 obj 文件中的纹理相关数据填充到纹理缓冲区对象中。
在 OBJ 文件中,我有 vt-lines:
vt 0.495011 0.389417
vt 0.500686 0.561346
和face-lines:
f 127/73/62 98/72/62 125/75/62
我的绘图例程看起来像(仅相关部分):
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glNormalPointer(GL10.GL_FLOAT, 0, normalsBuffer);
gl.glTexCoordPointer(2, GL10.GL_SHORT, 0, t.getvtBuffer());
gl.glDrawElements(GL10.GL_TRIANGLES, t.getFacesCount(), GL10.GL_UNSIGNED_SHORT, t.getFaceBuffer());
OBJ 文件计数的输出:
Vertex-count: 1023
Vns-count: 1752
Vts-count: 524
/////////////////////////
Part 0
Material name:default
Number of faces:2037
Number of vnPointers:2037
Number of vtPointers:2037
欢迎任何建议。
I'm parsing an OBJ-file in Android and my goal is to render & display the object. Everything works fine except the correct texture mapping (importing the resource/image into opengl etc works fine).
I don't know how to populate the texture related data from the obj-file into an texturebuffer-object.
In the OBJ-file I've vt-lines:
vt 0.495011 0.389417
vt 0.500686 0.561346
and face-lines:
f 127/73/62 98/72/62 125/75/62
My draw-routine looks like (only relevant parts):
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glNormalPointer(GL10.GL_FLOAT, 0, normalsBuffer);
gl.glTexCoordPointer(2, GL10.GL_SHORT, 0, t.getvtBuffer());
gl.glDrawElements(GL10.GL_TRIANGLES, t.getFacesCount(), GL10.GL_UNSIGNED_SHORT, t.getFaceBuffer());
Output of the counts of the OBJ-file:
Vertex-count: 1023
Vns-count: 1752
Vts-count: 524
/////////////////////////
Part 0
Material name:default
Number of faces:2037
Number of vnPointers:2037
Number of vtPointers:2037
Any advise is welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,不确定这在多大程度上是居高临下的,但从顶部开始:
表示文件中给出的第 127 个位置的顶点、文件中的第 73 个纹理位置和第 62 个法线以及 a 中指定的其他两个顶点之间的三角形。类似的方式。
在 OpenGL 中,您只为每个顶点指定一个索引(因为整个事物作为一个单元进行变换),因此您需要找出位置、纹理坐标和法线的所有不同组合,并安排您传递给 <相应地,代码>glVertexPointer、
glNormalPointer
和glTexCoordPointer
。例如,上面的面可能最终通过 GL_TRIANGLES 指定为顶点 0、1 和 2 之间,其中您将第 127 个位置复制到内部顶点缓冲区中的第一个位置,将第 73 个纹理坐标复制到第一个位置在您的内部纹理坐标缓冲区中,并将第 62 个法线复制到内部法线列表中的第一个法线。您很可能需要一个 HashMap,采用组合键顶点/纹理坐标/法线并映射到内部数组中的位置。当您遇到传入 OBJ 中的每个面时,您可以检查是否已将该组合分配到内部缓冲区中的某个位置,如果没有,则为其提供下一个可用的组合。
vt 是纹理坐标,因此例如纹理坐标:
表示在纹理空间内,x = 0.495011,y = 0.389417。根据记忆,OBJ 文件格式和 OpenGL 的 y 轴方向相反。因此,用 OpenGL 术语来说,您需要 s = 0.495011,t = 1 - 0.389417 = 0.610583。
如果您的对象使用适当复杂的纹理图集或蒙皮,我想 y 坐标翻转的情况并不明显;如果你只是有一个类似立方体的东西,在每个面上都完全重复相同的纹理,那么你可能只会看到它沿着垂直方向翻转。
这是否涵盖了混乱的根源?
Sorry, not sure to what extent this is patronising, but from the top:
Means a triangle between the vertex with the 127th position given in the file, the 73rd texture position in the file and the 62nd normal and the two other vertices specified in a similar way.
In OpenGL you specify only a single index per vertex (as the whole thing goes through the transform as a single unit), so you need to figure out all the different combinations of position, texture coordinate and normal and arrange the buffers you pass to
glVertexPointer
,glNormalPointer
andglTexCoordPointer
accordingly. For example, the above face might end up being specified via GL_TRIANGLES as between vertices 0, 1 and 2, where you'd copied the 127th position to the first in your internal vertex buffer, you'd copied the 73rd texture coordinate to the first in your internal texture coordinate buffer and you'd copied the 62nd normal to the first in your internal normal list.Quite probably you want a HashMap taking the combination key vertex/texture coordinate/normal and mapping to a position in your internal arrays. As you come across each face in the incoming OBJ, you can check whether you've already got that combination allocated to a spot in your internal buffers and give it the next available one if not.
vt is a texture coord, so e.g. the texture coordinate:
Means that, within texture space, x = 0.495011, y = 0.389417. From memory, the OBJ file format and OpenGL have the y axes in opposite directions. So in OpenGL terms you'd want s = 0.495011, t = 1 - 0.389417 = 0.610583.
If your object is using a suitably complicated texture atlas or skinning, I imagine it would be far from obvious that the y coordinates are flipped; if you just have something like a cube with the same texture repeated in its entirety on each face then you'd probably just see it as being flipped along the vertical.
Does that cover the source of confusion?