#include
#include
#include
#include
#include
const GLint screenWidth = 640;
const GLint screenHeight = 480;
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
}
void checkerboard(void)
{
for(GLint i = 0; i < 8; i++)
{
for(GLint j = 0; j < 8; j++)
{
if((i+j)%2 == 0)
{
glColor3f(0.0f, 0.0f, 0.0f);
}
else
{
glColor3f(1.0f, 1.0f, 1.0f);
}
glRecti(30*j, 30*i, 30*(j+1), 30*(i+1));
}
}
glFlush();
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
checkerboard();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(0, 0);
glutCreateWindow("chess");
glutDisplayFunc(myDisplay);
myInit();
glutMainLoop();
}
沒什麼問題,可能你配置有問題吧。或者編譯器不同。
我用的是VC2010+Win8.1+OpenGL1.4
你的程序不太友好,我加了按ESC退出。如下:
#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/glut.h>
const GLint screenWidth = 640;
const GLint screenHeight = 480;
void myInit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
}
void checkerboard(void)
{
for(GLint i = 0; i < 8; i++)
{
for(GLint j = 0; j < 8; j++)
{
if((i+j)%2 == 0)
{
glColor3f(0.0f, 0.0f, 0.0f);
}
else
{
glColor3f(1.0f, 1.0f, 1.0f);
}
glRecti(30*j, 30*i, 30*(j+1), 30*(i+1));
}
}
glFlush();
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
checkerboard();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(0, 0);
glutCreateWindow("chess");
glutDisplayFunc(myDisplay);
myInit();
glutKeyboardFunc(keyboard);
glutMainLoop();
}
運行結果: