3

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:

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());
    }


}
Community
  • 1
  • 1
john01dav
  • 1,842
  • 1
  • 21
  • 40
  • Have you tried putting your glewInit() code in main()? – vincent Feb 23 '17 at 18:39
  • 1
    You're calling `glewInit()` twice. – HolyBlackCat Feb 23 '17 at 18:47
  • @HolyBlackCat Good catch. I fixed that (updated code in question), but, the issue still occurs, – john01dav Feb 23 '17 at 19:46
  • @vincent I have added an MCVE to the post with everything in main(). – john01dav Feb 23 '17 at 19:53
  • #include glew before SDL – Ripi2 Feb 23 '17 at 20:22
  • The current code works on my system (Ubuntu 16.04 64-bit). Running nvidia-375, libglew-dev, libsdl2-dev. Compiled with g++ test.cc -lSDL2 -lGLEW – vincent Feb 23 '17 at 20:29
  • I do get the 'Missing GL version' error if I try to initialized glew before SDL, though. – vincent Feb 23 '17 at 20:34
  • Why aren't you verifying that `m_glContext` is non-`NULL`? What's generating the `Missing GL version` output? – genpfault Feb 23 '17 at 20:35
  • @Ripi2 Including glew before SDL has no effect. – john01dav Feb 23 '17 at 20:36
  • @vincent It works on some of my other computers too -- but not my main one with a gtx 1070, same packages, same build command, and nvidia-367 (the driver apparently tested by Ubuntu devs for functionality). – john01dav Feb 23 '17 at 20:37
  • @genpfault I'm somewhat new to C++. I didn't realize there was a way to check if an object (as opposed to a pointer, which m_glContext is not) is null. The Missing GL Version output is returned by `glewGetErrorString()`. – john01dav Feb 23 '17 at 20:39
  • 1
    What if you add a call to SDL_GetError() after each SDL call? – vincent Feb 23 '17 at 20:40
  • 1
    If it works on other similar computers, I'd say that some package (Mesa-dev?) is missing. – Ripi2 Feb 23 '17 at 20:41
  • @vincent SDL_Init() says "No available video device." I am trying installing xorg-dev. – john01dav Feb 23 '17 at 20:50
  • What does `glxinfo` shows? – Ripi2 Feb 23 '17 at 20:54
  • @vincent xorg-dev has no effect – john01dav Feb 23 '17 at 20:55
  • @Ripi2 http://pastebin.com/jtwc0Mya – john01dav Feb 23 '17 at 20:56
  • Nothing wrong from glxinfo. Stupid question: do you run it under `optirun` or similar? – Ripi2 Feb 23 '17 at 21:01
  • Out of curiousity, what happens if you: sudo apt install libsdl1.2 but don't change the code? – vincent Feb 23 '17 at 21:06
  • @Ripi2 I don't know what optirun is, but, it is apparently part of the `bumblebee` package, which I don't have installed. – john01dav Feb 23 '17 at 21:33
  • @vincent I can't install it. I get "Package libsdl1.2 is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source." I imagine something was changed between 16.04 and 16.10 that is causing this breakage. – john01dav Feb 23 '17 at 21:33
  • Sorry, libsdl1.2-dev – vincent Feb 23 '17 at 21:35
  • @vincent Just installing it has no effect (even after a recompile). If I attempt to build against it, via `pkg-config --libs sdl` rather than `pkg-config --libs sdl2` then my code no longer compiles due to undefined reference errors (I'd rather not go and learn what is frankly an antiquated library unless I have something better than "curiosity" to say that it will be helpful). – john01dav Feb 23 '17 at 21:37
  • 3
    @vincent Your sdl1.2 suggestion gave me an idea, and it worked. It turns out that some experimentation I did awhile ago left a custom-built version of sdl2 on my system. Removing that and installing sdl2 from the package manager fixed the issue. Thanks for all of your help :) – john01dav Feb 23 '17 at 22:01
  • @Ripi2 +1 for the mesa-dev. I was having exactly the same problem. After running `apt-get install mesa-common-dev` it worked fine. Thank you! – Xantium Mar 16 '21 at 11:00

0 Answers0