我正在 Linux 上学习 OpenGL,但我无法进行模式切换(全屏并返回)。
该窗口似乎进入全屏,但看起来不正确。要切换模式,将创建一个新窗口并销毁旧窗口。
void OpenGLWindow::FullScreen(bool fullScreen, int width, int height)
{
GLFWwindow *oldHandle = m_window;
m_fullscreen = fullScreen;
m_width = width;
m_height = height;
m_window = glfwCreateWindow(width, height, m_caption.c_str(),
fullScreen ? m_monitor : NULL, m_window);
if (m_window == NULL)
{
glfwTerminate();
throw std::runtime_error("Failed to recreate window.");
}
glfwDestroyWindow(oldHandle);
m_camera->Invalidate();
// Use entire window for rendering.
glViewport(0, 0, width, height);
glfwMakeContextCurrent(m_window);
glfwSwapInterval(1);
if (m_keyboardHandler) SetKeyboardHandler(m_keyboardHandler);
}
问题更新
我已更新代码以使用您的代码并遇到相同的问题。根据您的建议,我现在正在更新相机,但再次无济于事:(
void OpenGLCamera::Invalidate()
{
RecalculateProjection(m_perspProjInfo->Width(), m_perspProjInfo->Height());
m_recalculateViewMatrix = true;
m_recalculatePerspectiveMatrix = true;
m_recalculateProjectionMatrix = true;
}
void OpenGLCamera::RecalculateProjection(int width, int height)
{
float aspectRatio = float(width) / height;
float frustumYScale = cotangent(degreesToRadians(
m_perspProjInfo->FieldOfView() / 2));
float frustumXScale = frustumYScale;
if (width > height)
{
// Shrink the x scale in eye-coordinate space, so that when geometry is
// projected to ndc-space, it is widened out to become square.
m_projectionMatrix[0][0] = frustumXScale / aspectRatio;
m_projectionMatrix[1][1] = frustumYScale;
}
else {
// Shrink the y scale in eye-coordinate space, so that when geometry is
// projected to ndc-space, it is widened out to become square.
m_projectionMatrix[0][0] = frustumXScale;
m_projectionMatrix[1][1] = frustumYScale * aspectRatio;
}
}
兔子:当我调整大小时:
原文由 Gemma Morriss 发布,翻译遵循 CC BY-SA 4.0 许可协议
在下文中,我将描述一个小而方便的类,它处理调整 GLFW 窗口的大小并处理打开和关闭全屏窗口。
所有使用的 GLFW 函数都在 GLFW 文档 中有详细记录。
创建窗口时,将用户函数指针(
glfwSetWindowUserPointer
)设置为窗口管理类。调整大小回调由glfwSetWindowSizeCallback
设置。创建窗口后,其当前大小和位置可以通过glfwGetWindowPos
和glfwGetWindowSize
获得。当调整大小通知发生时,指向窗口管理类的指针可以通过
glfwGetWindowUserPointer
获得:通知窗口大小的任何更改并存储新的窗口大小(
glfwGetWindowSize
):当窗口大小发生变化时,视口必须适合窗口大小(
glViewport
)。这可以在应用程序的主循环中完成:如果当前窗口处于全屏模式,可以通过询问该窗口用于全屏模式的监视器来实现(
glfwGetWindowMonitor
):要打开和关闭全屏模式,必须调用
glfwSetWindowMonitor
,或者使用全屏模式的监视器,或者使用nullptr
: