2016-07-04 103 views
2

我做了一個短代碼,以便使用OpenGL 4.5(GLEW API)以黑色清除SDL2窗口。但是,它只在使用我的英特爾芯片組時才起作用(在這種情況下,應該使用較舊的OpenGL版本)。問題是,如果我使用我的Nvidia GTX 960M,窗口仍然是空白的。也許我忘了寫一些特定於OpenGL 4.5的東西?你有什麼想法嗎?以下是代碼示例:OpenGL 4.5:glClear()不起作用(使用SDL2和GLEW)

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

檢查SDL和GL返回的錯誤。 –

+0

'glewInit'在兩塊顯卡上都能成功嗎? – SurvivalMachine

+0

@SurvivalMachine glewInit()在GTX960M卡上返回FALSE。它在Intel芯片組上返回TRUE。所以這個錯誤似乎在這裏。但我不明白爲什麼......你認爲我應該使用另一個API嗎? – Ant0nin

回答

3

我將doublebuffer選項切換爲FALSE並且它可以工作。我意識到如果doublebuffer爲ON,我需要在看到黑色窗口背景之前做幾次「交換」。這很有道理,但很奇怪。順便說一句,我最終使用GLFW而不是SDL2。

+1

*「我最終使用GLFW而不是SDL2」* - 請記住,與SDL相比,它缺少許多有用的功能。你甚至無法用它最大化一個窗口。 – HolyBlackCat

相關問題