Search code examples
openglcameraglutmeshmotion

OpenGL/Glut: glutTimerFunc seems not to start


I would like to use glutTimerFunc to move the camera around the scene which is composed of some mesh models. The camera path is build with a Bezier curve like the following:

if(sel == MODE_CAMERA_MOTION) {
    mode = MODE_CAMERA_MOTION;
    stepsCounter = 0;
    // Building camera track

    int i, j, k;

    for(j=0; j<MAX_CV; j++) {
        tmpCV[j][0] = CV[j][0];
        tmpCV[j][1] = CV[j][1];
        tmpCV[j][2] = CV[j][2];
    }

    for(i=0; i<STEPS; i++) {

        for(j=1; j<MAX_CV; j++) {
            for(k=0; k<MAX_CV-j; k++) {
                lerp(i/(float)(STEPS*10), tmpCV[k], tmpCV[k+1], tmpCV[k]);
            }
        }

        cameraPosition[i][0] = tmpCV[0][0];
        cameraPosition[i][1] = tmpCV[0][1];
        cameraPosition[i][2] = tmpCV[0][2];

    }

    glutTimerFunc(250, moveCamera, STEPS);
}

where the moveCamera function is:

void moveCamera() {
  if (mode == MODE_CAMERA_MOTION) {

    camE[0] = cameraPosition[stepsCounter][0];
    camE[1] = cameraPosition[stepsCounter][1];
    camE[2] = cameraPosition[stepsCounter][2];

    if(stepsCounter < STEPS) {
        stepsCounter++;
    }

    else {
        /* come back to the first point */
        camE[0] = 8.8;
        camE[1] = 4.9;
        camE[2] = 9.0;
        stepsCounter = 0;
    }

  glutPostRedisplay();

  }
}

But nothing moves. Maybe I have misunderstood the behaviour of that function. Where am I wrong?


Solution

  • The prototype to the function passed to glutTimerFunc should be

    void (*func)(int value)
    

    Where the last parameter of glutTimerFunc is the passed value. So your moveCamera should be:

    void moveCamera( int STEPS )
    

    Also, even so, the moveCamera function will be called once. You need to reregister at end of execution. This might be a possible solution:

    // instead of global STEPS and stepsCounter, we decrement at reexecution
    void moveCamera( int STEPS ) {
      ... do stuff
      glutTimerFunc(250, moveCamera, STEPS-1 ); 
    }