I am attempting to get C++ to work with SDL2 and OpenGl on Ubuntu Linux 16.10 x64. When I run my code, I get a "Missing GL version" error. When I search through Google and Stack Exchange I find limited information that applies to my situation. The few "solutions" I find do not work.
List of some of the resources I have already checked:
- https://www.opengl.org/discussion_boards/showthread.php/163694-glew-Missing-GL-Version-error
- Failed to Initialize GLEW. Missing GL version
- Glew initialisation failed : Missing GL version (GLFW)
- http://en.sfml-dev.org/forums/index.php?topic=10262.0
The common theme in my research seems to be that I am not properly specifying my OpenGl version, but, I am attempting to do so with my calls to SDL_GL_SetAttribute
. It also seems to be a common theme that my context is not properly selected, but, calling SDL_GL_MakeCurrent
has no effect.
In addition to this, I have a gtx 1070 with the Nvidia binary blob drivers from the Ubuntu repositories (additional drivers dialog) installed, so, I doubt having the actual OpenGl verion I am requesting available is an issue either. Furthermore, other OpenGl programs (ie. Minecraft) work without issue.
Lastly, very similar code works just fine on other computers (that very similar code does not work on my present computer), and it even gets so far as to create and initalize an OpenGl context correctly on an older computer that does not have my required OpenGl version supported -- those older computers fail later when an unsupported function is called.
What am I doing incorrectly?
I have the following code:
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <sstream>
int main(int argc, char *argv[]){
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_Window *m_window = SDL_CreateWindow(
"test",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
800, 600,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
);
SDL_GLContext m_glContext = SDL_GL_CreateContext(m_window);
SDL_GL_MakeCurrent(m_window, m_glContext); //tried without this code
glewExperimental = GL_TRUE;
int c = glewInit();
if(c != GLEW_OK){
std::ostringstream sout;
sout << "Failed to initialize GLEW: " << glewGetErrorString(c);
throw std::runtime_error(sout.str());
}
}