# OpenGL Bad aspect ratio with glOrtho()

This topic is 4836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Don't take this to be another post from someone who couldn't be bothered to read the forum FAQ to understand how 2d can be performed in OpenGL because I have, but I'm still having some trouble with regards to using an orthographic projection. When using glOrtho(); I appear to be getting a poor aspect ratio given that my windows resolution is rectangular. Currently I'm doing this when setting up the projection matrix: glOrtho( -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f ); Now I know this isn't right because when drawing a perfect square I obviously get a rectangle which reflects the aspect ratio of my window. The near and far clipping planes are correct but the first 4 arguments aren't, what should I be doing that I'm not? At first I thought the second and fourth arguments should be my windows width and my windows height respectively...but it doesn't seem to work.

##### Share on other sites
Quote:
 glOrtho( -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f );
With that call, you are setting the bottom-left to be (-1,-1) and the top right to be (1,1) which as you know is "wrong" because of your window's aspect ratio. So you would need to account for the aspect ratio in the parameters you pass. Assuming a normal aspect ratio of 4:3 and if you want the origin to be the middle of the screen (because that's what your glOrtho call is doing) you would do something like...
glOrtho(-1, 1, -0.75, 0.75, -1, 1)
or use some other scaling to get the coordinates how you like, as long as it's still the same aspect ratio.
ie:
glOrtho(-4, 4, -3, 3, -1, 1)

Most uses of ortho mode are so that you can specify vertices in window coordinates, for that you would use...
glOrtho(0, width, 0, height, -1, 1)
(or swap the bottom and top parameters to use the top left as the origin instead of bottom left).

How does using the width and height not work for you? Are you still using -1 for the left and bottom parameters?

##### Share on other sites
The third and fourth parameter should be multiplied by the aspect ratio. Assuming the aspect ratio = width/height. Otherwise you miltipy the first and second parameters.

glOrtho(-1, 1, -1*aspect, 1*aspect, -1, 1);

The parameters for glOrtho is (l, r, b, t), where l determines the coordinate of the left edge, r is the right edge, b is the bottom edge, and t is the top edge. It does not, as glViewport for example, represent staring position and size, but starting and ending position.

##### Share on other sites
Thanks for that, solved it!

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631435
• Total Posts
3000056
×