PDA

View Full Version : Using glOrtho for 2D clipping?



WizMystery
06-05-2013, 12:18 PM
I'm trying to use HOR+ in my game so that the field of view expands at a higher aspect ratio. I'm using glOrtho to make sure that things are cut off equally on both sides by increasing the left parameter as the aspect ratio decreases:



screen_aspect_ratio = double(screen_width) / double(screen_height);

viewport_aspect_ratio = screen_aspect_ratio;
if(viewport_aspect_ratio > max_viewport_aspect_ratio) viewport_aspect_ratio = max_viewport_aspect_ratio;
else if(viewport_aspect_ratio < min_viewport_aspect_ratio) viewport_aspect_ratio = min_viewport_aspect_ratio;

if(screen_aspect_ratio > viewport_aspect_ratio) viewport_width = screen_width * (viewport_aspect_ratio / screen_aspect_ratio);
else viewport_width = screen_width;
if(screen_aspect_ratio < viewport_aspect_ratio) viewport_height = screen_height / (viewport_aspect_ratio / screen_aspect_ratio);
else viewport_height = screen_height;
viewport_x = (screen_width - viewport_width) / 2;
viewport_y = (screen_height - viewport_height) / 2;

ortho_width = viewport_aspect_ratio * HD_SCALE; //HD_SCALE is 1080
max_ortho_width = HD_ASPECT_RATIO * HD_SCALE; //HD_ASPECT_RATIO is 16:9
ortho_x = (max_ortho_width - ortho_width) / 2;
ortho_width += ortho_x;

ortho_y = 0;
ortho_height = HD_SCALE;

glViewport(viewport_x, viewport_y, viewport_width, viewport_height);
glOrtho(ortho_x, ortho_width, ortho_height, ortho_y, -1, 1);


This gets the job done fine, but the trick I have to do with ortho_width kind of confuses me. I'd expect that ortho_width defines the width of the clipping plane and that ortho_x defines where the clipping begins, but I have to provide the wrong value to get the right result. I'd expect this would work instead:



ortho_width = viewport_aspect_ratio * HD_SCALE;
max_ortho_width = HD_ASPECT_RATIO * HD_SCALE;
ortho_x = (max_ortho_width - ortho_width) / 2;


...but that stretches and moves the image to the right when the aspect ratio is lowered. Adding in ortho_width += ortho_x centers everything again... but then ortho_width is no longer the actual width of the clipping plane. Am I using the wrong function to do this?