objective-c - GL_TRIANGLE_STRIP 和顶点数组出错

标签 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;



}

最佳答案

对于三角形带,您添加的最后一个顶点会替换使用的最旧的顶点,因此您沿边使用了错误的顶点。用你的图更容易解释。

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

等等

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

关于objective-c - GL_TRIANGLE_STRIP 和顶点数组出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9493641/

相关文章:

iphone - 我需要让我的 UITabbar 滚动

ios - 连接到已经初始化的 View Controller

python - Objective-C 与 Python 中的 RegEx 性能

ios - 如何水平居中两个标签?

ios - 更新已从应用商店中删除的iOS应用

ios - 如何在不加载以前的数据的情况下从父 View 中删除和添加 UICollectionView ?

ios - 图像内存未被释放

ios - IOS 键盘按下

ios - CIFilter 减慢滚动速度

iOS 应用内购买拒绝 : Missing IAPs