停止背景移动


#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序列和固定函数管道矩阵堆栈绘制的方法一直不受欢迎。阅读有关固定函数管道的信息,并参阅顶点规范和着色器以了解最先进的渲染方式。


矩阵堆栈上的矩阵可以通过glPushMatrixglPopMatrix进行保存和恢复。

在绘制引出序号之前,使用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();
}

最新更新