问题
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.
回答1:
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:
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.
回答2:
It is impossible to have the finishing point under your mouse. Panning in perspective mode can be done in two ways. One is translating camera and the other is rotating camera. Translating camera will result in object rotation for user view. Rotation camera for panning has two constraints. one is not to change camera position and the other is to stop rotation with respect to camera direction. i.e., the axis of rotation should be parallel to the plane formed by camera right and up vectors.
来源:https://stackoverflow.com/questions/12097693/3d-scene-panning-in-perspective-projection-opengl