GL_TRIANGLE_STRIP和顶点数组出错

时间:2012-02-29 04:36:06

标签: objective-c ios ipad opengl-es

我有这个方法来准备posCoords数组中的坐标。它在大约30%的时间内正常工作,然后另外70%的前几个三角形在网格中混乱。 使用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;



}

3 个答案:

答案 0 :(得分:1)

使用三角形条带时,您添加的最后一个顶点将替换使用的最旧顶点,因此沿着边缘使用不良顶点。用你的绘图来解释会更容易。

  • 三角形1使用顶点1,2,3 - 有效三角形
  • 三角形2使用顶点2,3,4 - 有效三角形
  • 三角形3使用顶点4,5,6 - 有效三角形
  • 三角形4使用顶点5,6,7 - 直线,不会绘制任何内容
  • 三角形5使用顶点6,7,8 - 有效

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

答案 1 :(得分:0)

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

e.g。 [1,2,3,4,5,6; 6 10; 10,11,8,9,6,7]

中间部分称为简并三角形(例如零区域的三角形)。

另外,如果我不得不猜测为什么你会看到各种腐败,我会检查以确保你的顶点和索引正是你所期望的那样 - 通常你会看到那种腐败如果没有正确指定索引。

答案 2 :(得分:0)

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

相关问题