Trouble with splitting matrices

Hi I’m having trouble with moving some parts of my Model individually.

This shall represent a rubiks cube. And I want to rotate the sides individually. But if I try the whole cube rotates around the origin.
I tried to do it like that.


void drawCube(float size)
{
	glPushMatrix();
	for(int z = 0; z < cubesize; z++)
	{
		glPushMatrix();
		glRotatef(angle,0,0,1);
		for(int x = 0; x < cubesize; x++)
		{
			for(int y = 0; y < cubesize; y++)
			{
				
					float xPos = -(size+0.4)*cubesize/2+(size+0.4)*x;
					float yPos = -(size+0.4)*cubesize/2+(size+0.4)*y; 
					float zPos = (size+0.4)*cubesize/2+(size+0.4)*-z;;
								
					StoneFromCube(x,y,z, xPos,yPos,zPos, size);
				
			}
		}
		glPopMatrix();
	}
	glPopMatrix();
		
}

The method StoneFromCube draws a single Stone of the cube.

I’m still a newbie and don’t know how to solve problems like that would be great if anyone can help.
Thanks in advance^^

I don’t thing you will go very far with this approach.
Your mistake here is that you apply the same matrix to every ‘stone’.
A better way should be to create a class/struct that represent your ‘stone’ and this class should keep the actual rotation matrix.
then render them like that


for (std::vector<stones>::iterator stoneIt = stoneVector.begin(); stoneItint != stoneVector.end(); ++stoneIt)
{
  glPushMatrix();
  glLoadMatrix(stoneIt->getMatrixPtr());
  stoneIt->draw();
  glPopMatrix();
}

then when you want to rotate your object just multiply the current matrix for your rotation matrix. With some maths it’s not hard.