zyneragetro
zyneragetro

Reputation: 149

OpenGL - drawing indices from OBJ using GL_TRIANGLE_STRIP

I was trying to create an OBJ parser which will read OBJ files and render it using GL_TRIANGLE_STRIP. I know OBJ files behaves as TRIANGLES that's why as I read the faces, I rearrange them into STRIPS.

Example I have faces like this

    f 5 1 4
    f 5 4 8
    f 3 7 8

it will be in this order after I rearrange them minus 1 because OBJ files are 1 based indexes.

    f 4 0 3
    f 3 0 4
    f 0 4 3
    f 3 4 7
    f 4 7 2
    f 2 7 6
    f 7 6 7

I send this indices to OpenGL and draw them using glDrawElements but the output I get is wrong. Take a look here for the image.

I based the arrangement on the OpenGL documentation on using GL_TRIANGLE_STRIP and I'm stuck on this step as using GL_TRIANGLES works for me. What would cause the incorrect output?

Upvotes: 0

Views: 2454

Answers (1)

stgatilov
stgatilov

Reputation: 5533

The .obj file contains indexes suitable for rendering in GL_TRIANGLES mode. You render them in GL_TRIANGLES_STRIP mode. You are completely wrong here. Render them in GL_TRIANGLES mode and be happy.

Rendering the same index data in other mode is almost always incorrect. GL_TRIANGLES mode uses three indices per triangle, while GL_TRIANGLE_STRIP uses one index per triangle on average (it is compressed format). So unless you have compressed your index data in approximately three times, you cannot use your indices to draw as GL_TRIANGLE_STRIP.

See wikipedia article for details. Below you can see result of rendering indices {0, 1, 2, 3, 4, 5, 6} in GL_TRIANGLE_STRIP. Obviously, if you render them in GL_TRIANGLES mode, you'll see only 2 triangles instead of 5.

GL_TRIANGLES_STRIP example

Upvotes: 1

Related Questions