SFML Segmentation Fault when using VBOs?

I'm trying to follow along with the gltut tutorials and for some reason when I call GLDrawArrays my program segmentation faults. I've been looking at the state of my application with the Mac OpenGL Profiler and Googling and I can't seem to figure out why it isn't working. I may have missed setting some OpenGL state somewhere.

My code is here, it segfaults on line 32. If I don't call glEnableClientState(GL_VERTEX_ARRAY); and the respective glDisableClientState(GL_VERTEX_ARRAY); it compiles and runs, but I get no rendering, which is what I would expect given my limited OpenGL knowledge.

• do you link SFML as static or dynamic? the default build is for dynamic linking, when trying to link it as static, you'll get segmentation fault after exiting drawing functions, don't quote me on this, though, i'm no expert, i just had a similar issue and that was the problem – dreta Feb 15 '12 at 4:50
• I link it dynamically. I can do immediate mode drawing just fine. – sensae Feb 15 '12 at 5:17
• Is it because you're trying to draw a triangle with two vertices? – user10968 Feb 19 '12 at 5:48
• I guess I may have misunderstood how glDrawArrays works, but even if I try to draw two vertices it segfaults. I'll add that on my Mac it segfaults trying to read memory address 0x00000000. For some reason on my linux machine gdb can't get a backtrace, complaining Backtrace stopped: previous frame inner to this frame (corrupt stack?). – sensae Feb 19 '12 at 9:12
• Do you mean "even if I try to draw three vertices it segfaults"? Triangles have three vertices :P – user10968 Feb 19 '12 at 9:53

First:

glDrawArrays(GL_TRIANGLES, 0, 2);


That will never actually draw anything. Triangles are made of 3 vertices and you told it to read 2.

I doubt that's causing the seg-fault, but it might be. Just change the 2 to 3.

Next:

const float vertexPositions[] = {
0.75f, 0.75f, 0.0f, 0.2f,
0.75f, -0.75f, 0.0f, 0.2f,
-0.75f, -0.75f, 0.0f, 0.2f,
};


Again I'm not sure how you copied that wrong, but the "0.2f" for the W component should be "1.0f".

glEnableClientState(GL_VERTEX_ARRAY);


Why do you call this? OK, I know why you're calling it. You've somehow taken a tutorial that's supposed to be showing you how to do modern, shader-based OpenGL, and are adapting it to fixed-function stuff.

If you are, then you need to adapt it properly.

glVertexAttribPointer is for setting up generic vertex attributes. You cannot use generic vertex attributes with fixed-function rendering. If you're going to do things in fixed-function, you need to actually do them in fixed function:

glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(4, GL_FLOAT, 0, 0);

glDrawArrays(GL_TRIANGLES, 0, 3);

glDisableClientState(GL_VERTEX_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);

• Great reply, I was operating with an incorrect understanding of the distinction between the different rendering methods. – sensae Feb 19 '12 at 21:28

My guess: You have 12 floats for your positions. You are telling to OpenGL that each vertex should be 4 floats by second parameter in here: glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);, so you have 3 vertices for these.

Then you tell OpenGL to draw 2 triangles in glDrawArrays command. But this would need 6 vertices - 3 vertices per triangle. So this could cause segmentation fault. Try to draw just one triangle or add more vertices.

• Unless I'm reading it incorrectly, the OpenGL API states that the third argument to glDrawArrays is the number of points to read. Either way, calling GLDrawArrays(GL_TRIANGLES, 0, 1); results in the same error. – sensae Feb 15 '12 at 7:10
• Maybe you can also check for OpenGL errors. Try to call opengl.org/sdk/docs/man/xhtml/glGetError.xml after each function call. If it doesn't return some error earlier. – zacharmarz Feb 15 '12 at 8:04
• And also - is that fault in your first draw call or in some other? – zacharmarz Feb 15 '12 at 8:14
• glGetError returns GL_NO_ERROR. I'm not sure what your second question was, but the issue happens when I call glDrawArrays. – sensae Feb 15 '12 at 18:14
• Never mind. It was bad idea ;) – zacharmarz Feb 15 '12 at 19:39