Segmentation Fault at Glgenvertexarrays( 1, &Vao );

Segmentation fault at glGenVertexArrays( 1, &vao );

glewExperimental = GL_TRUE; 
glewInit();

Should do the magic


Experimental Drivers

GLEW obtains information on the supported extensions from the graphics
driver. Experimental or pre-release drivers, however, might not report
every available extension through the standard mechanism, in which
case GLEW will report it unsupported. To circumvent this situation,
the glewExperimental global switch can be turned on by setting it to
GL_TRUE before calling glewInit(), which ensures that all extensions
with valid entry points will be exposed.

glGenVertexArrays causes a segmentation fault, how do I properly set up an OpenGL context?

You have to Initialize GLEW. Call glewInit immediately after creating the OpenGL context:

SDL_GLContext context = SDL_GL_CreateContext(window);

if (glewInit() != GLEW_OK)
{
// error handling
// [...]
}

Note, that glewInit will return GLEW_OK f it was successful. glewInit initializes the function pointers for the OpenGL functions. If you try to call the function through an uninitialized function pointer, a segmentation fault occurs.

Segmentation fault when trying to wrap glGenVertexArrays call in a method

std::unique_ptr<Graphics> g;
g->createVAO();

Where do you actually populate g with something?

Right now it looks like you're dereferencing a NULL pointer.

Try this:

std::unique_ptr<Graphics> g( new Graphics );

OpengGL segmentation fault

OpenGL commands beyond those of version 1.1 need to be retrieved first from the driver (this is not true on Apple machines). For this task you are using Glew. Good. But you have to initialize it. Something like

  glfwMakeContextCurrent(window);

//Now we have a valid context as current, let's allow glew to do its job
glewExperimental = GL_TRUE; //Ensure it get all pointers
if ( GLEW_OK != glewInit() )
{
//glewInit failed, something is seriously wrong.
return false; //or any handling here
}

And then you can continuing creating shaders and like.

glDrawArrays segmentation fault

I dont see anything wrong with your code. I usually get this problem when my context isnt set correctly. For example, when I dont have GLEW set up correctly in my case. That may be the same problem for you.

Why does using glDrawElements() causes a Segmentation Fault?

The problem is with your index buffer binding. The index buffer (GL_ELEMENT_ARRAY_BUFFER) binding is part of the VAO state. Skipping the unrelated calls, you have the following overall sequence:

...
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
...
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
...
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

Since the GL_ELEMENT_ARRAY_BUFFER binding is part of the VAO state, the current index buffer binding is replaced with the index buffer binding in the VAO state when you call:

glBindVertexArray(vao);     

Since you never bind an index buffer while the VAO is bound, this means that the index buffer binding in the VAO state is 0. As a result, you don't have an index buffer bound after this call.

What happens next is that you make the glDrawElements() call with the last argument 0. Without an index buffer bound, the last argument is interpreted as a pointer to CPU memory. So OpenGL tries to read index data at address 0, which causes the crash.

To fix this, you simply have to bind the index buffer while the VAO is bound. You can either do that by changing the order of your calls, and create/bind the VAO before you set up the index buffer. Or you can bind it again when you set up the vertex attribute state. For example, at the very end of the Shader constructor, after the glVertexAttribPointer() and related calls, you add this call:

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);


Related Topics



Leave a reply



Submit