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

我的代码:

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


c&#43;&#43; - 在0x1000A456(glut32.dll)OpenGL中引发异常-LMLPHP

错误在这一行:

glutDisplayFunc(display2);


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

最佳答案

使用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;
}

10-05 21:37