the difference between glOrtho and glViewPort in openGL

crazyPixel picture crazyPixel · Jul 2, 2014 · Viewed 22.7k times · Source

I am struggling to figure out something let's say im rendering some image that has a height of 100 and a width of 100.

In scenario A

I am taking a glOrtho(0,100,0,100,-100,100) and glViewPort(0,0,50,50) when glOrthois defined as (left,right,bottom,top,zNear,zFar) and glViewPort is defined as (lower left corner x, lower left corner y, width, height).

In scenario B

I am taking a glOrtho(0,50,0,50,-100,100) and glViewPort(0,0,100,100) when glOrthois defined as (left,right,bottom,top,zNear,zFar) and glViewPort is defined as (lower left corner x, lower left corner y, width, height)

That basically means that in scenario A the image will be rendered to a lower width and height than it requires (i.e will be rendered s.t every two pixels). In the original image will be mapped to one in the destination "surface" but still the entire image will be seen.

In scenario B, however, the image will be clipped and so only the upper left quarter of it will be visible. Am I correct? - just to be clear, it's a question from a CG test im having tommorow and I want to make sure I got openGL correctly... (already read the API... =\)

Answer

CoffeDeveloper picture CoffeDeveloper · Jul 2, 2014

glViewPort is in screen pixel units: that's it, it has nothing to do with the 3D world "inside" your graphics card. It just tells wich part of the window will be used for rendering (or just will be visible).

glOrtho instead changes the "inner" world and is OpenGL units: More OpenGL units will fit into the visible part of the screen, so "bigger" objects will fit easily into viewable area if you increase the ortho size.

Modifying the viewport does not change the frustum, infact the same image will just be stretched to fit the new viewport.

Explicative Images:

Picture 1: viewport is half window

enter image description here

Picture 2: If I just double the viewport, the image becomes stretched (same frustum that fill a different surface)

enter image description here

So the only solution to keep aspect ratio is to double ortho size too (in this case i double left and right values)

Picture 3: final result (note that now a bigger part of the 3d world is visible):

enter image description here

Further details are available at quite familiar site on OpenGL NeHe productions.