Reputation: 968
I have this method that prepares the coordinates in the posCoords array. It works properly about 30% of the time, then the other 70% the first few triangles are messed up in the grid. The entire grid is drawn using GL_TRIANGLE_STRIP. I'm pulling my hair out trying to figure out whats wrong. Any ideas?
if(!ES2) {
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
int cols = floor(SCREEN_WIDTH/blockSize);
int rows = floor(SCREEN_HEIGHT/blockSize);
int cells = cols*rows;
NSLog(@"Cells: %i", cells);
coordCount = /*Points per coordinate*/2 * /*Coordinates per cell*/ 2 * cells + /* additional coord per row */2*2*rows;
NSLog(@"Coord count: %i", coordCount);
if(texCoords) free(texCoords);
if(posCoords) free(posCoords);
if(dposCoords) free(dposCoords);
texCoords = malloc(sizeof(GLfloat)*coordCount);
posCoords = malloc(sizeof(GLfloat)*coordCount);
dposCoords = malloc(sizeof(GLfloat)*coordCount);
int index = 0;
float lowY, hiY = 0;
int x,y = 0;
BOOL drawLeftToRight = YES;
for(y=0;y<SCREEN_HEIGHT;y+=blockSize) {
lowY = y;
hiY = y + blockSize;
// Draw a single row
for(x=0;x<=SCREEN_WIDTH;x+=blockSize) {
CGFloat px,py,px2,py2 = 0;
// Top point of triangle
if(drawLeftToRight) {
px = x;
py = lowY;
// Bottom point of triangle
px2 = x;
py2 = hiY;
}
else {
px = SCREEN_WIDTH-x;
py = lowY;
// Bottom point of triangle
px2 = SCREEN_WIDTH-x;
py2 = hiY;
}
// Top point of triangle
posCoords[index] = px;
posCoords[index+1] = py;
// Bottom point of triangle
posCoords[index+2] = px2;
posCoords[index+3] = py2;
texCoords[index] = px/SCREEN_WIDTH;
texCoords[index+1] = py/SCREEN_HEIGHT;
texCoords[index+2] = px2/SCREEN_WIDTH;
texCoords[index+3] = py2/SCREEN_HEIGHT;
index+=4;
}
drawLeftToRight = !drawLeftToRight;
}
Upvotes: 1
Views: 1204
Reputation: 968
Found the issue: the texture buffer was overflowing in to the vertex buffer, it was random because some background tasks where shuffling memory around on a timer (sometimes)
Upvotes: 0
Reputation: 1476
I tend to draw left to right and at the end of the row add a degenerate triangle, then left to right again.
e.g. [1, 2, 3, 4, 5, 6; 6 10; 10, 11, 8, 9, 6, 7]
The middle part is called a degenerate triangle (e.g. triangles of zero area).
Also, if I had to take a guess at why you are seeing various kinds of corruption, I'd check to make sure that your vertices and indices are exactly what you expect them to be - normally you see that kind of corruption when you don't specify indices correctly.
Upvotes: 0
Reputation: 11
With a triangle strip the last vertex you add replaces the the oldest vertex used so you're using bad vertices along the edge. It's easier to explain with your drawing.
etc.
If you want your strips to work, you'll need to pad your strips with degenerate triangles or break your strips up.
Upvotes: 1