Tulip  4.6.0
Better Visualization Through Research
library/tulip-ogl/include/tulip/Camera.h
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 
00020 #ifndef TLPCAMERA_H
00021 #define TLPCAMERA_H
00022 
00023 #include <tulip/Coord.h>
00024 #include <tulip/Matrix.h>
00025 #include <tulip/BoundingBox.h>
00026 #include <tulip/Observable.h>
00027 
00028 namespace tlp {
00029 
00030 class GlScene;
00031 
00032 /**
00033  * \ingroup OpenGL
00034  * \brief Tulip OpenGL camera object
00035  *
00036  * This camera can be a 2D or 3D camera
00037  * After setup you can do some basic operation :
00038  *  - Move, rotate, strafeLeftRight and strafeUpDown to modify poitn of view
00039  *  - You can directly modify camera parameters with setSceneRadius, setZoomFactor, setEyes, setCenter and setUp
00040  *  - You can transform screen coordinates to 3D world coordinates with screenTo3DWorld() function and 3D world coordinates to screen coordinates with worldTo2DScreen() function
00041  * A camera is a main component of GlLayer and GlScene
00042  * @see GlLayer
00043  * @see GlScene
00044  */
00045 class TLP_GL_SCOPE Camera : public Observable {
00046 public:
00047 
00048   /**
00049    * @brief Constructor
00050    * @param scene A layer is attached to a scene so we have to specify it in the constructor
00051    * @param center 3D coordinates of point visualized by the camera
00052    * @param eye 3D position of the camera
00053    * @param up normalized up 3D coordinates of the camera
00054    * @param zoomFactor level of zoom of the camera
00055    * @param sceneRadius scene radius of the camera
00056    */
00057   Camera(GlScene* scene,Coord center=Coord(0,0,0),
00058          Coord eyes=Coord(0,0,10), Coord up=Coord(0,-1,0),
00059          double zoomFactor=0.5, double sceneRadius=10);
00060   /**
00061    * @brief Constructor : used to create a 2D camera
00062    */
00063   Camera(GlScene* scene,bool d3);
00064 
00065   Camera& operator=(const Camera& camera);
00066 
00067   /**
00068    * @brief Destructor
00069    */
00070   ~Camera();
00071 
00072   /**
00073    * @brief Set the camera's scene
00074    * The viewport is store in the scene, so we must attach camera to a scene
00075    */
00076   void setScene(GlScene *scene);
00077 
00078   /**
00079    * @brief Return the camera's scene
00080    */
00081   GlScene *getScene() const {
00082     return scene;
00083   }
00084 
00085   /**
00086    * @brief Load this camera parameters (eye, center, zoom factor) with an other camera parameters
00087    */
00088   void loadCameraParametersWith(const Camera &camera) {
00089     *this=camera;
00090   }
00091 
00092   /**
00093    * @brief Return the camera bounding box
00094    *
00095    * This bounding box is the part of the scene visualized by this camera.
00096    */
00097   BoundingBox getBoundingBox() const;
00098 
00099   /**
00100    * @brief This function moves the camera forward or backward depending on the speed
00101    */
00102   void move(float speed);
00103   /**
00104    * @brief This function strafes the camera left and right depending on the speed (-/+)
00105    */
00106   void strafeLeftRight(float speed);
00107   /**
00108    * @brief This function strafes the camera up and down depending on the speed (-/+)
00109    */
00110   void strafeUpDown(float speed);
00111   /**
00112    * @brief This function rotates the camera's eyes around the center depending on the values passed in.
00113    */
00114   void rotate(float angle, float x, float y, float z);
00115 
00116   /**
00117    * @brief Return if the camera is a 3D one
00118    */
00119   bool is3D() const {
00120     return d3;
00121   }
00122 
00123   /**
00124    * @brief Return the viewport of the attached scene
00125    */
00126   Vector<int, 4> getViewport() const;
00127 
00128   /**
00129    * @brief Return the scene radius
00130    */
00131   double getSceneRadius() const {
00132     return sceneRadius;
00133   }
00134 
00135   /**
00136    * @brief Set the zoom factor
00137    *
00138    * level of zoom of the camera
00139    */
00140   void setZoomFactor(double zoomFactor);
00141   /**
00142    * @brief Return the zoom factor
00143    *
00144    * level of zoom of the camera
00145    */
00146   double getZoomFactor() const {
00147     return zoomFactor;
00148   }
00149 
00150   /**
00151    * @brief Set the eye
00152    *
00153    * 3D position of the camera
00154    */
00155   void setEyes(const Coord& eyes);
00156   /**
00157    * @brief Return the eyes
00158    *
00159    * 3D position of the camera
00160    */
00161   Coord getEyes() const {
00162     return eyes;
00163   }
00164 
00165   /**
00166    * @brief Set the center
00167    *
00168    * 3D coordinates of point visualized by the camera
00169    */
00170   void setCenter(const Coord& center);
00171   /**
00172    * @brief Return the center
00173    *
00174    * 3D coordinates of point visualized by the camera
00175    */
00176   Coord getCenter() const {
00177     return center;
00178   }
00179 
00180   /**
00181    * @brief Set the up vector
00182    *
00183    * normalized up 3D coordinates of the camera
00184    */
00185   void setUp(const Coord& up);
00186   /**
00187    * @brief Return the up vector
00188    *
00189    * normalized up 3D coordinates of the camera
00190    */
00191   Coord getUp() const {
00192     return up;
00193   }
00194 
00195   /**
00196    * @brief Return the 3D world coordinate for the given screen point
00197    * \warning This function set up the projection and modelview matrix
00198    */
00199   Coord screenTo3DWorld(const Coord &point) const;
00200 
00201   /**
00202    * @brief Return the screen position for the given 3D coordinate
00203    * \warning This function set up the projection and modelview matrix
00204    */
00205   Coord worldTo2DScreen(const Coord &obj) const;
00206 
00207   /**
00208    * @brief Function to export data in outString (in XML format)
00209    */
00210   virtual void getXML(std::string &outString);
00211 
00212   /**
00213    * @brief Function to set data with inString (in XML format)
00214    */
00215   virtual void setWithXML(const std::string &inString, unsigned int &currentPosition);
00216 
00217 ///@cond DOXYGEN_HIDDEN
00218 
00219   /**
00220    * Get the modelview matrix
00221    */
00222   void getModelviewMatrix(Matrix<float, 4> &modelviewMatrix) const {
00223     modelviewMatrix=this->modelviewMatrix;
00224   }
00225 
00226   /**
00227    * Get the projection matrix
00228    */
00229   void getProjectionMatrix(Matrix<float, 4> &projectionMatrix) const {
00230     projectionMatrix=this->projectionMatrix;
00231   }
00232 
00233   /**
00234    * Get the transform matrix : transformMatrix = projectionMatrix * modelviewMatrix
00235    */
00236   void getTransformMatrix(Matrix<float, 4> &transformMatrix) const {
00237     transformMatrix=this->transformMatrix;
00238   }
00239 
00240   /**
00241    * Get the projection and the modelview matrix generated with the given viewport
00242    */
00243   void getProjAndMVMatrix(const Vector<int, 4>& viewport,Matrix<float, 4> &projectionMatrix,Matrix<float, 4> &modelviewMatrix) const;
00244 
00245   /**
00246    * Get the transform matrix generated with the given viewport
00247    */
00248   void getTransformMatrix(const Vector<int, 4>& viewport,Matrix<float, 4> &transformMatrix) const;
00249 
00250   /**
00251    * @brief Init Gl parameters
00252    */
00253   void initGl();
00254 
00255   /**
00256    * @brief Init light
00257    */
00258   void initLight();
00259 
00260   /**
00261    * @brief Init projection with the gived viewport. Load identity matrix if reset is set as true
00262    */
00263   void initProjection(const Vector<int, 4>& viewport,bool reset=true);
00264 
00265   /**
00266    * @brief Init projection with the scene viewport. Load identity matrix if reset is set as true
00267    */
00268   void initProjection(bool reset=true);
00269 
00270   /**
00271    * @brief Init modelview
00272    */
00273   void initModelView();
00274 
00275   /**
00276    * @brief Set the scene radius
00277    */
00278   void setSceneRadius(double sceneRadius,const BoundingBox sceneBoundingBox=BoundingBox());
00279 
00280 ///@endcond
00281 
00282 private:
00283 
00284   bool matrixCoherent;
00285 
00286   Coord center,eyes,up;
00287   double zoomFactor;
00288   double sceneRadius;
00289   BoundingBox sceneBoundingBox;
00290 
00291   GlScene* scene;
00292 
00293   Matrix<float, 4> modelviewMatrix;
00294   Matrix<float, 4> projectionMatrix;
00295   Matrix<float, 4> transformMatrix;
00296 
00297   bool d3;
00298 
00299 };
00300 
00301 }
00302 
00303 #endif
 All Classes Files Functions Variables Enumerations Enumerator Properties