OpenGL 4.5:glClear()不起作用(使用SDL2和GLEW)

时间:2016-07-04 16:36:46

标签: c++ opengl sdl-2 glew opengl-4

我制作了一个简短的代码,以便使用OpenGL 4.5(GLEW API)清除黑色的SDL2窗口。但是,它仅在我使用我的英特尔芯片组时才有效(在这种情况下,应该使用较旧的OpenGL版本)。问题是如果我使用我的Nvidia GTX 960M,窗口仍然是空白的。也许我忘了写一些特定于OpenGL 4.5的东西?你对此有任何想法吗?这是代码示例:

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

1 个答案:

答案 0 :(得分:4)

我将doublebuffer选项切换为FALSE并且它可以正常工作。我意识到如果双缓冲器打开,我需要在看到黑色窗口背景之前做几次“交换”。这很有道理,但很奇怪。顺便说一下,我最终使用GLFW而不是SDL2。