【问题标题】:Stop the background from moving停止背景移动
【发布时间】:2019-05-17 00:24:30
【问题描述】:
#include <stdio.h>
#include "glut.h"
#include <math.h>

float squareX = 0.0f;
float squareY = -0.3f;
float squareZ = 0.0f;
static int flag = 1;

// The background
void drawBackground() {
    // draw the green ground
    glBegin(GL_POLYGON);
    glColor3f(0.0, 0.60, 0.0);
    glVertex2f(800, 100);
    glVertex2f(800, 0);
    glVertex2f(0, 0);
    glVertex2f(0, 100);
    glVertex2f(800, 100);
    glEnd();
    // draw the blue sky
    glBegin(GL_POLYGON);
    glColor3f(0.0, 0.0, 1.0);
    glVertex2f(800, 100);
    glVertex2f(800, 800);
    glVertex2f(0, 800);
    glVertex2f(0, 100);
    glVertex2f(800, 100);
    glEnd();
    glFlush();
}

// the hot air balloon
void drawAirBalloon(void) {
    glTranslatef(squareX, squareY, squareZ);
    // draw the balloon
    float theta;
    int cutsegment = 45;
    int start = -90 + cutsegment / 2;
    int end = 270 - cutsegment / 2;
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POLYGON);
    glColor3f(1.0, 0.0, 0.0);
    for (int i = -45; i <= 225; i++) {
        theta = i * 3.142 / 180;
        glVertex2f(355 + 70 * cos(theta), 225 + 90 * sin(theta));
    }
    glEnd();
    // draw first rope on the left
    glBegin(GL_LINES);
    glColor3f(0.0, 0.0, 0.0);
    glVertex2f(320, 95);
    glVertex2f(295, 177);
    glEnd();
    // draw first rope on the right
    glBegin(GL_LINES);
    glColor3f(0.0, 0.0, 0.0);
    glVertex2f(415, 180);
    glVertex2f(390, 95);
    glEnd();
    // draw propane burner
    glBegin(GL_POLYGON);
    glColor3f(0.1, 0.1, 0.1);
    glVertex2f(335, 140);
    glVertex2f(335, 120);
    glVertex2f(375, 120);
    glVertex2f(375, 140);
    glVertex2f(335, 140);
    glEnd();
    // draw basket
    glBegin(GL_POLYGON);
    glColor3f(0.6, 0.35, 0.1);
    glVertex2f(320, 95);
    glVertex2f(320, 40);
    glVertex2f(390, 40);
    glVertex2f(390, 95);
    glVertex2f(320, 95);
    glEnd();
}

void initRendering() {
    glEnable(GL_DEPTH_TEST);
}

// handles the size of the screen
void handleResize(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0f, (float)w, 0.0f, (float)h, -1.0f, 1.0f);
}   

// display
void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    // draw hot air balloon
    drawAirBalloon();
    // draw background
    drawBackground();
    glutSwapBuffers();
}

// move the hot air balloon up
void update(int value) {
    if (flag) {
        squareY += 1.0f;
        if (squareY > 350.0) {
            flag = 0;
        }
    }
    glutPostRedisplay();
    glutTimerFunc(25, update, 0);
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Hot Air Balloon");
    initRendering();
    glutDisplayFunc(drawScene);
    glutReshapeFunc(handleResize);
    glutTimerFunc(25, update, 0);
    glutMainLoop();
    return(0);
}

我正在尝试制作一个热气球,让它从地面飘向天空。我使用 GL_POLYGON 创建背景并将其包含在一个单独的空白中。热气球工作得很好,但我无法阻止背景移动。我只希望热气球升上去。我希望背景保持在其位置。我怎样才能做到这一点?

【问题讨论】:

    标签: c++ visual-studio opengl opengl-compat


    【解决方案1】:

    请注意,几十年来,glBegin/glEnd 序列和固定函数管道矩阵堆栈的绘图已被弃用。 阅读Fixed Function Pipeline 并查看Vertex SpecificationShader,了解最先进的渲染方式。


    矩阵堆栈上的矩阵可以通过glPushMatrix and glPopMatrix保存和恢复。

    在绘制气球之前使用glPushMatrix矩阵来推送(保存)模型视图矩阵。气球绘制后使用glPopMatrix矩阵弹出(恢复)模型视图矩阵。这导致翻译 (glTranslatef) 仅应用于气球:

    void drawScene() {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        // draw hot air balloon
        glPushMatrix();
        drawAirBalloon();
        glPopMatrix();
    
        // draw background
        drawBackground();
        glutSwapBuffers();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多