i'm trying to implement a Rubic's Cube in Android Studio using Java and OpenGL.
Rendering and rotating the whole cube works just fine but rotating just one side (9 cubelets) doesn't
As far as i know the glTranslatef(x,y,z) method simply multiplies the current matrix with the translation matrix which i can specifiy as x,y,z. I'm pretty sure that i can somehow use that and the glRotatef() method to rotate just one side, but i don't know how.
@Override
public void onDrawFrame(GL10 gl) {
// Clear color and depth buffers using clear-value set earlier
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
//Front-Faces
gl.glLoadIdentity(); // Reset the current matrix to the Identity matrix
gl.glTranslatef(0.0f, 0.0f, z); // Translate into the screen
gl.glRotatef(angleX, 1.0f, 0.0f, 0.0f);
gl.glRotatef(angleY, 0.0f, 1.0f, 0.0f);
gl.glRotatef(angleCube, 0.0f, 0.0f, 1.0f); // Rotate
cube.draw(gl);
gl.glTranslatef(offSetPositive, 0.0f, 0.0f); //Middle-Right
cube.draw(gl);
gl.glTranslatef(0.0f, offSetPositive, 0.0f); //Top-Right
cube.draw(gl);
gl.glTranslatef(offSetNegative, 0.0f, 0.0f); //Top-Middle
cube.draw(gl);
gl.glTranslatef(offSetNegative, 0.0f, 0.0f); //Top-Left
cube.draw(gl);
gl.glTranslatef(0.0f, offSetNegative, 0.0f); //Middle-Left
cube.draw(gl);
gl.glTranslatef(0.0f, offSetNegative, 0.0f); //Bottom-Left
cube.draw(gl);
gl.glTranslatef(offSetPositive, 0.0f, 0.0f); //Bottom-Middle
cube.draw(gl);
gl.glTranslatef(offSetPositive, 0.0f, 0.0f); //Bottom-Right
cube.draw(gl);
//Middle
gl.glTranslatef(0.0f, 0.0f, -2.1f); //Bottom-Right-Middle
cube.draw(gl);
gl.glTranslatef(offSetNegative, 0.0f, 0.0f); //Bottom-Middle-Middle
cube.draw(gl);
gl.glTranslatef(offSetNegative, 0.0f, 0.0f); //Bottom-Left-Middle
cube.draw(gl);
gl.glTranslatef(0.0f, 2.1f, 0.0f); //Middle-Right-Middle
cube.draw(gl);
gl.glTranslatef(offSetPositive+offSetPositive, 0.0f, 0.0f); //Middle-Left-Middle
cube.draw(gl);
gl.glTranslatef(0.0f, 2.1f, 0.0f); //Top-Right-Middle
cube.draw(gl);
gl.glTranslatef(offSetNegative, 0.0f, 0.0f); //Top-Middle-Middle
cube.draw(gl);
gl.glTranslatef(offSetNegative, 0.0f, 0.0f); //Top-Left-Middle
cube.draw(gl);
// Update the rotational angle after each refresh
angleX += speedX;
angleY += speedY;
// On ArrowDown-Key Rotate 90 degrees
if((angleCube > -90.0f) && (rotate == true)) {
angleCube += speedCube;
System.out.println(angleCube +" " +rotate);
} else if((angleCube < 0.0f) && (rotate == false)) {
angleCube -= speedCube;
System.out.println(angleCube +" " +rotate);
}
}
}
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise orientation
gl.glEnable(GL10.GL_CULL_FACE); // Enable cull face
gl.glCullFace(GL10.GL_BACK); // Cull the back face (don't display)
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
// Render all the faces
for (int face = 0; face < numFaces; face++) {
// Set the color for each of the faces
gl.glColor4f(colors[face][0], colors[face][1], colors[face][2], colors[face][3]);
// Draw the primitive from the vertex-array directly
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, face*4, 4);
}
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisable(GL10.GL_CULL_FACE);
}
}