在OpenGL / GLFW 3.2中切换窗口和全屏

时间:2017-11-20 23:22:25

标签: c++ opengl glfw

我正在学习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);
}

初始窗口 enter image description here

全屏(不正确) enter image description here

返回窗口 enter image description here

问题更新

我已更新代码以使用您的代码并获得相同的问题。根据你的建议,我现在正在更新相机,但再次无济于事(

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

Rabbid:当我调整大小时:

enter image description here

Rabbid:当我全屏播出时: enter image description here

3 个答案:

答案 0 :(得分:5)

在下文中,我将描述一个小而方便的课程,该课程涉及调整GLFW窗口的大小并打开和关闭开关全屏窗口。
所有使用过的GLFW函数都在GLFW documentation中有详细记录。

#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include <array>
#include <stdexcept>

class OpenGLWindow
{
private:

    std::array< int, 2 > _wndPos         {0, 0};
    std::array< int, 2 > _wndSize        {0, 0};
    std::array< int, 2 > _vpSize         {0, 0};
    bool                 _updateViewport = true;
    GLFWwindow *         _wnd            = nullptr;
    GLFWmonitor *        _monitor        = nullptr;

    void Resize( int cx, int cy );

public:

    void Init( int width, int height );
    static void CallbackResize(GLFWwindow* window, int cx, int cy);
    void MainLoop ( void );
    bool IsFullscreen( void );
    void SetFullScreen( bool fullscreen );
};

创建窗口时,用户函数指针(glfwSetWindowUserPointer)设置为窗口管理类。调整大小的回调由glfwSetWindowSizeCallback设置。创建窗口后,其当前大小和位置可以通过glfwGetWindowPosglfwGetWindowSize获得。

void OpenGLWindow::Init( int width, int height )
{
    _wnd = glfwCreateWindow( width, height, "OGL window", nullptr, nullptr );
    if ( _wnd == nullptr )
    {
        glfwTerminate();
        throw std::runtime_error( "error initializing window" ); 
    }

    glfwMakeContextCurrent( _wnd );

    glfwSetWindowUserPointer( _wnd, this );
    glfwSetWindowSizeCallback( _wnd, OpenGLWindow::CallbackResize );

    _monitor =  glfwGetPrimaryMonitor();
    glfwGetWindowSize( _wnd, &_wndSize[0], &_wndSize[1] );
    glfwGetWindowPos( _wnd, &_wndPos[0], &_wndPos[1] );
    _updateViewport = true;
}

发生调整大小通知时,可以通过glfwGetWindowUserPointer获取指向窗口管理类的指针:

static void OpenGLWindow::CallbackResize(GLFWwindow* window, int cx, int cy)
{
    void *ptr = glfwGetWindowUserPointer( window );
    if ( OpenGLWindow *wndPtr = static_cast<OpenGLWindow*>( ptr ) )
        wndPtr->Resize( cx, cy );
}

通知窗口大小的任何更改并存储新的窗口大小(glfwGetWindowSize):

void OpenGLWindow::Resize( int cx, int cy )
{
    _updateViewport = true;
}

当窗口大小发生变化时,视口必须适合窗口大小(glViewport)。这可以在应用程序的主循环中完成:

void OpenGLWindow::MainLoop ( void )
{
    while (!glfwWindowShouldClose(_wnd))
    {
        if ( _updateViewport )
        {
            glfwGetFramebufferSize( _wnd, &_vpSize[0], &_vpSize[1] );
            glViewport( 0, 0, _vpSize[0], _vpSize[1] );
            _updateViewport = false;
        }

        // ..... render the scene

        glfwSwapBuffers(_wnd);
        glfwPollEvents();
    }
}  

如果当前窗口处于全屏模式,可以通过询问窗口用于全屏模式的监视器(glfwGetWindowMonitor)来实现:

bool OpenGLWindow::IsFullscreen( void )
{
    return glfwGetWindowMonitor( _wnd ) != nullptr;
} 

要打开和关闭全屏模式,必须使用全屏模式的显示器或glfwSetWindowMonitor调用nullptr

void SetFullScreen( bool fullscreen )
{
    if ( IsFullscreen() == fullscreen )
    return;

    if ( fullscreen )
    {
        // backup windwo position and window size
        glfwGetWindowPos( _wnd, &_wndPos[0], &_wndPos[1] );
        glfwGetWindowSize( _wnd, &_wndSize[0], &_wndSize[1] );

        // get reolution of monitor
        const GLFWvidmode * mode = glfwGetVideoMode(glfwGetPrimaryMonitor());

        // swithc to full screen
        glfwSetWindowMonitor( _wnd, _monitor, 0, 0, mode->width, mode->height, 0 );
    }
    else
    {
        // restore last window size and position
        glfwSetWindowMonitor( _wnd, nullptr,  _wndPos[0], _wndPos[1], _wndSize[0], _wndSize[1], 0 );
    }

    _updateViewport = true;
}

答案 1 :(得分:1)

您的代码存在一些问题:

  1. 假设glfwCreateWindow在全屏模式下将分辨率设置为width * height是不正确的。 GLFW documentation州(强调我的):

      

    对于全屏窗口,指定的大小将成为窗口所需视频模式的分辨率。只要没有图标化全屏窗口,就会为指定的监视器设置支持的视频模式最匹配所需的视频模式

  2. 假设窗口大小在&#34;像素&#34;中指定也不正确。再次引用relevant part of the documentation

      

    虽然窗口大小是以屏幕坐标测量的,但OpenGL可以使用像素。例如,传入glViewport的大小应以像素为单位。在某些机器上,屏幕坐标和像素是相同的,但在其他机器上它们不会。还有第二组函数用于检索窗口的帧缓冲区的大小(以像素为单位)。

  3. 问题1和2可以通过在创建窗口后调用glfwGetFramebufferSize来解决。这给我们留下了问题3:

    1. 您在没有当前GL背景的情况下致电glViewport - 导致未定义的行为,特别是在根本不设置视口时。现在这实际上是一个有趣的,因为新上下文的初始视口将是全新窗口,因此您的错误1和2没有直接影响。如果您的代码依赖于包含有用值的m_widthm_height,它们之后仍可能会产生一些影响。

答案 2 :(得分:1)

如果您只想在窗口和全屏之间切换,我建议您不要使用glfwCreateWindow创建新窗口。请改用glfwSetWindowMonitor

创建启用了全屏的窗口时,必须传递与监视器上的视频模式兼容的参数。您可以在主监视器上获得标准视频模式,如下所示:

GLFWmonitor *monitor = glfwGetPrimaryMonitor();
const GLFWvidmode *mode = glfwGetVideoMode(monitor);

并切换到全屏:

glfwSetWindowMonitor(window, mode, 0, 0, v->width, v->height, v->refreshRate);

只需传递nullptr - 模式和您自己的值:

glfwSetWindowMonitor(window, nullptr, 0, 0, windowWidth, windowHeight, windowRefreshRate);

不要忘记调整视口大小并更新相机。

您是在调整视口大小并在用户调整窗口大小时更新相机吗?

相关问题