我將嘗試在當前的'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;
}
}
}
請加你用繪製每個對象的代碼。 –
我在一個類中創建了多個對象。 – JavaHell
但是,她在創建對象時通過生成一個隨機頂點數組來放置它,要繪製的位置是不同的。 – JavaHell