0
\$\begingroup\$

code

Vertices are created by an .obj file. (loading/OBJFile.java)

I draw a cube perfectly fine with glDrawArrays. (VertexModel.java)
created like this:

new VertexModel(square.getIndexedVertexBuffer());  

However, when I try to do the same with glDrawElements i get a distorted cube.. (IndexedVertexModel.java)
The IndexedVertexModel is created like this:

new IndexedVertexModel(square.getVertexBuffer(), square.getVertexIndecies());

The result can be seen here.
I've tried to figure out what's wrong, but i really have no idea what causes it to behave this way.

The OBJ file I'm using looks like this:

v -1.00000000000000 -1.00000000000000 -1.00000000000000
v -1.00000000000000 1.00000000000000 -1.00000000000000
v 1.00000000000000 -1.00000000000000 -1.00000000000000
v 1.00000000000000 1.00000000000000 -1.00000000000000
v 1.00000000000000 -1.00000000000000 1.00000000000000
v 1.00000000000000 1.00000000000000 1.00000000000000
v -1.00000000000000 -1.00000000000000 1.00000000000000
v -1.00000000000000 1.00000000000000 1.00000000000000

f 1/4/1 2/3/1 4/2/1
f 3/4/3 4/3/3 6/2/3
f 5/4/4 6/3/5 8/2/4
f 7/4/6 8/3/6 2/2/6
f 2/4/7 8/3/7 6/2/7
f 7/4/8 1/3/8 3/2/8
f 1/4/1 4/4/1 3/3/1
f 3/4/3 6/4/3 5/3/3
f 5/4/4 8/4/4 7/3/4
f 7/4/6 2/4/6 1/3/6
f 2/4/7 6/4/7 4/3/7
f 7/4/8 3/4/8 5/3/8
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

OBJ vertex index values starts at 1

OpenGL vertex index values starts at 0

Subtract 1 from your loaded indices:

vertexIndices.add(Integer.parseInt(innerpart[0])-1);
textureIndices.add(Integer.parseInt(innerpart[1])-1);
normalIndices.add(Integer.parseInt(innerpart[2])-1);

And remove the -1 from your functions getIndexedTextureBuffer and the likes.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .