Reputation: 45
could someone tell me what's the point of the if-else statement in this segment of code? what does it exactly do? especially the part where it divide hight by width in glOrtho.
*in case the method is ambiguous to anyone, this quote might help.. "This reshape preserves shapes by making the viewport and world window have the same aspect ratio"
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-50.0, 50.0, -50.0*(GLfloat)h / (GLfloat)w,
50.0*(GLfloat)h / (GLfloat)w, -1.0, 1.0);
else
glOrtho(-50.0*(GLfloat)w / (GLfloat)h,
50.0*(GLfloat)w / (GLfloat)h, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Thanx in advance!
Upvotes: 1
Views: 2206
Reputation: 485
Aspect ratio can be specified in two different ways:
width/height
height/width
If the width is greater than the height, then width/height > 1
and height/width < 1
.
Likewise if the height is greater than the width, width/height < 1
and height/width > 1
We cannot set the width and height of the projection to the same value if the dimensions of the screen are not equal. Otherwise, everything would appear stretched:
Thus, we multiply one of the dimensions with the aspect ratio.
If the aspect ratio is larger than 1, it should be multiplied onto the larger dimension.
Likewise, if the aspect ratio is smaller than 1, it should be multiplied onto the smaller dimension.
What your example does, is making sure that the aspect ratio always stays larger than 1.
Therefore, it would always have to be multiplied onto the larger dimension, which may vary. That's what the if-else case is there for:
w <= h
(height is the greater dimension), the aspect ratio is multiplied onto the top/bottom arguments of glOrtho();h < w
, width is the greater dimension), the aspect ratio is multiplied onto the left/right arguments of glOrtho();Effectively, the the X and Y dimension (with the use of the if-else case) cannot be lower than 100 units (-50 to +50) - since the aspect ratio is always greater than 1.
Upvotes: 3
Reputation: 162164
Essentially the idea is to keep the projection a square aspect ratio (that's what the division of w
through h
is for) and that a square in the XY plane with side length 100 (world space units) around the chosen XY origin (0,0) is fit to the short size of the window (that's what the if
is for, to decide, which of the two sizes of a window is the short size).
Upvotes: 3