Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MayaCamUI.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Vector.h"
26 #include "cinder/Camera.h"
27 
28 namespace cinder {
29 
30 class MayaCamUI {
31  public:
32  MayaCamUI() { mInitialCam = mCurrentCam = CameraPersp(); }
33  MayaCamUI( const CameraPersp &aInitialCam ) { mInitialCam = mCurrentCam = aInitialCam; }
34 
35  void mouseDown( const Vec2i &mousePos )
36  {
37  mInitialMousePos = mousePos;
38  mInitialCam = mCurrentCam;
39  mLastAction = ACTION_NONE;
40  }
41 
42  void mouseDrag( const Vec2i &mousePos, bool leftDown, bool middleDown, bool rightDown )
43  {
44  int action;
45  if( rightDown || ( leftDown && middleDown ) )
46  action = ACTION_ZOOM;
47  else if( middleDown )
48  action = ACTION_PAN;
49  else if( leftDown )
50  action = ACTION_TUMBLE;
51  else
52  return;
53 
54  if( action != mLastAction ) {
55  mInitialCam = mCurrentCam;
56  mInitialMousePos = mousePos;
57  }
58 
59  mLastAction = action;
60 
61  if( action == ACTION_ZOOM ) { // zooming
62  int mouseDelta = ( mousePos.x - mInitialMousePos.x ) + ( mousePos.y - mInitialMousePos.y );
63 
64  float newCOI = powf( 2.71828183f, -mouseDelta / 500.0f ) * mInitialCam.getCenterOfInterest();
65  Vec3f oldTarget = mInitialCam.getCenterOfInterestPoint();
66  Vec3f newEye = oldTarget - mInitialCam.getViewDirection() * newCOI;
67  mCurrentCam.setEyePoint( newEye );
68  mCurrentCam.setCenterOfInterest( newCOI );
69  }
70  else if( action == ACTION_PAN ) { // panning
71  float deltaX = ( mousePos.x - mInitialMousePos.x ) / 1000.0f * mInitialCam.getCenterOfInterest();
72  float deltaY = ( mousePos.y - mInitialMousePos.y ) / 1000.0f * mInitialCam.getCenterOfInterest();
73  Vec3f mW = mInitialCam.getViewDirection().normalized();
74  Vec3f mU = Vec3f::yAxis().cross( mW ).normalized();
75  Vec3f mV = mW.cross( mU ).normalized();
76  mCurrentCam.setEyePoint( mInitialCam.getEyePoint() + mU * deltaX + mV * deltaY );
77  }
78  else { // tumbling
79  float deltaY = ( mousePos.y - mInitialMousePos.y ) / 100.0f;
80  float deltaX = ( mousePos.x - mInitialMousePos.x ) / -100.0f;
81  Vec3f mW = mInitialCam.getViewDirection().normalized();
82  bool invertMotion = ( mInitialCam.getOrientation() * Vec3f::yAxis() ).y < 0.0f;
83  Vec3f mU = Vec3f::yAxis().cross( mW ).normalized();
84 
85  if( invertMotion ) {
86  deltaX = -deltaX;
87  deltaY = -deltaY;
88  }
89 
90  Vec3f rotatedVec = Quatf( mU, deltaY ) * ( mInitialCam.getEyePoint() - mInitialCam.getCenterOfInterestPoint() );
91  rotatedVec = Quatf( Vec3f::yAxis(), deltaX ) * rotatedVec;
92 
93  mCurrentCam.setEyePoint( mInitialCam.getCenterOfInterestPoint() + rotatedVec );
94  mCurrentCam.setOrientation( mInitialCam.getOrientation() * Quatf( mU, deltaY ) * Quatf( Vec3f::yAxis(), deltaX ) );
95  }
96  }
97 
98  const CameraPersp& getCamera() const { return mCurrentCam; }
99  void setCurrentCam( const CameraPersp &aCurrentCam ) { mCurrentCam = aCurrentCam; }
100 
101  private:
102  enum { ACTION_NONE, ACTION_ZOOM, ACTION_PAN, ACTION_TUMBLE };
103 
104  Vec2i mInitialMousePos;
105  CameraPersp mCurrentCam, mInitialCam;
106  int mLastAction;
107 };
108 
109 }; // namespace cinder
float getCenterOfInterest() const
Definition: Camera.h:44
GLenum GLint GLint y
Definition: GLee.h:987
Quatf getOrientation() const
Definition: Camera.h:59
Vec3f getViewDirection() const
Definition: Camera.h:56
MayaCamUI()
Definition: MayaCamUI.h:32
void mouseDown(const Vec2i &mousePos)
Definition: MayaCamUI.h:35
T x
Definition: Vector.h:71
void setCenterOfInterest(float aCenterOfInterest)
Definition: Camera.h:45
static Vec3< float > yAxis()
Definition: Vector.h:684
Quaternion< float > Quatf
Definition: Quaternion.h:774
void setOrientation(const Quatf &aOrientation)
Definition: Camera.cpp:50
Vec3f getCenterOfInterestPoint() const
Definition: Camera.h:47
Vec3f getEyePoint() const
Definition: Camera.h:41
void setEyePoint(const Vec3f &aEyePoint)
Definition: Camera.cpp:31
T y
Definition: Vector.h:71
Definition: Camera.h:133
void mouseDrag(const Vec2i &mousePos, bool leftDown, bool middleDown, bool rightDown)
Definition: MayaCamUI.h:42
const CameraPersp & getCamera() const
Definition: MayaCamUI.h:98
Vec3< T > cross(const Vec3< T > &rhs) const
Definition: Vector.h:423
Definition: MayaCamUI.h:30
MayaCamUI(const CameraPersp &aInitialCam)
Definition: MayaCamUI.h:33
GLclampf f
Definition: GLee.h:15307
void setCurrentCam(const CameraPersp &aCurrentCam)
Definition: MayaCamUI.h:99
Vec2< int > Vec2i
Definition: Vector.h:1313
Vec3< T > normalized() const
Definition: Vector.h:492