Objectives

Lab Setup

int windowWidth;
int windowHeight;
  if (w <= h)
  {
    windowWidth = 100;
    windowHeight = 100 / aspectRatio;
    glOrtho(-100.0, 100.0, -windowHeight, windowHeight, 1.0, -1.0);
  }
  else
  {
    windowWidth = 100 * aspectRatio;
    windowHeight = 100;
    glOrtho(-windowWidth, windowWidth, -100.0, 100.0, 1.0, -1.0);
  }
float x = -25.0f;
float y =  25.0f;
  glRectf(x, y, x+50, y-50);
  //glRectf(-25.0f, 25.0f, 25.0f, -25.0f);

Timers

void timerFunction(int value)
{
}
  glutTimerFunc(33, timerFunction, 1);
  static float xstep = 1.0f;
  static float ystep = 1.0f;
  if (x > windowWidth - 50 || x < -windowWidth)
    xstep = -xstep;

  if (y > windowHeight || y < -windowHeight + 50)
    ystep = -ystep;

  x += xstep;
  y += ystep;
  cout << "x:" << x << " y:" << y << endl;
  glutTimerFunc(33, timerFunction, 1);

Refactor

Exercises

1.Timing

2.Idle loop

void idleFunction()
{
}

int main(int argc, char* argv[])
{
  //...
  glutIdleFunc(idleFunction);
  //...
}
void idleFunction()
{
  update();
  glutPostRedisplay();
}
  static clock_t lastTime = 0;

  if (lastTime == 0)
    lastTime = clock();
  clock_t currTime = clock();
  clock_t deltaTime = currTime - lastTime;
  float secondsDelta = (float)deltaTime/CLOCKS_PER_SEC;
  if (secondsDelta >= 0.025)
  {
    update();
    lastTime = currTime;
    glutPostRedisplay();
  }

3. Another Rectangle