Advertisement Jump to content
Sign in to follow this  
PeteUK

Zoom about cursor position

This topic is 3468 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!