gluOrtho2D and glViewport

Hi,

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.

Thanks!

For 2D the dimensions in glOrtho should match that of your viewport and then you have a one to one relationship between screen pixels and your 2D coordinates.

Is that what you need?

What scratt is saying is the gerenal recommended stuff, that makes it easy to work in 2d-like apps. Of couse, you are free to mix and match your glViewport and glOrtho calls to whatever you want, the trick is to understand exactly what each does:

gluOrtho2d is just some handy way to call a simplified version of glOrtho.

gluOrtho2D sets up a two-dimensional orthographic viewing region. This is equivalent to calling glOrtho with near = -1 and far = 1 .

What glOrtho does is define what area(volume) of your infinite 3d space is visible on your screen. The params of glOrtho are

glOrtho(left,right,bottom,top,nearVal,farVal);

so, what that means is that any part of your geometry that has coordinates outside these bounds , will be out of the visible area. But, if a geometry (triangles or whatever you are drawing) have some coordinates inside the visible area and some outside, they will be automagically cut nicely so you will see part of that geometry that is inside and won’t see the part of that geometry that is outside (but you don’t have to wory about that).

So , if you call an imaginary function glDrawCircle(x=2,y=3,radius=4), and, before that , you call gluOrtho2d(-2,6,-1,7), then your entire circle will be visible and will be “stretched” onto your entire viewport (more on this later). But, if you call gluOrtho2d(2,6,3,7), you will only see a quarter of your circle stretched on the entire viewport.

About the viewport:

Name

glViewport — set the viewport

C Specification

void glViewport( GLint x,
GLint y,
GLsizei width,
GLsizei height);
Parameters

x, y
Specify the lower left corner of the viewport rectangle, in pixels. The initial value is (0,0).

width, height
Specify the width and height of the viewport. When a GL context is first attached to a window, width and height are set to the dimensions of that window.

The viewport is somewhat relative to your window that you’re drawing into. What the viewport does is take the visible geometry that you helped define with gluOrtho2d (or glOrtho or glFrustum ) and “stretches” it onto the part of the window you specified with glViewport. Usually, unless you draw something else in that window, you want your viewport to stretch for the entire client area of the window, meaning the lower left corner would be 0,0 and the width and height will be the width and height of the entire client area of the window.

Coming back to what scratt was saying, it is easier, especially when just starting with all of this, to match the glOrtho and glViewport parameters to the dimension of your window, because that will allow you to think in pixels: i.e. I will center my circle around pixel 2,3 and it will have a radius of 4 pixels (of course, this will result in a “small” circle).

later edit: I see you specified “not distorted” as well… well, for the “not distorted” part, it’s a bit more dificult for your circle to cover the ENTIRE viewport and not be distorted if your viewport is not square . Again, thinking in pixels helps in this case. But, for a quick and dirty fix, you will somehow have to place the circle in the center of your visible area and have it’s radius to be half of the smallest dimension between the visible area’s height and width.(please notice I am not being specific here, details depend on what your ortho and viewport settings are, because you could distort the image from either of those if they don’t match nicely - for them to match nicely means they have to have the same aspect ratio- width/height - , at least).