Algorithm 3d orbiting camera control with mouse drag

谁说胖子不能爱 提交于 2019-12-23 04:01:49

问题


Please help,

I couldn't find detailed explanation about this which is not language specific and not library dependent, because I want to control the math myself for some reason.

How to create orbiting camera control with mouse, like middle-click drag in Google SketchUp?

* In Google SketchUp, the camera can move circularly orbiting imaginary object in the middle of the plane (not always 0,0,0) by dragging the mouse. It can orbit horizontally, vertically, even diagonally, all directions.


回答1:


The simplest solution is to store X/Y direction angles and eventually a zoom level. Then, you modify the angles in some MouseMove event, and use them to compute camera direction.

Here's some pseudo-code for mouse drags:

OnMouseDown:
    mouseDragging = true;
    lastX = mouse.x;
    lastY = mouse.y;

OnMouseUp:
    mouseDragging = false;

OnMouseMove:
    if( mouseDragging ) {
        angleX += (mouse.x-lastX) * speedX;
        angleY += (mouse.y-lastY) * speedY;
    }

OnMouseWheel:
    zoom += mouse.wheelDelta;

Given those data you can build a camera position. I don't know what are you using for this but here's an example from OpenGL:

glLoadIdentity();
glTranslatef( 0.0f, 0.0f, -zoom );
glRotatef( angleY, 1.0f, 0.0f, 0.0f );
glRotatef( angleX, 0.0f, 1.0f, 0.0f );
glTranslatef( -orbitCenterX, -orbitCenterY, -orbitCenterZ );



回答2:


The term to google for is "arcball". See for example https://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Arcball

But in general there are many subtle differences that make interactions better.




回答3:


I know your question was 2 1/2 years ago but for anyone happening upon this the helix toolkit may have what you need. I was looking for the sketchup behavior as well and only recently found it in this project. It's .NET but source code is available so it should offer some insight into what you are looking for.

The orbiting is done in their control by the user press and holding the right mouse button and then moving the mouse around, FYI. And there's a property in the control that determines whether you want the behavior to orbit around the center of the model or the currrent mouse location so I found it to be identical to sketchup's behavior if desired.




回答4:


You could take a look at gluLookAt() - many images on web showing camera vectors, with explanation. Basically camera has a position, a look at vector, and an up vector. As far as manipulation, the most common way usually involves quaternion math. Lots of detailed tutorials on that as well. The final piece would involve mapping 2D screen mouse movement to 3D camera commands. That part should be straight forward once you have your camera math working. Hope that helps.



来源:https://stackoverflow.com/questions/21603412/algorithm-3d-orbiting-camera-control-with-mouse-drag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!