1

i am trying to add the ability to resize the app window without causing the contents of the window to stretch at the same time. i have tried changing the if statements and tried different ratio calculations to no affect

protected override void OnResize(EventArgs e) { base.OnResize(e); if (mShader != null) { int uProjectionLocation = GL.GetUniformLocation(mShader.ShaderProgramID, "uProjection"); int windowHeight = ClientRectangle.Height; int windowWidth = ClientRectangle.Width; if (windowHeight > windowWidth) { if (windowWidth < 1) { windowWidth = 1; } float ratio = windowWidth / windowHeight; Matrix4 projection = Matrix4.CreateOrthographic(ratio * 10, ratio * 10, -1, 1); GL.UniformMatrix4(uProjectionLocation, true, ref projection); } else if(windowHeight < windowWidth) { if (windowHeight < 1) { windowHeight = 1; } float ratio = windowWidth / windowHeight; Matrix4 projection = Matrix4.CreateOrthographic(ratio * 10, ratio * 10, -1, 1); GL.UniformMatrix4(uProjectionLocation, true, ref projection); } } GL.Viewport(this.ClientRectangle); } 

1 Answer 1

2

First and foremost,

float ratio = windowWidth / windowHeight; 

most likely doesn't do what you actually had in mind when writing this code. windowWidth and windowHeight are both of type int, so / will perform an integer division before the result is converted to float. As a result, ratio will spend most of its time being exactly 1. And sometimes maybe 0 (if height > width). And occasionally maybe 2 (on some wide screen setups). Most likely, what you want is something more like this:

float ratio = (float)windowWidth / (float)windowHeight; 

Even then, however, as you compute your matrix

Matrix4 projection = Matrix4.CreateOrthographic(ratio * 10, ratio * 10, -1, 1); 

you scale both width and height with ratio, which means no matter what ratio ends up being, you will end up with a matrix for a square viewport again (since width and height are the same). Most likely, you want to, e.g, scale only the width with the aspect ratio

Matrix4 projection = Matrix4.CreateOrthographic(ratio * 10, 10, -1, 1); 

Apart from all that, note how most of the actual functionality is duplicated into both branches of your if statement. Also note that you simply skip the case that width and height are equal. You could rewrite this whole bunch of ifs, e.g., like so

float ratio = (float)ClientRectangle.Width / (float)Math.Max(ClientRectangle.Height, 1); Matrix4 projection = Matrix4.CreateOrthographic(ratio * 10, 10, -1, 1); GL.UniformMatrix4(uProjectionLocation, true, ref projection); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.