PDA

View Full Version : Problem with drawArrays segmentation fault



Hiram
04-30-2011, 08:07 AM
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), &amp;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.

uruk
04-30-2011, 08:36 AM
glDrawArrays(GL_TRIANGLES, 0, 3);
You only have 3 vertices, not 4...

Hiram
04-30-2011, 05:52 PM
Good spotting! I found my problem was actually enabling color arrays without calling glColorPointer, which of course makes sense.

Edit: Actually, that doesn't make sense. Wouldn't 0-3 mean 0, 1, 2, so 3 vertices? Because if I change the call to glDrawArrays(GL_TRIANGLES, 0, 2) then I get nothing drawn to the screen, where as 0, 3 gives me a triangle.

uruk
05-01-2011, 04:19 AM
It means "draw 3 vertices starting from index 0" i.e. vertices 0, 1 and 2.

Hiram
05-01-2011, 05:17 AM
Awesome, thanks again!

Offhand, I've noticed that my terrain is only rendering about 20% of the heightmap, would you have any idea where I should start looking to fix this?

Edit; disregard! I was using the size of my input height map when calling glDrawArrays, which was naughty for my implementation.