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.
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.