You need to draw the farthest polygons first and the closest polygons last, i.e. sort by depth (you can sort by depth of face centroid, for example), or alternatively, draw or not depending on visual handedness (backface culling). Backface culling only works as a method for convex objects, like a cube. Maybe I have missed where you do either of those.
Of course, you can also draw in actual 3D and let OpenGL handle the hidden surface invisibility, either with a depth buffer or with backface culling, or both (good for performance, but overkill for this case).
There should be 12 faces but you only have 11 listed in lines2. You also have a duplicate face, {8,3,4} is the same as {4,3,8} (except for handedness but you don't use it currently). Face {8,2,3} doesn't make sense, it does not lay in one of the cube faces, it crosses the cube through the inside.
Advice on how to build the faces: list the four vertices of each cube face (you need 6 lists), and for each, determine two diagonal points and place them in both triangles.
Code: Select all
lines2 = {
-- face 1, 2, 3, 4 - 2 and 3 are diagonal
{1, 2, 3}, {3, 2, 4},
-- face 1, 2, 5, 6 - 2 and 5 are diagonal
{1, 2, 5}, {5, 2, 6},
-- ... and so on
}
Handling handedness is a bit more complicated. Instead of that, you can draw an unfolded cube and label the vertices. Once labelled, you can easily create the list.
Edit: Here it is for you; this labelling follows your vertex order choice. Always enumerate them in counter-clockwise direction. Advice: Place diagonal vertices first. If you do it right, you should have one pair of vertices of the diagonal with one order, and one with the opposite.
Example:
Code: Select all
{8, 5, 6}, {5, 8, 7}, {7, 1, 5}, {1, 7, 3}, ...
Note how the first one starts with 8, 5 and the second with 5, 8, and so on. Each pair should have them swapped like that.
Note also how going from 8 to 5 to 6 to 8 to 5 to 6... follows a path that turns left (counter-clockwise). That gives you the right handedness.