停止背景移动 [英] Stop the background from moving

查看:134
本文介绍了停止背景移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#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来创建背景并将其包含在单独的空隙中.热气球工作正常,但我无法阻止背景移动.我只希望热气球升起.我希望背景保持原样.我该如何实现?

I am trying to create a hot air balloon to float up to the sky from the ground. I used GL_POLYGON to create the background and included it into a separate void. The hot air balloon works perfectly fine but I am having trouble stopping the background from moving. I only want the hot air balloon to go up. I want the background to stay at its position. How can I achieve this?

推荐答案

请注意,使用glBegin/glEnd序列和固定功能流水线矩阵堆栈进行绘制已有几十年的历史了. 阅读有关固定功能管道的信息,并参见

Note, that drawing by glBegin/glEnd sequences and the fixed function pipeline matrix stack is deprecated since decades. Read about Fixed Function Pipeline and see Vertex Specification and Shader for a state of the art way of rendering.

可以通过

The matrices on the matrix stack can be saved an restored by glPushMatrix and glPopMatrix.

使用glPushMatrix矩阵在绘制气球之前推入(保存)模型视图矩阵.绘制气球后,使用glPopMatrix矩阵弹出(还原)模型视图矩阵.这导致翻译(glTranslatef)仅应用于气球:

Use glPushMatrix matrix to push (save) the model view matrix before the balloon is drawn. Use glPopMatrix matrix to pop (restore) the model view matrix after the balloon is drawn. This cause that the translation (glTranslatef) is applied to the balloon only:

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

这篇关于停止背景移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆