Zoom about cursor position

Started by
2 comments, last by PeteUK 14 years, 9 months ago
Hi, I need to zoom in/out at the cursor position but am having trouble figuring out exactly what operations are needed. My projection is setup using glOrtho and the left,right,bottom, and top parameters are multiplied by the current zoom. So, zoom is done on the projection and not by scaling the world. Zooming in/out of the centre works really well. I want to implement zooming about the mouse point. This will obviously require adjusting the zoom factor as normal, but I can't get my head around how to figure out what the modelview translation should be. I maintain a floating point variable "worldUnitsPerPixel" which of course will be the figure *before* the zoom. How can I work out the translation? N.B. Any translation must be on the modelview and not the projection. Answers most welcome! Pete
Advertisement
If you are zooming about the centre by default then it is a matter of finding where your cursor is wrt the centre. Move your view by this amount, zoom an then move your view minus the initial amount and viola! You are now zooming about the mouse.
It might help for both of you to draw a picture illustrating what "zooming about the mouse" really means.

Show the initial zoom region, a chosen mouse point, and then a zoomed region about that mouse point. I think this picture would help you easily formulate the solution.
Quote:Original post by bzroom
It might help for both of you to draw a picture illustrating what "zooming about the mouse" really means.

Show the initial zoom region, a chosen mouse point, and then a zoomed region about that mouse point. I think this picture would help you easily formulate the solution.

It did help. Drew a picture pre-zoom, post-zoom at the centre of the screen, and then that translated to where I wanted it to be.

Pseudocode is:

Store previous world units per pixel (prevWUPP).
Create vector V from middle of screen to cursor position in screen coordinates.
Update zoom factor for however much we're zooming in/out, and recalculate world units per pixel (WUPP).
Multiply vector V by difference in world units per pixel between zooms. Negate as we're moving the world by the opposite of what we want the view to do:
V *= -(prevWUPP - WUPP).
Apply the translation in V to the view matrix.

I maintain a square aspect ratio. I think the above would need adjustment if I didn't.

Works nice. Now on to rotate so I might see you again soon... :)

Thanks again,

Pete

This topic is closed to new replies.

Advertisement