3

I made a short code in order to clear a SDL2 window with black color using OpenGL 4.5 (GLEW API). However, it works only when I use my Intel chipset (In this case an older OpenGL version should be used). The problem is that if I use my Nvidia GTX 960M, the window is still blank. Maybe I forgot to write something which is specific to OpenGL 4.5? Have you got any idea about this? Here is the code sample:

DisplayContext::DisplayContext(PropertiesDictionary properties)
{
    const string windowTitle = properties.getString("window_title");
    const int screenX = properties.getNumber("screen_resolution_x");
    const int screenY = properties.getNumber("screen_resolution_y");
    const bool isFullscreen = properties.getBoolean("fullscreen");

    const int gl_majorVersion = properties.getNumber("gl_major_version");
    const int gl_minorVersion = properties.getNumber("gl_minor_version");
    const int doublebuffer = properties.getNumber("gl_doublebuffer");
    const int depthSize = properties.getNumber("gl_depth_size");
    const bool isGlewExperimental = properties.getBoolean("glew_experimental");

    SDL_Init(SDL_INIT_VIDEO);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, gl_majorVersion); // 4
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, gl_minorVersion); // 5
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, doublebuffer); // TRUE
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, depthSize); // 24

    window = SDL_CreateWindow(
        windowTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screenX, screenY,
        SDL_WINDOW_OPENGL | (isFullscreen ? SDL_WINDOW_FULLSCREEN : NULL));

    context = SDL_GL_CreateContext(window);
    glewExperimental = isGlewExperimental ? GL_TRUE : GL_FALSE; // TRUE
    glewInit();

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    SDL_GL_SwapWindow(window);
}
SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
Antonin
  • 181
  • 2
  • 11
  • Check for errors returned by SDL and GL. – Ivan Rubinson Jul 04 '16 at 16:42
  • Does `glewInit` succeed on both graphics cards? – SurvivalMachine Jul 04 '16 at 16:42
  • @SurvivalMachine glewInit() returns FALSE when on GTX960M card. And it returns TRUE on Intel chipset. So the error seems to be here. But I don't see why... Do you think I should use another API ? – Antonin Jul 04 '16 at 19:13
  • Is your GTX driver up-to-date? Can you create an older OpenGL context for it, like 3.3? – SurvivalMachine Jul 04 '16 at 19:15
  • 1
    For what you want to do you don't need GLEW. Most likely glewInit fails, because you don't get a valid OpenGL in the first place. My best guess is, that you're trying to use your NVidia GPU using the Mesa OpenGL implementation. Using multi vendor OpenGL used to be a PITA. Eventually NVidia developed https://github.com/NVIDIA/libglvnd which takes away much of the pain when having multiple OpenGL implementations installed side by side. I strongly suggest you install and use it. May require some manual work to make your Linux distribution to cooperate. – datenwolf Jul 04 '16 at 19:24

1 Answers1

4

I switched the doublebuffer option to FALSE and it works. I realized that if doublebuffer is ON, I need to do several "swap" before seeing a black window background. It makes sense but it's quite strange. By the way I finally use GLFW instead of SDL2.

Antonin
  • 181
  • 2
  • 11
  • 1
    *"I finally use GLFW instead of SDL2"* - Keep in mind that it lacks *many* useful features compared to SDL. You can't even maximize a window with it. – HolyBlackCat Jul 04 '16 at 23:24