在特定时间淡入和淡出对象

时间:2018-12-27 12:38:33

标签: c++ opengl glut

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();
}

热气球可以很好地向上移动,但是风力涡轮机却保持非常快的渐进渐出,这是我不想要的。我希望它在第一个场景中可见,在第二个场景中不可见,然后在第三个场景中再次可见。我知道问题出在glutTimerFunc代码上,因为它使用25毫秒,但是我的热气球需要它。如果有人可以帮助我解决此问题,我将不胜感激。

Click here to see the full code

场景1

enter image description here

Click here to see GIF

场景2

enter image description here

Click here to see GIF

场景3

enter image description here

Click here to see GIF

1 个答案:

答案 0 :(得分:2)

  

我希望它在第一个场景中可见,在第二个场景中不可见,然后在第三个场景中再次可见。

turbines_visible的条件必须为

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