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