rendering space ships

I am programming a little space game. I have drawn two space ships to the screen. I want them to act independently of each other.


void draw_ship()
{
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	
	glColor3f(0.0f,1.0f,0.0f);
	glBegin(GL_LINE_LOOP);
	glVertex3f(-1.5f,-0.25f,0.0f);
	glVertex3f(-1.75f,-0.5f,0.0f);
	glVertex3f(-2.0f,-0.5f,0.0f);
	glVertex3f(-1.5f,0.5f,0.0f);
	glVertex3f(-1.0f,-0.5f,0.0f);
	glVertex3f(-1.25f,-0.5f,0.0f);
	glVertex3f(-1.5f,-0.25f,0.0f);
	glEnd();

	glColor3f(1.0f,0.0f,0.0f);
	glBegin(GL_LINE_LOOP);
	glVertex3f(1.5f,-0.25f,0.0f);
	glVertex3f(1.25f,-0.5f,0.0f);
	glVertex3f(1.0f,-0.5f,0.0f);
	glVertex3f(1.5f,0.5f,0.0f);
	glVertex3f(2.0f,-0.5f,0.0f);
	glVertex3f(1.75f,-0.5f,0.0f);
	glVertex3f(1.5f,-0.25f,0.0f);
	glEnd();

	glColor3f(1.0f,0.0f,0.0f);
	glPointSize(2.0f);
	glBegin(GL_POINTS);
	glVertex3f(1.5f,up_two++,0.0f);
	glEnd();

	glColor3f(0.0f,1.0f,0.0f);
	glPointSize(2.0f);
	glBegin(GL_POINTS);
	glVertex3f(-1.5f,up++,0.0f);
	glEnd();
	
	glutSwapBuffers();
}

You need to supply a translation matrix for each ship. You must save the view matrix, set the translation for ship 1, restore view matrix, save view matrix, set tranlation for ship 2, restore view matrix.

And make sure you remove glClear & glutSwapBuffers from the drawship() function, and instead have them at the beginning/end of the main draw function.

With legacy OpenGL, your draw function might look like this:

void draw()
{
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
 
  //set up projection matrix
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective (50.0*zoomFactor, (float)width/(float)height, zNear, zFar);
  
  //set up modelview matrix
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(eyex,eyey,eyez,centerx,centery,centerz,upx,upy,upz);

  // push a copy of the current model-view matrix onto the model-view matrix stack
  glPushMatrix();
  // adjust the model-view matrix so the ship will be drawn where we want
  glMultMatrixf(some_ship_matrix); //or glScale/glRotate/glTranslate etc.
  // draw the ship
  draw_ship();
  // pop the matrix off the stack, restoring the current model-view matrix to the value it had at the time of the glPushMatrix call
  glPopMatrix();

  // draw lots of ships
  for (int i=0; i<NUM_SHIPS; i++)
  {
    glPushMatrix();
    glMultMatrixf(ship_matrix[i]);
    draw_ship(i);
    glPopMatrix();
  }

  glutSwapBuffers();
}

With modern OpenGL it would be a case of feeding the correct matrices to the shader program before drawing.

so I should use push and pop matrix functions?