Changsheng_S
Changsheng_S
采纳率100%
2016-01-07 04:55

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

10
已采纳

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

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • qq_27183003 ysuwood 5年前

    没什么问题,可能你配置有问题吧。或者编译器不同。
    我用的是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();
    }
    

    运行结果:
    图片说明

    点赞 1 评论 复制链接分享
  • qq_28249373 shifenglv 5年前

    ""glutDisplayFunc(myDisplay);""这段代码有错!传入的参数应该是&myDisplay而不是myDisplay.

    点赞 评论 复制链接分享
  • qq_27183003 ysuwood 5年前

    OpenGL需要在IDE进行配置,配置好了吗?百度搜搜,很多介绍如何配置的。
    先运行个测试程序。

    点赞 评论 复制链接分享