by zorro » Thu Apr 22, 2010 2:22 pm
First, in your code you use GL11 and hardware buffers, but that is not going to help (in your case). That's because buffers help when you have large amounts of data, while your buffers contain only a small number of vertices. You can use plain GL10 arrays and the FPS should not go down.
In your code you have for each face:
- bind texture face
- setup arrays
- draw face
What am i saying is that you can get rid of 'setup arrays' part and just:
do once: setup arrays
do six times:
- bind texture
- transform face
- draw face
Now, how can you modify your code? I see you have PlaneVertBufferIndex1 for the first face. Make that a quad with the vertices in the following positions: (d,d,0) (d,-d,0) (-d,-d,0) (-d,d,0). D is the half of the face width. As you can see the face is in the XoY plane, the Z is always 0. Now you must draw all the faces using this buffer.
First, translate the modelview matrix to the point where the center of the cube will be.
gl.glLoadIdentity(); // reset matrix
gl.glTranslate (cubex, cubey, cubez);
If you want to rotate the cube, here is where you put your main rotation, around Y axis
gl.glRotatef( angle, 0,1,0);
Then bind the arrays once:
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, PlaneVertBufferIndex1);
gl11.glVertexPointer(3, GL10.GL_FIXED, 0, 0);
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, textureCoordBufferIndex);
gl11.glTexCoordPointer(2, GL11.GL_FIXED, 0, 0);
gl11.glBindBuffer(GL11.GL_ELEMENT_ARRAY_BUFFER, indexBufferIndex);
After that draw each face with these transformations:
// face 1 (right in front of the screen)
gl.glPushMatrix();
gl11.glBindTexture ...
gl.glTranslatef(0,0,d); // translate with 'd' towards the camera (on the Z axis), no rotation
gl11.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_SHORT, 0);
gl.glPopMatrix();
// face 2 (on the right)
gl11.glBindTexture ...
gl.glPushMatrix();
gl.glRotatef(90, 0,1,0); // rotate 90 degrees to the right
gl.glTranslatef(0,0,d); // translate 'd' on the same Z axis, which is now rotated
gl11.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_SHORT, 0);
gl.glPopMatrix();
// face 3 (on the left)
gl11.glBindTexture ...
gl.glPushMatrix();
gl.glRotatef(-90, 0,1,0); // rotate 90 degrees to the right
gl.glTranslatef(0,0,d); // translate 'd' on the same Z axis, which is now rotated
gl11.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_SHORT, 0);
gl.glPopMatrix();
// face 4 (in the back)
gl11.glBindTexture ...
gl.glPushMatrix();
gl.glRotatef(180, 0,1,0); // rotate 180 degrees to the right
gl.glTranslatef(0,0,d); // translate 'd' on the same Z axis, which is now rotated
gl11.glDrawElements(GL11.GL_TRIANGLES, 6, GL11.GL_UNSIGNED_SHORT, 0);
gl.glPopMatrix();
With this you have all the faces drawn with the buffers binded only once, at the beginning.
You can try to implement this, it should speed up the cube. If there are problems, let me know.