Tulip  4.3.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
Camera.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef TLPCAMERA_H
21 #define TLPCAMERA_H
22 
23 #include <tulip/Coord.h>
24 #include <tulip/Matrix.h>
25 #include <tulip/BoundingBox.h>
26 #include <tulip/Observable.h>
27 
28 namespace tlp {
29 
30 class GlScene;
31 
32 /**
33  * \ingroup OpenGL
34  * \brief Tulip OpenGL camera object
35  *
36  * This camera can be a 2D or 3D camera
37  * After setup you can do some basic operation :
38  * - Move, rotate, strafeLeftRight and strafeUpDown to modify poitn of view
39  * - You can directly modify camera parameters with setSceneRadius, setZoomFactor, setEyes, setCenter and setUp
40  * - You can transform screen coordinates to 3D world coordinates with screenTo3DWorld() function and 3D world coordinates to screen coordinates with worldTo2DScreen() function
41  * A camera is a main component of GlLayer and GlScene
42  * @see GlLayer
43  * @see GlScene
44  */
45 class TLP_GL_SCOPE Camera : public Observable {
46 public:
47 
48  /**
49  * @brief Constructor
50  * @param scene A layer is attached to a scene so we have to specify it in the constructor
51  * @param center 3D coordinates of point visualized by the camera
52  * @param eye 3D position of the camera
53  * @param up normalized up 3D coordinates of the camera
54  * @param zoomFactor level of zoom of the camera
55  * @param sceneRadius scene radius of the camera
56  */
57  Camera(GlScene* scene,Coord center=Coord(0,0,0),
58  Coord eyes=Coord(0,0,10), Coord up=Coord(0,-1,0),
59  double zoomFactor=0.5, double sceneRadius=10);
60  /**
61  * @brief Constructor : used to create a 2D camera
62  */
63  Camera(GlScene* scene,bool d3);
64 
65  Camera& operator=(const Camera& camera);
66 
67  /**
68  * @brief Destructor
69  */
70  ~Camera();
71 
72  /**
73  * @brief Set the camera's scene
74  * The viewport is store in the scene, so we must attach camera to a scene
75  */
76  void setScene(GlScene *scene);
77 
78  /**
79  * @brief Return the camera's scene
80  */
81  GlScene *getScene() const {
82  return scene;
83  }
84 
85  /**
86  * @brief Load this camera parameters (eye, center, zoom factor) with an other camera parameters
87  */
88  void loadCameraParametersWith(const Camera &camera) {
89  *this=camera;
90  }
91 
92  /**
93  * @brief Return the camera bounding box
94  *
95  * This bounding box is the part of the scene visualized by this camera.
96  */
97  BoundingBox getBoundingBox() const;
98 
99  /**
100  * @brief This function moves the camera forward or backward depending on the speed
101  */
102  void move(float speed);
103  /**
104  * @brief This function strafes the camera left and right depending on the speed (-/+)
105  */
106  void strafeLeftRight(float speed);
107  /**
108  * @brief This function strafes the camera up and down depending on the speed (-/+)
109  */
110  void strafeUpDown(float speed);
111  /**
112  * @brief This function rotates the camera's eyes around the center depending on the values passed in.
113  */
114  void rotate(float angle, float x, float y, float z);
115 
116  /**
117  * @brief Return if the camera is a 3D one
118  */
119  bool is3D() const {
120  return d3;
121  }
122 
123  /**
124  * @brief Return the viewport of the attached scene
125  */
126  Vector<int, 4> getViewport() const;
127 
128  /**
129  * @brief Return the scene radius
130  */
131  double getSceneRadius() const {
132  return sceneRadius;
133  }
134 
135  /**
136  * @brief Set the zoom factor
137  *
138  * level of zoom of the camera
139  */
140  void setZoomFactor(double zoomFactor);
141  /**
142  * @brief Return the zoom factor
143  *
144  * level of zoom of the camera
145  */
146  double getZoomFactor() const {
147  return zoomFactor;
148  }
149 
150  /**
151  * @brief Set the eye
152  *
153  * 3D position of the camera
154  */
155  void setEyes(const Coord& eyes);
156  /**
157  * @brief Return the eyes
158  *
159  * 3D position of the camera
160  */
161  Coord getEyes() const {
162  return eyes;
163  }
164 
165  /**
166  * @brief Set the center
167  *
168  * 3D coordinates of point visualized by the camera
169  */
170  void setCenter(const Coord& center);
171  /**
172  * @brief Return the center
173  *
174  * 3D coordinates of point visualized by the camera
175  */
176  Coord getCenter() const {
177  return center;
178  }
179 
180  /**
181  * @brief Set the up vector
182  *
183  * normalized up 3D coordinates of the camera
184  */
185  void setUp(const Coord& up);
186  /**
187  * @brief Return the up vector
188  *
189  * normalized up 3D coordinates of the camera
190  */
191  Coord getUp() const {
192  return up;
193  }
194 
195  /**
196  * @brief Return the 3D world coordinate for the given screen point
197  * \warning This function set up the projection and modelview matrix
198  */
199  Coord screenTo3DWorld(const Coord &point) const;
200 
201  /**
202  * @brief Return the screen position for the given 3D coordinate
203  * \warning This function set up the projection and modelview matrix
204  */
205  Coord worldTo2DScreen(const Coord &obj) const;
206 
207  /**
208  * @brief Function to export data in outString (in XML format)
209  */
210  virtual void getXML(std::string &outString);
211 
212  /**
213  * @brief Function to set data with inString (in XML format)
214  */
215  virtual void setWithXML(const std::string &inString, unsigned int &currentPosition);
216 
217 ///@cond DOXYGEN_HIDDEN
218 
219  /**
220  * Get the modelview matrix
221  */
222  void getModelviewMatrix(Matrix<float, 4> &modelviewMatrix) const {
223  modelviewMatrix=this->modelviewMatrix;
224  }
225 
226  /**
227  * Get the projection matrix
228  */
229  void getProjectionMatrix(Matrix<float, 4> &projectionMatrix) const {
230  projectionMatrix=this->projectionMatrix;
231  }
232 
233  /**
234  * Get the transform matrix : transformMatrix = projectionMatrix * modelviewMatrix
235  */
236  void getTransformMatrix(Matrix<float, 4> &transformMatrix) const {
237  transformMatrix=this->transformMatrix;
238  }
239 
240  /**
241  * Get the projection and the modelview matrix generated with the given viewport
242  */
243  void getProjAndMVMatrix(const Vector<int, 4>& viewport,Matrix<float, 4> &projectionMatrix,Matrix<float, 4> &modelviewMatrix) const;
244 
245  /**
246  * Get the transform matrix generated with the given viewport
247  */
248  void getTransformMatrix(const Vector<int, 4>& viewport,Matrix<float, 4> &transformMatrix) const;
249 
250  /**
251  * @brief Init Gl parameters
252  */
253  void initGl();
254 
255  /**
256  * @brief Init light
257  */
258  void initLight();
259 
260  /**
261  * @brief Init projection with the gived viewport. Load identity matrix if reset is set as true
262  */
263  void initProjection(const Vector<int, 4>& viewport,bool reset=true);
264 
265  /**
266  * @brief Init projection with the scene viewport. Load identity matrix if reset is set as true
267  */
268  void initProjection(bool reset=true);
269 
270  /**
271  * @brief Init modelview
272  */
273  void initModelView();
274 
275  /**
276  * @brief Set the scene radius
277  */
278  void setSceneRadius(double sceneRadius,const BoundingBox sceneBoundingBox=BoundingBox());
279 
280 ///@endcond
281 
282 private:
283 
284  bool matrixCoherent;
285 
286  Coord center,eyes,up;
287  double zoomFactor;
288  double sceneRadius;
289  BoundingBox sceneBoundingBox;
290 
291  GlScene* scene;
292 
293  Matrix<float, 4> modelviewMatrix;
294  Matrix<float, 4> projectionMatrix;
295  Matrix<float, 4> transformMatrix;
296 
297  bool d3;
298 
299 };
300 
301 }
302 
303 #endif