以下代码是在OpenGL 代码2的基础上进行修改,分为作画,初始化,和主程序3个部分进行的:
#include <GL/glut.h>
void drawSquare() {
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
}
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);//单缓冲区,RGB模式绘制
glutInitWindowSize(600,600);
glutInitWindowPosition(-1,-1);
glutCreateWindow("Square");
init();
glutDisplayFunc(drawSquare);
//init();
glutMainLoop();