I'm developing an app for iOS-Plaftorms using OpenGL.
Currently I'm having a weird issue when painting a plane (terrain) which consists of multiple subplanes, where each subplane consists of 2 triangles forming a rect. I'm painting this terrain as a wireframe by using a call to glDrawElements and provide the parameters GL_Line_Strip and the precalculated indices. The problem is that the triangles get painted in the wrong order or are rather vertically mirrored. They do not get painted in the order how I specified the indices, which is confusing.
This is the simplified code to generate the vertices:
for(NSInteger y = - gridSegmentsY / 2; y < gridSegmentsY / 2; y ++) {
for(NSInteger x = - gridSegmentsX / 2; x < gridSegmentsX / 2; x ++) {
vertices[pos++] = x * 5;
vertices[pos++] = y * 5;
vertices[pos++] = 0;
}
}
This is how I generate the indices including degenerated ones (To use as IBO).
pos = 0;
for(int y = 0; y < gridSegmentsY - 1; y ++) {
if (y > 0) {
// Degenerate begin: repeat first vertex
indices[pos++] = (unsigned short)(y * gridSegmentsY);
}
for(int x = 0; x < gridSegmentsX; x++) {
// One part of the strip
indices[pos++] = (unsigned short)((y * gridSegmentsY) + x);
indices[pos++] = (unsigned short)(((y + 1) * gridSegmentsY) + x);
}
if (y < gridSegmentsY - 2) {
// Degenerate end: repeat last vertex
indices[pos++] = (unsigned short)(((y + 1) * gridSegmentsY) + (gridSegmentsX - 1));
}
}
So in this part...
indices[pos++] = (unsigned short)((y * gridSegmentsY) + x);
indices[pos++] = (unsigned short)(((y + 1) * gridSegmentsY) + x);
...I'm setting the first index in the indices array to point to the current (x,y) and the next index to (x,y+1). I'm doin' this for all x's in the current strip, then I'm handling degenerated triangles and repeat this procedure for the next strip (y+1).
This method is taken from http://www.learnopengles.com/android-lesson-eight-an-introduction-to-index-buffer-objects-ibos/
So I expect the resulting mesh to get painted like:
a----b----c
| /| /|
| / | / |
| / | / |
|/ |/ |
d----e----f
| /| /|
| / | / |
| / | / |
|/ |/ |
g----h----i
by painting it as described using:
glDrawElements(GL_LINE_STRIP, indexCount, GL_UNSIGNED_SHORT, 0);
...since I expect GL_Line_Strip to paint first a line from (a-d), then from (d-b), then (b, e)... and so on (as specified in the indices calculation)
But what actually gets painted is:
*----*----*
|\ |\ |
| \ | \ |
| \ | \ |
| \| \|
*----*----*
|\ |\ |
| \ | \ |
| \ | \ |
| \| \|
*----*----*
So the triangles are somehow painted in the wrong order and I need to know why? ;). Does somebody know? Does the problem lie in using GL_Line_Strip or is there a bug in my code?
My eye is at (0.0f, 0.0f, 20.0f) and looks at (0,0,0). The mesh is painted along the x-axis & y-axis from left to right with z = 0, so the mesh should not be flipped or anything.