I'm trying to code a simple triangle. However glGenVertexArrays(1, &VertexArrayID)
casues a segmentation fault whenever I run it.
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_Window* window = SDL_CreateWindow("OpenGL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL);
SDL_GLContext context = SDL_GL_CreateContext(window);
SDL_Event event;
SDL_bool quit = SDL_FALSE;
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID); // This causes crash
I found the exact same thing here OpenGL Segfaults on glGenVertexArrays but
I forgot to setup an OpenGL context. All fixed now.
doesn't help, as I believe I have already created an OpenGL context SDL_GLContext context = SDL_GL_CreateContext(window);
.
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.