Why is the vertex array slower than the glbegin/glend? And how could i improve the performance.
The code is part of a particle engine.
vertex array:
init:
vertices = com.sun.opengl.util.BufferUtil.newFloatBuffer(4*3*5000);
colors = com.sun.opengl.util.BufferUtil.newFloatBuffer(5000*4*4);
texturecord = com.sun.opengl.util.BufferUtil.newFloatBuffer(5000*4*2);
gl.glEnableClientState(GL.GL_COLOR_ARRAY);
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
gl.glColorPointer(4, GL.GL_FLOAT, 0, colors);
gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertices);
gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, texturecord);
draw:
if (particleList_.size() > 0) {
vertices.clear();
vertices.rewind();
colors.clear();
colors.rewind();
texturecord.clear();
texturecord.rewind();
for (Particle particle: particleList_){
particle.paint(vertices,colors,texturecord,right,up,view);
}
vertices.flip(); //rewind();
colors.flip(); //rewind();
texturecord.flip();
int test=vertices.limit()/3;
gl.glDrawArrays(GL.GL_QUADS, 0, test);
}
Particle:
public void paint(FloatBuffer vertex, FloatBuffer color,FloatBuffer texturecord, Vector3D right, Vector3D up, Vector3D view){
float r=color_.getR();
float g=color_.getG();
float b=color_.getB();
color.put(r);
color.put(g);
color.put(b);
color.put(alpha_);
color.put(r);
color.put(g);
color.put(b);
color.put(alpha_);
color.put(r);
color.put(g);
color.put(b);
color.put(alpha_);
color.put(r);
color.put(g);
color.put(b);
color.put(alpha_);
float x=position.getX();
float y=position.getY();
float z=position.getZ();
vertex.put(x+up.getX()*size);
vertex.put(y+up.getY()*size);
vertex.put(z+up.getZ()*size);
vertex.put(x);
vertex.put(y);
vertex.put(z);
vertex.put(x+right.getX()*size);
vertex.put(y+right.getY()*size);
vertex.put(z+right.getZ()*size);
vertex.put(x+(right.getX()+up.getX())*size);
vertex.put(y+(right.getY()+up.getY())*size);
vertex.put(z+(right.getZ()+up.getZ())*size);
texturecord.put(0f);
texturecord.put(0f);
texturecord.put(0f);
texturecord.put(1f);
texturecord.put(1f);
texturecord.put(1f);
texturecord.put(1f);
texturecord.put(0f);
}
glBegin/glEnd
draw:
gl.glBegin(GL.GL_QUADS);
for (Particle particle: particleList_){
particle.paint(gl,right,up,view);
}
gl.glEnd();
Particle
public void paint(GL gl, Vector3D right, Vector3D up, Vector3D view){
gl.glColor4f(color_.getR(), color_.getG(), color_.getB(), alpha_);
float x=position.getX();
float y=position.getY();
float z=position.getZ();
gl.glTexCoord2f(0,0);
gl.glVertex3f(x+up.getX()*size, y+up.getY()*size, z+up.getZ()*size);
gl.glTexCoord2f(0,1);
gl.glVertex3f(x, y, z);
gl.glTexCoord2f(1,1);
gl.glVertex3f(x+right.getX()*size, y+right.getY()*size, z+right.getZ()*size);
gl.glTexCoord2f(1,0);
gl.glVertex3f(x+(right.getX()+up.getX())*size, y+(right.getY()+up.getY())*size, z+(right.getZ()+up.getZ())*size);
}