帮助从网格中获取顶点索引

时间:2011-08-10 12:48:36

标签: opengl-es gl-triangle-strip

我正在接受这个关于如何从网格中获取索引以构建GL_TRIANGLE_STRIP网格的小教程http://dan.lecocq.us/wordpress/2009/12/25/triangle-strip-for-grids-a-construction/

我按正确的顺序得到了一些索引,但我无法在最后一个图中显示的顶点7和8上校正逻辑

这是我的代码:

cols = 4;
rows = 4;

sizeW = 320.0f;
sizeH = 240.0f;

float spaceX = sizeW / cols;
float spaceY = sizeH / rows;

// Mesh indices
for ( int x = 0; x < cols-1; x++ ) {
    for ( int y = 0; y < rows-1; y++ ) {
        int i = y + x * rows;

        cout << "{a, b, c}: " << i << ", " << i+4 << ", " << (i+4)-3;
        cout << endl;
    }
    cout << "------------" << endl;
}
vboMesh.setMesh( mesh, GL_DYNAMIC_DRAW );

cout << "mesh number of vertices: " << mesh.getNumVertices() << endl;

这是我的输出:

0, 4, 1
1, 5, 2
2, 6, 3
--------
4, 8, 5
5, 9, 6
6, 10, 7
--------
8, 12, 9
9, 13, 10
10, 14, 11

更新 在评论之后,我研究另一种算法来获得指数,这是我到目前为止所做的:

// Mesh indices
int totalQuads  = (cols-1) * (rows-1);
int totalTriangles  = totalQuads * 2;
int totalIndices    = (cols*2) * (rows-1);
cout << "total number of quads: " << totalQuads << endl;
cout << "total number of triangles: " << totalTriangles << endl;
cout << "total number of indices: " << totalIndices << endl;

int n = 0;
int ind = 0;
vector<int> indices;
for ( int i = 0; i < totalIndices; i++ ) {
    //cout << i % (cols*2) << ", ";
    ind = i % (cols*2);
    if ( i % (cols*2) == 0 ) {
        n++;
        cout << n << endl;

        if ( n%2 == 1 ) {
            cout << "forward" << endl;
        }
        else {
            cout << "backward" << endl;
        }
    }

    indices.push_back( ind );
}
//cout << endl;

这段代码告诉我什么时候需要前进,当我需要后退时,通过 i%(cols * 2)我得到一个像0,1,2,3这样的列表, 4,5,6,7,0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,理论上我现在需要做的就是前进+4 -3,后退+4 -5

更新2: 取得了一些进展,这些都是新成果 0,4,1,5,2,6,3,7,7,11,6,10,5,9,4,8​​, 14,18,15,19,16,20,17,21 最后一组数字仍然是错误的

// Mesh indices
int totalQuads      = (cols-1) * (rows-1);
int totalTriangles  = totalQuads * 2;
int totalIndices    = (cols*2) * (rows-1);
cout << "total number of quads: " << totalQuads << endl;
cout << "total number of triangles: " << totalTriangles << endl;
cout << "total number of indices: " << totalIndices << endl;

bool isGoingBackwards = false;
int n = 0;
int ind = 0;
vector<int> indices;

for ( int i = 0; i < totalIndices; i++ ) {
    if ( i % (cols*2) == 0 ) {
        ind++;
        if ( ind%2 == 1 ) {
        n = ((cols*2) - 1) * (ind-1);
            cout << "forward " << n << endl;
        isGoingBackwards = false;
        }
        else {
            n = ((cols*2) - 1) * (ind-1);
        cout << "backward " << n << endl;
        isGoingBackwards = true;
        }
    }

    indices.push_back( n );


    if ( i%2 == 0 ) {
        n += 4;
    }
    else {
        ( isGoingBackwards ) ? n -= 5 : n -= 3;
    }
}

更新3:

我终于明白了!这是新代码

int n   = 0;
int colSteps = cols * 2;
int rowSteps = rows - 1;
vector<int> indices;
for ( int r = 0; r < rowSteps; r++ ) {
    for ( int c = 0; c < colSteps; c++ ) {
        int t = c + r * colSteps;

        if ( c == colSteps - 1 ) {
            indices.push_back( n );
        }
        else {
            indices.push_back( n );

            if ( t%2 == 0 ) {
                n += cols;
            }
            else {
                (r%2 == 0) ? n -= (cols-1) : n -= (cols+1);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

第一行的顶点顺序实际上是

0 4 1 5 2 6 3 7 

然后继续

7 11 6 10 5 9 4 8
8 12 9 13 10 14 11 15

三角形条的剔除会反转每个三角形。你必须将7和8放在两次的原因是,在那些点上剔除实际上不应该被逆转。实现这一目标的唯一可能性是通过反转剔除两次(实际渲染一个不可见的多边形),以便继续使用与之前相同的剔除方向。

答案 1 :(得分:0)

你这样写并写出来的方式显然是错误的。请记住,您正在生成三角形条带而不是单个三角形。我没有你的代码(但他很好地解释了原理),但是你必须以交替的方向遍历行,这意味着从左到右的顶行和从右到左的下一行,正如他解释的那样在他的文字和数字中。重复顶点(在本例中为7和8)背后的逻辑是,您需要在这些顶点处更改条带的方向,因此您需要复制这些顶点(并且这样插入退化三角形,这不会绘制任何像素)。