I am using Qt + OpenGL and i am a beginner to OpenGL.
I am not able to figure out aspect ratio issue. Aspect ratio should remain same whether i zoom-in, zoom-out, resize window.
While i searched for this issue and used good suggestions, i see that those suggestions work out if coordinates are symmetric (like between -1 and 1). But for unsymmetric cases those suggestions do not work out.
Here is a snippet from my code:
void GLWidget::resizeGL(int w, int h)
{
canvas_width = (double)w;
canvas_height = (double)h;
aspect_ratio = canvas_width/canvas_height;
left_plane = 245;
right_plane = 315;
bottom_plane = -5;
top_plane = 65;
z_near_plane = 1;
z_far_plane = -1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if( canvas_width > canvas_height ){
glOrtho(left_plane*aspect_ratio, right_plane*aspect_ratio, bottom_plane, top_plane, z_near_plane, z_far_plane);
}else{
glOrtho(left_plane, right_plane, bottom_plane/aspect_ratio, top_plane/aspect_ratio, z_near_plane, z_far_plane);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0); // red
glBegin(GL_POLYGON);
//glVertex2f(-30,0); // These coordinates work
//glVertex2f(30,0);
//glVertex2f(0,60);
glVertex2f(250,0); // These do not as 250*aspect_ratio makes left plane 350 (if aspect ratio is 1.4). That is why i am not able to see anything as everything is clipped.
glVertex2f(310,0);
glVertex2f(280,60);
glEnd();
}
Please help me on this. Thanks in advance.
PS: If my bounding box is (x_min, x_max, y_min, y_max) then how should we modify clipping planes in glOrtho to have same aspect ratio. If coordinates are symmetric or bounding box is symmetric like in above case, if triangle was (-30,0), (30, 0), (0,60) then glOrtho in above code snippet works but if coordinates are (250, 0), (310, 0), (280, 60) then it fails. I hope am making sense here.