The OBJ format specifies UNIQUE texture coordinates, vertices and faces.
Opengl has nothing to do with OBJ, of course, so all it wants is the right information at the right place.
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
Your INDEX buffer like this:
1 2 3 1 4 5
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
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
Because v1 and t1 are linked together by your indexbuffer, notice the difference between the vertices in the OBJ and in OpenGL.
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
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…