Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: rendering space ships

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    26

    Question 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.
    Code :
    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();
    }

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,105
    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.

  3. #3
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    433
    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:
    Code :
    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.

  4. #4
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    26
    so I should use push and pop matrix functions?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •