Search code examples
c++openglglut

Fade in and fade out object on a specific time


int state = 1;
bool turbines_visible = true;

// move the hot air balloon up
// make the square go up
void update(int value) {
    // 1 : move up
    if (state == 1) {
        squareY += 1.0f;
        if (squareY > 650.0) {
            state = 2;
            squareX = -400.0f;
            squareY = 200.0f;
        }
    }
    // 2 : move right
    else if (state == 2) {
        squareX += 1.0f;
        if (squareX > 500.0) {
            state = 3;
            squareX = 0.0f;
            squareY = 600.0f;
        }
    }
    // 3 : move down
    else if (state == 3) {
        squareY -= 1.0f;
        if (squareY < 0.0) {
            state = 0;
        }
    }
    glutTimerFunc(25, update, 0);
    turbines_visible = !turbines_visible;
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    switch (state) {
    case 0:
        drawBackground1();
        break;
    case 1:
        drawBackground1();
        break;
    case 2:
        drawBackground2();
        break;
    case 3:
        drawBackground1();
        break;
    }
    glPushMatrix();
    glTranslatef(squareX, squareY, squareZ);
    // display spray
    drawSpray();
    // display hot air balloon
    drawAirBalloon();
    glPopMatrix();  
    if (turbines_visible) {
        // display first (left) wind turbine
        drawLeftTurbine();
        // display first (right) wind turbine
        drawRightTurbine();
    }
    // display rain
    drawRain();
    calcFPS();
    counter++;
    glFlush();
    glutSwapBuffers();
    glutPostRedisplay();
}

The hot air balloon travels up perfectly fine, but the wind turbines keep faded in and out really fast, which is what I don't want. I want it to be visible in the first scene, invisible in the second scene, and visible again in the third scene. I know that the problem is with the glutTimerFunc code because it is using 25 milliseconds, but I need it for my hot air balloon. I would appreciate if someone could help me solve this problem.

Click here to see the full code

Scene 1

enter image description here

Click here to see GIF

Scene 2

enter image description here

Click here to see GIF

Scene 3

enter image description here

Click here to see GIF


Solution

  • I want it to be visible in the first scene, invisible in the second scene, and visible again in the third scene.

    The condition for turbines_visible has to be

    turbines_visible = !turbines_visible;
    turbines_visible = state != 2;