我想用OpenGL ES 2.0在Android上批量繪製一堆線,我需要知道最好的方法來做到這一點。Android上的OpenGL ES 2.0中的快速動態頂點
現在我創建了一個名爲LineEngine的類,它建立了一個FloatBuffer,用於繪製所有的頂點,然後一次繪製所有的線。問題在於,顯然FloatBuffer.put()非常慢,並且像瘋了似的吞噬CPU時間。
這裏是我的類
public class LineEngine {
private static final float[] IDENTIY = new float[16];
private FloatBuffer mLinePoints;
private FloatBuffer mLineColors;
private int mCount;
public LineEngine(int maxLines) {
Matrix.setIdentityM(IDENTIY, 0);
ByteBuffer byteBuf = ByteBuffer.allocateDirect(maxLines * 2 * 4 * 4);
byteBuf.order(ByteOrder.nativeOrder());
mLinePoints = byteBuf.asFloatBuffer();
byteBuf = ByteBuffer.allocateDirect(maxLines * 2 * 4 * 4);
byteBuf.order(ByteOrder.nativeOrder());
mLineColors = byteBuf.asFloatBuffer();
reset();
}
public void addLine(float[] position, float[] color){
mLinePoints.put(position, 0, 8); //These lines
mLineColors.put(color, 0, 4); // are taking
mLineColors.put(color, 0, 4); // the longest!
mCount++;
}
public void reset(){
mLinePoints.position(0);
mLineColors.position(0);
mCount = 0;
}
public void draw(){
mLinePoints.position(0);
mLineColors.position(0);
GraphicsEngine.setMMatrix(IDENTIY);
GraphicsEngine.setColors(mLineColors);
GraphicsEngine.setVertices4d(mLinePoints);
GraphicsEngine.disableTexture();
GLES20.glDrawArrays(GLES20.GL_LINES, 0, mCount * 2);
GraphicsEngine.disableColors();
reset();
}
}
有批量所有這些線一起更好的辦法?
哪個android版本是設備運行? [有關FloatBuffer的問題](http://www.badlogicgames.com/wordpress/?p=899)。 GLES20要求API等級8,所以它至少是Froyo。你可以在運行Honeycomb的設備上測試嗎? –
我在Galaxy Nexus的ICS上運行它。我會仔細檢查它確實需要很長時間。 – EmbMicro
我剛剛測試了批量生產線,未在我的Galaxy Nexus和EVO 4G上進行批量生產。 EVO將FPS從28.8提高到30,Galaxy Nexus保持在58.4。然而,在方法分析器中,它顯示繪圖函數(包括FloatBuffer.put和實際的OpenGL繪圖調用)佔用了更多時間。它從CPU時間的31.5%降至53.1%。這只是探查器搞亂了事情嗎? – EmbMicro