【问题标题】:Make fire float up让火飘起来
【发布时间】:2019-05-20 06:41:15
【问题描述】:
// this is the display function it is called when ever you want to draw something all drawing should be called form here
void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    // draw background 
    drawBackground();
    glPushMatrix();
    // draw hot air balloon
    drawAirBalloon();
    // draw spray
    drawSpray();
    glPopMatrix();
    // draw rain
    drawRain();
    // draw fire
    drawSpray();
    calcFPS();
    counter++;
    glFlush();
    glutSwapBuffers();
    glutPostRedisplay();
}

问题是我不能用我的热气球让火上升。我之所以只将代码放在上面(显示)是因为我认为问题与这个特定的代码有关。

要查看完整代码,请点击以下链接:

hot air balloon code

这就是问题所在:

【问题讨论】:

    标签: c++ visual-studio opengl


    【解决方案1】:

    这是因为您在函数circle 中将模型视图矩阵设置为单位矩阵。

    将函数circle()中的glLoadIdentity替换为glPushMatrix()/glPopMatrix()

    void circle(double radius, double xc, double yc) {
          int i;
          double angle = 2 * 3.1415 / 20;   // circle is drawn using 20 line.
          double circle_xy[100][40];
          circle_xy[0][0] = radius + xc;
          circle_xy[0][1] = yc;
          glMatrixMode(GL_MODELVIEW); 
          glPushMatrix();
          // set fire position
          glTranslatef(-40.0, 60.0, 0.0);
          glBegin(GL_POLYGON);
          for (i = 1; i < 20; i++) {
              circle_xy[i][0] = radius * cos(i * angle) + xc;
              circle_xy[i][1] = radius * sin(i * angle) + yc;
              glVertex2f(circle_xy[i - 1][0], circle_xy[i - 1][1]);
              glVertex2f(circle_xy[i][0], circle_xy[i][1]);
          }
          glEnd();
        glPopMatrix();
    }
    

    为了使代码更易于理解,我建议将glTranslatef 删除为drawAirBalloon,但在display 中进行:

    void drawAirBalloon(void) {
        // glTranslatef(squareX, squareY, squareZ); <--- remove
    
        // ....
    } 
    
    void display() {
        glClear(GL_COLOR_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        // draw background 
        drawBackground();
    
        glPushMatrix();
        glTranslatef(squareX, squareY, squareZ); // <--- insert
        // draw spray
        drawSpray();
        // draw hot air balloon
        drawAirBalloon();
        glPopMatrix();
    
        // draw rain
        drawRain();
        calcFPS();
        counter++;
        glFlush();
        glutSwapBuffers();
        glutPostRedisplay();
    }
    

    您付出了很多努力,并编写了很多棘手且结构良好的代码。我转达建议阅读Vertex SpecificationShader。继续,但开始摆脱已弃用的固定功能管道并切换到现代渲染方式。

    【讨论】:

    • 如何将气球放在火前?
    • @Muddy 交换 drawSpraydrawAirBalloon。我刚刚在答案中交换了它。
    猜你喜欢
    • 1970-01-01
    • 2020-06-09
    • 2020-11-29
    • 1970-01-01
    • 2021-09-23
    • 2022-12-13
    • 2021-09-12
    • 2020-12-09
    • 2019-07-14
    相关资源
    最近更新 更多