Tulip  4.0.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 Return the camera bounding box
90  *
91  * This bounding box is the part of the scene visualized by this camera.
92  */
93  BoundingBox getBoundingBox() const;
94 
95  /**
96  * @brief This function moves the camera forward or backward depending on the speed
97  */
98  void move(float speed);
99  /**
100  * @brief This function strafes the camera left and right depending on the speed (-/+)
101  */
102  void strafeLeftRight(float speed);
103  /**
104  * @brief This function strafes the camera up and down depending on the speed (-/+)
105  */
106  void strafeUpDown(float speed);
107  /**
108  * @brief This function rotates the camera's eyes around the center depending on the values passed in.
109  */
110  void rotate(float angle, float x, float y, float z);
111 
112  /**
113  * @brief Return if the camera is a 3D one
114  */
115  bool is3D() const {
116  return d3;
117  }
118 
119  /**
120  * @brief Return the viewport of the attached scene
121  */
122  Vector<int, 4> getViewport() const;
123 
124  /**
125  * @brief Return the scene radius
126  */
127  double getSceneRadius() const {
128  return sceneRadius;
129  }
130 
131  /**
132  * @brief Set the zoom factor
133  *
134  * level of zoom of the camera
135  */
136  void setZoomFactor(double zoomFactor);
137  /**
138  * @brief Return the zoom factor
139  *
140  * level of zoom of the camera
141  */
142  double getZoomFactor() const {
143  return zoomFactor;
144  }
145 
146  /**
147  * @brief Set the eye
148  *
149  * 3D position of the camera
150  */
151  void setEyes(const Coord& eyes);
152  /**
153  * @brief Return the eyes
154  *
155  * 3D position of the camera
156  */
157  Coord getEyes() const {
158  return eyes;
159  }
160 
161  /**
162  * @brief Set the center
163  *
164  * 3D coordinates of point visualized by the camera
165  */
166  void setCenter(const Coord& center);
167  /**
168  * @brief Return the center
169  *
170  * 3D coordinates of point visualized by the camera
171  */
172  Coord getCenter() const {
173  return center;
174  }
175 
176  /**
177  * @brief Set the up vector
178  *
179  * normalized up 3D coordinates of the camera
180  */
181  void setUp(const Coord& up);
182  /**
183  * @brief Return the up vector
184  *
185  * normalized up 3D coordinates of the camera
186  */
187  Coord getUp() const {
188  return up;
189  }
190 
191  /**
192  * @brief Return the 3D world coordinate for the given screen point
193  * \warning This function set up the projection and modelview matrix
194  */
195  Coord screenTo3DWorld(const Coord &point) const;
196 
197  /**
198  * @brief Return the screen position for the given 3D coordinate
199  * \warning This function set up the projection and modelview matrix
200  */
201  Coord worldTo2DScreen(const Coord &obj) const;
202 
203  /**
204  * @brief Function to export data in outString (in XML format)
205  */
206  virtual void getXML(std::string &outString);
207 
208  /**
209  * @brief Function to set data with inString (in XML format)
210  */
211  virtual void setWithXML(const std::string &inString, unsigned int &currentPosition);
212 
213 ///@cond DOXYGEN_HIDDEN
214 
215  /**
216  * Get the modelview matrix
217  */
218  void getModelviewMatrix(Matrix<float, 4> &modelviewMatrix) const {
219  modelviewMatrix=this->modelviewMatrix;
220  }
221 
222  /**
223  * Get the projection matrix
224  */
225  void getProjectionMatrix(Matrix<float, 4> &projectionMatrix) const {
226  projectionMatrix=this->projectionMatrix;
227  }
228 
229  /**
230  * Get the transform matrix : transformMatrix = projectionMatrix * modelviewMatrix
231  */
232  void getTransformMatrix(Matrix<float, 4> &transformMatrix) const {
233  transformMatrix=this->transformMatrix;
234  }
235 
236  /**
237  * Get the projection and the modelview matrix generated with the given viewport
238  */
239  void getProjAndMVMatrix(const Vector<int, 4>& viewport,Matrix<float, 4> &projectionMatrix,Matrix<float, 4> &modelviewMatrix) const;
240 
241  /**
242  * Get the transform matrix generated with the given viewport
243  */
244  void getTransformMatrix(const Vector<int, 4>& viewport,Matrix<float, 4> &transformMatrix) const;
245 
246  /**
247  * @brief Init Gl parameters
248  */
249  void initGl();
250 
251  /**
252  * @brief Init light
253  */
254  void initLight();
255 
256  /**
257  * @brief Init projection with the gived viewport. Load identity matrix if reset is set as true
258  */
259  void initProjection(const Vector<int, 4>& viewport,bool reset=true);
260 
261  /**
262  * @brief Init projection with the scene viewport. Load identity matrix if reset is set as true
263  */
264  void initProjection(bool reset=true);
265 
266  /**
267  * @brief Init modelview
268  */
269  void initModelView();
270 
271  /**
272  * @brief Set the scene radius
273  */
274  void setSceneRadius(double sceneRadius,const BoundingBox sceneBoundingBox=BoundingBox());
275 
276 ///@endcond
277 
278 private:
279 
280  bool matrixCoherent;
281 
282  Coord center,eyes,up;
283  double zoomFactor;
284  double sceneRadius;
285  BoundingBox sceneBoundingBox;
286 
287  GlScene* scene;
288 
289  Matrix<float, 4> modelviewMatrix;
290  Matrix<float, 4> projectionMatrix;
291  Matrix<float, 4> transformMatrix;
292 
293  bool d3;
294 
295 };
296 
297 }
298 
299 #endif