Reputation: 1269
I'm making a Connect4 game using freeglut and glew. This is how my game looks. The circles (white/red/blue) were the only objects rendered at every single call back of the display() function.
Circle * ChessPiece = new Circle(0.08f);
Every thing worked fine, except that if I let the app sit for about 2 minutes, it crashed with the following error message:
The way I resolved this issue was to add these 2 lines right above glGenVertexArrays()
glewExperimental = GL_TRUE;
glewInit();
This worked. However, it resulted in a performance hit (since glewInit() was called every single time a circle was rendered, at every display() call back).
I tried to put glewExperimental and glewInit() inside the display() call-back function hoping to improve the performance, but the app still crashed after 2 minutes of running.
Another work-around was to pre-render all the circles at the very beginning, and then just change the color as the game progresses. This worked also. However, I would prefer to dynamically generate circles during the game progress, not at the beginning of the game.
So, my question is:
Is this a GLEW bug? or is this my code problem? The reason why I'm thinking its' a glew bug was because when I added glewExperimental and glewInit() right before glGenVertexArrays(), the game no longer crashed. The only downside was the massive performance hit.
Putting glewExperimental and glewInit() in the main() function or in the display() call-back function didn't work either. The app would compile and run fine for the first 2 minutes before it crashed.
If it's a GLEW bug, how do I get around this issue? (without having to pre-render the circles at the beginning of the game).
Code snipet for drawing the circle:
void Circle::draw(float x, float y, float z)
{
//Codes to generate circle vertices
//These stop the app from crashing after 2 minutes, but I don't want to use them
//glewExperimental = GL_TRUE;
//glewInit();
glGenVertexArrays(1, &vertexArray_Circle);
glBindVertexArray(vertexArray_Circle);
glGenBuffers(1, &vertexBuffer_Circle);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer_Circle);
glBufferData(GL_ARRAY_BUFFER, (sizeof(Circle_vertices)/sizeof(float))*sizeof(GLfloat), Circle_vertices, GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
//Draw circle
glBindVertexArray(vertexArray_Circle);
glVertexAttrib3f((GLuint)1, RED, GREEN, BLUE);
glDrawArrays(GL_TRIANGLE_FAN, 0, index/3);
glDisableVertexAttribArray(0);
}
Upvotes: 2
Views: 376
Reputation: 16612
It would be nice to see the actual code, rather than just a screen-shot of it, but it looks like you're generating a new vertex array every time you draw, and it fails just after this...
I suspect that you're not actually deleting those vertex arrays, and given long enough, you're running out of resources and the call fails. Without checking for errors, you end up crashing.
Really you should not regenerate the array unless necessary, and if it is, make sure you delete the old one.
Upvotes: 2