如何在调整GLFW窗口大小时绘制?

时间:2017-08-25 11:21:21

标签: c++ opengl glfw window-resize

每当我调整GLFW窗口的大小时,它都不会在我调整窗口大小时绘制。窗口的新曝光部分仅在我完成窗口大小调整后才会被绘制。你可以在下面的图片中看到它:

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

解释此问题的任何解决方案。

4 个答案:

答案 0 :(得分:1)

每当调整窗口大小时,事件处理(glfwPollEvents)就会停止,但是在执行此操作时,事件处理(glfwSwapBuffers)会不断发出调整大小事件,这些事件将由您已经使用的调整大小回调进行动态处理。您可以从此处重新绘制场景,并调用glfwPollEvents进行渲染,即使在void draw() { // Rendering code goes here glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glfwSwapBuffers(window); } 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); // Re-render the scene because the current frame was drawn for the old resolution draw(); } 中也是如此。实际上,这可以通过以下代码来实现:

glClearColor

并将渲染(glClearglfwSwapBuffersdraw)从主循环移到draw中。在您的主循环中留下对window的调用,并将draw设为全局变量,或在调用它时将其传递给draw,否则它将不在glfwSwapBuffers的范围内,module_init(init_module) //for insmod module_exit(cleanup_module) //for rmmod 所需的位置。

仅当用户按住移动鼠标时才起作用-仅在调整大小窗口上按住鼠标左键仍会停滞。要解决此问题,您需要在之外的另外一个线程中进行渲染。(不,没有线程就无法做到这一点。很抱歉,这是GLFW的工作方式,除了它们之外,没人可以更改它。

答案 1 :(得分:-1)

Windows事件处理程序没有将控制权返回给主线程。它是如何工作的,你无法改变它。

然而,您可以将所有渲染和glfw命令移动到不同的线程,这不会被Windows停滞。

答案 2 :(得分:-1)

您无需设置单独的线程。

您只需处理WM_SIZE消息并在发生时重新绘制,可以使用glfwSetWindowSizeCallback函数进行访问。您可以调用循环内的所有内容,或者只调用渲染和交换缓冲区部分。

当拖动操作正在进行时,Windows不会从消息处理返回,但会继续处理事件。所以你只需要在回调中做任何你想做的事情,加上足以重新配置你的视口和矩阵以及任何新的客户端大小。这个原则也适用于窗户移动或任何类似的东西。

对于Win32或glfw,基本流程为:

void MyStart()
{
  for glfw set the callback to MyCallback via glfwSetWindowSizeCallback

  while(!ShouldExit)
  {
    some code
    MyFrame()
  }
}

void MyFrame()
{
  do things I normally do per frame

  paint this and that

  swap buffers
}

void MyCallback()
{ 
    get new client area size
    reset viewport or fov
    MyFrame();   
}

// for windows without glfw
void WndProc()
{
  WM_SIZE:
    MyCallback();
}

答案 3 :(得分:-1)

我不敢相信这里的“答案”。
您只需将其放在调整大小功能中即可:

glfwSwapBuffers(window);