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 2012 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: virtual void Load(sdf::ElementPtr _sdf);
89 
91  public: virtual void Load();
92 
94  public: virtual void Init();
95 
98  public: void SetRenderRate(double _hz);
99 
102  public: double GetRenderRate() const;
103 
108  public: void Render();
109 
113  public: virtual void PostRender();
114 
120  public: virtual void Update();
121 
125  public: virtual void Fini();
126 
129  public: inline bool IsInitialized() const {return this->initialized;}
130 
134  public: void SetWindowId(unsigned int _windowId);
135 
138  public: unsigned int GetWindowId() const;
139 
142  public: void SetScene(Scene *_scene);
143 
146  public: math::Pose GetWorldPose();
147 
150  public: math::Vector3 GetWorldPosition() const;
151 
154  public: math::Quaternion GetWorldRotation() const;
155 
158  public: virtual void SetWorldPose(const math::Pose &_pose);
159 
162  public: void SetWorldPosition(const math::Vector3 &_pos);
163 
166  public: void SetWorldRotation(const math::Quaternion &_quat);
167 
170  public: void Translate(const math::Vector3 &_direction);
171 
174  public: void RotateYaw(math::Angle _angle);
175 
178  public: void RotatePitch(math::Angle _angle);
179 
183  public: void SetClipDist(float _near, float _far);
184 
187  public: void SetHFOV(math::Angle _angle);
188 
191  public: math::Angle GetHFOV() const;
192 
195  public: math::Angle GetVFOV() const;
196 
200  public: void SetImageSize(unsigned int _w, unsigned int _h);
201 
204  public: void SetImageWidth(unsigned int _w);
205 
208  public: void SetImageHeight(unsigned int _h);
209 
212  public: unsigned int GetImageWidth() const;
213 
216  public: unsigned int GetTextureWidth() const;
217 
220  public: unsigned int GetImageHeight() const;
221 
224  public: unsigned int GetImageDepth() const;
225 
228  public: std::string GetImageFormat() const;
229 
232  public: unsigned int GetTextureHeight() const;
233 
236  public: size_t GetImageByteSize() const;
237 
243  public: static size_t GetImageByteSize(unsigned int _width,
244  unsigned int _height,
245  const std::string &_format);
246 
252  public: double GetZValue(int _x, int _y);
253 
256  public: double GetNearClip();
257 
260  public: double GetFarClip();
261 
264  public: void EnableSaveFrame(bool _enable);
265 
268  public: void SetSaveFramePathname(const std::string &_pathname);
269 
273  public: bool SaveFrame(const std::string &_filename);
274 
277  public: Ogre::Camera *GetOgreCamera() const;
278 
281  public: Ogre::Viewport *GetViewport() const;
282 
285  public: unsigned int GetViewportWidth() const;
286 
289  public: unsigned int GetViewportHeight() const;
290 
293  public: math::Vector3 GetUp();
294 
297  public: math::Vector3 GetRight();
298 
301  public: virtual float GetAvgFPS() {return 0;}
302 
305  public: virtual unsigned int GetTriangleCount() {return 0;}
306 
309  public: void SetAspectRatio(float _ratio);
310 
313  public: float GetAspectRatio() const;
314 
317  public: void SetSceneNode(Ogre::SceneNode *_node);
318 
321  public: Ogre::SceneNode *GetSceneNode() const;
322 
328  public: virtual const unsigned char *GetImageData(unsigned int i = 0);
329 
332  public: std::string GetName() const;
333 
336  public: void SetName(const std::string &_name);
337 
339  public: void ToggleShowWireframe();
340 
343  public: void ShowWireframe(bool _s);
344 
352  public: void GetCameraToViewportRay(int _screenx, int _screeny,
353  math::Vector3 &_origin,
354  math::Vector3 &_dir);
355 
358  public: void SetCaptureData(bool _value);
359 
362  public: void CreateRenderTexture(const std::string &_textureName);
363 
366  public: Scene *GetScene() const;
367 
374  public: bool GetWorldPointOnPlane(int _x, int _y,
375  const math::Plane &_plane, math::Vector3 &_result);
376 
379  public: virtual void SetRenderTarget(Ogre::RenderTarget *_target);
380 
389  public: void AttachToVisual(const std::string &_visualName,
390  bool _inheritOrientation,
391  double _minDist = 0.0, double _maxDist = 0.0);
392 
395  public: void TrackVisual(const std::string &_visualName);
396 
399  public: Ogre::Texture *GetRenderTexture() const;
400 
403  public: math::Vector3 GetDirection() const;
404 
409  public: template<typename T>
411  {return newImageFrame.Connect(_subscriber);}
412 
417 
426  public: static bool SaveFrame(const unsigned char *_image,
427  unsigned int _width, unsigned int _height, int _depth,
428  const std::string &_format,
429  const std::string &_filename);
430 
431 
435 
440  public: bool IsVisible(VisualPtr _visual);
441 
446  public: bool IsVisible(const std::string &_visualName);
447 
450  public: bool GetInitialized() const;
451 
456  public: virtual bool MoveToPosition(const math::Pose &_pose,
457  double _time);
458 
466  public: bool MoveToPositions(const std::vector<math::Pose> &_pts,
467  double _time,
468  boost::function<void()> _onComplete = NULL);
469 
471  protected: virtual void RenderImpl();
472 
476  protected: bool TrackVisualImpl(const std::string &_visualName);
477 
481  protected: virtual bool TrackVisualImpl(VisualPtr _visual);
482 
492  protected: virtual bool AttachToVisualImpl(const std::string &_name,
493  bool _inheritOrientation,
494  double _minDist = 0, double _maxDist = 0);
495 
505  protected: virtual bool AttachToVisualImpl(VisualPtr _visual,
506  bool _inheritOrientation,
507  double _minDist = 0, double _maxDist = 0);
508 
516  private: void ConvertRGBToBAYER(unsigned char *_dst, unsigned char *_src,
517  std::string _format, int _width, int _height);
518 
520  private: void SetClipDist();
521 
525  private: static int GetOgrePixelFormat(const std::string &_format);
526 
529  protected: std::string GetFrameFilename();
530 
532  private: void CreateCamera();
533 
534  protected: std::string name;
535 
536  protected: sdf::ElementPtr sdf;
537 
538  protected: unsigned int windowId;
539 
540  protected: unsigned int textureWidth, textureHeight;
541 
542  protected: Ogre::Camera *camera;
543  protected: Ogre::Viewport *viewport;
544  protected: Ogre::SceneNode *sceneNode;
545  protected: Ogre::SceneNode *pitchNode;
546 
547  // Info for saving images
548  protected: unsigned char *saveFrameBuffer;
549  protected: unsigned char *bayerFrameBuffer;
550  protected: unsigned int saveCount;
551 
552  protected: int imageFormat;
553  protected: int imageWidth, imageHeight;
554 
555  protected: Ogre::RenderTarget *renderTarget;
556 
557  protected: Ogre::Texture *renderTexture;
558 
559  private: static unsigned int cameraCounter;
560  private: unsigned int myCount;
561 
562  protected: bool captureData;
563 
564  protected: bool newData;
565 
567 
568  protected: Scene *scene;
569 
570  protected: event::EventT<void(const unsigned char *,
571  unsigned int, unsigned int, unsigned int,
572  const std::string &)> newImageFrame;
573 
574  protected: std::vector<event::ConnectionPtr> connections;
575  protected: std::list<msgs::Request> requests;
576  private: friend class Scene;
577 
578  private: sdf::ElementPtr imageElem;
579 
580  protected: bool initialized;
581  private: VisualPtr trackedVisual;
582 
583  protected: Ogre::AnimationState *animState;
585  protected: boost::function<void()> onAnimationComplete;
586 
587  private: Ogre::CompositorInstance *dsGBufferInstance;
588  private: Ogre::CompositorInstance *dsMergeInstance;
589 
590  private: Ogre::CompositorInstance *dlGBufferInstance;
591  private: Ogre::CompositorInstance *dlMergeInstance;
592 
593  private: Ogre::CompositorInstance *ssaoInstance;
594 
595  private: std::deque<std::pair<math::Pose, double> > moveToPositionQueue;
596 
598  private: common::Time renderPeriod;
599  };
601  }
602 }
603 #endif