Tulip  5.1.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, setCenter and setUp
40  * - You can transform viewport coordinates to 3D world coordinates with viewportTo3DWorld() function and 3D world coordinates to viewport coordinates with worldTo2DViewport() 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 viewport point
197  * \warning This function set up the projection and modelview matrix
198  */
199  Coord viewportTo3DWorld(const Coord &point) const;
200 
201  /**
202  * @brief Return the 3D world coordinate for the given viewport point
203  * \warning This function set up the projection and modelview matrix
204  */
205  Coord screenTo3DWorld(const Coord &point) const {
206  return viewportTo3DWorld(point);
207  }
208 
209  /**
210  * @brief Return the viewport position for the given 3D coordinate
211  * \warning This function set up the projection and modelview matrix
212  */
213  Coord worldTo2DViewport(const Coord &obj) const;
214 
215  /**
216  * @brief Return the viewport position for the given 3D coordinate
217  * \warning This function set up the projection and modelview matrix
218  */
219  Coord worldTo2DScreen(const Coord &obj) const {
220  return worldTo2DViewport(obj);
221  }
222 
223  /**
224  * @brief Function to export data in outString (in XML format)
225  */
226  virtual void getXML(std::string &outString);
227 
228  /**
229  * @brief Function to set data with inString (in XML format)
230  */
231  virtual void setWithXML(const std::string &inString, unsigned int &currentPosition);
232 
233 ///@cond DOXYGEN_HIDDEN
234 
235  /**
236  * Get the modelview matrix
237  */
238  void getModelviewMatrix(Matrix<float, 4> &modelviewMatrix) const {
239  modelviewMatrix=this->modelviewMatrix;
240  }
241 
242  /**
243  * Get the projection matrix
244  */
245  void getProjectionMatrix(Matrix<float, 4> &projectionMatrix) const {
246  projectionMatrix=this->projectionMatrix;
247  }
248 
249  /**
250  * Get the transform matrix : transformMatrix = projectionMatrix * modelviewMatrix
251  */
252  void getTransformMatrix(Matrix<float, 4> &transformMatrix) const {
253  transformMatrix=this->transformMatrix;
254  }
255 
256  /**
257  * Get the projection and the modelview matrix generated with the given viewport
258  */
259  void getProjAndMVMatrix(const Vector<int, 4>& viewport,Matrix<float, 4> &projectionMatrix,Matrix<float, 4> &modelviewMatrix) const;
260 
261  /**
262  * Get the transform matrix generated with the given viewport
263  */
264  void getTransformMatrix(const Vector<int, 4>& viewport,Matrix<float, 4> &transformMatrix) const;
265 
266  /**
267  * @brief Init Gl parameters
268  */
269  void initGl();
270 
271  /**
272  * @brief Init light
273  */
274  void initLight();
275 
276  /**
277  * @brief Init projection with the gived viewport. Load identity matrix if reset is set as true
278  */
279  void initProjection(const Vector<int, 4>& viewport,bool reset=true);
280 
281  /**
282  * @brief Init projection with the scene viewport. Load identity matrix if reset is set as true
283  */
284  void initProjection(bool reset=true);
285 
286  /**
287  * @brief Init modelview
288  */
289  void initModelView();
290 
291  /**
292  * @brief Set the scene radius
293  */
294  void setSceneRadius(double sceneRadius,const BoundingBox sceneBoundingBox=BoundingBox());
295 
296 ///@endcond
297 
298 private:
299 
300  bool matrixCoherent;
301 
302  Coord center,eyes,up;
303  double zoomFactor;
304  double sceneRadius;
305  BoundingBox sceneBoundingBox;
306 
307  GlScene* scene;
308 
309  Matrix<float, 4> modelviewMatrix;
310  Matrix<float, 4> projectionMatrix;
311  Matrix<float, 4> transformMatrix;
312 
313  bool d3;
314 
315 };
316 
317 }
318 
319 #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:119
double getZoomFactor() const
Return the zoom factor.
Definition: Camera.h:146
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:191
Coord getCenter() const
Return the center.
Definition: Camera.h:176
double getSceneRadius() const
Return the scene radius.
Definition: Camera.h:131
Tulip scene class.
Definition: GlScene.h:153
Coord screenTo3DWorld(const Coord &point) const
Return the 3D world coordinate for the given viewport point.
Definition: Camera.h:205
This class represents the 3D bounding box of an object. It is mostly used to determine whether or not...
Definition: BoundingBox.h:63
Tulip OpenGL camera object.
Definition: Camera.h:45
Coord getEyes() const
Return the eyes.
Definition: Camera.h:161
The Observable class is the base of Tulip&#39;s observation system.
Definition: Observable.h:123
Coord worldTo2DScreen(const Coord &obj) const
Return the viewport position for the given 3D coordinate.
Definition: Camera.h:219