而调整窗口大小
问题描述:
每当我调整的OpenGL窗口,而我调整窗口的大小不画如何画。完成窗口大小调整后,窗口的新暴露部分才会被绘制。您可以在下面的图片中看到它自己:而调整窗口大小
这里是我的应用程序的代码。我在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);
}
}
解释此问题的任何解决方案。
答
这不是控制返回到主线程中的窗口事件处理程序。它的窗户如何工作,你不能改变它。
您可以将所有的渲染和GLFW命令但是移动到不同的线程,这将不会被Windows停滞不前。
它通常是一件好事,是调整结束的时候,因为你经常是窗口的大小相同的帧缓冲区的控制研究只能返回。这些只能在调整大小完成后调整大小。 – dari
我只是想继续更新我所创建的一切。像运行视频或实时数据一样,至少捕获需要自己的线程来避免失速。 –
@JonathanOlson你会如何暗示这一点?我尝试了两次来自己实现它,一旦显示了所有内容,但总是显示加载光标。在我第二次尝试显示黑屏。 –