2017-08-25 4 views
1

Chaque fois que je redimensionne une fenêtre OpenGL, elle ne dessine pas lorsque je redimensionne la fenêtre. La partie de la fenêtre qui vient d'être exposée n'est dessinée qu'une fois le redimensionnement de la fenêtre terminé. Vous pouvez voir par vous-même dans l'image ci-dessous:Comment dessiner pendant le redimensionnement d'une fenêtre

image

Voici le code pour mon application. Je cours Windows 10 sur Visual Studio 2015

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

Expliquer toutes les solutions à ce problème.

Répondre

0

Il s'agit du gestionnaire d'événements Windows qui ne retourne pas le contrôle au thread principal. C'est comment fonctionne Windows et vous ne pouvez pas le changer.

Vous pouvez cependant déplacer toutes vos commandes de rendu et de commande glfw vers un thread différent, qui ne sera pas bloqué par Windows.

+0

C'est généralement une bonne chose que le contrôleur ne soit renvoyé que lorsque le redimensionnement est terminé, car vous avez souvent des framebuffers de la même taille que la fenêtre. Ceux-ci ne doivent être redimensionnés qu'une fois lorsque le redimensionnement est terminé. – dari

+0

J'ai toujours voulu rester à jour dans tout ce que j'ai construit. Des choses comme l'exécution de vidéo ou de données en direct, au strict minimum, la capture a besoin de son propre thread pour éviter les décrochages. –

+0

@JonathanOlson comment exactement implémenteriez-vous cela? J'ai essayé deux fois de l'implémenter moi-même et une fois tout affiché mais le curseur de chargement serait toujours affiché. Sur mon deuxième essai, un écran noir s'affiche. –