I have an object defined in world coordinates, say a circle centered at (2,3) with radius 4. If I want the circle to not be distorted, to be entirely visible in the viewport and to be as big as possible within the viewport, how can I formulate a gluOrtho2D command to create a world window based on the aforementioned specs given that:
glViewport(20, 30, 1000, 500)?
I am getting confused with the whole viewport vs world vs screen, etc coordinates. Can someone walk me through it? I really want to get the hang of this.
The gluOrtho2D function sets up a two-dimensional orthographic viewing region. This is equivalent to calling glOrtho with zNear = -1 and zFar = 1.
The difference is that glFrustum takes clipping planes as arguments, and gluPerspective takes a field of view and aspect ratio. glOrtho and gluOrtho2D set up 2D projection modes (i.e., parallel projection). Such are not necessarily 2D, but they do imply that farther objects are not any smaller than closer ones.
In your example, the viewport is 1000 pixels across by 500 pixels high. So you need to specify glOrtho coordinates that have the same aspect ratio (2:1).
Your circle is 4 units in radius, so you need a view that is 8 units high by 8 units wide atleast. Considering the 2:1 aspect ratio, let's make that 16 units wide by 8 units high.
The center is at (2, 3). So centering these 16 x 8 around that you should get:
glOrtho2D (2 - 8, 2 + 8, 3 - 4, 3 + 4);
That is:
glOrtho2D (-6, 10, -1, 7);
This effectively maps the X coordinate of -6 to the left edge of the viewport. The glViewport mapping then maps that to the actual location on the screen. As the screen size changes, you must adjust the glOrtho2D coordinates to compensate for the aspect ratio, but as long as the viewport is 2:1, these glOrtho2D calls will not need to change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With