View Full Version : cannot figure out opengl viewport, orthographic projection, aspect ratio issues.

03-05-2016, 06:33 AM
I'm still having a real difficult time sorting out opengl's glViewport, orthographic projection matrix and aspect ratio getting them to work together to get the effect that I want.

I understand that glViewport maps opengl NDC cube to whatever size you pass in to glviewport from left, bottom, top, right.

I create my glViewport and orthographic projection like this.

int width = 320;
int height = 480;
point3 eye, center;
vec3 up;
vmathP3MakeFromElems(&eye, 0, 0, -20);
vmathP3MakeFromElems(&center, 0, 0, -1);
vmathV3MakeFromElems(&up, 0, 1, 0);
vmathM4MakeLookAt(&v_mat, &eye, &center, &up);

vmathM4MakeOrthographic(&p_mat, 0, width, 0, height, 1, 100);
glViewport(0, 0, width, height);

I don't currently move the view position because I want 0,0 to be in the lower left, width to go from 0-width from left to right and height to go from 0-height from bottom to top.

What I just can't wrap my head around is how to use the aspect ratio. Not to mention when I try to do the resize function.

vmathM4MakeOrthographic(&p_mat, 0, w / aspect, 0, h, 1, 100);
glViewport(0, 0, w, h);

the above code what I would like to do is keep the height fixed and scale the width.

I'd like to have a design resolution say 320x480 view that's always centered and if I have a fixed height the width scales out horizontally or fixed width and have the top and bottom stretch like the image below.


There's the view projection matrix, the glviewport and orthographic projection that all just seem to be a bit much to wrap my head around it all.

how can I achieve the look that I am going for?

03-05-2016, 08:36 AM
You should just use glViewport with fixed width and height values, and the origin would be calculated from the actual window size (origin = (window_size-fixed_size)/2 would do it).