2011-03-18 55 views
0

我意識到這是如此基本的一個問題,但我很茫然,因爲我是新來的Java,新的Android開發,但有很強的C/C++背景,在OpenGL的工作代碼oes我在這裏嘗試。FloatBuffer Android中的OpenGL

我已經採取了基本Cube.java和CubeRenderer.java代碼示例和修改它們包括一個二十面體的數據。我想使用float而不是固定的,但我不知道是否可能或如何去做。

有人能告訴我,我做錯了什麼?它編譯和發佈沒有錯誤,但只顯示黑度。 Cube()中的其他繪圖算法是相同的。我已經嘗試減少PON的尺寸(正1.0),MON(-1.0),PGR(正黃金比例),MGR(減去黃金比例),以一半的電流值。仍然不顯示渲染的三角形。

我真的不知所措,並希望得到任何幫助。

預先感謝您。 :-)

class Icosahedron 
{ 
    public Icosahedron() 
    { 
     int one = 0x10000; 
     float zro = 0.0f; 
     float pon = 1.0f; 
     float mon = -1.0f; 
     float pgr = 1.618033989f; 
     float mgr = -1.618033989f; 
     float vertices[] = { 
      pgr, pon, zro, 
      pgr, mon, zro, 
      pon, zro, pgr, 
      pon, zro, mgr, 
      zro, pgr, pon, 
      zro, pgr, mon, 
      zro, mgr, pon, 
      zro, mgr, mon, 
      mon, zro, pgr, 
      mon, zro, mgr, 
      mgr, pon, zro, 
      mgr, mon, zro 
     }; 

     int colors[] = { 
       0, 0, 0, one, 
      one, 0, 0, one, 
      one, one, 0, one, 
       0, one, 0, one, 
       0, 0, one, one, 
      one, 0, one, one, 
      one, one, one, one, 
       0, one, one, one, 
       0, 0, one, one, 
      one, 0, one, one, 
      one, one, one, one, 
       0, one, one, one, 
       0, 0, one, one 
     }; 

     byte indices[] = { 
      0, 5, 4, 
      0, 4, 2, 
      0, 2, 1, 
      0, 1, 3, 
      0, 3, 5, 
      1, 6, 7, 
      1, 7, 3, 
      1, 2, 6, 
      2, 8, 6, 
      2, 4, 8, 
      3, 7, 9, 
      3, 9, 5, 
      4, 10, 8, 
      4, 5, 10, 
      5, 9, 10, 
      6, 11, 7, 
      6, 8, 11, 
      7, 11, 9, 
      8, 10, 11, 
      9, 11, 10 
     }; 

     ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4); 
     vbb.order(ByteOrder.nativeOrder()); 
     mVertexBuffer = vbb.asFloatBuffer(); 
     mVertexBuffer.put(vertices); 
     mVertexBuffer.position(0); 

     ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4); 
     cbb.order(ByteOrder.nativeOrder()); 
     mColorBuffer = cbb.asIntBuffer(); 
     mColorBuffer.put(colors); 
     mColorBuffer.position(0); 

     mIndexBuffer = ByteBuffer.allocateDirect(indices.length); 
     mIndexBuffer.put(indices); 
     mIndexBuffer.position(0); 
    } 

    public void draw(GL10 gl) 
    { 
     gl.glFrontFace(GL10.GL_CW); 
     gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer); 
     gl.glColorPointer(4, GL10.GL_FIXED, 0, mColorBuffer); 
     gl.glDrawElements(GL10.GL_TRIANGLES, 20, GL10.GL_UNSIGNED_BYTE, mIndexBuffer); 
    } 

    private FloatBuffer mVertexBuffer; 
    private IntBuffer  mColorBuffer; 
    private ByteBuffer mIndexBuffer; 
} 

回答

1

我看到的唯一錯誤是,你需要通過60的第二個參數glDrawElements(),而不是20。你需要指定索引的數量,而不是三角形的數量。

+0

這是正確的。第二個參數應該是'indices'數組的長度,即indices.length。 – 2016-07-28 21:20:11