2013-03-20 53 views
0

我正在Android設備上使用OpenGL ES 2.0。通過極座標確定球體頂點並渲染它

我想獲得一個領域,運行和繪圖。 Currentley,我幾乎有一個領域,但顯然它做得非常非常錯誤。

在我的應用程序中,我持有Vector3的列表,我將其轉換爲ByteBuffer,並傳遞給OpenGL。 我知道我的代碼是好的,因爲我有一個立方體和四面體繪製nicley。 我改變了兩個部分: 確定頂點 繪製頂點。

下面是有問題的代碼snippits。我究竟做錯了什麼? 確定極座標:

private void ConstructPositionVertices() 
{ 
    for (float latitutde = 0.0f; latitutde < (float)(Math.PI * 2.0f); latitutde += 0.1f) 
    { 
     for (float longitude = 0.0f; longitude < (float)(2.0f * Math.PI); longitude += 0.1f) 
     { 
      mPositionVertices.add(ConvertFromSphericalToCartesian(1.0f, latitutde, longitude)); 
     } 
    } 
} 

從極地轉換成直角座標:

public static Vector3 ConvertFromSphericalToCartesian(float inLength, float inPhi, float inTheta) 
{ 
    float x = inLength * (float)(Math.sin(inPhi) * Math.cos(inTheta)); 
    float y = inLength * (float)(Math.sin(inPhi) * Math.sin(inTheta)); 
    float z = inLength * (float)Math.cos(inTheta); 
    Vector3 convertedVector = new Vector3(x, y, z); 
    return convertedVector; 
} 

畫圓:

inGL.glDrawArrays(GL10.GL_TRIANGLES, 0, numVertices); 

很顯然,我省略了一些代碼,但我肯定我的錯誤在於在這些snippits某處。 我沒有做任何事情比傳遞給OpenGL更多,然後調用Triangles,它應該爲我連接點..對不對?編輯: 圖片可能是不錯的!

SphereFail

回答

1

您的Z應該在用披計算。 float z = inLength * (float)Math.cos(inPhi);

此外,所產生的點不是三角形所以這將是最好使用GL_LINE_STRIP

+0

緯度的範圍也是從0到PI只有對不對? – DanP 2013-03-21 07:37:40

+0

尤普,我的Z是錯的。使用線條圖,它作爲一個球體出現,謝謝!現在我只需要弄清楚如何將這些頂點索引爲三角形:x – MintyAnt 2013-03-21 17:15:43

1

使用極地球面三角形帶是如在對圖點作爲容易,例如:

const float GL_PI = 3.141592f; 

GLfloat x, y, z, alpha, beta; // Storage for coordinates and angles   
GLfloat radius = 60.0f; 
const int gradation = 20; 

for (alpha = 0.0; alpha < GL_PI; alpha += GL_PI/gradation) 
{   
    glBegin(GL_TRIANGLE_STRIP); 
    for (beta = 0.0; beta < 2.01*GL_PI; beta += GL_PI/gradation)    
    {    
     x = radius*cos(beta)*sin(alpha); 
     y = radius*sin(beta)*sin(alpha); 
     z = radius*cos(alpha); 
     glVertex3f(x, y, z); 
     x = radius*cos(beta)*sin(alpha + GL_PI/gradation); 
     y = radius*sin(beta)*sin(alpha + GL_PI/gradation); 
     z = radius*cos(alpha + GL_PI/gradation);    
     glVertex3f(x, y, z);    
    }   
    glEnd(); 
} 

輸入的第一個點如下公式,第二個點移動α角的單個步驟(從下一個平行線開始)。