简体   繁体   中英

Algorithm to zoom into mouse(OpenGL)

I have an OpenGL scene with a top left coordinate system. When I glScale it zooms in from (0,0) the top left. I want it to zoom in from the mouse's coordinate (relative to the OGL frame). How is this done? Thanks

I believe this can be done in four steps:

  1. Find the mouse's x and y coordinates using whatever function your windowing system (ie GLUT or SDL) has for that, and use gluUnProject to get the object coordinates that correspond to those window coordinates
  2. Translate by (x,y,0) to put the origin at those coordinates
  3. Scale by your desired vector (i,j,k)
  4. Translate by (-x,-y,0) to put the origin back at the top left

I did a smooth zoom in using glortho . The skeleton of my solution is

glortho(initial viewport x,y & size)
glcalllist(my display list)
render
.
.
loop to gradually go to final viewrport coordinates/size . Implement your timing and FPS requirements
.
.
glortho(final viewport x,y & size)
glcalllist(my display list)
render

I hope you get the general idea. There are few other methods to acheive this, but I find glortho the method the easiest to comprehend.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM