开发者社区> 问答> 正文

OpenGL初学,请问我这段代码有错吗?为什么在窗口显示不出来

#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();
}

展开
收起
a123456678 2016-03-09 09:45:49 1916 0
1 条回答
写回答
取消 提交回答
  • 没什么问题,可能你配置有问题吧。或者编译器不同。
    我用的是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();
    }
    2019-07-17 18:55:24
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
《0代码搭应用》 立即下载
不止代码 立即下载
《15分钟打造你自己的小程序》 立即下载