2013-03-08 54 views
2

我一直試圖在OpenGL中創建一個圓,但是我不能使用三角形風扇,因爲我已經閱讀過它們在directx中不再可用,我也將進行directx調用。Circle with Triangle Strip

我真的不明白三角形條是如何工作的。我的所有實現都有漏洞或奇怪的怪癖,任何人都可以幫助我,我怎樣才能以最好的方式實現它?

另外,三角形帶和分離三角形之間真的有任何性能差異,例如可以說每個帶有1000個三角形的10個圓圈。它會有很大的不同嗎?指定與三角形帶圓

+0

應指定的目標,其Direct3D的版本。 – 2013-03-08 03:13:06

+0

@sion sheevok我在問怎麼在opengl中用三角形條畫一個實心的圓。 – Etherealone 2013-03-08 08:41:54

+0

你還有一個directx標籤,你只針對OpenGL嗎?如果是這樣,請刪除directx標籤。 – 2013-03-09 02:48:25

回答

5

的一種方法是如下:

for each step 
    add next position on circle 
    add circle center 

這將包括圓的中心位置。不包括中心的另一種方法是這樣的:

add left most vertex 
for each phi in (-PI/2, Pi/2) //ommit the first and last one 
    x = r * sin(phi) 
    y = r * cos(phi) 
    add (x, y) 
    add (x, -y) 
add right most vertex 

您可能需要調整取決於您的背面剔除設置

的拓撲需要頂點的不同數量的循環。對於三角形列表10個圓圈1000個三角形需要30,000個頂點。使用三角形條時,每個圓需要1002個頂點,因此總共需要10,020個頂點。這幾乎是傳輸到CPU時的三倍,應該快一點。如果這反映在FPS取決於幾件事情。

+0

不錯的一個。第二種方法比三角形風扇少一個垂直。 :-) – yoyo 2013-11-07 00:51:20

0
for each phi in (-PI, Pi) //ommit the first and last one 
    x1 = r * sin(phi) 
    y1 = r * cos(phi) 

    x2 = r * sin(phi + Pi) 
    y2 = r * cos(phi + Pi) 

    add (x1, y1) 
    add (x2, y2) 

相反,如上圖所示僞代碼在圓的一側上下襬動,這個圓形的中心將呈鋸齒形。這將有更圓滿的循環。

2

這是使用VAO/VBO的代碼和最小點數。與截至目前發佈的其他答案不同,它只需要一個cos()和一個sin()呼叫。

設置:

// Number of points used for half circle. 
const unsigned HALF_PREC = 10; 

const float angInc = M_PI/static_cast<float>(HALF_PREC); 
const float cosInc = cos(angInc); 
const float sinInc = sin(angInc); 

GLfloat* coordA = new GLfloat[2 * HALF_PREC * 2]; 
unsigned coordIdx = 0; 

coordA[coordIdx++] = 1.0f; 
coordA[coordIdx++] = 0.0f; 

float xc = 1.0f; 
float yc = 0.0f; 
for (unsigned iAng = 1; iAng < HALF_PREC; ++iAng) { 
    float xcNew = cosInc * xc - sinInc * yc; 
    yc = sinInc * xc + cosInc * yc; 
    xc = xcNew; 

    coordA[coordIdx++] = xc; 
    coordA[coordIdx++] = yc; 

    coordA[coordIdx++] = xc; 
    coordA[coordIdx++] = -yc; 
} 

coordA[coordIdx++] = -1.0f; 
coordA[coordIdx++] = 0.0f; 

GLuint vaoId = 0; 
glGenVertexArrays(1, &vaoId); 
glBindVertexArray(vaoId); 

GLuint vboId = 0; 
glGenBuffers(1, &vboId); 
glBindBuffer(GL_ARRAY_BUFFER, vboId); 
glBufferData(GL_ARRAY_BUFFER, 2 * HALF_PREC * 2 * sizeof(GLfloat), 
      coordA, GL_STATIC_DRAW); 

delete[] coordA; 

glEnableVertexAttribArray(0); 
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); 

glBindBuffer(GL_ARRAY_BUFFER, 0); 
glBindVertexArray(0); 

抽獎:

glBindVertexArray(vaoId); 
glDrawArrays(GL_TRIANGLE_STRIP, 0, 2 * HALF_PREC); 
glBindVertexArray(0);