Contents / Previous / Next


Transformations Program Example

To determine the order of modeling transformations, visualize what happens to the local coordinate system.
An initial glRotate*() rotates the local coordinate system that initially coincides with the grand coordinate system.
Next, glTranslate*() moves the local coordinate system to a position on the planet's orbit; the distance moved should equal the radius of the orbit. Thus, the initial glRotate*() actually determines where along the orbit the planet is (or what time of year it is).
A second glRotate*() rotates the local coordinate system around the local axes, thus determining the time of day for the planet. Once you've issued all these transformation commands, the planet can be drawn.
void display(void)
{
   ...

   // Position the camera.
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

   glPushMatrix();

   glutWireSphere(1.0, 20, 16);   /* draw sun */
   glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);
   glTranslatef (2.0, 0.0, 0.0);
   glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);
   glutWireSphere(0.2, 10, 8);    /* draw smaller planet */

   glPopMatrix();

   ...
}
The Sun and a planet with a moon using matrix stacks:
class SolarSystem {


  // Solar system.
  float hourofday = 0f;
  float dayofyear = 0f;
  float dayofmonth = 10f;


 public void draw( GL gl, GLU glu ) 
    {
      
      float angleDay = ( hourofday / 24f ) * 360f;
      float angleYear = ( dayofyear / 365f ) * 360f;
      float angleMonth = ( dayofmonth / 28f ) * 360f;
      // Clocktick (unit is 1 hour): step time.
      final float clocktick = 20f;
      hourofday = (hourofday+clocktick) % 24f;
      dayofmonth = (dayofmonth+(clocktick/24f)) % 28f;
      dayofyear = (dayofyear+(clocktick/24f)) % 365f;

      //   System.out.println( "Day: " + angleDay +
      //	             "  Month: " + angleMonth +
      //		     "  Year: " + angleYear );//ddd

      gl.glPushMatrix(); {
	// Sun
	gl.glColor4f( 1f, 1f, 1f, 1f );
	glu.gluSphere( qobj0, 0.8f, 10, 10) ;      

	gl.glPushMatrix(); {
	  // Planet 1 
	  gl.glRotatef( angleYear, 0.0f, 1.0f, 0.0f ); 
	  gl.glTranslatef ( 3.0f, 0.0f, 0.0f ); 
	  gl.glColor4f( 0f, 1f, 0f, 1f );
	  glu.gluSphere( qobj0, 0.3f, 10, 10) ;      

	  // Moon 11
	  gl.glPushMatrix(); {
	    gl.glRotatef( angleMonth, 0.0f, 1.0f, 0.0f ); 
	    gl.glTranslatef (0.8f, 0.0f, 0.0f ); 
	    glu.gluSphere( qobj0, 0.1f, 10, 10 ) ;      
	  } gl.glPopMatrix(); 
	
	} gl.glPopMatrix(); // Planet 1 

      } gl.glPopMatrix(); // sun

    }
}