c++ - 阻止背景移动

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

#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 创建背景并将其包含在一个单独的空隙中。热气球工作得很好,但我无法阻止背景移动。我只想让热气球升上去。我希望背景留在原处。我怎样才能做到这一点?

最佳答案

请注意,几十年来,由 glBegin/glEnd 序列和固定功能流水线矩阵堆栈进行的绘制已被弃用。 了解 Fixed Function Pipeline并查看 Vertex SpecificationShader用于最先进的渲染方式。


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

在绘制气球之前使用glPushMatrix矩阵插入(保存)模型 View 矩阵。使用glPopMatrix矩阵在气球绘制完成后弹出(恢复)模型 View 矩阵。这导致翻译 (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();
}

关于c++ - 阻止背景移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53801620/

相关文章:

visual-studio - 防止 Visual Studio 嵌套 typescript 文件

c++ - RegQueryValueEx 得到不正确的数据

visual-studio - 使用 VS2015 添加需要凭据的 NuGet 包源

c++ - 相对于彼此重载 operator++ 前缀/后缀?

oracle - "Test connection succeeded"但 "An unexpected error occurred in the ODP.NET, Managed Driver"

java - 使用已弃用的 glTranslatef 还是使用 glBufferSubData 更新坐标更好?

c++ - 在 OpenGL 中对四边形进行纹理化

c++ - 如何将opengl进程绑定(bind)到具体的gpu卡上?

c++ - 非成员(member)运营商作为外国类(class)的私有(private)成员(member)

c++ - 从体积或区域中的起点向外迭代而不对其进行排序