Problem with drawArrays segmentation fault

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.

	// Enable vertex array and such

	// Push a new matrix to the GL_MODELVIEW stack.

	// 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

	glDrawArrays(GL_QUADS, 0, 4);

	// Pop the matrix from the stack.

	// Diable vertex array and such

	// Flip the buffer for double buffering
	glBindBuffer(GL_ARRAY_BUFFER, 0);

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.

glDrawArrays(GL_TRIANGLES, 0, 3);

You only have 3 vertices, not 4…

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.

It means “draw 3 vertices starting from index 0” i.e. vertices 0, 1 and 2.

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.