camera.h
1 /****************************************************************************
2 
3  Copyright (C) 2002-2013 Gilles Debunne. All rights reserved.
4 
5  This file is part of the QGLViewer library version 2.5.2.
6 
7  http://www.libqglviewer.com - contact@libqglviewer.com
8 
9  This file may be used under the terms of the GNU General Public License
10  versions 2.0 or 3.0 as published by the Free Software Foundation and
11  appearing in the LICENSE file included in the packaging of this file.
12  In addition, as a special exception, Gilles Debunne gives you certain
13  additional rights, described in the file GPL_EXCEPTION in this package.
14 
15  libQGLViewer uses dual licensing. Commercial/proprietary software must
16  purchase a libQGLViewer Commercial License.
17 
18  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
19  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 
21 *****************************************************************************/
22 
23 #ifndef QGLVIEWER_CAMERA_H
24 #define QGLVIEWER_CAMERA_H
25 
26 #include "keyFrameInterpolator.h"
27 class QGLViewer;
28 
29 namespace qglviewer {
30 
31 class ManipulatedCameraFrame;
32 
84 class QGLVIEWER_EXPORT Camera : public QObject
85 {
86 #ifndef DOXYGEN
87  friend class ::QGLViewer;
88 #endif
89 
90  Q_OBJECT
91 
92 public:
93  Camera();
94  virtual ~Camera();
95 
96  Camera(const Camera& camera);
97  Camera& operator=(const Camera& camera);
98 
99 
105  enum Type { PERSPECTIVE, ORTHOGRAPHIC };
106 
109 public:
110  Vec position() const;
111  Vec upVector() const;
112  Vec viewDirection() const;
113  Vec rightVector() const;
114  Quaternion orientation() const;
115 
116  void setFromModelViewMatrix(const GLdouble* const modelViewMatrix);
117  void setFromProjectionMatrix(const float matrix[12]);
118 
119 public Q_SLOTS:
120  void setPosition(const Vec& pos);
121  void setOrientation(const Quaternion& q);
122  void setOrientation(float theta, float phi);
123  void setUpVector(const Vec& up, bool noMove=true);
124  void setViewDirection(const Vec& direction);
126 
127 
130 public Q_SLOTS:
131  void lookAt(const Vec& target);
132  void showEntireScene();
133  void fitSphere(const Vec& center, float radius);
134  void fitBoundingBox(const Vec& min, const Vec& max);
135  void fitScreenRegion(const QRect& rectangle);
136  void centerScene();
137  void interpolateToZoomOnPixel(const QPoint& pixel);
138  void interpolateToFitScene();
139  void interpolateTo(const Frame& fr, float duration);
141 
142 
145 public:
158  Type type() const { return type_; }
159 
170  float fieldOfView() const { return fieldOfView_; }
171 
179  float horizontalFieldOfView() const { return 2.0 * atan ( tan(fieldOfView()/2.0) * aspectRatio() ); }
180 
185  float aspectRatio() const { return static_cast<float>(screenWidth_)/static_cast<float>(screenHeight_); }
190  int screenWidth() const { return screenWidth_; }
195  int screenHeight() const { return screenHeight_; }
196  void getViewport(GLint viewport[4]) const;
197  float pixelGLRatio(const Vec& position) const;
198 
210  float zNearCoefficient() const { return zNearCoef_; }
229  float zClippingCoefficient() const { return zClippingCoef_; }
230 
231  virtual float zNear() const;
232  virtual float zFar() const;
233  virtual void getOrthoWidthHeight(GLdouble& halfWidth, GLdouble& halfHeight) const;
234  void getFrustumPlanesCoefficients(GLdouble coef[6][4]) const;
235 
236 public Q_SLOTS:
237  void setType(Type type);
238 
239  void setFieldOfView(float fov);
240 
246  void setHorizontalFieldOfView(float hfov) { setFieldOfView( 2.0 * atan (tan(hfov / 2.0) / aspectRatio()) ); }
247 
248  void setFOVToFitScene();
249 
262  void setAspectRatio(float aspect) { setScreenWidthAndHeight(int(100.0*aspect), 100); }
263 
264  void setScreenWidthAndHeight(int width, int height);
266  void setZNearCoefficient(float coef) { zNearCoef_ = coef; projectionMatrixIsUpToDate_ = false; }
268  void setZClippingCoefficient(float coef) { zClippingCoef_ = coef; projectionMatrixIsUpToDate_ = false; }
270 
271 
274 public:
284  float sceneRadius() const { return sceneRadius_; }
285 
297  Vec sceneCenter() const { return sceneCenter_; }
298  float distanceToSceneCenter() const;
299 
300 public Q_SLOTS:
301  void setSceneRadius(float radius);
302  void setSceneCenter(const Vec& center);
303  bool setSceneCenterFromPixel(const QPoint& pixel);
304  void setSceneBoundingBox(const Vec& min, const Vec& max);
306 
307 
310 public Q_SLOTS:
311  void setPivotPoint(const Vec& point);
312  bool setPivotPointFromPixel(const QPoint& pixel);
313 
314 public:
315  Vec pivotPoint() const;
316 
317 #ifndef DOXYGEN
318 public Q_SLOTS:
319  void setRevolveAroundPoint(const Vec& point);
320  bool setRevolveAroundPointFromPixel(const QPoint& pixel);
321 public:
322  Vec revolveAroundPoint() const;
323 #endif
324 
325 
326 
329 public:
334  ManipulatedCameraFrame* frame() const { return frame_; }
335 public Q_SLOTS:
336  void setFrame(ManipulatedCameraFrame* const mcf);
338 
339 
342 public:
343  KeyFrameInterpolator* keyFrameInterpolator(int i) const;
344 
345 public Q_SLOTS:
346  void setKeyFrameInterpolator(int i, KeyFrameInterpolator* const kfi);
347 
348  virtual void addKeyFrameToPath(int i);
349  virtual void playPath(int i);
350  virtual void deletePath(int i);
351  virtual void resetPath(int i);
352  virtual void drawAllPaths();
354 
355 
358 public:
359  virtual void loadProjectionMatrix(bool reset=true) const;
360  virtual void loadModelViewMatrix(bool reset=true) const;
361  void computeProjectionMatrix() const;
362  void computeModelViewMatrix() const;
363 
364  virtual void loadProjectionMatrixStereo(bool leftBuffer=true) const;
365  virtual void loadModelViewMatrixStereo(bool leftBuffer=true) const;
366 
367  void getProjectionMatrix(GLfloat m[16]) const;
368  void getProjectionMatrix(GLdouble m[16]) const;
369 
370  void getModelViewMatrix(GLfloat m[16]) const;
371  void getModelViewMatrix(GLdouble m[16]) const;
372 
373  void getModelViewProjectionMatrix(GLfloat m[16]) const;
374  void getModelViewProjectionMatrix(GLdouble m[16]) const;
376 
377 
380 #ifndef DOXYGEN
381  static void drawCamera(float scale=1.0, float aspectRatio=1.33, float fieldOfView=M_PI/4.0);
382 #endif
383  virtual void draw(bool drawFarPlane=true, float scale=1.0) const;
385 
386 
389 public:
390  Vec cameraCoordinatesOf(const Vec& src) const;
391  Vec worldCoordinatesOf(const Vec& src) const;
392  void getCameraCoordinatesOf(const float src[3], float res[3]) const;
393  void getWorldCoordinatesOf(const float src[3], float res[3]) const;
395 
396 
399 public:
400  Vec projectedCoordinatesOf(const Vec& src, const Frame* frame=NULL) const;
401  Vec unprojectedCoordinatesOf(const Vec& src, const Frame* frame=NULL) const;
402  void getProjectedCoordinatesOf(const float src[3], float res[3], const Frame* frame=NULL) const;
403  void getUnprojectedCoordinatesOf(const float src[3], float res[3], const Frame* frame=NULL) const;
404  void convertClickToLine(const QPoint& pixel, Vec& orig, Vec& dir) const;
405  Vec pointUnderPixel(const QPoint& pixel, bool& found) const;
407 
408 
411 public:
412  float flySpeed() const;
413 public Q_SLOTS:
414  void setFlySpeed(float speed);
416 
417 
420 public:
425  float IODistance() const { return IODistance_; }
426 
434  float physicalDistanceToScreen() const { return physicalScreenWidth() / 2.0f / tan(horizontalFieldOfView() / 2.0); }
435 
440  float physicalScreenWidth() const { return physicalScreenWidth_; }
441 
452  float focusDistance() const { return focusDistance_; }
453 public Q_SLOTS:
455  void setIODistance(float distance) { IODistance_ = distance; }
456 
457 #ifndef DOXYGEN
458 
459  void setPhysicalDistanceToScreen(float distance) { Q_UNUSED(distance); qWarning("setPhysicalDistanceToScreen is deprecated, use setPhysicalScreenWidth instead"); }
460 #endif
461 
463  void setPhysicalScreenWidth(float width) { physicalScreenWidth_ = width; }
464 
466  void setFocusDistance(float distance) { focusDistance_ = distance; }
468 
469 
472 public:
473  virtual QDomElement domElement(const QString& name, QDomDocument& document) const;
474 public Q_SLOTS:
475  virtual void initFromDOMElement(const QDomElement& element);
477 
478 
479 private Q_SLOTS:
480  void onFrameModified();
481 
482 private:
483  // F r a m e
484  ManipulatedCameraFrame* frame_;
485 
486  // C a m e r a p a r a m e t e r s
487  int screenWidth_, screenHeight_; // size of the window, in pixels
488  float fieldOfView_; // in radians
489  Vec sceneCenter_;
490  float sceneRadius_; // OpenGL units
491  float zNearCoef_;
492  float zClippingCoef_;
493  float orthoCoef_;
494  Type type_; // PERSPECTIVE or ORTHOGRAPHIC
495  mutable GLdouble modelViewMatrix_[16]; // Buffered model view matrix.
496  mutable bool modelViewMatrixIsUpToDate_;
497  mutable GLdouble projectionMatrix_[16]; // Buffered projection matrix.
498  mutable bool projectionMatrixIsUpToDate_;
499 
500  // S t e r e o p a r a m e t e r s
501  float IODistance_; // inter-ocular distance, in meters
502  float focusDistance_; // in scene units
503  float physicalScreenWidth_; // in meters
504 
505  // P o i n t s o f V i e w s a n d K e y F r a m e s
506  QMap<int, KeyFrameInterpolator*> kfi_;
507  KeyFrameInterpolator* interpolationKfi_;
508 };
509 
510 } // namespace qglviewer
511 
512 #endif // QGLVIEWER_CAMERA_H
A keyFrame Catmull-Rom Frame interpolator.
The ManipulatedCameraFrame class represents a ManipulatedFrame with Camera specific mouse bindings...
Vec sceneCenter() const
Returns the position of the scene center, defined in the world coordinate system. ...
Definition: camera.h:297
Type
Enumerates the two possible types of Camera.
Definition: camera.h:105
float focusDistance() const
Returns the focus distance used by stereo display, expressed in OpenGL units.
Definition: camera.h:452
int screenWidth() const
Returns the width (in pixels) of the Camera screen.
Definition: camera.h:190
void setZClippingCoefficient(float coef)
Sets the zClippingCoefficient() value.
Definition: camera.h:268
FARSA_UTIL_TEMPLATE const T max(const T &t1, const U &t2)
void setIODistance(float distance)
Sets the IODistance().
Definition: camera.h:455
ManipulatedCameraFrame * frame() const
Returns the ManipulatedCameraFrame attached to the Camera.
Definition: camera.h:334
void setFocusDistance(float distance)
Sets the focusDistance(), in OpenGL scene units.
Definition: camera.h:466
int screenHeight() const
Returns the height (in pixels) of the Camera screen.
Definition: camera.h:195
float fieldOfView() const
Returns the vertical field of view of the Camera (in radians).
Definition: camera.h:170
void setPhysicalDistanceToScreen(float distance)
This method is deprecated.
Definition: camera.h:459
The Vec class represents 3D positions and 3D vectors.
Definition: vec.h:65
void setHorizontalFieldOfView(float hfov)
Sets the horizontalFieldOfView() of the Camera (in radians).
Definition: camera.h:246
float aspectRatio() const
Returns the Camera aspect ratio defined by screenWidth() / screenHeight().
Definition: camera.h:185
float physicalDistanceToScreen() const
Returns the physical distance between the user's eyes and the screen (in meters). ...
Definition: camera.h:434
void setPhysicalScreenWidth(float width)
Sets the physical screen (monitor or projected wall) width (in meters).
Definition: camera.h:463
A versatile 3D OpenGL viewer based on QGLWidget.
Definition: qglviewer.h:62
float zClippingCoefficient() const
Returns the coefficient used to position the near and far clipping planes.
Definition: camera.h:229
A perspective or orthographic camera.
Definition: camera.h:84
The Quaternion class represents 3D rotations and orientations.
Definition: quaternion.h:66
float physicalScreenWidth() const
Returns the physical screen width, in meters.
Definition: camera.h:440
Type type() const
Returns the Camera::Type of the Camera.
Definition: camera.h:158
void setZNearCoefficient(float coef)
Sets the zNearCoefficient() value.
Definition: camera.h:266
float IODistance() const
Returns the user's inter-ocular distance (in meters).
Definition: camera.h:425
void setAspectRatio(float aspect)
Defines the Camera aspectRatio().
Definition: camera.h:262
FARSA_UTIL_TEMPLATE const T min(const T &t1, const U &t2)
The Frame class represents a coordinate system, defined by a position and an orientation.
Definition: frame.h:121
float sceneRadius() const
Returns the radius of the scene observed by the Camera.
Definition: camera.h:284
float horizontalFieldOfView() const
Returns the horizontal field of view of the Camera (in radians).
Definition: camera.h:179
float zNearCoefficient() const
Returns the coefficient which is used to set zNear() when the Camera is inside the sphere defined by ...
Definition: camera.h:210