将我的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();
}
}
}发布于 2021-02-13 07:23:33
使用GL.Viewport将视图矩形设置为窗口的子区域。作为GL.Clear的“复制”操作不受视口矩形的影响。因此,您还需要设置一个剪刀试验。
您需要知道窗口的当前大小(current_w,current_h)和窗口的原始大小(original_w,original_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, 0, w, this.Size.Y);
GL.Viewport(x, 0, w, 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, 0, current_w, current_h);
GL.ClearColor(0.0f, 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, 0, w, this.Size.Y);
GL.Viewport(x, 0, w, 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, y, this.Size.X, h);
}
GL.ClearColor(Color.CornflowerBlue);
GL.Clear(ClearBufferMask.ColorBufferBit);
// [...]
this.SwapBuffers();
}https://stackoverflow.com/questions/66182561
复制相似问题