将我的 GameWindow 设置为全屏会将其拉伸到桌面分辨率

问题描述

将我的 GameWindow 设置为全屏会拉伸它,如何在不丢失原始窗口分辨率的情况下使用黑条将其设置为全屏?

这是我的代码

using System;
using System.Collections.Generic;
using System.Text;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Windowing.Common;
using OpenTK.Windowing.GraphicslibraryFramework;
using OpenTK.Windowing.Desktop;
using System.Drawing;

namespace OnlineGame
{
    public class Game : GameWindow
    {
        public Game(GameWindowSettings gameWindowSettings,NativeWindowSettings nativeWindowSettings)
             : base(gameWindowSettings,nativeWindowSettings)
        {
            WindowState = WindowState.Fullscreen;
        }

        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            if (KeyboardState.IsKeyDown(Keys.Escape))
            {
                Close();
            }

            base.OnUpdateFrame(e);

            GL.Clear(ClearBufferMask.ColorBufferBit);
            GL.ClearColor(Color.CornflowerBlue);

            this.SwapBuffers();
        }
    }
}

解决方法

使用 GL.Viewport 将视口矩形设置为窗口的子区域。 GL.Clear 的“复制”操作不受视口矩形的影响。因此,您还需要设置一个 Scissors Test

您需要知道窗口的当前大小(current_wcurrent_h)和窗口的原始大小(original_woriginal_h)。计算当前窗口的纵横比(current_aspect)和原始窗口的纵横比(original_aspect):

double current_aspect = (double)current_w / current_h;
double original_aspect = (double)original_w / original_h;

计算子区域并设置视口和剪刀矩形:

int w = (int)(current_w * original_aspect / current_aspect);
int x = (current_w - w) / 2;
GL.Scissor(x,w,this.Size.Y);
GL.Viewport(x,this.Size.Y);

用黑色清除整个窗口。然后限制视口矩形并启用剪刀测试。
请注意,您必须在 GL.ClearColor 指令之前设置清除颜色 (GL.Clear)。

protected override void OnUpdateFrame(FrameEventArgs e)
{
    if (KeyboardState.IsKeyDown(Keys.Escape))
    {
        Close();
    }

    base.OnUpdateFrame(e);

    int original_w = ...;
    int original_h = ...;
    int current_w = this.Size.X;
    int current_h = this.Size.Y;
    double current_aspect = (double)current_w / current_h;
    double original_aspect = (double)original_w / original_h;

    GL.Disable(EnableCap.ScissorTest);
    GL.Viewport(0,current_w,current_h);
    GL.ClearColor(0.0f,0.0f,0.0f);
    GL.Clear(ClearBufferMask.ColorBufferBit);

    GL.Enable(EnableCap.ScissorTest);
    if (current_aspect > original_aspect)
    {
        int w = (int)(current_w * original_aspect / current_aspect);
        int x = (current_w - w) / 2;
        GL.Scissor(x,this.Size.Y);
        GL.Viewport(x,this.Size.Y);
    }
    else
    {
        int h = (int)(current_h * current_aspect / original_aspect);
        int y = (current_h - h) / 2;
        GL.Scissor(0,y,this.Size.X,h);
        GL.Viewport(0,h);
    }
    
    GL.ClearColor(Color.CornflowerBlue);
    GL.Clear(ClearBufferMask.ColorBufferBit);

    // [...]    

    this.SwapBuffers();
}