Getting an object to 'orbit' another object

I have 2 crates, both rotating about their origin, but I want the second crate to rotate about the first crates origin to ‘orbit’ it… how would I go about doing this?


/*-------------------------------------Defininitions----------------------------------------
-------------------------------------------------------------------------------------------*/

float _angle = 0;										//The angle of the cube
GLuint _textureId;										//The OpenGL ID of the texture
GLuint _textureId2;										//The OpenGL ID of the second texture
int level = 1;											//The level the player is on
bool helpMenu = true;									//Display the help menu during the game? Can be turned on/off during gameplay

struct point3D {										//Create the structure for a 3D point which uses the width(x), height(y), and depth(z)
	float x, y, z;
};

struct camera{											//Create the structure for a camera which has an (x,y,z) position, an (x,y,z) position of where it is looking, and which way 'up' is
	point3D pos, lookAt, up;
}; 

camera cam = {0, 0, 56.5, 0, 0, 0, 0, 1, 0};			//Create the camera using the above structure(s)

void handleKeypress(unsigned char Key, int x, int y);	//Handles all keypresses from the keyboard
void initRendering();									//Loads the texture to be displayed from the texture ID
GLuint loadTexture(Image* image);						//Turns the loaded image into a texture, and returns the Texture ID
void handleResize(int w, int h);						//Handles decisions for when the window is resized
void drawTexturedCube(int BOX_SIZE);					//Draws a textured cube, of boxSize width and height
void drawLevels();										//Draws the scenes for each level
void update(int value);									//Updates the game screen
int main(int argc, char** argv);

/*----------------------------------------Methods-------------------------------------------
-------------------------------------------------------------------------------------------*/

void handleKeypress(unsigned char key, int x, int y) {
	switch (key) {
		case 27:										//Escape key closes the game
			exit(0);
		case 32:										//Space bar resets the camera coordinates
			cam.pos.x = 0;
			cam.pos.y = 0;
			cam.pos.z = 56.5;
			cam.lookAt.x = 0;
			cam.lookAt.y = 0;
			cam.lookAt.z = 0;
			cam.up.x = 0;
			cam.up.y = 1;
			cam.up.z = 0;
			break;
		case 97:										//'a' key move the camera -0.5 along the x-axis
			cam.pos.x -= 0.5;
			break;
		case 100:										//'d' key moves the camera +0.5 along the x-axis
			cam.pos.x += 0.5;
			break;
		case 104:										//'h' key turns the help menu on/off
			if (helpMenu == true)
				helpMenu = false;
			else
				helpMenu = true;
			break;
		case 115:										//'s' key moves the camera -0.5 along the y-axis
			cam.pos.y -= 0.5;							
			break;
		case 119:										//'w' key moves the camera +0.5 along the y-axis
			cam.pos.y += 0.5;	
			break;
		case 120:										//'x' key moves the camera -0.5 along the z-axis
			cam.pos.z -= 0.5;
			break;
		case 122:										//'z' key moves the camera +0.5 along the z-axis
			cam.pos.z += 0.5;
			break;
	}
}

GLuint loadTexture(Image* image) {
	GLuint textureId;
	glGenTextures(1, &textureId);
	glBindTexture(GL_TEXTURE_2D, textureId);
	glTexImage2D(GL_TEXTURE_2D,
				 0,
				 GL_RGB,
				 image->width, image->height,
				 0,
				 GL_RGB,
				 GL_UNSIGNED_BYTE,
				 image->pixels);
	return textureId;
}

void initRendering() {
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_NORMALIZE);
	glEnable(GL_COLOR_MATERIAL);
	
	Image* image = loadBMP("Crate_Sides.bmp");
	Image* image2 = loadBMP("Crate_Top.bmp");
	_textureId = loadTexture(image);
	_textureId2 = loadTexture(image2);
	delete image;
}

void handleResize(int w, int h) {
	glViewport(0, 0, w, h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0);
}

void drawTexturedCube(int cubeSize){ 

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, _textureId2);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin(GL_QUADS);
	
	//Top face

	glNormal3f(0.0, 1.0f, 0.0f);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-cubeSize / 2, cubeSize / 2, -cubeSize / 2);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(-cubeSize / 2, cubeSize / 2, cubeSize / 2);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(cubeSize / 2, cubeSize / 2, cubeSize / 2);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(cubeSize / 2, cubeSize / 2, -cubeSize / 2);
	glEnd();

	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, _textureId);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glColor3f(1.0f, 1.0f, 1.0f);
	glBegin(GL_QUADS);

	//Bottom face

	glNormal3f(0.0, -1.0f, 0.0f);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-cubeSize / 2, -cubeSize / 2, -cubeSize / 2);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(cubeSize / 2, -cubeSize / 2, -cubeSize / 2);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(cubeSize / 2, -cubeSize / 2, cubeSize / 2);
	glTexCoord2f(0.0f, 01.0f);
	glVertex3f(-cubeSize / 2, -cubeSize / 2, cubeSize / 2);
	
	//Left face

	glNormal3f(-1.0, 0.0f, 0.0f);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-cubeSize / 2, -cubeSize / 2, -cubeSize / 2);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(-cubeSize / 2, -cubeSize / 2, cubeSize / 2);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(-cubeSize / 2, cubeSize / 2, cubeSize / 2);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(-cubeSize / 2, cubeSize / 2, -cubeSize / 2);
	
	//Right face

	glNormal3f(1.0, 0.0f, 0.0f);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(cubeSize / 2, -cubeSize / 2, -cubeSize / 2);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(cubeSize / 2, cubeSize / 2, -cubeSize / 2);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(cubeSize / 2, cubeSize / 2, cubeSize / 2);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(cubeSize / 2, -cubeSize / 2, cubeSize / 2);

	//Front face

	glNormal3f(0.0, 0.0f, 1.0f);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-cubeSize / 2, -cubeSize / 2, cubeSize / 2);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(cubeSize / 2, -cubeSize / 2, cubeSize / 2);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(cubeSize / 2, cubeSize / 2, cubeSize / 2);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(-cubeSize / 2, cubeSize / 2, cubeSize / 2);
	
	//Back face

	glNormal3f(0.0, 0.0f, -1.0f);
	glTexCoord2f(0.0f, 0.0f);
	glVertex3f(-cubeSize / 2, -cubeSize / 2, -cubeSize / 2);
	glTexCoord2f(1.0f, 0.0f);
	glVertex3f(-cubeSize / 2, cubeSize / 2, -cubeSize / 2);
	glTexCoord2f(1.0f, 1.0f);
	glVertex3f(cubeSize / 2, cubeSize / 2, -cubeSize / 2);
	glTexCoord2f(0.0f, 1.0f);
	glVertex3f(cubeSize / 2, -cubeSize / 2, -cubeSize / 2);
	
	glEnd();
	glDisable(GL_TEXTURE_2D);
}


void drawLevels() {

	switch (level) {
		case 1:

			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
			glMatrixMode(GL_MODELVIEW);
			glLoadIdentity();

			gluLookAt(cam.pos.x, cam.pos.y, cam.pos.z, 
				cam.lookAt.x, cam.lookAt.y, cam.lookAt.z, 
				cam.up.x, cam.up.y, cam.up.z);
			glEnd();
	
			glPushMatrix();
			glTranslatef(0.0f, 0.0f, -30.0f);
			glRotatef(_angle, 1.0f, 1.0f, 0.0f);

			drawTexturedCube(7);
			glPopMatrix();

			glPushMatrix();
			glTranslatef(15.0f, 0.0f, -30.0f);
			glRotatef((_angle*2), 0.0f, 1.0f, 0.0f);
			drawTexturedCube(6);
			glPopMatrix();

			glutSwapBuffers();
	}
}

void update(int value) {
	_angle += 1.0f;							//Increase the angle of the cube by 1
	if (_angle > 360) {
		_angle -= 360;						//If the angle of the cube is over 360 (A full rotation), set the angle back to 0
	}
	glutPostRedisplay();					//Redisplay the cube
	glutTimerFunc(25, update, 0);			//Called every 25 milliseconds
}

int main(int argc, char** argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(400, 400);
	
	glutCreateWindow("Game of Cubes");
	
	initRendering();
	
	glutDisplayFunc(drawLevels);
	glutKeyboardFunc(handleKeypress);
	glutReshapeFunc(handleResize);
	glutTimerFunc(25, update, 0);
	
	glutMainLoop();
	return 0;
}

Also, how would I go about getting the camera to rotate around the scene instead of just moving left/right/up/down…?

Thank you very much!

All you need to do to orbit the second cube around the first is to swap the order of glTranslate and glRotate

Sorted, thank you so much!

Sorry for another post, but how would I then get an object to rotate one of the objects rotating? (Like the Moon>Earth>Sun as it were)?