Rotating several objects as one (asteroid)

Hi! I’m working on a Solar System model. I have done an asteroid consisting of several random spheres. I can get the spheres to rotate one and one but I want to make them rotate as if they were one object. Here’s my code as it is right now.



glPushMatrix();

        glRotatef(orbitalAngle, 0.0, 1.0, 0.0);

	for (int i = 0; i < numOfSpheres; i++)
	{
		glPushMatrix();
			GLdouble enlargedRadiusZ = (orbitalRadius + spheresPosition[i].z) * ENLARGED_RADIUS;
			GLdouble enlargedRadiusX = spheresPosition[i].x * ENLARGED_RADIUS;
			GLdouble enlargedRadiusY = spheresPosition[i].y * ENLARGED_RADIUS;

			colorMixer->set_color(WHITE);
			glTranslatef(enlargedRadiusX, enlargedRadiusY, enlargedRadiusZ);
			glRotatef(90.0, 1.0, 0.0, 0.0);
			gluSphere(sphere, 1, 5, 5);
		glPopMatrix();
	}

        glRotatef(rotationAngleX, 1.0, 0.0, 0.0);
	glRotatef(rotationAngleY, 0.0, 1.0, 0.0);
	glRotatef(rotationAngleZ, 0.0, 0.0, 1.0);

glPopMatrix();


I iterate through all the spheres and draw them and after that I want to rotate it as one object but it doesn’t work. If I put the rotations just before I draw the sphere they rotate, but just one and one.

Thanx!

Pseudo Code would look like this:


// draw sun

for each planet
   glPushMatrix()
   glRotatef (...) // rotation around the sun
   glTranslatef (...) // distance from the sun
   // draw planet
   glPopMatrix()
end for

That’s not really what I meant, but thanx anyway. I can get the asteroid and all the spheres it consist of to rotate around the sun. What I want to do is to get it to rotate around its axis as one unit, i.e. the individual spheres doesn’t rotate as separate object but rather as one so it looks like the asteroid is one object.

Thanx.

I think you need some sort of hierarchical tree structure to represent transformations relative to the parent object. Something like:


struct Object3D {
   Object3D children;
   Transformation trans;
}; 

void drawObject(Object3D obj) {
   glPushMatrix();
   applyTrans(obj.trans);
   foreach (Object3D o in obj.children) {
       drawObject(o); // Here transformations are applied relative to the parent object
   }
   glPopMatrix()
}

  • Code is supposed to be crappy c pseudo-code