0

So I am working on a simple project in openGL and C++ where i have just a white circle drawn on screen for now.

I added a function to maintain the aspect ratio in the canvas i am using and it seems to be working flawlessly.

If you resize the window horizontally, clipping will occur (this is what i intended)

But if you resize the window vertically, no clipping occurs and instead the objects scale down/up..

Now i am stuck at figuring out how to keep the aspect ratio correct, while also preventing the objects from scaling and while keeping in mind that i will be implementing Panning and Zoom later on.

With that in mind, here is the code i am using to adjust the ratio.

Please note that glViewPort is also set correctly on resize, but i only copied this section because i know the problem is somewhere here.

if ( dx/dy < w/h ){
    // Need to expand dx
    GLdouble diff = w/h*dy - dx;

    minX -= 0.5*diff;
    maxX += 0.5*diff;
}
else{
    // Need to shrink dx
    GLdouble diff = h/w * dx - dy;

    minX += 0.5*diff;
    maxX -= 0.5*diff;
}

glOrtho( minX, maxX, minY, maxY, minZ, maxZ );
Dany Khalife
  • 1,850
  • 3
  • 20
  • 47
  • yes duh, but if you compute Y then X would have to readjust too and this is where i am stuck actually... and lol yes i admit it, but in my defense we all have times when your brain shuts down – Dany Khalife Jan 29 '13 at 14:49
  • Hm, if you are manipulating the viewport, I would just leave the projection unchanged then. – Bartek Banachewicz Jan 29 '13 at 14:50
  • but if you only manipulate the viewport like this `glViewPort(0,0,w,h);` won't this mess up the ratio? – Dany Khalife Jan 29 '13 at 14:57
  • It's unclear what exactly you want to happen. Are you saying that you want a fixed size of display? – Nicol Bolas Jan 29 '13 at 15:10
  • I believe he wants letterboxed image of fixed aspect ratio. – Bartek Banachewicz Jan 29 '13 at 15:24
  • @NicolBolas ok lets say you are using Google Chrome. If you shrink the window horizontally chrome will update the horizontal scrollbar and will also clip the page so you see less content while keeping everything in the window to scale. i just want to implement the same behavior. let me know if it's more clear – Dany Khalife Jan 29 '13 at 15:24
  • That's funny, because IIRC that's the default behaviour (i.e. not modifying projection and viewport) – Bartek Banachewicz Jan 29 '13 at 15:48
  • possible duplicate of [Preserve aspect ratio of 2D object on window resize](http://stackoverflow.com/questions/4338729/preserve-aspect-ratio-of-2d-object-on-window-resize) – genpfault Jan 29 '13 at 15:49

0 Answers0