All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Camera.hh
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Nate Koenig
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 /* Desc: A persepective OGRE Camera
18  * Author: Nate Koenig
19  * Date: 15 July 2003
20  */
21 
22 #ifndef _RENDERING_CAMERA_HH_
23 #define _RENDERING_CAMERA_HH_
24 
25 #include <boost/enable_shared_from_this.hpp>
26 #include <string>
27 #include <utility>
28 #include <list>
29 #include <vector>
30 #include <deque>
31 
32 #include "common/Event.hh"
33 #include "common/Time.hh"
34 
35 #include "math/Angle.hh"
36 #include "math/Pose.hh"
37 #include "math/Plane.hh"
38 #include "math/Vector2i.hh"
39 
40 #include "msgs/MessageTypes.hh"
41 #include "rendering/RenderTypes.hh"
42 #include "sdf/sdf.hh"
43 
44 // Forward Declarations
45 namespace Ogre
46 {
47  class Texture;
48  class RenderTarget;
49  class Camera;
50  class Viewport;
51  class SceneNode;
52  class AnimationState;
53  class CompositorInstance;
54 }
55 
56 namespace gazebo
57 {
60  namespace rendering
61  {
62  class MouseEvent;
63  class ViewController;
64  class Scene;
65 
69 
74  class Camera : public boost::enable_shared_from_this<Camera>
75  {
80  public: Camera(const std::string &_namePrefix, Scene *_scene,
81  bool _autoRender = true);
82 
84  public: virtual ~Camera();
85 
88  public: void Load(sdf::ElementPtr _sdf);
89 
91  public: void Load();
92 
94  public: void Init();
95 
98  public: void SetRenderRate(double _hz);
99 
104  public: void Render();
105 
109  public: virtual void PostRender();
110 
116  public: virtual void Update();
117 
121  public: void Fini();
122 
125  public: inline bool IsInitialized() const {return this->initialized;}
126 
130  public: void SetWindowId(unsigned int _windowId);
131 
134  public: unsigned int GetWindowId() const;
135 
138  public: void SetScene(Scene *_scene);
139 
142  public: math::Pose GetWorldPose();
143 
146  public: math::Vector3 GetWorldPosition() const;
147 
150  public: math::Quaternion GetWorldRotation() const;
151 
154  public: virtual void SetWorldPose(const math::Pose &_pose);
155 
158  public: void SetWorldPosition(const math::Vector3 &_pos);
159 
162  public: void SetWorldRotation(const math::Quaternion &_quat);
163 
166  public: void Translate(const math::Vector3 &_direction);
167 
170  public: void RotateYaw(math::Angle _angle);
171 
174  public: void RotatePitch(math::Angle _angle);
175 
179  public: void SetClipDist(float _near, float _far);
180 
183  public: void SetHFOV(math::Angle _angle);
184 
187  public: math::Angle GetHFOV() const;
188 
191  public: math::Angle GetVFOV() const;
192 
196  public: void SetImageSize(unsigned int _w, unsigned int _h);
197 
200  public: void SetImageWidth(unsigned int _w);
201 
204  public: void SetImageHeight(unsigned int _h);
205 
208  public: unsigned int GetImageWidth() const;
209 
212  public: unsigned int GetTextureWidth() const;
213 
216  public: unsigned int GetImageHeight() const;
217 
220  public: unsigned int GetImageDepth() const;
221 
224  public: std::string GetImageFormat() const;
225 
228  public: unsigned int GetTextureHeight() const;
229 
232  public: size_t GetImageByteSize() const;
233 
239  public: static size_t GetImageByteSize(unsigned int _width,
240  unsigned int _height,
241  const std::string &_format);
242 
248  public: double GetZValue(int _x, int _y);
249 
252  public: double GetNearClip();
253 
256  public: double GetFarClip();
257 
260  public: void EnableSaveFrame(bool _enable);
261 
264  public: void SetSaveFramePathname(const std::string &_pathname);
265 
269  public: bool SaveFrame(const std::string &_filename);
270 
273  public: Ogre::Camera *GetOgreCamera() const;
274 
277  public: Ogre::Viewport *GetViewport() const;
278 
281  public: unsigned int GetViewportWidth() const;
282 
285  public: unsigned int GetViewportHeight() const;
286 
289  public: math::Vector3 GetUp();
290 
293  public: math::Vector3 GetRight();
294 
297  public: virtual float GetAvgFPS() {return 0;}
298 
301  public: virtual unsigned int GetTriangleCount() {return 0;}
302 
305  public: void SetAspectRatio(float _ratio);
306 
309  public: float GetAspectRatio() const;
310 
313  public: void SetSceneNode(Ogre::SceneNode *_node);
314 
317  public: Ogre::SceneNode *GetSceneNode() const;
318 
324  public: virtual const unsigned char *GetImageData(unsigned int i = 0);
325 
328  public: std::string GetName() const;
329 
332  public: void SetName(const std::string &_name);
333 
335  public: void ToggleShowWireframe();
336 
339  public: void ShowWireframe(bool _s);
340 
348  public: void GetCameraToViewportRay(int _screenx, int _screeny,
349  math::Vector3 &_origin,
350  math::Vector3 &_dir);
351 
354  public: void SetCaptureData(bool _value);
355 
358  public: void CreateRenderTexture(const std::string &_textureName);
359 
362  public: Scene *GetScene() const;
363 
370  public: bool GetWorldPointOnPlane(int _x, int _y,
371  const math::Plane &_plane, math::Vector3 &_result);
372 
375  public: virtual void SetRenderTarget(Ogre::RenderTarget *_target);
376 
385  public: void AttachToVisual(const std::string &_visualName,
386  bool _inheritOrientation,
387  double _minDist = 0.0, double _maxDist = 0.0);
388 
391  public: void TrackVisual(const std::string &_visualName);
392 
395  public: Ogre::Texture *GetRenderTexture() const;
396 
399  public: math::Vector3 GetDirection() const;
400 
405  public: template<typename T>
407  {return newImageFrame.Connect(_subscriber);}
408 
413 
422  public: static bool SaveFrame(const unsigned char *_image,
423  unsigned int _width, unsigned int _height, int _depth,
424  const std::string &_format,
425  const std::string &_filename);
426 
427 
431 
436  public: bool IsVisible(VisualPtr _visual);
437 
442  public: bool IsVisible(const std::string &_visualName);
443 
446  public: bool GetInitialized() const;
447 
451  public: virtual bool MoveToPosition(const math::Pose &_pose,
452  double _time);
453 
459  public: bool MoveToPositions(const std::vector<math::Pose> &_pts,
460  double _time,
461  boost::function<void()> _onComplete = NULL);
462 
464  protected: virtual void RenderImpl();
465 
469  protected: bool TrackVisualImpl(const std::string &_visualName);
470 
474  protected: virtual bool TrackVisualImpl(VisualPtr _visual);
475 
485  protected: virtual bool AttachToVisualImpl(const std::string &_name,
486  bool _inheritOrientation,
487  double _minDist = 0, double _maxDist = 0);
488 
498  protected: virtual bool AttachToVisualImpl(VisualPtr _visual,
499  bool _inheritOrientation,
500  double _minDist = 0, double _maxDist = 0);
501 
509  private: void ConvertRGBToBAYER(unsigned char *_dst, unsigned char *_src,
510  std::string _format, int _width, int _height);
511 
513  private: void SetClipDist();
514 
518  private: static int GetOgrePixelFormat(const std::string &_format);
519 
522  protected: std::string GetFrameFilename();
523 
525  private: void CreateCamera();
526 
527  protected: std::string name;
528 
529  protected: sdf::ElementPtr sdf;
530 
531  protected: unsigned int windowId;
532 
533  protected: unsigned int textureWidth, textureHeight;
534 
535  protected: Ogre::Camera *camera;
536  protected: Ogre::Viewport *viewport;
537  protected: Ogre::SceneNode *sceneNode;
538  protected: Ogre::SceneNode *pitchNode;
539 
540  // Info for saving images
541  protected: unsigned char *saveFrameBuffer;
542  protected: unsigned char *bayerFrameBuffer;
543  protected: unsigned int saveCount;
544 
545  protected: int imageFormat;
546  protected: int imageWidth, imageHeight;
547 
548  protected: Ogre::RenderTarget *renderTarget;
549 
550  protected: Ogre::Texture *renderTexture;
551 
552  private: static unsigned int cameraCounter;
553  private: unsigned int myCount;
554 
555  protected: bool captureData;
556 
557  protected: bool newData;
558 
560 
561  protected: Scene *scene;
562 
563  protected: event::EventT<void(const unsigned char *,
564  unsigned int, unsigned int, unsigned int,
565  const std::string &)> newImageFrame;
566 
567  protected: std::vector<event::ConnectionPtr> connections;
568  protected: std::list<msgs::Request> requests;
569  private: friend class Scene;
570 
571  private: sdf::ElementPtr imageElem;
572 
573  protected: bool initialized;
574  private: VisualPtr trackedVisual;
575 
576  protected: Ogre::AnimationState *animState;
578  protected: boost::function<void()> onAnimationComplete;
579 
580  private: Ogre::CompositorInstance *dsGBufferInstance;
581  private: Ogre::CompositorInstance *dsMergeInstance;
582 
583  private: Ogre::CompositorInstance *dlGBufferInstance;
584  private: Ogre::CompositorInstance *dlMergeInstance;
585 
586  private: Ogre::CompositorInstance *ssaoInstance;
587 
588  private: std::deque<std::pair<math::Pose, double> > moveToPositionQueue;
589  };
591  }
592 }
593 #endif