OpenGL-使用glDrawElements纹理映射不正确 [英] OpenGL - texture are mapped incorrectly using glDrawElements

查看:649
本文介绍了OpenGL-使用glDrawElements纹理映射不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个OBJ解析器,该解析器读取文件并使用glDrawElements进行绘制.这里的事情是正确绘制了模型,但是当我尝试给它贴图时,它的映射不正确.我已阅读此处,其中"OpenGL只能使用一个索引缓冲区,而OBJ按属性使用一个索引缓冲区".我还研究了使用glDrawElements时纹理映射不正确的情况,发现我需要重新排列纹理,因为仅共享顶点,而不共享纹理.说完所有这些,我应该如何重新排列纹理?我应该复制一些纹理吗?请参阅此链接以获取我得到的示例输出.

I am creating an OBJ parser which reads the file and draw it using glDrawElements. The thing here is the model is correctly drawn but when I try to give it a texture, it is incorrectly mapped. I have read here that "OpenGL can only use one index buffer, whereas OBJ use one index buffer by attribute". I have also researched about the textures being incorrectly mapped when using glDrawElements and found out that I need to rearrange the textures because only vertices are shared but not the textures. Having said all of this, how should I suppose to rearrange the textures? Should I duplicate some textures? Please refer to this link for the sample output I get.

推荐答案

OBJ格式指定UNIQUE纹理坐标,顶点和面.

The OBJ format specifies UNIQUE texture coordinates, vertices and faces.

Opengl与OBJ无关,因此,它所需要的只是在正确的位置提供正确的信息.

Opengl has nothing to do with OBJ, of course, so all it wants is the right information at the right place.

在您的OBJ中想象两个这样的面(两个面都注意顶点1 texcoords)

Imagine two faces like this in your OBJ (Notice vertex 1 texcoords in both faces)

f:    v1 |t1|     v2 t2      v3 t3 
f:    v1 |t2|     v4 t4      v5 t5 

您的INDEX缓冲区是这样的:

Your INDEX buffer like this:

1 2 3     1 4 5

您的opengl VERTEX缓冲区首先看起来像这样:

Your opengl VERTEX buffer first looks like this:

v1 v2 v3 v4 v5

如果添加纹理坐标,应在哪里添加它们?这样吗?

If you add texture coordinates, where do you add them? Like this?

v1 t1     v2 t2     v3 t3     v4 t4      v5 t5

这很可能是您现在所拥有的,这是错误的,因为opengl会像这样:

This is likely what you have now, its wrong because opengl makes the faces like this:

f:    v1 |t1|      v2 t2      v3 t3 
f:    v1 |t1|      v4 t4      v5 t5 

由于v1和t1通过索引缓冲区链接在一起,因此请注意OBJ和OpenGL中的顶点之间的差异.

Because v1 and t1 are linked together by your indexbuffer, notice the difference between the vertices in the OBJ and in OpenGL.

有两种方法可以解决此问题.我建议您删除索引缓冲区,因为它只会使所有事情变得复杂.没有索引缓冲区,您只需简单地提供所有面,就好像它们是唯一的一样,并使用不同的绘制调用(glDrawArrays).您的顶点缓冲区将如下所示:

There are a couple of ways to solve this. I'd recommend getting rid of the indexbuffer, because it only complicates everything. Without the index buffer, you simply supply all faces as if they are unique, and use a different draw call (glDrawArrays). Your vertex buffer will look like this:

v1 |t1|      v2 t2       v3 t3       v1 |t2|       v4 t4        v5 t5

您复制了v1,但实际上大部分时间它并没有太多额外的数据,因为您不必再​​指定索引缓冲区.

You duplicate v1, but really its not so much extra data most of the time, since you don't have to specify an index buffer anymore.

这种东西真的很烦人,我尽力使它变得平易近人,希望我没有犯任何错误.

This kind of stuff is really annoying to think about, I tried my best to make it approachable, and I hope I didn't make any mistakes.

这篇关于OpenGL-使用glDrawElements纹理映射不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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