2012-02-29 45 views
1

我有这种方法准备posCoords数组中的坐标。它在大约30%的时间内正常工作,然后其他70%的前三个三角形在网格中混乱。整个网格使用GL_TRIANGLE_STRIP绘制。 我拉我的头发试图找出什么是错的。有任何想法吗?GL_TRIANGLE_STRIP和顶点数组的错误

enter image description here

enter image description here

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; 



} 

回答

0

发现问题:纹理缓冲区溢出到顶点缓冲区,它是随机的,因为一些后台任务在计时器上混洗内存(有时)

1

随着三角形带你添加的最后一个顶点替换使用让你在使用沿着边缘坏顶点最古老的顶点。用你的绘图解释起来更容易。

  • 三角1使用顶点1,2,3 - 有效三角形
  • 三角2使用顶点2,3,4 - 有效三角形
  • 三角3使用顶点4,5,6 - 有效三角形
  • 三角4使用顶点5,6,7 - 直线,没有什么会被吸引
  • 三角5使用顶点6,7,8 - 有效

如果你想让你的钢带工作,你需要用退化的三角形来填充你的钢带或打破你的钢带。

0

我倾向于从左到右画,在行的末尾添加一个退化的三角形,然后再从左到右。

例如[1,2,3,4,5,6; 6 10; 10,11,8,9,6,7]

中间部分被称为退化三角形(例如零面积的三角形)。另外,如果我不得不猜测为什么你会看到各种各样的腐败现象,我会检查以确保你的顶点和索引正是你期望的那样 - 通常你会看到那种如果您没有正确指定索引,就会发生腐败