阻止背景移动
Stop the background from moving
#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 and see Vertex Specification and 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();
}
#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 and see Vertex Specification and 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();
}