I have created an object by connecting a cylinder and a rectangle. I’m trying to translate them both together (in 2D) but can’t figure out how to translate the rectangle with respect to the cylinder so it appears to be one object throughout their movement. Right now, they start out connected, but when they start moving, they become disjoined and fly in similar directions but not connected.
Because matrix operations effect each object drawn after them, you must use a command to store the matrix state before you call routines like glRotate/translate.
// Setup Projection
// Setup Camera
//Create object 1
glPushMatrix(); //Since we want our camara/model matrix to stay the same for each object, we save the current matrix state
glTranslate3f(…); //Location of our object.
glRotatef(…); // Rotation of whole object.
glPushMatrix(); // Save matrix again so that out
glTranslate3f(…); //Translate rectangle to is spot relative to cylinder
glRotatef(…); // Rotate rectangle
glPopMatrix(); // Now we have drawn rectangle restore matrix back for next object.
glPopMatrix(); // End cylinder
glPopMatrix(); //End object
Note matrix operations are first in first out, which means the last saved matrix will be the first matrix to be restored when pop’ed.