Broken polygon rendering

Hi,

I’m still new to OpenGL and recently I’ve stumbled into a problem which I still can’t solve. I am using my own .obj loader to load simple cube from 3DS Max, but when I try to draw it in OpenGL, while moving with my camera the polygons are “flickering?” The image shown below should be self explanatory, atleast I hope. I’m sure somebody of you also encountered similar problem and know how to solve it, but if some part of my code is needed for you, I will post it.

[ATTACH=CONFIG]1331[/ATTACH]

The topology may be wrong. Did you remember to subtract one from the indices (or add a dummy “vertex zero”)? OBJ uses one-based indices, glDrawElements() uses zero-based indices (as do arrays in C).

Is the cube made of quads or triangles?

Sometimes polygon flickering is caused by GL trying to render concave polygons.

Look at the ‘f’ statements in the .obj file. Do any of them have more than 3 or more than 4 vertices?

Thank you for your replies, I think I’ve taken both things you mentioned into account when i parsed the .obj file. This is the information from .obj file I’m parsing:


v  -0.1000 -0.1000 0.1000
v  0.1000 -0.1000 0.1000
v  -0.1000 -0.1000 -0.1000
v  0.1000 -0.1000 -0.1000
v  -0.1000 0.1000 0.1000
v  0.1000 0.1000 0.1000
v  -0.1000 0.1000 -0.1000
v  0.1000 0.1000 -0.1000
# 8 vertices

vn 0.0000 -1.0000 -0.0000
vn 0.0000 -1.0000 -0.0000
vn 0.0000 -1.0000 -0.0000
vn 0.0000 -1.0000 -0.0000
vn 0.0000 -1.0000 -0.0000
vn 0.0000 -1.0000 -0.0000
vn 0.0000 1.0000 -0.0000
vn 0.0000 1.0000 -0.0000
vn 0.0000 1.0000 -0.0000
vn 0.0000 1.0000 -0.0000
vn 0.0000 1.0000 -0.0000
vn 0.0000 1.0000 -0.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 0.0000 1.0000
vn 1.0000 0.0000 -0.0000
vn 1.0000 0.0000 -0.0000
vn 1.0000 0.0000 -0.0000
vn 1.0000 0.0000 -0.0000
vn 1.0000 0.0000 -0.0000
vn 1.0000 0.0000 -0.0000
vn 0.0000 0.0000 -1.0000
vn 0.0000 0.0000 -1.0000
vn 0.0000 0.0000 -1.0000
vn 0.0000 0.0000 -1.0000
vn 0.0000 0.0000 -1.0000
vn 0.0000 0.0000 -1.0000
vn -1.0000 0.0000 -0.0000
vn -1.0000 0.0000 -0.0000
vn -1.0000 0.0000 -0.0000
vn -1.0000 0.0000 -0.0000
vn -1.0000 0.0000 -0.0000
vn -1.0000 0.0000 -0.0000
# 36 vertex normals

vt 0.0000 0.0000 0.0000
vt 1.0000 0.0000 0.0000
vt 0.0000 1.0000 0.0000
vt 1.0000 1.0000 0.0000
vt 0.0000 0.0000 0.0000
vt 1.0000 0.0000 0.0000
vt 0.0000 1.0000 0.0000
vt 1.0000 1.0000 0.0000
vt 0.0000 0.0000 0.0000
vt 1.0000 0.0000 0.0000
vt 0.0000 1.0000 0.0000
vt 1.0000 1.0000 0.0000
# 12 texture coords

f 1/10/1 3/12/2 4/11/3 
f 4/11/4 2/9/5 1/10/6 
f 5/9/7 6/10/8 8/12/9 
f 8/12/10 7/11/11 5/9/12 
f 1/5/13 2/6/14 6/8/15 
f 6/8/16 5/7/17 1/5/18 
f 2/1/19 4/2/20 8/4/21 
f 8/4/22 6/3/23 2/1/24 
f 4/5/25 3/6/26 7/8/27 
f 7/8/28 8/7/29 4/5/30 
f 3/1/31 1/2/32 5/4/33 
f 5/4/34 7/3/35 3/1/36 
# 12 faces

And this is the information I’m sending for drawing elements after all the parsing. First position, then normals, then UVs.

Vertex 1: [-0.1, -0.1, 0.1], [0 -1 -0], [1, 0]
Vertex 2: [-0.1, -0.1, -0.1], [0 -1 -0], [1, 1]
Vertex 3: [0.1, -0.1, -0.1], [0 -1 -0], [0, 1]
Vertex 4: [0.1, -0.1, 0.1], [0 -1 -0], [0, 0]
Vertex 5: [-0.1, 0.1, 0.1], [0 1 -0], [0, 0]
Vertex 6: [0.1, 0.1, 0.1], [0 1 -0], [1, 0]
Vertex 7: [0.1, 0.1, -0.1], [0 1 -0], [1, 1]
Vertex 8: [-0.1, 0.1, -0.1], [0 1 -0], [0, 1]
Vertex 9: [-0.1, -0.1, 0.1], [0 0 1], [0, 0]
Vertex 10: [0.1, -0.1, 0.1], [0 0 1], [1, 0]
Vertex 11: [0.1, 0.1, 0.1], [0 0 1], [1, 1]
Vertex 12: [-0.1, 0.1, 0.1], [0 0 1], [0, 1]
Vertex 13: [0.1, -0.1, 0.1], [1 0 -0], [0, 0]
Vertex 14: [0.1, -0.1, -0.1], [1 0 -0], [1, 0]
Vertex 15: [0.1, 0.1, -0.1], [1 0 -0], [1, 1]
Vertex 16: [0.1, 0.1, 0.1], [1 0 -0], [0, 1]
Vertex 17: [0.1, -0.1, -0.1], [0 0 -1], [0, 0]
Vertex 18: [-0.1, -0.1, -0.1], [0 0 -1], [1, 0]
Vertex 19: [-0.1, 0.1, -0.1], [0 0 -1], [1, 1]
Vertex 20: [0.1, 0.1, -0.1], [0 0 -1], [0, 1]
Vertex 21: [-0.1, -0.1, -0.1], [-1 0 -0], [0, 0]
Vertex 22: [-0.1, -0.1, 0.1], [-1 0 -0], [1, 0]
Vertex 23: [-0.1, 0.1, 0.1], [-1 0 -0], [1, 1]
Vertex 24: [-0.1, 0.1, -0.1], [-1 0 -0], [0, 1]

Index 1: 1
Index 2: 2
Index 3: 3
Index 4: 3
Index 5: 4
Index 6: 1
Index 7: 5
Index 8: 6
Index 9: 7
Index 10: 7
Index 11: 8
Index 12: 5
Index 13: 9
Index 14: 10
Index 15: 11
Index 16: 11
Index 17: 12
Index 18: 9
Index 19: 13
Index 20: 14
Index 21: 15
Index 22: 15
Index 23: 16
Index 24: 13
Index 25: 17
Index 26: 18
Index 27: 19
Index 28: 19
Index 29: 20
Index 30: 17
Index 31: 21
Index 32: 22
Index 33: 23
Index 34: 23
Index 35: 24
Index 36: 21

I hope that i understand how this indexing works, but if anybody could look at this and see if I’m making any mistake in this process I’ll be very grateful.

That much is correct, provided that you’re explicitly adding one to the indices which you print, and aren’t actually using one-based indices in the element array (even if you’re using a language which uses one-based array indices, glDrawElements() requires zero-based indices).