2013-10-02 35 views
0

我將嘗試在當前的'OnDrawFrame'中繪製一組對象。如何在Android OpenGL ES中繪製對象數組'OnDrawFrame'

@Override 
public void onDrawFrame(GL10 gl) { 
for(int i=0; i<objectArray.length; i++){ 
    objectArray[i].draw(gl); 
    } 
} 

當我嘗試使用上述的「For」語句,浪費內存似乎是太可怕了。

即使您在實踐中喜歡它,它仍會繼續移除,繪圖對象將顯示在最後。

通過做什麼,所有或將繪製的對象數組?

而且,什麼我需要做的,使內存的使用效率

我們通過什麼白天到夜晚的泄漏困擾。


畫一條線類.......

public class LINETEST { 

float[] vertices = null; 
private short[] indices = null; 

public FloatBuffer verticesBuffer; 
private ShortBuffer indexBuffer; 

public LINETEST() { 

} 

public void draw(GL10 gl) { 

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, verticesBuffer); 
    gl.glLineWidth(2); 
    gl.glColor4f(0f, 0f, 0f, 0f); 

    gl.glDrawElements(GL10.GL_LINE_STRIP, indices.length, 
       GL10.GL_UNSIGNED_SHORT, indexBuffer); 

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); 
} 

public void setBuffer() { 

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

      ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2); 
      ibb.order(ByteOrder.nativeOrder()); 
      indexBuffer = ibb.asShortBuffer(); 
      indexBuffer.put(indices); 
      indexBuffer.position(0);    
} 

public void setVertices(float[] verticesAl, short[] indicesAl){ 

    this.vertices = verticesAl; 
    this.indices = indicesAl;  

} 

}


的函數,上述類的對象的陣列。

public void setVertices(float[] vertice, short[] indice, int lineNumber){ 
    this.vertices = vertice; 
    this.indices = indice; 
    this.number = lineNumber; 

linetest[number] = new LINETEST(); 
linetest[number].setVertices(vertices, indices);    
linetest[number].setBuffer(); 

} 

最後,它是利用上述函數來繪製的OnDraw方法。它在渲染器類中運行。

@Override 
public void onDrawFrame(GL10 gl) { 

    int OBJL = 30; 
    switch (OBJL){ 
    case 30: 
     if(vertices != null){ 
      if(linetest[number] != null){ 
       for(int i = 0; i<number; i++){ 
        linetest[number].draw(gl); 
       } 
       linetest[number].draw(gl); 
      } 

     }else{ 
      break; 
     } 
    } 

}

+0

請加你用繪製每個對象的代碼。 –

+0

我在一個類中創建了多個對象。 – JavaHell

+0

但是,她在創建對象時通過生成一個隨機頂點數組來放置它,要繪製的位置是不同的。 – JavaHell

回答

2

您可以使用glDrawArrays()glDrawElements()使用只是一個GL調用渲染陣列。

參考文獻:

http://www.opengl.org/sdk/docs/man/xhtml/glDrawArrays.xml http://www.opengl.org/sdk/docs/man/xhtml/glDrawElements.xml

例如:

// Vertices are stored in counter-clockwise order 
float vertsCoords[] = {-1.0f, 1.0f, 0.0f, // V1 
         1.0f, 1.0f, 0.0f, // V2 
         0.0f, 0.0f, 0.0f, // V3 
        }; 

@Override 
public void onDrawFrame(GL10 gl) { 
    gl.glEnableClientState(GL10.GL_GL_VERTEX_ARRAY); 
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertsCoords); // 3 components per vertex, float in size, from offset 0 at array 'vertsCoords' 
    gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); // draw 3 vertices 
    gl.glDisableClientState(GL10.GL_GL_VERTEX_ARRAY); 
} 

這可能看起來像一個大量的代碼,但你最終只是給4 GL調用,而不是至少objectArray。在你的情況下長度調用。

如果你想進一步,你可以看看頂點緩衝對象:

http://www.learnopengles.com/android-lesson-seven-an-introduction-to-vertex-buffer-objects-vbos/

+0

感謝您的回答flobo〜 – JavaHell

+0

但是,我不明白上面的代碼。我創建了一個繪製圖片的類,如上面的代碼所示。然後,通過使用創建的類,您爲該類的一個對象創建了一個多重數組。 – JavaHell

+0

我想要的是,是否有什麼東西可以在作爲此對象數組創建的對象中繪製完全繪製函數。 – JavaHell