none
Why My OpenGL code can not run on Windows 8.1 RRS feed

  • 问题

  • The following is my code about OpenGL :

    #include <windows.h>

    #include <glut.h>

    void Setup()
    {
    glEnable(GL_DEPTH_TEST);
    glFrontFace(GL_CCW);
    glEnable(GL_CULL_FACE);
    glShadeModel(GL_SMOOTH);
    }

    void Resize(GLsizei w,GLsizei h)
    {
    if(h == 0)
    h = 1;
    glViewport(0,0,w,h);

    float ratio = (float)w/(float)h;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45,ratio,1,1000);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

    void RenderScene()
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(0.0f,0.0f,0.3f,1.0f);

    glBegin(GL_TRIANGLES);
    glVertex3f(-2,-2,-5.0);
    glVertex3f(2,0.0,-5.0);
    glVertex3f(0.0,2,-5.0);
    glEnd();

    glutSwapBuffers();
    }

    int main(int argc, char** argv)
    {
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition(50,50);
    glutInitWindowSize(800,600);
    glutCreateWindow("TestOpengl");

    glutDisplayFunc(RenderScene);
    glutReshapeFunc(Resize);

    Setup();

    glutMainLoop();

    return 0;
    }

    I use visual studio 2013 for compiling the code,but when running the program, it can not run well, and visual studio hits

    :"Unhandled exception at 0x03814840 in TestOpenGL.exe: 0xC0000005: Access violation executing location 0x03814840."

    it say "glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);" is wrong.

    I can not find the error.Please help me, thank you .

    2014年6月13日 5:47