Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: OpenGL Aspect Ratio Issue

Hybrid View

  1. #1
    Newbie Newbie
    Join Date
    May 2013
    Posts
    1

    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:

    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.
    Last edited by pankaj; 05-01-2013 at 10:20 AM.

  2. #2
    Junior Member Newbie
    Join Date
    Oct 2012
    Posts
    15
    Hi,

    You need to calculate the six planes of the camera frustum.
    Take a look to this.
    http://www.gamedev.net/topic/319713-...wing-distance/

    Cheers,

    Carlos.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •