LeapUtil.cpp
Go to the documentation of this file.
00001 /******************************************************************************\
00002 * Copyright (C) Leap Motion, Inc. 2011-2013.                                   *
00003 * Leap Motion proprietary and  confidential.  Not for distribution.            *
00004 * Use subject to the terms of the Leap Motion SDK Agreement available at       *
00005 * https://developer.leapmotion.com/sdk_agreement, or another agreement between *
00006 * Leap Motion and you, your company or other organization.                     *
00007 \******************************************************************************/
00008 #include "LeapUtil.h"
00009 
00010 namespace LeapUtil {
00011 
00012 using namespace Leap;
00013 
00017 
00018 void Camera::SetPOVLookAt( const Vector& vCameraPosition, const Vector& vTarget, Vector vUp )
00019 {
00020   Vector vZBasis = (vCameraPosition - vTarget).normalized();
00021 
00022   // if the zero vector was passed try to preserve the current camera up as much as possible
00023   if ( vUp == Vector::zero() )
00024   {
00025     vUp = m_mtxPOV.yBasis;
00026   }
00027   else
00028   {
00029     vUp = vUp.normalized();
00030   }
00031 
00032   // if the z direction is parallel to the desired up direction
00033   // we need to pick another up.
00034   if ( IsNearZero(1.0f - fabs(vZBasis.dot(vUp))) )
00035   {
00036     // make the new up perpendicular to the plane of the new z axis and the old right direction.
00037     // it's a decent guess.
00038     vUp = vZBasis.cross( m_mtxPOV.xBasis ).normalized();
00039   }
00040 
00041   Vector vXBasis = vUp.cross(vZBasis).normalized();
00042   Vector vYBasis = vZBasis.cross( vXBasis ).normalized();
00043 
00044   m_mtxPOV.xBasis = vXBasis;
00045   m_mtxPOV.yBasis = vYBasis;
00046   m_mtxPOV.zBasis = vZBasis;
00047   m_mtxPOV.origin = vCameraPosition;
00048 
00049   updateOrbitDistance();
00050 }
00051 
00052 void Camera::RotateOrbit( float fDeltaMagnitude, float fDeltaLongitude, float fDeltaLatitude )
00053 {
00054   m_fOrbitDistance += fDeltaMagnitude;
00055 
00056   Vector  vSphereZBasis = CartesianToSpherical( m_mtxPOV.zBasis );
00057   float   fNewLatitude  = Clamp(  vSphereZBasis.z + fDeltaLatitude,
00058                                   -m_fMaxOrbitLatitude,
00059                                   m_fMaxOrbitLatitude );
00060 
00061   Vector  vSphereNewZBasis( 1, vSphereZBasis.y + fDeltaLongitude, fNewLatitude );
00062   Vector  vSphereNewYBasis( 1, vSphereNewZBasis.y, vSphereNewZBasis.z + kfHalfPi );
00063 
00064   m_mtxPOV.zBasis = SphericalToCartesian( NormalizeSpherical( vSphereNewZBasis ) );
00065   m_mtxPOV.yBasis = SphericalToCartesian( NormalizeSpherical( vSphereNewYBasis ) );
00066   m_mtxPOV.xBasis = m_mtxPOV.yBasis.cross( m_mtxPOV.zBasis ).normalized();
00067   m_mtxPOV.yBasis = m_mtxPOV.zBasis.cross( m_mtxPOV.xBasis ).normalized();
00068   m_mtxPOV.origin = m_mtxPOV.zBasis * m_fOrbitDistance;
00069 }
00070 
00071 void Camera::OnMouseWheel( float fDeltaZ )
00072 {
00073   static const float kfZScale = -0.15f;
00074 
00075   Move( m_mtxPOV.zBasis * fDeltaZ * kfZScale * m_fOrbitDistance );
00076   updateOrbitDistance();
00077 }
00078 
00079 void Camera::OnMouseDown( const Vector& vMousePos )
00080 {
00081   m_vLastMousePos   = vMousePos;
00082   updateOrbitDistance();
00083 }
00084 
00085 void Camera::OnMouseMoveOrbit( const Vector& vMousePos )
00086 {
00087   static const float kfMouseOrbitAngleScale = 1/1024.0f;
00088 
00089   Vector vOrbitDelta  = (vMousePos - m_vLastMousePos) * kfMouseOrbitAngleScale * kf2Pi;
00090 
00091   RotateOrbit( 0, vOrbitDelta.x, vOrbitDelta.y );
00092 
00093   m_vLastMousePos = vMousePos;
00094 }
00095 
00096 }


leap_motion
Author(s): Florian Lier , Mirza Shah , Isaac IY Saito
autogenerated on Sat Jun 8 2019 18:47:25