Tulip  5.2.0
Large graphs analysis and drawing
Camera.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
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,
40  * setCenter and setUp
41  * - You can transform viewport coordinates to 3D world coordinates with viewportTo3DWorld()
42  * function and 3D world coordinates to viewport coordinates with worldTo2DViewport() function
43  * A camera is a main component of GlLayer and GlScene
44  * @see GlLayer
45  * @see GlScene
46  */
47 class TLP_GL_SCOPE Camera : public Observable {
48 public:
49  /**
50  * @brief Constructor
51  * @param scene A layer is attached to a scene so we have to specify it in the constructor
52  * @param center 3D coordinates of point visualized by the camera
53  * @param eye 3D position of the camera
54  * @param up normalized up 3D coordinates of the camera
55  * @param zoomFactor level of zoom of the camera
56  * @param sceneRadius scene radius of the camera
57  */
58  Camera(GlScene *scene, Coord center = Coord(0, 0, 0), Coord eyes = Coord(0, 0, 10),
59  Coord up = Coord(0, -1, 0), 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() override;
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
113  * in.
114  */
115  void rotate(float angle, float x, float y, float z);
116 
117  /**
118  * @brief Return if the camera is a 3D one
119  */
120  bool is3D() const {
121  return d3;
122  }
123 
124  /**
125  * @brief Return the viewport of the attached scene
126  */
127  Vector<int, 4> getViewport() const;
128 
129  /**
130  * @brief Return the scene radius
131  */
132  double getSceneRadius() const {
133  return sceneRadius;
134  }
135 
136  /**
137  * @brief Set the zoom factor
138  *
139  * level of zoom of the camera
140  */
141  void setZoomFactor(double zoomFactor);
142  /**
143  * @brief Return the zoom factor
144  *
145  * level of zoom of the camera
146  */
147  double getZoomFactor() const {
148  return zoomFactor;
149  }
150 
151  /**
152  * @brief Set the eye
153  *
154  * 3D position of the camera
155  */
156  void setEyes(const Coord &eyes);
157  /**
158  * @brief Return the eyes
159  *
160  * 3D position of the camera
161  */
162  Coord getEyes() const {
163  return eyes;
164  }
165 
166  /**
167  * @brief Set the center
168  *
169  * 3D coordinates of point visualized by the camera
170  */
171  void setCenter(const Coord &center);
172  /**
173  * @brief Return the center
174  *
175  * 3D coordinates of point visualized by the camera
176  */
177  Coord getCenter() const {
178  return center;
179  }
180 
181  /**
182  * @brief Set the up vector
183  *
184  * normalized up 3D coordinates of the camera
185  */
186  void setUp(const Coord &up);
187  /**
188  * @brief Return the up vector
189  *
190  * normalized up 3D coordinates of the camera
191  */
192  Coord getUp() const {
193  return up;
194  }
195 
196  /**
197  * @brief Return the 3D world coordinate for the given viewport point
198  * \warning This function set up the projection and modelview matrix
199  */
200  Coord viewportTo3DWorld(const Coord &point) const;
201 
202  /**
203  * @brief Return the 3D world coordinate for the given viewport point
204  * \warning This function set up the projection and modelview matrix
205  */
206  Coord screenTo3DWorld(const Coord &point) const {
207  return viewportTo3DWorld(point);
208  }
209 
210  /**
211  * @brief Return the viewport position for the given 3D coordinate
212  * \warning This function set up the projection and modelview matrix
213  */
214  Coord worldTo2DViewport(const Coord &obj) const;
215 
216  /**
217  * @brief Return the viewport position for the given 3D coordinate
218  * \warning This function set up the projection and modelview matrix
219  */
220  Coord worldTo2DScreen(const Coord &obj) const {
221  return worldTo2DViewport(obj);
222  }
223 
224  /**
225  * @brief Function to export data in outString (in XML format)
226  */
227  virtual void getXML(std::string &outString);
228 
229  /**
230  * @brief Function to set data with inString (in XML format)
231  */
232  virtual void setWithXML(const std::string &inString, unsigned int &currentPosition);
233 
234  ///@cond DOXYGEN_HIDDEN
235 
236  /**
237  * Get the modelview matrix
238  */
239  void getModelviewMatrix(Matrix<float, 4> &modelviewMatrix) const {
240  modelviewMatrix = this->modelviewMatrix;
241  }
242 
243  /**
244  * Get the projection matrix
245  */
246  void getProjectionMatrix(Matrix<float, 4> &projectionMatrix) const {
247  projectionMatrix = this->projectionMatrix;
248  }
249 
250  /**
251  * Get the transform matrix : transformMatrix = projectionMatrix * modelviewMatrix
252  */
253  void getTransformMatrix(Matrix<float, 4> &transformMatrix) const {
254  transformMatrix = this->transformMatrix;
255  }
256 
257  /**
258  * Get the projection and the modelview matrix generated with the given viewport
259  */
260  void getProjAndMVMatrix(const Vector<int, 4> &viewport, Matrix<float, 4> &projectionMatrix,
261  Matrix<float, 4> &modelviewMatrix) const;
262 
263  /**
264  * Get the transform matrix generated with the given viewport
265  */
266  void getTransformMatrix(const Vector<int, 4> &viewport, Matrix<float, 4> &transformMatrix) const;
267 
268  /**
269  * @brief Init Gl parameters
270  */
271  void initGl();
272 
273  /**
274  * @brief Init light
275  */
276  void initLight();
277 
278  /**
279  * @brief Init projection with the gived viewport. Load identity matrix if reset is set as true
280  */
281  void initProjection(const Vector<int, 4> &viewport, bool reset = true);
282 
283  /**
284  * @brief Init projection with the scene viewport. Load identity matrix if reset is set as true
285  */
286  void initProjection(bool reset = true);
287 
288  /**
289  * @brief Init modelview
290  */
291  void initModelView();
292 
293  /**
294  * @brief Set the scene radius
295  */
296  void setSceneRadius(double sceneRadius, const BoundingBox sceneBoundingBox = BoundingBox());
297 
298  ///@endcond
299 
300 private:
301  bool matrixCoherent;
302 
303  Coord center, eyes, up;
304  double zoomFactor;
305  double sceneRadius;
306  BoundingBox sceneBoundingBox;
307 
308  GlScene *scene;
309 
310  Matrix<float, 4> modelviewMatrix;
311  Matrix<float, 4> projectionMatrix;
312  Matrix<float, 4> transformMatrix;
313 
314  bool d3;
315 };
316 }
317 
318 #endif
GlScene * getScene() const
Return the camera&#39;s scene.
Definition: Camera.h:81
bool is3D() const
Return if the camera is a 3D one.
Definition: Camera.h:120
double getZoomFactor() const
Return the zoom factor.
Definition: Camera.h:147
void loadCameraParametersWith(const Camera &camera)
Load this camera parameters (eye, center, zoom factor) with an other camera parameters.
Definition: Camera.h:88
Coord getUp() const
Return the up vector.
Definition: Camera.h:192
Coord getCenter() const
Return the center.
Definition: Camera.h:177
double getSceneRadius() const
Return the scene radius.
Definition: Camera.h:132
Tulip scene class.
Definition: GlScene.h:160
Coord screenTo3DWorld(const Coord &point) const
Return the 3D world coordinate for the given viewport point.
Definition: Camera.h:206
This class represents the 3D bounding box of an object. It is mostly used to determine whether or not...
Definition: BoundingBox.h:66
Tulip OpenGL camera object.
Definition: Camera.h:47
Coord getEyes() const
Return the eyes.
Definition: Camera.h:162
The Observable class is the base of Tulip&#39;s observation system.
Definition: Observable.h:141
Coord worldTo2DScreen(const Coord &obj) const
Return the viewport position for the given 3D coordinate.
Definition: Camera.h:220