2011-08-10 14 views
3

我folloing如何從電網建設GL_TRIANGLE_STRIP獲得指數這個小教程網http://dan.lecocq.us/wordpress/2009/12/25/triangle-strip-for-grids-a-construction/幫助從電網獲取頂點索引

我得到的一些指標以正確的順序,但我可以「T鍛鍊上的頂點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 

UPDATE: 繼意見,我鍛鍊另一種算法來獲得指標,這是我到目前爲止有:

// 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 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放入兩次的原因是,在這些點上,淘汰賽實際上不應該逆轉。實現這一點的唯一可能性是通過反轉剔除兩次(實際渲染一個不可見的多邊形),以便繼續使用與之前相同的剔除方向。

+0

我看,我的for循環都是錯誤的,我所需要做的就是構建並排列這個序列,好吧,我會試試看...我是opengl的新手,所以這一切都證明了有點難以消化 –

0

你這樣做並寫出來的方式顯然是錯誤的。請記住,您正在生成三角形條而不是單個三角形。我沒有代碼給你(但他很好地解釋了這些原則),但是你必須沿着交替的方向遍歷行,這意味着從左到右的第一行和從右到左的下一行,正如他解釋的那樣在他的文字和數字。重複頂點(在本例中爲7和8)背後的邏輯是,您需要在這些頂點上更改條的方向,因此您需要複製這些頂點(並且以這種方式插入退化三角形,不會繪製任何像素)。