Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3D Scene Panning in perspective projection (OpenGL)

I have designed a C++ class that abstracts the user from trackball rotation, zooming and panning. I have got the rotation (using trackball) and zooming working as expected. However, panning does not behave as expected. When I pick a point and drag, I expect that on finishing drag, the picked point continues to be under the mouse. My understanding of panning in perspective projection is as follows. The target and the camera position, both will be affected by a pan operation. The camera target and the camera position (eye) should be translated proportional to the drag. The proportionality (may not be constant) should be based on z depth.

Panning is straight forward in orthographic projection but poses a problem in perspective. It will be useful if one can explain the math and the implementation detail for OpenGL.

like image 992
Ram Avatar asked Aug 23 '12 18:08

Ram


1 Answers

I don't know about the OpenGL specifics, but if I understand your question correctly I can help out with the mathematics:

I assume you have already selected the object, by clicking the object and thus "sending" a ray through your scene that hits your object in the anchorpoint p.

To understand the following:

Panning with perspective projection

Now you drag your mouse along vector t. Using the intercept theorem you can easily calculate the vector s by which p has to be translated to "keep it under the cursor":

|p| / |q| = |s| / |t|

|s| = ( |p| / |q| ) * |t|

s is parallel to t, so it is t normalized multiplicated by |s|:

s = t / |t| * |s|

s = t / |t| * ( |p| / |q| ) * |t|

s = t * ( |p| / |q| )

If you are panning, you are doing the exact same thing, just that you are not shifting p by s, but you have to translate your whole scene by -s.

like image 150
flix Avatar answered Sep 25 '22 14:09

flix