Tulip  5.6.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 point 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(const Camera &) = default;
66 
67  Camera &operator=(const Camera &camera);
68 
69  /**
70  * @brief Destructor
71  */
72  ~Camera() override;
73 
74  /**
75  * @brief Set the camera's scene
76  * The viewport is store in the scene, so we must attach camera to a scene
77  */
78  void setScene(GlScene *scene);
79 
80  /**
81  * @brief Return the camera's scene
82  */
83  GlScene *getScene() const {
84  return scene;
85  }
86 
87  /**
88  * @brief Load this camera parameters (eye, center, zoom factor) with an other camera parameters
89  */
90  void loadCameraParametersWith(const Camera &camera) {
91  *this = camera;
92  }
93 
94  /**
95  * @brief Return the camera bounding box
96  *
97  * This bounding box is the part of the scene visualized by this camera.
98  */
99  BoundingBox getBoundingBox() const;
100 
101  /**
102  * @brief This function moves the camera forward or backward depending on the speed
103  */
104  void move(float speed);
105  /**
106  * @brief This function strafes the camera left and right depending on the speed (-/+)
107  */
108  void strafeLeftRight(float speed);
109  /**
110  * @brief This function strafes the camera up and down depending on the speed (-/+)
111  */
112  void strafeUpDown(float speed);
113  /**
114  * @brief This function rotates the camera's eyes around the center depending on the values passed
115  * 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  const 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  const 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  const 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  const Coord &getUp() const {
195  return up;
196  }
197 
198  /**
199  * @brief Return the 3D world coordinate for the given viewport point
200  * \warning This function set up the projection and modelview matrix
201  */
202  Coord viewportTo3DWorld(const Coord &point) const;
203 
204  /**
205  * @brief Return the 3D world coordinate for the given viewport point
206  * \warning This function set up the projection and modelview matrix
207  */
208  Coord screenTo3DWorld(const Coord &point) const {
209  return viewportTo3DWorld(point);
210  }
211 
212  /**
213  * @brief Return the viewport position for the given 3D coordinate
214  * \warning This function set up the projection and modelview matrix
215  */
216  Coord worldTo2DViewport(const Coord &obj) const;
217 
218  /**
219  * @brief Return the viewport position for the given 3D coordinate
220  * \warning This function set up the projection and modelview matrix
221  */
222  Coord worldTo2DScreen(const Coord &obj) const {
223  return worldTo2DViewport(obj);
224  }
225 
226  /**
227  * @brief Function to export data in outString (in XML format)
228  */
229  virtual void getXML(std::string &outString);
230 
231  /**
232  * @brief Function to set data with inString (in XML format)
233  */
234  virtual void setWithXML(const std::string &inString, unsigned int &currentPosition);
235 
236  ///@cond DOXYGEN_HIDDEN
237 
238  /**
239  * Get the modelview matrix
240  */
241  void getModelviewMatrix(Matrix<float, 4> &modelviewMatrix) const {
242  modelviewMatrix = this->modelviewMatrix;
243  }
244 
245  /**
246  * Get the projection matrix
247  */
248  void getProjectionMatrix(Matrix<float, 4> &projectionMatrix) const {
249  projectionMatrix = this->projectionMatrix;
250  }
251 
252  /**
253  * Get the transform matrix : transformMatrix = projectionMatrix * modelviewMatrix
254  */
255  void getTransformMatrix(Matrix<float, 4> &transformMatrix) const {
256  transformMatrix = this->transformMatrix;
257  }
258 
259  /**
260  * Get the projection and the modelview matrix generated with the given viewport
261  */
262  void getProjAndMVMatrix(const Vector<int, 4> &viewport, Matrix<float, 4> &projectionMatrix,
263  Matrix<float, 4> &modelviewMatrix) const;
264 
265  /**
266  * Get the transform matrix generated with the given viewport
267  */
268  void getTransformMatrix(const Vector<int, 4> &viewport, Matrix<float, 4> &transformMatrix) const;
269 
270  /**
271  * @brief Init Gl parameters
272  */
273  void initGl();
274 
275  /**
276  * @brief Init light
277  */
278  void initLight();
279 
280  /**
281  * @brief Init projection with the given viewport. Load identity matrix if reset is set as true
282  */
283  void initProjection(const Vector<int, 4> &viewport, bool reset = true);
284 
285  /**
286  * @brief Init projection with the scene viewport. Load identity matrix if reset is set as true
287  */
288  void initProjection(bool reset = true);
289 
290  /**
291  * @brief Init modelview
292  */
293  void initModelView();
294 
295  /**
296  * @brief Set the scene radius
297  */
298  void setSceneRadius(double sceneRadius, const BoundingBox sceneBoundingBox = BoundingBox());
299 
300  ///@endcond
301 
302 private:
303  bool matrixCoherent;
304 
305  Coord center, eyes, up;
306  double zoomFactor;
307  double sceneRadius;
308  BoundingBox sceneBoundingBox;
309 
310  GlScene *scene;
311 
312  Matrix<float, 4> modelviewMatrix;
313  Matrix<float, 4> projectionMatrix;
314  Matrix<float, 4> transformMatrix;
315 
316  bool d3;
317 };
318 } // namespace tlp
319 
320 #endif
tlp::Camera::getCenter
const Coord & getCenter() const
Return the center.
Definition: Camera.h:179
tlp::Camera::screenTo3DWorld
Coord screenTo3DWorld(const Coord &point) const
Return the 3D world coordinate for the given viewport point.
Definition: Camera.h:208
tlp::Camera::getUp
const Coord & getUp() const
Return the up vector.
Definition: Camera.h:194
tlp::Camera::worldTo2DScreen
Coord worldTo2DScreen(const Coord &obj) const
Return the viewport position for the given 3D coordinate.
Definition: Camera.h:222
tlp::Camera::getEyes
const Coord & getEyes() const
Return the eyes.
Definition: Camera.h:164
tlp::Camera::getScene
GlScene * getScene() const
Return the camera's scene.
Definition: Camera.h:83
tlp::Camera::getZoomFactor
double getZoomFactor() const
Return the zoom factor.
Definition: Camera.h:149
tlp::Camera::getSceneRadius
double getSceneRadius() const
Return the scene radius.
Definition: Camera.h:134
tlp::Camera::loadCameraParametersWith
void loadCameraParametersWith(const Camera &camera)
Load this camera parameters (eye, center, zoom factor) with an other camera parameters.
Definition: Camera.h:90
tlp::BoundingBox
This class represents the 3D bounding box of an object. It is mostly used to determine whether or not...
Definition: BoundingBox.h:67
tlp::Camera::is3D
bool is3D() const
Return if the camera is a 3D one.
Definition: Camera.h:122
tlp
Definition: AbstractProperty.h:34
tlp::GlScene
Tulip scene class.
Definition: GlScene.h:144
tlp::Observable
The Observable class is the base of Tulip's observation system.
Definition: Observable.h:127
tlp::Camera
Tulip OpenGL camera object.
Definition: Camera.h:47