2017-08-25 296 views
1

每當我調整的OpenGL窗口,而我調整窗口的大小不畫如何畫。完成窗口大小調整後,窗口的新暴露部分纔會被繪製。您可以在下面的圖片中看到它自己:而調整窗口大小

image

這裏是我的應用程序的代碼。我在Visual Studio 2015上運行Windows 10

#include <glad/glad.h> 
#include <GLFW/glfw3.h> 
#include <iostream> 

void framebuffer_size_callback(GLFWwindow* window, int width, int height); 
void processInput(GLFWwindow *window); 
void get_resolution(int* window_width, int* window_height); 
void initGlfwSettings(); 
GLFWwindow* initGlfwWindow(); 
void initGlad(); 

// settings 
const unsigned int SCR_WIDTH = 800; 
const unsigned int SCR_HEIGHT = 600; 

int main() 
{ 
    initGlfwSettings(); 

    GLFWwindow* window = initGlfwWindow(); 

    initGlad(); 

    // glad: load all OpenGL function pointers 
    // --------------------------------------- 


    // render loop 
    // ----------- 
    while (!glfwWindowShouldClose(window)) 
    { 
     int width, height; 
     glfwGetWindowSize(window, &width, &height); 


     glClearColor(0.2f, 0.3f, 0.3f, 1.0f); 
     glClear(GL_COLOR_BUFFER_BIT); 

     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); 
     // input 
     // ----- 
     processInput(window); 

     // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) 
     // ------------------------------------------------------------------------------- 
     glfwSwapBuffers(window); 
     glfwPollEvents(); 
    } 

    // glfw: terminate, clearing all previously allocated GLFW resources. 
    // ------------------------------------------------------------------ 
    glfwTerminate(); 
    return 0; 
} 

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly 
// --------------------------------------------------------------------------------------------------------- 
void processInput(GLFWwindow *window) 
{ 
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) 
     glfwSetWindowShouldClose(window, true); 
} 

// glfw: whenever the window size changed (by OS or user resize) this callback function executes 
// --------------------------------------------------------------------------------------------- 
void framebuffer_size_callback(GLFWwindow* window, int width, int height) 
{ 
    // make sure the viewport matches the new window dimensions; note that width and 
    // height will be significantly larger than specified on retina displays. 
    glViewport(0, 0, width, height); 
} 

void get_resolution(int* window_width, int* window_height) { 
    const GLFWvidmode * mode = glfwGetVideoMode(glfwGetPrimaryMonitor()); 

    *window_width = mode->width; 
    *window_height = mode->height; 
} 

void initGlfwSettings() 
{ 
    // glfw: initialize and configure 
    // ------------------------------ 
    glfwInit(); 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 


    #ifdef __APPLE__ 
     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X 
    #endif 
} 

GLFWwindow* initGlfwWindow() 
{ 
    /*GLFWmonitor* monitor = glfwGetPrimaryMonitor(); 
    int width; 
    int height; 

    get_resolution(&width, &height);*/ 



    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "learning opengl", NULL, NULL); 
    if (window == NULL) 
    { 
     std::cout << "Failed to create GLFW window" << std::endl; 
     glfwTerminate(); 
     exit(1); 
    } 

    glfwMakeContextCurrent(window); 
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); 
    glfwSwapInterval(1); 

    return window; 
} 

void initGlad() 
{ 
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) 
    { 
     std::cout << "Failed to initialize GLAD" << std::endl; 
     exit(1); 
    } 
} 

解釋此問題的任何解決方案。

回答

0

這不是控制返回到主線程中的窗口事件處理程序。它的窗戶如何工作,你不能改變它。

您可以將所有的渲染和GLFW命令但是移動到不同的線程,這將不會被Windows停滯不前。

+0

它通常是一件好事,是調整結束的時候,因爲你經常是窗口的大小相同的幀緩衝區的控制研究只能返回。這些只能在調整大小完成後調整大小。 – dari

+0

我只是想繼續更新我所創建的一切。像運行視頻或實時數據一樣,至少捕獲需要自己的線程來避免失速。 –

+0

@JonathanOlson你會如何暗示這一點?我嘗試了兩次來自己實現它,一旦顯示了所有內容,但總是顯示加載光標。在我第二次嘗試顯示黑屏。 –