#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来创建背景并将其包含在单独的空隙中。热气球工作正常,但我无法阻止背景移动。我只希望热气球升起。我希望背景保持原样。我该如何实现?
答案 0 :(得分:4)
请注意,自几十年来,不赞成使用glBegin
/ glEnd
序列进行绘制以及使用固定功能管线矩阵堆栈。
阅读有关Fixed Function Pipeline的信息,并参阅Vertex Specification和Shader了解最新的渲染方式。
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();
}