异常抛出0x1000A456(glut32.dll)OpenGL

时间:2016-08-26 18:34:50

标签: c++ opengl exception glut

当我运行我的程序显示此错误时,您会知道告诉我这是什么问题?我该如何解决?

我的代码:

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include <GLFW/glfw3.h>
#include "Rect.h"

const int rows = 40;
const int cols = 20;
const int length = rows * cols;
int width = 905;
int height = 720;
Rect * list[length];

void createList() {
    for (int i = 0; i < length; i++) {
        list[i] = new Rect();
    }
}

void createQuad(int index) {
    int wQuad = (width / 20) - 5;
    int hQuad = 12;
    int margin = 5;
    int marginTop = (index / 20) * (hQuad + margin) + margin;
    int marginLeft = (index % 20) * (wQuad + margin) + margin;

    if (list[index]->getVisible()) {
        glColor3ub(list[index]->getR(), list[index]->getG(), list[index]-   >getB());
    } else {
        glColor3ub(0, 0, 0);
    }

    glBegin(GL_QUADS);  
        glVertex2i(marginLeft        , marginTop         ); // A
        glVertex2i(marginLeft + wQuad, marginTop         ); // B
        glVertex2i(marginLeft + wQuad, marginTop + hQuad); // D
        glVertex2i(marginLeft        , marginTop + hQuad); // C     
    glEnd();
}

void display2(void) {
    glClear(GL_COLOR_BUFFER_BIT);

    for (int p = 0; p < length; p++) {
        createQuad(p);
    }   

    glutSwapBuffers();
}

void init() {
    glClearColor(0, 0, 0, 0);
    glOrtho(0, 905, 720, 0, -0.5f, 0.5f);
    glViewport(0, 0, 905, 720);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();   
}

int CALLBACK WinMain(
    __in  HINSTANCE hInstance,
    __in  HINSTANCE hPrevInstance,
    __in  LPSTR lpCmdLine,
    __in  int nCmdShow
) {
    GLFWwindow* window;

    // Initialize the library
    if (!glfwInit())
        return -1;

    // Create the window
    window = glfwCreateWindow(905, 720, "Color Game", NULL, NULL);
    if (!window) {
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window); 

    init();

    // Create list...
    createList();
    char *myargv[1];
    int myargc = 1;
    myargv[0] = strdup("ColorGame");    

    while (!glfwWindowShouldClose(window)) {
        glutInit(&myargc, myargv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
        glutInitWindowPosition(100, 100);
        glutInitWindowSize(width, height);
        glutDisplayFunc(display2);
        // display2();
        // glutReshapeFunc(reshape);
        glfwSwapBuffers(window);

        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

Exception error

错误在这一行:

glutDisplayFunc(display2);

如果我只放了display2();工作,但我需要把glutDisplayFunc(display2);因为在我需要用这行glutMouseFunc(鼠标)处理鼠标事件之后;如果我把glutMouseFunc(鼠标);显示相同的错误。

2 个答案:

答案 0 :(得分:2)

使用GLUT xor GLFW。它们都是需要单独控制事件循环并且不能同时使用的应用程序框架。选择一个并坚持下去。

使用GLUT的示例(必须猜测Rect的实现):

#include <GL/glut.h>

const int rows = 40;
const int cols = 20;
const int length = rows * cols;
int width = 905;
int height = 720;

class Rect
{
public:
    Rect()
        : mVisible( rand() % 100 < 80 )
        , mR( rand() % 255 )
        , mG( rand() % 255 )
        , mB( rand() % 255 )
    {
    }

    bool getVisible() { return mVisible; }
    unsigned char getR() { return mR; }
    unsigned char getG() { return mG; }
    unsigned char getB() { return mB; }

private:
    bool mVisible;
    unsigned char mR;
    unsigned char mG;
    unsigned char mB;
};

Rect* list[length];

void createList()
{
    for (int i = 0; i < length; i++)
    {
        list[i] = new Rect();
    }
}

void createQuad(int index)
{
    int wQuad = (width / 20) - 5;
    int hQuad = 12;
    int margin = 5;
    int marginTop = (index / 20) * (hQuad + margin) + margin;
    int marginLeft = (index % 20) * (wQuad + margin) + margin;

    if( list[index]->getVisible() )
    {
        glColor3ub
            (
            list[index]->getR(),
            list[index]->getG(),
            list[index]->getB()
            );
    }
    else
    {
        glColor3ub(0, 0, 0);
    }

    glBegin(GL_QUADS);  
    glVertex2i( marginLeft        , marginTop         ); // A
    glVertex2i( marginLeft + wQuad, marginTop         ); // B
    glVertex2i( marginLeft + wQuad, marginTop + hQuad ); // D
    glVertex2i( marginLeft        , marginTop + hQuad ); // C     
    glEnd();
}

void display2(void)
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();   
    glOrtho(0, 905, 720, 0, -0.5f, 0.5f);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();   

    for (int p = 0; p < length; p++)
    {
        createQuad(p);
    }   

    glutSwapBuffers();
}

int main( int argc, char** argv )
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(width, height);
    glutCreateWindow("ColorGame");
    createList();
    glutDisplayFunc(display2);
    glutMainLoop();
    return 0;
}

答案 1 :(得分:0)

你混淆了过剩和glfw,这是一个坏主意。您应该在线阅读这些库的文档。如果你把这个代码带出循环:

    glutInit(&myargc, myargv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(width, height);
    glutDisplayFunc(display2);

删除glfw *并添加以下行:

    glutMainLoop();

然后它应该工作。