Cava Cava - 3 months ago 20
C++ Question

Exception thrown at 0x1000A456 (glut32.dll) OpenGL

When I run my program shows this error, you would know tell me what is the problem? And how can I fix it?

My code:

#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

The error is in this line:

glutDisplayFunc(display2);


If I put only display2(); works, but I need put glutDisplayFunc(display2); because after I need to treat a mouse event with this line glutMouseFunc(mouse); If I put glutMouseFunc(mouse); shows the same error.

Answer

Use GLUT xor GLFW. They're both application frameworks that want sole control of your event loop and can't be used simultaneously. Pick one and stick with it.

Example using GLUT (had to guess at Rect's implementation):

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