CEDRIC  Revision_backup-2009-02
MyCamera.h
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * Copyright (C) 2008 Boris Iven, Max Hermann
4  *
5  * This file is part of the "CEDRIC Event Display" application.
6  *
7  *****************************************************************************/
8 
9 #ifndef MYCAMERA
10 #define MYCAMERA
11 
12 #include <osg/ref_ptr>
13 #include <osg/Vec3>
14 #include <osg/Quat>
15 #include <osg/Matrixd>
16 #include <osg/Camera>
17 #include <osg/BoundingBox>
18 
19 #include <assert.h>
20 
21 #include <list>
22 
23 #define MYCAMERA_SUPPORT_QT
24 
42 #ifdef MYCAMERA_SUPPORT_QT
43  #include <QObject>
44  class MyCamera : public QObject
45  {
46  Q_OBJECT
47 
48  signals:
49  void myCameraChanged();
50  void myCameraChanged( MyCamera* );
51  private:
52 #else
53  class MyCamera
54  {
55 #endif
56 public:
58  enum Projection {
61  };
62 
64  struct Frustum
65  {
67 
69  : left(-1.f),right(1.f),bottom(-1.f),top(1.f),zNear(-1.f),zFar(1.f)
70  {}
71 
72  Frustum( float l, float r, float b, float t, float n, float f )
73  : left(l),right(r),bottom(b),top(t),zNear(n),zFar(f)
74  {}
75  };
76 
78  MyCamera();
80  virtual ~MyCamera() {}
82  #ifdef MYCAMERA_SUPPORT_QT // QObject should be explicitly initialized in copy contructor
83  MyCamera( const MyCamera& other ):QObject() { setLike( other ); }
84  #else
85  MyCamera( const MyCamera& other ) { setLike( other ); }
86  #endif
87  MyCamera& operator = ( const MyCamera& other ) { if( this!=&other ) setLike(other); return *this; }
90  void setLike( const MyCamera& other )
91  {
92  m_projection = other.getProjection();
93  m_fov = other.getFov();
94  m_aspect = other.getAspect();
95  getLookAt( m_eye, m_center, m_up );
96  m_rotation = other.rotation();
97  m_camera = other.getCamera();
98  m_autoComputeAspect = other.isAutoComputeAspect();
99  }
100 
102  void setProjection( int proj )
103  {
104  switch( proj )
105  {
106  default:
107  case PERSPECTIVE: m_projection=PERSPECTIVE; break;
108  case ORTHOGRAPHIC: m_projection=ORTHOGRAPHIC; break;
109  }
110 
111  update();
112  }
114  int getProjection() const { return m_projection; }
115 
117  void setFov( float fov ) { m_fov = fov; updateProj(); }
119  void setAspect( float aspect, bool autoComputeAspect=false ) { m_aspect=aspect; m_autoComputeAspect=autoComputeAspect; updateProj(); }
121  void setFovAspect( float fov, float aspect, bool autoComputeAspect=false ) { m_fov=fov; m_aspect=aspect; m_autoComputeAspect=autoComputeAspect; updateProj(); }
124  void enableAutoComputeAspect() { m_autoComputeAspect = false; }
125  bool isAutoComputeAspect() const { return m_autoComputeAspect; }
126 
128  void setLookAt( osg::Vec3 eye, osg::Vec3 center, osg::Vec3 up );
130  void setDistance( float dist )
131  {
132  // rotation is not affected
133  m_eye = m_center - north()*dist;
134  updateView();
135  }
137  void setRotation( osg::Quat q ); //{ m_rotation = q; updateView(); }
139  void setCenter( osg::Vec3 center )
140  {
141  // rotation is changed!
142  setLookAt( m_eye, center, m_up );
143  }
144 
146  void translate( osg::Vec3 t ) { m_center+=t; m_eye+=t; updateView(); }
148  void pan( float dx, float dy );
152  void zoom( int x0, int y0, int x1, int y1 );
153 
155  void zoom( const osg::BoundingBox& bbox );
156 
158  void apply( osg::Camera* cam ) const;
159 
161  inline float getFov() const { return m_fov; }
163  inline float getAspect() const { return m_aspect; }
164 
166  inline osg::Quat rotation() const { return m_rotation; }
168  inline float distance() const { return los().length(); }
170  inline osg::Vec3 los() const { return m_center - m_eye; }
172  inline osg::Vec3 north() const { osg::Vec3 n(m_center-m_eye); n.normalize(); return n; }
174  inline osg::Vec3 east() const { osg::Vec3 s=north() ^ m_up; s.normalize(); return s; }
176  inline osg::Vec3 up() const { return m_up; }
178  inline osg::Vec3 center() const { return m_center; }
179 
181 
186  inline void getLookAt( osg::Vec3& eye, osg::Vec3& center, osg::Vec3& up ) const
187  { eye=m_eye; center=m_center; up=m_up; }
188 
189 
193  void updateView() { update(); }
194 
198  void updateProj() { update(); }
199 
201  void update();
202 
204  void associate( osg::Camera* cam ) { m_camera=cam; update(); }
206  osg::Camera* getCamera() const { return m_camera.get(); }
207 
209  void setLikeCamera( osg::Camera* cam );
210 
211 
212  void link( MyCamera* other );
213  void unlink( MyCamera* other );
214  void unlinkAll() { m_linked.clear(); }
215 
216 
217  void setFixedOrientation( bool fixed ) { m_fixedOrientation = fixed; }
218  bool isFixedOrientation() const { return m_fixedOrientation; }
219 
220 
222 
228  osg::Vec3 unproject( int x, int y, osg::Camera* cam=0 ) const;
229 
230 
232  osg::Matrixd getViewMatrix() const;
233 
235  osg::Matrixd getInverseViewMatrix() const;
236 
237 
238 protected:
240  Frustum makeFrustum() const;
241 
242  // HACK
243  void disableNextUpdate() { m_disableNextUpdate = true; }
244 
245 private:
246  int m_projection;
247  float m_fov, m_aspect;
248  osg::Vec3 m_eye, m_center, m_up;
249  osg::Quat m_rotation;
250 
251  osg::ref_ptr<osg::Camera> m_camera;
252 
253  std::list<MyCamera*> m_linked;
254 
255  bool m_autoComputeAspect;
256  bool m_disableNextUpdate;
257  bool m_fixedOrientation;
258 };
259 
260 #endif
bool isFixedOrientation() const
Definition: MyCamera.h:218
void setRotation(osg::Quat q)
Set rotation (adjusts eye and up to match rotation, center stays fixed)
Definition: MyCamera.cpp:98
void zoom(int x0, int y0, int x1, int y1)
Definition: MyCamera.cpp:277
void setCenter(osg::Vec3 center)
Only change center (rarely used, most times you want translate())
Definition: MyCamera.h:139
osg::Vec3 unproject(int x, int y, osg::Camera *cam=0) const
Get the world space coordinates (on a screen-parallel plane through center) of a pixel on screen...
Definition: MyCamera.cpp:239
float bottom
Definition: MyCamera.h:66
void myCameraChanged()
Manage modelview and projection, allows switching between ORTHOGRAPHIC and PERSPECTIVE projection...
Definition: MyCamera.h:44
osg::Vec3 center() const
Get center.
Definition: MyCamera.h:178
osg::Vec3 los() const
Get line-of-sight = center - eye.
Definition: MyCamera.h:170
void setFovAspect(float fov, float aspect, bool autoComputeAspect=false)
Set fov and aspect ratio simultaneous.
Definition: MyCamera.h:121
void getLookAt(osg::Vec3 &eye, osg::Vec3 &center, osg::Vec3 &up) const
Get eye, center and up vector.
Definition: MyCamera.h:186
osg::Camera * getCamera() const
Get pointer to associated Camera (if any)
Definition: MyCamera.h:206
Perspective projection mode.
Definition: MyCamera.h:60
osg::Matrixd getViewMatrix() const
Provided for convenience use in MatrixManipulator classes (rarely used, OSG calls getInverseMatrix() ...
Definition: MyCamera.cpp:52
osg::Matrixd getInverseViewMatrix() const
Provided for convenience use in MatrixManipulator classes (OSG uses this one for the modelview) ...
Definition: MyCamera.cpp:60
osg::Vec3 north() const
Get "north" vector = normalized(center - eye)
Definition: MyCamera.h:172
Viewing frustum.
Definition: MyCamera.h:64
float top
Definition: MyCamera.h:66
void updateProj()
Definition: MyCamera.h:198
osg::Vec3 up() const
Get up vector.
Definition: MyCamera.h:176
float getFov() const
Get field of view (in radians)
Definition: MyCamera.h:161
void unlinkAll()
Definition: MyCamera.h:214
void setAspect(float aspect, bool autoComputeAspect=false)
Set projection aspect ratio.
Definition: MyCamera.h:119
void setFov(float fov)
Set projection fov.
Definition: MyCamera.h:117
float distance() const
Get distance from eye to center.
Definition: MyCamera.h:168
Frustum(float l, float r, float b, float t, float n, float f)
Definition: MyCamera.h:72
int getProjection() const
Get current Projection setting.
Definition: MyCamera.h:114
Projection
Supported projection modes.
Definition: MyCamera.h:58
virtual ~MyCamera()
Destructor (does nothing)
Definition: MyCamera.h:80
MyCamera(const MyCamera &other)
Copy constructor.
Definition: MyCamera.h:83
MyCamera()
Default constructor.
Definition: MyCamera.cpp:40
Frustum makeFrustum() const
Compute screen parallel plane through center matching fov, aspect and distance.
Definition: MyCamera.cpp:119
float zNear
Definition: MyCamera.h:66
void setLookAt(osg::Vec3 eye, osg::Vec3 center, osg::Vec3 up)
Set modelview (same syntax as gluLookAt)
Definition: MyCamera.cpp:68
float zFar
Definition: MyCamera.h:66
float getAspect() const
Get aspect ratio.
Definition: MyCamera.h:163
MyCamera & operator=(const MyCamera &other)
Copy assignment.
Definition: MyCamera.h:88
void setLike(const MyCamera &other)
Copy settings from another MyCamera.
Definition: MyCamera.h:90
Orhographic projection mode.
Definition: MyCamera.h:59
float right
Definition: MyCamera.h:66
void apply(osg::Camera *cam) const
Apply our projection and modelview to a Camera.
Definition: MyCamera.cpp:190
void setProjection(int proj)
Set either ORTHOGRAPHIC or PERSPECTIVE projection (default is PERSPECTIVE)
Definition: MyCamera.h:102
void unlink(MyCamera *other)
Definition: MyCamera.cpp:234
void updateView()
Definition: MyCamera.h:193
void setFixedOrientation(bool fixed)
Definition: MyCamera.h:217
void setDistance(float dist)
Set distance between eye and center keeping center fix and moving eye along viewing direction ("north...
Definition: MyCamera.h:130
void associate(osg::Camera *cam)
Associate single Camera which is updated everytime our modelview or projection changes.
Definition: MyCamera.h:204
Frustum()
Definition: MyCamera.h:68
float left
Definition: MyCamera.h:66
void link(MyCamera *other)
Definition: MyCamera.cpp:223
void update()
Update modelview and projection of associated camera.
Definition: MyCamera.cpp:136
void pan(float dx, float dy)
Pan center in direction of rotated up and east vector.
Definition: MyCamera.cpp:266
void disableNextUpdate()
Definition: MyCamera.h:243
osg::Vec3 east() const
Get "east" vector = cross product between north and up.
Definition: MyCamera.h:174
void setLikeCamera(osg::Camera *cam)
Set projection and modelview like given Camera (NO IMPLEMENTATION YET)
Definition: MyCamera.cpp:215
void translate(osg::Vec3 t)
Translate center.
Definition: MyCamera.h:146
bool isAutoComputeAspect() const
Definition: MyCamera.h:125
osg::Quat rotation() const
Get rotation.
Definition: MyCamera.h:166
void enableAutoComputeAspect()
Definition: MyCamera.h:124