2016-02-16 140 views
1

我一直在尋找幾個小時來找到一個在JOGL中繪製紋理球體的好方法。我需要的只是朝正確的方向發展,或者某些代碼適用於某人。到目前爲止,我所能找到的是helloTexture(雖然它有效,但顯然不是球體),是否有辦法將其轉換爲球體,或者我應該在其他地方嘗試運氣?在JOGL中渲染紋理球體

+1

你可以建立。只需添加更多的頂點以形成一個球體並在這些頂點之間繪製三角形。如果你不知道如何建議你先找一個關於繪製三角形的教程,並試着理解它是如何工作的。從這裏走向球體或任何其他形狀只是繪製更多三角形的問題。 – Thomas

+0

看看這個答案。應該是同樣的原則。只是編碼在C + +:http://stackoverflow.com/questions/7687148/drawing-sphere-in-opengl-without-using-glusphere – flakes

+1

更好不@flkes,使用不贊成的'GL_QUADS' – elect

回答

1

我寫這個給你,這是未經測試,因爲我修改了它與GL_TRIANGLES工作,試圖讓我知道

radius是球體半徑,rings是水平切片,sectors垂直那些

private void createGeometry(float radius, short rings, short sectors) { 

    float R = 1f/(float)(rings - 1); 
    float S = 1f/(float)(sectors - 1); 
    short r, s; 
    float x, y, z; 

    points = new float[rings * sectors * 3]; 
    normals = new float[rings * sectors * 3]; 
    texcoords = new float[rings * sectors * 2]; 

    int t = 0, v = 0, n = 0; 
    for(r = 0; r < rings; r++) { 
     for(s = 0; s < sectors; s++) { 
      x = (float) (Math.cos(2 * Math.PI * s * S) * Math.sin(Math.PI * r * R)); 
      y = (float) Math.sin(-Math.PI/2 + Math.PI * r * R); 
      z = (float) (Math.sin(2 * Math.PI * s * S) * Math.sin(Math.PI * r * R)); 

      texcoords[t++] = s * S; 
      texcoords[t++] = r * R; 

      points[v++] = x * radius; 
      points[v++] = y * radius; 
      points[v++] = z * radius; 

      normals[n++] = x; 
      normals[n++] = y; 
      normals[n++] = z; 
     } 


    } 
    int counter = 0; 
    indices = new short[rings * sectors * 6]; 
    for(r = 0; r < rings - 1; r++){ 
     for(s = 0; s < sectors-1; s++) { 
      indices[counter++] = (short) (r * sectors + s);  
      indices[counter++] = (short) (r * sectors + (s + 1)); 
      indices[counter++] = (short) ((r + 1) * sectors + (s + 1)); 
      indices[counter++] = (short) ((r + 1) * sectors + (s + 1)); 
      indices[counter++] = (short) (r * sectors + (s + 1));  
      indices[counter++] = (short) ((r + 1) * sectors + s);  
     } 
    } 
} 
+0

我會如何顯示這樣的東西?在點,法線和textcoords之前放置了一個float []後,我纔開始工作。不過謝謝你。 –

+0

是的,基本上在我的示例中,您可以將'points'和'texcoords'放在'vertexData'裏面。 'indexData'內的'indices'。目前跳過「法線」,除非您執行照明,否則不需要這些。相應地修改頂點屬性參數,因爲現在你的位置是3而不是2;) – elect

+0

我還沒有理解。對不起,我吮吸這個...我如何把點和texcoors?我試着把它們放在這裏:'private float [] vertexData = new float [] {texcoords,points }; private short [] indexData = new short [] { indicantly };',但這只是給了我一個藍屏。對不起,如果我錯過了一些非常明顯的東西... –