【发布时间】:2018-12-24 14:31:55
【问题描述】:
#include <stdio.h> // this library is for standard input and output
#include "glut.h" // this library is for glut the OpenGL Utility Toolkit
#include <math.h>
// left square
void drawShape1(void) {
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(82, 250);
glVertex2f(82, 200);
glVertex2f(140, 200);
glVertex2f(140, 250);
glEnd();
}
// right square
void drawShape2(void) {
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(232, 250);
glVertex2f(232, 200);
glVertex2f(290, 200);
glVertex2f(290, 250);
glEnd();
}
void initRendering() {
glEnable(GL_DEPTH_TEST);
}
// called when the window is resized
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);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawShape1();
drawShape2();
glutSwapBuffers();
glutPostRedisplay();
}
// the timer code
void update(int value) {
// add code here
glutPostRedisplay();
glutTimerFunc(5, update, 0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("Squares");
initRendering();
glutDisplayFunc(display);
glutReshapeFunc(handleResize);
glutTimerFunc(5, update, 0);
glutMainLoop();
return(0);
}
我在中间有两个正方形。一个正方形在左边,另一个正方形在右边(见下面的截图)。我试图让左边的方块每 5 秒消失/出现一次。我已经添加了计时器代码,但我正在努力使对象消失/出现。
预览:
【问题讨论】:
-
您正在使用固定功能管道。使用混合来完成它
-
一些题外话。请不要在 2018 年使用 glBegin/glEnd。它完全是过时的遗留 API。如果您开始学习加速图形编程,请至少从带有缓冲区对象的 OpenGL 3.0 开始。
-
如果不想渲染左边的正方形,就不要调用
drawShape1?使用全局标志来指示是否应该绘制正方形,并在计时器滴答事件中设置它。 -
gluttimerfunc 将 ms 作为其首要参数。 5 ms 和 5 s 非常不同。
-
你的意思是要让它淡入淡出?