我做了一个简短的代码,以便使用OpenGL 4.5 (GLEW API)清除黑色的SDL2窗口。然而,它只有在我使用英特尔芯片组时才有效(在这种情况下,应该使用旧的OpenGL版本)。问题是,如果我使用我的Nvidia GTX 960M,窗口仍然是空白的。也许我忘了写一些特定于opengl4.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);
}
我将doublebuffer选项切换为FALSE,它可以工作。我意识到,如果doublebuffer是ON,我需要在看到黑色窗口背景之前做几个"交换"。这说得通,但很奇怪。顺便说一下,我最后使用GLFW而不是SDL2。