OpenGL Aspect Ratio Issue

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);
	if( canvas_width > canvas_height ){	
		glOrtho(left_plane*aspect_ratio, right_plane*aspect_ratio, bottom_plane, top_plane, z_near_plane, z_far_plane);
		glOrtho(left_plane, right_plane, bottom_plane/aspect_ratio, top_plane/aspect_ratio, z_near_plane, z_far_plane);


void GLWidget::paintGL()
    glColor3f(1,0,0); // red

		//glVertex2f(-30,0); // These  coordinates work
		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.

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.


You need to calculate the six planes of the camera frustum.
Take a look to this.