PDA

View Full Version : simple viewport explanation



appdeveloper
10-15-2009, 03:35 AM
Can anyone explain me something basic about viewports that i can't understand?
I'm trying to configure viewports in my application.
I've defined:
Gl.glOrtho(0, 2500, 0, 2500, -4.0, 4.0)

and i'm trying to create one viewport that will occupy 100% of the screen (for now, later i'll need 3 with diferent width). So i tryed this:
Gl.glViewport(0, 0, 2500, 2500)

but it doesn't work. But it works if i define the viewport as:
Gl.glViewport(0, 0, 400, 400)

How can this be possible if i have defined a width/height that is a lot smaller than the one i defined in glOrtho? Shouldn't the viewport be very small? maybe i'm mixing everything wrong.

My thanks in advanced

dletozeun
10-15-2009, 03:42 AM
In your previous thread (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Main=51397&Number=2655 54#Post265554), marshats asked you this:



Gl.glViewport takes arguments in pixel units. Does your monitor have 2500x2500 pixel resolution?


Have you checked this?

appdeveloper
10-15-2009, 03:59 AM
My monitor has 1024/768
I'm using the Tao SimpleOpenGLControl. Don't know if has something to do with that, but now that you say that i see that it has 365/302 inside a windows form

appdeveloper
10-15-2009, 04:54 AM
Well, tested it. You are absolutly right, but then again, if that its true, doesn't Gl.glOrtho takes arguments in pixel units too?

dletozeun
10-15-2009, 05:58 AM
Well, tested it. You are absolutly right, but then again, if that its true, doesn't Gl.glOrtho takes arguments in pixel units too?

No glOrtho does not take arguments in pixels, it just sets an orthographic or parallel projection.

For example if you set 2D projection like this:
Gl.glOrtho(0, 10, 0, 20, -1.0, 1.0)

All points whose coordinates (x,y) are in the interval [0 10]x[0 20] will be rasterized.

However you can set the projection in a way that it fits the window size. eg, for a 800x600 window:
Gl.glOrtho(0, 800, 0, 600, -1.0, 1.0)

a vertex whose coordinates are (40, 50) will be drawn exactly at the position (40, 50) in pixels unit (starting from the bottom left corner).

glViewport just set which portion of the window is used for drawing. By default, viewport is set to the dimensions of the created window.

marshats
10-15-2009, 08:43 AM
Some background reading from the Redbook online (http://www.glprogramming.com/red/chapter03.html) will be beneficial. Chapter 3 explains all about the connections between viewport + projections + gluLookAt.

The arguments to Viewport are in pixel units and the rectangle you define with it must be no greater than the actual window itself.

To follow up on dletozeun's reply,
glOrtho maps global X,Y,Z cooridinates to the viewport pixel cooridinates I,J as follows
suppose you set


Gl.Viewport(10,20,50,50)
Gl.glOrtho(0, 34.2, 0, 90.6, -1.0, 1.0)

A point at (XYZ)=(0,0,0) would be mapped to the window at the far lower left pixel (IJ)=(10,20) and a point at (XYZ)=(34.2,90.6,0) would be mapped to the upper right window pixel (IJ)=(10+50,20+50). Points outside of the ortho box will simply be clipped ie not plotted to the screen.

So glOrtho allows you to define your global coordinate system in convenient units (ie not in pixels) and points will be automatically mapped back to the viewport pixels -- this is a valuable feature because it is best to avoid thinking in terms of pixels and think in terms of global coordinates appropriate to you model. Typically, the only time you think in terms of pixels is when you set Viewport, and from then on openGL maps your global coordinate system back to the screen for you without you needing to think about pixels again. This is a great help when you allow the window size to change and you only have to change the code accordingly in one spot, the Gl.Viewport() settings!