PDA

View Full Version : ortho and viewport



XaBerr
10-11-2013, 03:16 AM
I apologize in advance for this post , I am yet another person who did not understand glotho . In fact I understand what it does but I have doubts .
Suppose my window starts from 1920x1080, then I will set :
glViewport (0 , 0, 1920 , 1080 ) ;
glMatrixMode ( GL_PROJECTION ) ;
glLoadIdentity ();
glOrtho ( 0,1920,1080 , 0, 1, -1 ) ;

In this way ortho tells me that the window has a grid that goes from 0 to 1920 or 1080 .
so if I wanted to draw a square button a button disegnerei of 10 units per side from x = 1910 y = 1070.

If I wanted to bring to 800x600 , become:
glViewport (0 , 0 , 800, 600 ) ;
glMatrixMode ( GL_PROJECTION ) ;
glLoadIdentity ();
glOrtho ( 0,800,600 , 0, 1, -1 ) ;

However, the button would not be seen anymore because it would not be in the cooridinate the viewport ! But if I did so :
glViewport (0 , 0 , 800, 600 ) ;
glMatrixMode ( GL_PROJECTION ) ;
glLoadIdentity ();
glOrtho ( 0,1920,1080 , 0, 1, -1 ) ;

the button would be within the coordinates but would no longer be square ...
then what am I supposed to do in this case?
what is good programming practice ?

GClements
10-11-2013, 04:47 PM
then what am I supposed to do in this case?
what is good programming practice ?
If you want to lay out items relative to the edges of the window while maintaining the correct aspect ratio, you have to take the window dimensions into account. Well, not the absolute dimensions, but the aspect ratio.

If you were only ever going to deal with e.g. a 4:3 aspect ratio (640x480, 800x600, 1024x768, 1280x960, etc), then you could pass the actual window dimensions to glViewport() and the "logical" dimensions (e.g. 640x480) to glOrtho(), then render in terms of the logical dimensions.

If you need to deal with arbitrary aspect ratios, then you can fix one of the logical dimensions and vary the other one according to the aspect ratio, e.g.


glViewport (0, 0, width, height) ;
glMatrixMode(GL_PROJECTION) ;
glLoadIdentity();
glOrtho(0, 640, 640*height/width, 0, 1, -1) ;


The above would result in a coordinate system which is always 640 units wide but the height varies with the aspect ratio. You can lay out items along the top edge and the upper portion of the left and right edges using fixed coordinates, but if you wanted to lay out items relative to the bottom of the screen, you would need to adjust the Y coordinates according to the aspect ratio.

Also: if you're concerned about using the correct aspect ratio, you need to use the physical size of the screen/window (in millimetres) rather than the dimensions in pixels.