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,
theglewExperimental
global switch can be turned on by setting it to
GL_TRUE
before callingglewInit()
, 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
How to Export Templated Classes from a Dll Without Explicit Specification
At^Sysinfo and a C++ Terminal Program
Is There Still a Use for Inline
Boost.Asio-Based Http Client Library (Like Libcurl)
How to Generate Assembly Code with Clang in Intel Syntax
Accessing Protected Members of Superclass in C++ with Templates
Skipping Incompatible Libraries at Compile
Why Do We Need to Use 'Int Main' and Not 'Void Main' in C++
What's a Good Hash Function for English Words
In C++, How to Forward Declare a Class as Inheriting from Another Class
Conditional Operator Used in Cout Statement
What Is the Best Modern C++ Approach to Construct and Manipulate a 2D Array
Building Qt5 with Visual Studio 2012/Visual Studio 2013, and Integrating with the Ide
Can MACros Be Overloaded by Number of Arguments
Calling the Base Class Constructor from the Derived Class Constructor
Template Function as a Template Argument
How to Update a Printed Message in Terminal Without Reprinting