我想用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