camera.h
1 /****************************************************************************
2 
3  Copyright (C) 2002-2008 Gilles Debunne. All rights reserved.
4 
5  This file is part of the QGLViewer library version 2.3.10.
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 "manipulatedCameraFrame.h"
27 #include "keyFrameInterpolator.h"
28 
29 namespace qglviewer {
81  class QGLVIEWER_EXPORT Camera : public QObject
82  {
83 #ifndef DOXYGEN
84  friend class ::QGLViewer;
85 #endif
86 
87  Q_OBJECT
88 
89  public:
90  Camera();
91  virtual ~Camera();
92 
93  Camera(const Camera& camera);
94  Camera& operator=(const Camera& camera);
95 
96 
102  enum Type { PERSPECTIVE, ORTHOGRAPHIC };
103 
106  public:
114  Vec position() const { return frame()->position(); };
115 
123  Vec upVector() const
124  {
125  return frame()->inverseTransformOf(Vec(0.0, 1.0, 0.0));
126  }
134  Vec viewDirection() const { return frame()->inverseTransformOf(Vec(0.0, 0.0, -1.0)); };
135 
142  Vec rightVector() const
143  {
144  return frame()->inverseTransformOf(Vec(1.0, 0.0, 0.0));
145  }
146 
151  Quaternion orientation() const { return frame()->orientation(); };
152 
153  void setFromModelViewMatrix(const GLdouble* const modelViewMatrix);
154  void setFromProjectionMatrix(const float matrix[12]);
155 
156  public Q_SLOTS:
158  void setPosition(const Vec& pos) { frame()->setPosition(pos); };
159  void setOrientation(const Quaternion& q);
160  void setOrientation(float theta, float phi);
161  void setUpVector(const Vec& up, bool noMove=true);
162  void setViewDirection(const Vec& direction);
164 
165 
168  public Q_SLOTS:
169  void lookAt(const Vec& target);
170  void showEntireScene();
171  void fitSphere(const Vec& center, float radius);
172  void fitBoundingBox(const Vec& min, const Vec& max);
173  void fitScreenRegion(const QRect& rectangle);
174  void centerScene();
175  void interpolateToZoomOnPixel(const QPoint& pixel);
176  void interpolateToFitScene();
177  void interpolateTo(const Frame& fr, float duration);
179 
180 
183  public:
196  Type type() const { return type_; };
197 
208  float fieldOfView() const { return fieldOfView_; };
209 
217  float horizontalFieldOfView() const { return 2.0 * atan ( tan(fieldOfView()/2.0) * aspectRatio() ); };
218 
223  float aspectRatio() const { return static_cast<float>(screenWidth_)/static_cast<float>(screenHeight_); };
228  int screenWidth() const { return screenWidth_; };
233  int screenHeight() const { return screenHeight_; };
234  void getViewport(GLint viewport[4]) const;
235  float pixelGLRatio(const Vec& position) const;
236 
248  float zNearCoefficient() const { return zNearCoef_; };
267  float zClippingCoefficient() const { return zClippingCoef_; }
268 
269  virtual float zNear() const;
270  virtual float zFar() const;
271  virtual void getOrthoWidthHeight(GLdouble& halfWidth, GLdouble& halfHeight) const;
272  void getFrustumPlanesCoefficients(GLdouble coef[6][4]) const;
273 
274  public Q_SLOTS:
275  void setType(Type type);
276 
280  void setFieldOfView(float fov) { fieldOfView_ = fov; setFocusDistance(sceneRadius() / tan(fov/2.0)); };
281 
287  void setHorizontalFieldOfView(float hfov) { setFieldOfView( 2.0 * atan (tan(hfov / 2.0) / aspectRatio()) ); };
288 
289  void setFOVToFitScene();
290 
303  void setAspectRatio(float aspect) { setScreenWidthAndHeight(int(100.0*aspect), 100); };
304 
305  void setScreenWidthAndHeight(int width, int height);
307  void setZNearCoefficient(float coef) { zNearCoef_ = coef; };
309  void setZClippingCoefficient(float coef) { zClippingCoef_ = coef; }
311 
312 
315  public:
325  float sceneRadius() const { return sceneRadius_; };
326 
338  Vec sceneCenter() const { return sceneCenter_; };
339  float distanceToSceneCenter() const;
340 
341  public Q_SLOTS:
342  void setSceneRadius(float radius);
343  void setSceneCenter(const Vec& center);
344  bool setSceneCenterFromPixel(const QPoint& pixel);
345  void setSceneBoundingBox(const Vec& min, const Vec& max);
347 
348 
351  public Q_SLOTS:
352  void setRevolveAroundPoint(const Vec& rap);
353  bool setRevolveAroundPointFromPixel(const QPoint& pixel);
354 
355  public:
361  Vec revolveAroundPoint() const { return frame()->revolveAroundPoint(); };
363 
364 
367  public:
372  ManipulatedCameraFrame* frame() const { return frame_; };
373  public Q_SLOTS:
374  void setFrame(ManipulatedCameraFrame* const mcf);
376 
377 
380  public:
381  KeyFrameInterpolator* keyFrameInterpolator(int i) const;
382 
383 public Q_SLOTS:
384  void setKeyFrameInterpolator(int i, KeyFrameInterpolator* const kfi);
385 
386  virtual void addKeyFrameToPath(int i);
387  virtual void playPath(int i);
388  virtual void deletePath(int i);
389  virtual void resetPath(int i);
390  virtual void drawAllPaths();
392 
393 
396  public:
397  virtual void loadProjectionMatrix(bool reset=true) const;
398  virtual void loadModelViewMatrix(bool reset=true) const;
399  void computeProjectionMatrix() const;
400  void computeModelViewMatrix() const;
401 
402  virtual void loadProjectionMatrixStereo(bool leftBuffer=true) const;
403  virtual void loadModelViewMatrixStereo(bool leftBuffer=true) const;
404 
405  void getProjectionMatrix(GLdouble m[16]) const;
406  void getModelViewMatrix(GLdouble m[16]) const;
407  void getModelViewProjectionMatrix(GLdouble m[16]) const;
408 
409 #ifndef DOXYGEN
410  // Required for windows which otherwise silently fills
411  void getProjectionMatrix(GLfloat m[16]) const;
412  void getModelViewMatrix(GLfloat m[16]) const;
413 #endif
414 
415 
416 
419 #ifndef DOXYGEN
420  static void drawCamera(float scale=1.0, float aspectRatio=1.33, float fieldOfView=M_PI/4.0);
421 #endif
422  virtual void draw(bool drawFarPlane=true, float scale=1.0) const;
424 
425 
428  public:
435  Vec cameraCoordinatesOf(const Vec& src) const { return frame()->coordinatesOf(src); };
440  Vec worldCoordinatesOf(const Vec& src) const { return frame()->inverseCoordinatesOf(src); };
441  void getCameraCoordinatesOf(const float src[3], float res[3]) const;
442  void getWorldCoordinatesOf(const float src[3], float res[3]) const;
444 
445 
448  public:
449  Vec projectedCoordinatesOf(const Vec& src, const Frame* frame=NULL) const;
450  Vec unprojectedCoordinatesOf(const Vec& src, const Frame* frame=NULL) const;
451  void getProjectedCoordinatesOf(const float src[3], float res[3], const Frame* frame=NULL) const;
452  void getUnprojectedCoordinatesOf(const float src[3], float res[3], const Frame* frame=NULL) const;
453  void convertClickToLine(const QPoint& pixel, Vec& orig, Vec& dir) const;
454  Vec pointUnderPixel(const QPoint& pixel, bool& found) const;
456 
457 
460  public:
468  float flySpeed() const { return frame()->flySpeed(); };
469  public Q_SLOTS:
473  void setFlySpeed(float speed) { frame()->setFlySpeed(speed); };
475 
476 
479  public:
484  float IODistance() const { return IODistance_; };
485 
507  float physicalDistanceToScreen() const { return physicalDistanceToScreen_; };
508 
515  float physicalScreenWidth() const { return physicalScreenWidth_; };
516 
527  float focusDistance() const { return focusDistance_; };
528  public Q_SLOTS:
530  void setIODistance(float distance) { IODistance_ = distance; };
531 
533  void setPhysicalDistanceToScreen(float distance) { physicalDistanceToScreen_ = distance; };
534 
536  void setPhysicalScreenWidth(float width) { physicalScreenWidth_ = width; };
537 
539  void setFocusDistance(float distance) { focusDistance_ = distance; };
541 
542 
545  public:
546  virtual QDomElement domElement(const QString& name, QDomDocument& document) const;
547  public Q_SLOTS:
548  virtual void initFromDOMElement(const QDomElement& element);
550 
551 
552  private:
553  // F r a m e
554  ManipulatedCameraFrame* frame_;
555 
556  // C a m e r a p a r a m e t e r s
557  int screenWidth_, screenHeight_; // size of the window, in pixels
558  float fieldOfView_; // in radians
559  Vec sceneCenter_;
560  float sceneRadius_; // OpenGL units
561  float zNearCoef_;
562  float zClippingCoef_;
563  float orthoCoef_;
564  Type type_; // PERSPECTIVE or ORTHOGRAPHIC
565  mutable GLdouble modelViewMatrix_[16]; // Buffered model view matrix.
566  mutable GLdouble projectionMatrix_[16]; // Buffered projection matrix.
567 
568  // S t e r e o p a r a m e t e r s
569  float IODistance_; // inter-ocular distance, in meters
570  float focusDistance_; // in scene units
571  float physicalDistanceToScreen_; // in meters
572  float physicalScreenWidth_; // in meters
573 
574  // 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
575  QMap<int, KeyFrameInterpolator*> kfi_;
576  KeyFrameInterpolator* interpolationKfi_;
577  };
578 
579 } // namespace qglviewer
580 
581 #endif // QGLVIEWER_CAMERA_H