Hi guys,
I’m playing with terrain generation, and I’m getting a segfault whenever I call drawArrays. I have no idea how to trace this.
Here’s the code that’s not working for me:
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
vector<Vec4> firstTriangle;
firstTriangle.push_back(Vec4(0, 0, 0, 1));
firstTriangle.push_back(Vec4(0, 1, 0, 1));
firstTriangle.push_back(Vec4(1, 0, 0, 1));
glBufferData(GL_ARRAY_BUFFER, firstTriangle.size() * sizeof(Vec4), &firstTriangle, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
Where Vec4 is a class defined as follows:
class Vec4
{
float[4] data;
// methods and such omitted
}
And finally, the code that actually causes the crash, my render function:
// Clear the frame buffer each time we draw.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Enable vertex array and such
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
// Push a new matrix to the GL_MODELVIEW stack.
glPushMatrix();
// Multiply the matrix by the object's transformation matrix.
// ..
// Bind the VBO
glBindBuffer(GL_ARRAY_BUFFER, buffer);
// Pass vertex information
glVertexPointer(4, GL_FLOAT, sizeof(Vec4), 0);
// Draw object
glColor3f(1,1,1);
// THIS IS THE PART THAT CAUSES THE CRASH
glDrawArrays(GL_QUADS, 0, 4);
// Pop the matrix from the stack.
glPopMatrix();
// Diable vertex array and such
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
// Flip the buffer for double buffering
glBindBuffer(GL_ARRAY_BUFFER, 0);
SDL_GL_SwapBuffers();
Whenever I put the program through gdb, this is the only output I can get:
Program received signal SIGSEGV, Segmentation fault.
0xb5added8 in ?? ()
(gdb) backtrace
Any help would be greatly appreciated. Thanks guys.
Edit: I should probably add, using cout statements I traced it to glDrawArrays, thus how I know that’s where my program is dying. I think I’m setting something up incorrectly though, just not sure what I’ve missed.