Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm to zoom into mouse(OpenGL)

Tags:

c++

c

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

like image 582
jmasterx Avatar asked Jun 06 '10 03:06

jmasterx


2 Answers

I believe this can be done in four steps:

  1. Find the mouse's x and y coordinates using whatever function your windowing system (i.e. 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
like image 81
Jordan Lewis Avatar answered Oct 17 '22 01:10

Jordan Lewis


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.

like image 31
Gary Avatar answered Oct 17 '22 01:10

Gary