Exemple #1
0
void drawScene() {
  glPushMatrix();

  //Set the animation control variables.
  if (!paused) frame++;
  int ticks = frame % 1440;
  if (frame % 1440 == 0) {
    ticks = 0;

    //Reset animation object positions and rotations.
    planePriorX = 0, planePriorY = 0, planePriorZ = 0;
    planePostX = 0, planePostY = 0, planePostZ = 0;
    planeRotX = 0, planeRotY = 0, planeRotZ = 0;

    eaglePriorX = -0.5, eaglePriorY = 0.03, eaglePriorZ = -0.1;
    eaglePostX = 0, eaglePostY = 0, eaglePostZ = 0;
    eagleRotX = 0, eagleRotY = 0, eagleRotZ = 0;
  }

  //Apply scene transformations.
  glRotatef(30, 0, 1, 0);
  glTranslatef(0, 0, -1500 * frame * SCALE_FACTOR);

  //Make sure angles are in bounds.
  bounds(planeRotX); bounds(planeRotY); bounds(planeRotZ);
  bounds(eagleRotX); bounds(eagleRotY); bounds(eagleRotZ);

  if (!paused) {
    //Airplane transformation events.
    //Constant swaying.
    planeRotZ -= 0.5 * cos(ticks * 2 * PI / 180);
    planePostY -= 0.01 * cos(ticks * PI / 180);

    //Spin upside-down.
    if (ticks >= 180 && ticks < 360) planeRotZ -= PI * 0.5 * sin(ticks * PI / 180);

    //Spin back round.
    if (ticks >= 360 && ticks < 540) planeRotZ += PI * 0.5 * sin(ticks * PI / 180);

    //Dive.
    if (ticks >= 720 && ticks < 900) {
      planeRotX -= 0.25 * PI * sin(ticks * 2 * PI / 180);
      planePriorY -= 0.005 * PI * sin(ticks * PI / 180);
      planePriorZ -= 0.01 * PI * sin(ticks * 0.5 * PI / 180);
    }

    //Loop-the-loop
    if (ticks >= 900 && ticks < 1220) {
      planeRotX += 1;
      planePriorY += 0.015 * PI * sin((ticks - 180) * PI / 180);
      planePriorZ -= 0.015 * PI * cos((ticks - 180) * PI / 180);
    }

    //Reset position.
    if (ticks >= 1220 && ticks < 1440) {
      // These values were found by minimising errors using binary division.
      planeRotX -= 0.1262345 * PI * sin(ticks * PI / 180);
      planePriorY -= 0.002379563 * PI * sin(ticks * 0.5 * PI / 180);
      planePriorZ -= 0.003826425 * PI * sin(ticks * 0.5 * PI / 180);
    }

    //Eagle transformation events.
    //Set default position.
    eaglePriorX = -1;
    eaglePriorZ = 0.4;

    //Set start position over left wing.
    if (ticks >= 0 && ticks < 180) {
      eaglePriorX = -0.5;
      eaglePriorY = 0.03;
      eaglePriorZ = -0.1;
    }

    //Move a little with the wing.
    if (ticks >= 0 && ticks < 90) {
      eagleRotZ -= cos(ticks * 2 * PI / 180);
      eaglePostY -= 0.009 * sin(ticks * 2 * PI / 180);
    }

    //Move back, up and left.
    if (ticks >= 90 && ticks < 180) {
      eaglePriorX -= (float)0.5 / 90 * (ticks - 90);
      eaglePriorZ += (float)0.5 / 90 * (ticks - 90);
      eaglePostY += 0.009 * sin((ticks - 90) * 2 * PI / 180);
    }

    //Swaying.
    if (ticks >= 90 && ticks < 1350) {
      eagleRotZ -= 0.6 * cos((ticks + 90) * PI / 180);
      eaglePostY -= 0.01 * cos((ticks + 90) * 2 * PI / 180);
    }

    //Avoid plane collision.
    if (ticks >= 360 && ticks < 540) eagleRotZ += 0.25 * PI * sin(ticks * 2 * PI / 180);

    //Glide from side to side.
    if (ticks >= 720 && ticks < 1080) eaglePriorX += 0.4 * PI * sin(ticks * 0.5 * PI / 180);

    //Move back to start position.
    if (ticks >= 1350 && ticks < 1440) {
      eaglePriorX += (float)0.5 / 90 * (ticks - 1350);
      eaglePriorZ -= (float)0.5 / 90 * (ticks - 1350);
    }

    //Avoid plane collision.
    if (ticks >= 1400 && ticks < 1420) eaglePriorY += 0.01;
    if (ticks >= 1420 && ticks < 1440) eaglePriorY -= 0.01;
  }

  //Apply the transformations then draw the airplane.
  glPushMatrix();
  glTranslatef(planePriorX, planePriorY, planePriorZ);
  glRotatef(planeRotX, 1, 0, 0);
  glRotatef(planeRotY, 0, 1, 0);
  glRotatef(planeRotZ, 0, 0, 1);
  glTranslatef(planePostX, planePostY, planePostZ);
  drawAirplane();
  glPopMatrix();

  //Apply the transformations then draw the eagle.
  glPushMatrix();
  glTranslatef(eaglePriorX, eaglePriorY, eaglePriorZ);
  glRotatef(eagleRotX, 1, 0, 0);
  glRotatef(eagleRotY, 0, 1, 0);
  glRotatef(eagleRotZ, 0, 0, 1);
  glTranslatef(eaglePostX, eaglePostY, eaglePostZ);
  drawEagle();
  glPopMatrix();
  glPopMatrix();
}
Exemple #2
0
void redraw(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    /* Only need this if you care to look at the stencil buffer */
    if(dataChoice == STENCIL)
	glClear(GL_STENCIL_BUFFER_BIT);

    glPushMatrix();
    glScalef(.5, .5, .5);

    if(stage == 1)
       goto doneWithFrame;

    setupLight();

    setupNormalDrawingState();
    glPushMatrix();
    glTranslatef(0, 1, 4);
    glRotatef(135, 0, 1, 0);
    drawAirplane();
    glPopMatrix();

    if(stage == 2)
       goto doneWithFrame;

    setupBasePolygonState(3);	/* 2 decals */
    drawGround();

    if(stage == 3)
       goto doneWithFrame;

    setupDecalState(1);		/* decal # 1 = the runway asphalt */
    drawAsphalt();

    if(stage == 4)
       goto doneWithFrame;

    setupDecalState(2);		/* decal # 2 = yellow paint on the runway */
    drawStripes();

    if(stage == 5)
       goto doneWithFrame;

    setupDecalState(3);		/* decal # 3 = the plane's shadow */
    glDisable(GL_LIGHTING);
    glEnable(GL_BLEND);
    glPushMatrix();
    glColor4f(0, 0, 0, .5);
    glTranslatef(0, 0, 4);
    glRotatef(135, 0, 1, 0);
    glScalef(1, 0, 1);
    drawAirplane();
    glPopMatrix();
    glDisable(GL_BLEND);
    glEnable(GL_LIGHTING);

doneWithFrame:

    setupNormalDrawingState();

    glPopMatrix();

    switch(dataChoice) {
        case COLOR:
	    break; /* color already in back buffer */

	case STENCIL:
	    copyStencilToColor(GL_BACK);
	    break;

	case DEPTH:
	    copyDepthToColor(GL_BACK);
	    break;
    }

    glutSwapBuffers();
}