How to change the viewport resolution in OpenTK
I 开发者_StackOverflow社区am using the OpenTk GameWindow. I have been adding graphics settings to my project. But I cannot figure out how to correctly change the resolution or go in or out of fullscreen mode in runtime.
Can someone please explain the correct procedure to changing the resolution and/or fullscreen state in while the game is running.
Using WindowState = WindowState.Fullscreen;
and WindowState = WindowState.Fullscreen;
work, but they modify the viewing area and setting GL.Viewport doesn't fix it.
I am currently changing the monitor resolution with DisplayDevice.GetDisplay(DisplayIndex.Default).ChangeResolution
If you're not in full screen mode, you can resize the window by simply calling the Size
property on the GameWindow
object. You already know about the WindowState
propery.
The main thing you need to do is override the OnResize
method in your GameWindow
class. This is automatically called when your game window is resized, including setting it into full screen mode. From there, you can re-initialize your viewport.
For example, in the project I'm currently testing with, the following code correctly resizes the view port whenever I resize the window or move in/out of full screen mode. Although I don't make use of resolution switching when moving to full screen mode, I would imagine it would work perfectly well for that too. In the below example yoursizehere
is 640x640 and is scaled using the GL.Ortho method to fit the GameWindow.ClientSize
. (I'm a novice at OpenTK (and OpenGL for that matter) so I still have a ton to learn myself - but the below works for me)
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
GL.Viewport(this.ClientRectangle);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Ortho(0, yoursizehere.Width, yoursizehere.Height, 0, -1, 0);
}
The above example is for a 2D view port which is what I'm currently experimenting in... having enough troubles getting to grasp with OpenGL concepts in two dimensions let alone three!
Hope this helps
Look at the GameWindow constructors. Most of them take width and height as their first parameters. The fullscreen is set via GameWindowFlags parameter.
精彩评论