3

グリッドからインデックスを取得して 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);
            }
        }
    }
}
4

2 に答える 2

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 を 2 回入れなければならない理由は、これらの時点でカリングが実際に逆になってはならないからです。これを達成する唯一の可能性は、カリングを 2 回逆にする (実際には 1 つの非表示のポリゴンをレンダリングする) ことであり、以前と同じカリング方向で続行します。

于 2011-08-10T13:06:50.577 に答える
0

あなたがそれをしてそれを書き出す方法は明らかに間違っています。個々の三角形ではなく、三角ストリップを生成していることを忘れないでください。私はあなたのためのコードを持っていません(しかし彼は原則を非常によく説明しています)が、彼が説明するように、あなたは行を交互の方向にトラバースする必要があります。彼のテキストと図で。頂点(この場合は7と8)の繰り返しの背後にあるロジックは、これらの頂点でストリップの方向を変更する必要があるため、これらの頂点を複製する必要があるということです(このようにして、縮退した三角形を挿入します。任意のピクセルを描画します)。

于 2011-08-10T12:58:42.393 に答える