2010-08-14 94 views
2

我使用GPC鑲嵌庫並輸出三角形條。 的實例給出呈現這樣的:將三角形條轉換爲三角形?

for (s = 0; s < tri.num_strips; s++) 
{ 
    glBegin(GL_TRIANGLE_STRIP); 
    for (v = 0; v < tri.strip[s].num_vertices; v++) 
     glVertex2d(tri.strip[s].vertex[v].x, tri.strip[s].vertex[v].y); 
    glEnd(); 
} 

的問題是事實,這使得多個三角形帶。這對我來說是個問題。我的應用程序使用VBO渲染,特別是1個VBO渲染1個多邊形。我需要一種方法來修改上面的代碼,以便它可以看起來更像這樣:

glBegin(GL_TRIANGLES); 
for (s = 0; s < tri.num_strips; s++) 
{ 
    // How should I specify vertices here?  
} 
glEnd(); 

我該怎麼做?

回答

10

perticularly 1 VBO 1個多邊形

哇。 1每個多邊形的VBO不會有效。殺死頂點緩衝區的全部原因。頂點緩衝區的想法是將盡可能多的頂點塞進它。您可以將多個三角形條放入一個頂點緩衝區,或者呈現存儲在一個緩衝區中的單獨基元。

我需要一種方法來修改上面的代碼,以便相反,它可能看起來更像是這樣的:

這應該工作:

glBegin(GL_TRIANGLES); 
for (v= 0; v < tri.strip[s].num_vertices-2; v++) 
    if (v & 1){ 
     glVertex2d(tri.strip[s].vertex[v].x, tri.strip[s].vertex[v].y); 
     glVertex2d(tri.strip[s].vertex[v+1].x, tri.strip[s].vertex[v+1].y); 
     glVertex2d(tri.strip[s].vertex[v+2].x, tri.strip[s].vertex[v+2].y); 
    } 
    else{ 
     glVertex2d(tri.strip[s].vertex[v].x, tri.strip[s].vertex[v].y); 
     glVertex2d(tri.strip[s].vertex[v+2].x, tri.strip[s].vertex[v+2].y); 
     glVertex2d(tri.strip[s].vertex[v+1].x, tri.strip[s].vertex[v+1].y); 
    } 
glEnd(); 

因爲trianglestrip三角是這樣的(數字代表頂點索引):

0----2 
| /| 
|/| 
|/| 
|/ | 
1----3 

注意:I a假設三角形標籤中的頂點以與我的圖片相同的順序存儲,並且您希望三角形頂點按照逆時針順序發送。如果你想要它們是CW,那麼使用不同的代碼:

glBegin(GL_TRIANGLES); 
for (v= 0; v < tri.strip[s].num_vertices-2; v++) 
    if (v & 1){ 
     glVertex2d(tri.strip[s].vertex[v].x, tri.strip[s].vertex[v].y); 
     glVertex2d(tri.strip[s].vertex[v+2].x, tri.strip[s].vertex[v+2].y); 
     glVertex2d(tri.strip[s].vertex[v+1].x, tri.strip[s].vertex[v+1].y); 
    } 
    else{ 
     glVertex2d(tri.strip[s].vertex[v].x, tri.strip[s].vertex[v].y); 
     glVertex2d(tri.strip[s].vertex[v+1].x, tri.strip[s].vertex[v+1].y); 
     glVertex2d(tri.strip[s].vertex[v+2].x, tri.strip[s].vertex[v+2].y); 
    } 
glEnd(); 
+0

By 1 vbo per polygon我的意思是一系列的三角形,對不起 – jmasterx 2010-08-14 21:25:13