I need to create a 3d cube, with a different texture on each side. Here is my cube class, which my other classes extend:
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
public class Cube {
FloatBuffer vertexBuffer;
int numFaces = 6;
float[][] colors = { { 1.0f, 0.5f, 0.0f, 1.0f },
{ 1.0f, 0.0f, 1.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f },
{ 0.0f, 0.0f, 1.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f },
{ 1.0f, 1.0f, 0.0f, 1.0f } };
float[] vertices = { // Vertices of the 6 faces
// FRONT
-1.0f, -1.0f, 1.0f, // 0. left-bottom-front
1.0f, -1.0f, 1.0f, // 1. right-bottom-front
-1.0f, 1.0f, 1.0f, // 2. left-top-front
1.0f, 1.0f, 1.0f, // 3. right-top-front
// BACK
1.0f, -1.0f, -1.0f, // 6. right-bottom-back
-1.0f, -1.0f, -1.0f, // 4. left-bottom-back
1.0f, -1.0f, -1.0f, // 7. right-top-back
-1.0f, 1.0f, -1.0f, // 5. left-top-back
// LEFT
-1.0f, -1.0f, -1.0f, // 4. left-bottom-back
-1.0f, -1.0f, 1.0f, // 0. left-bottom-front
-1.0f, 1.0f, -1.0f, // 5. left-top-back
-1.0f, 1.0f, 1.0f, // 2. left-top-front
// RIGHT
1.0f, -1.0f, 1.0f, // 1. right-bottom-front
1.0f, -1.0f, -1.0f, // 6. right-bottom-back
1.0f, 1.0f, 1.0f, // 3. right-top-front
1.0f, 1.0f, -1.0f, // 7. right-top-back
// TOP
-1.0f, 1.0f, 1.0f, // 2. left-top-front
1.0f, 1.0f, 1.0f, // 3. right-top-front
-1.0f, 1.0f, -1.0f, // 5. left-top-back
1.0f, 1.0f, -1.0f, // 7. right-top-back
// BOTTOM
-1.0f, -1.0f, -1.0f, // 4. left-bottom-back
1.0f, -1.0f, -1.0f, // 6. right-bottom-back
-1.0f, -1.0f, 1.0f, // 0. left-bottom-front
1.0f, -1.0f, 1.0f // 1. right-bottom-front
};
public void setVertices(float[] vertices) {
this.vertices = vertices;
}
public void initializeVertexBuffer() {
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
}
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.glCullFace(GL10.GL_BACK);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
for (int face = 0; face < numFaces; face++) {
gl.glColor4f(colors[face][0], colors[face][1], colors[face][2],
colors[face][3]);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, face * 4, 4);
}
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
}
}
One of the classes which extends it:
public class Head extends Cube {
private float[] headVertices = { // Vertices of the 6 faces
// FRONT
-0.4f, 0.0f, 0.4f, // 0. left-bottom-front
0.4f, 0.0f, 0.4f, // 1. right-bottom-front
-0.4f, 0.8f, 0.4f, // 2. left-top-front
0.4f, 0.8f, 0.4f, // 3. right-top-front
// BACK
0.4f, 0.0f, -0.4f, // 6. right-bottom-back
-0.4f, 0.0f, -0.4f, // 4. left-bottom-back
0.4f, 0.8f, -0.4f, // 7. right-top-back
-0.4f, 0.8f, -0.4f, // 5. left-top-back
// LEFT
-0.4f, 0.0f, -0.4f, // 4. left-bottom-back
-0.4f, 0.0f, 0.4f, // 0. left-bottom-front
-0.4f, 0.8f, -0.4f, // 5. left-top-back
-0.4f, 0.8f, 0.4f, // 2. left-top-front
// RIGHT
0.4f, 0.0f, 0.4f, // 1. right-bottom-front
0.4f, 0.0f, -0.4f, // 6. right-bottom-back
0.4f, 0.8f, 0.4f, // 3. right-top-front
0.4f, 0.8f, -0.4f, // 7. right-top-back
// TOP
-0.4f, 0.8f, 0.4f, // 2. left-top-front
0.4f, 0.8f, 0.4f, // 3. right-top-front
-0.4f, 0.8f, -0.4f, // 5. left-top-back
0.4f, 0.8f, -0.4f, // 7. right-top-back
// BOTTOM
-0.4f, 0.0f, -0.4f, // 4. left-bottom-back
0.4f, 0.0f, -0.4f, // 6. right-bottom-back
-0.4f, 0.0f, 0.4f, // 0. left-bottom-front
0.4f, 0.0f, 0.4f, // 1. right-bottom-front
};
public Head() {
setVertices(headVertices);
super.initializeVertexBuffer();
}
}
(I removed the package declarations on these.)
I will need to get .png file I have in my drawable folder, divide it, as it is a single image which holds all the face's images, kind of like a spritesheet, and then apply the images to the cube's faces. Can you tell me what is the best approach to this, as I am very new to openGL-ES, and never worked with it before? PS: I do know how to divide the image to get the bitmaps, I just don't know how to apply them to the faces.
Thanks in advance
PS: Got the code from Nehe's examples.