17 #ifndef GZ_RENDERING_BASE_BASECAMERA_HH_
18 #define GZ_RENDERING_BASE_BASECAMERA_HH_
22 #include <gz/math/Matrix3.hh>
23 #include <gz/math/Pose3.hh>
25 #include <gz/common/Event.hh>
26 #include <gz/common/Console.hh>
27 #include <gz/utils/SuppressWarning.hh>
39 inline namespace GZ_RENDERING_VERSION_NAMESPACE {
52 public:
virtual unsigned int ImageWidth()
const override;
54 public:
virtual void SetImageWidth(
const unsigned int _width)
override;
56 public:
virtual unsigned int ImageHeight()
const override;
58 public:
virtual void SetImageHeight(
const unsigned int _height)
override;
60 public:
virtual PixelFormat ImageFormat()
const override;
62 public:
virtual unsigned int ImageMemorySize()
const override;
64 public:
virtual void SetImageFormat(
PixelFormat _format)
override;
68 public:
virtual void SetHFOV(
const math::Angle &_hfov)
override;
70 public:
virtual double AspectRatio()
const override;
72 public:
virtual void SetAspectRatio(
const double _ratio)
override;
74 public:
virtual unsigned int AntiAliasing()
const override;
76 public:
virtual void SetAntiAliasing(
const unsigned int _aa)
override;
78 public:
virtual double FarClipPlane()
const override;
80 public:
virtual void SetFarClipPlane(
const double _far)
override;
82 public:
virtual double NearClipPlane()
const override;
84 public:
virtual void SetNearClipPlane(
const double _near)
override;
87 public:
virtual void PreRender()
override;
90 public:
virtual void PostRender()
override;
92 public:
virtual void Update()
override;
94 public:
virtual Image CreateImage()
const override;
96 public:
virtual void Capture(
Image &_image)
override;
98 public:
virtual void Copy(
Image &_image)
const override;
100 public:
virtual bool SaveFrame(
const std::string &_name)
override;
109 &_mousePos)
override;
118 public:
virtual void SetProjectionMatrix(
const math::Matrix4d &_matrix)
125 public:
virtual void SetProjectionType(
134 public:
virtual void SetMaterial(
const MaterialPtr &_material)
138 public:
virtual void SetTrackTarget(
const NodePtr &_target,
140 const bool _worldFrame)
override;
143 public:
virtual NodePtr TrackTarget()
const override;
146 public:
virtual void SetTrackOffset(
const math::Vector3d &_offset)
153 public:
virtual void SetTrackPGain(
const double _pGain)
override;
156 public:
virtual double TrackPGain()
const override;
159 public:
virtual void SetFollowTarget(
const NodePtr &_target,
164 public:
virtual NodePtr FollowTarget()
const override;
167 public:
virtual void SetFollowOffset(
const math::Vector3d &_offset)
174 public:
virtual void SetFollowPGain(
const double _pGain)
override;
177 public:
virtual double FollowPGain()
const override;
180 public:
virtual unsigned int RenderTextureGLId()
const override;
183 public:
virtual void RenderTextureMetalId(
void *_textureIdPtr)
187 public:
virtual void AddRenderPass(
const RenderPassPtr &_pass)
override;
190 public:
virtual void RemoveRenderPass(
const RenderPassPtr &_pass)
194 public:
virtual unsigned int RenderPassCount()
const override;
197 public:
virtual RenderPassPtr RenderPassByIndex(
unsigned int _index)
201 public:
virtual void SetShadowsDirty()
override;
203 protected:
virtual void *CreateImageBuffer()
const;
205 protected:
virtual void Load()
override;
207 protected:
virtual void Reset();
211 GZ_UTILS_WARN_IGNORE__DLL_INTERFACE_MISSING
212 protected:
common::EventT<void(
const void *,
unsigned int,
unsigned int,
218 protected:
double nearClip = 0.01;
221 protected:
double farClip = 1000.0;
224 protected:
double aspect = 1.3333333;
230 protected:
unsigned int antiAliasing = 0u;
236 protected:
bool trackWorldFrame =
false;
244 protected:
double trackPGain = 1.0;
248 GZ_UTILS_WARN_RESUME__DLL_INTERFACE_MISSING
251 protected:
bool followWorldFrame =
false;
255 protected:
double followPGain = 1.0;
293 this->SetAspectRatio(
294 static_cast<double>(_width) /
static_cast<double>(this->ImageHeight()));
309 this->SetAspectRatio(
310 static_cast<double>(this->ImageWidth()) /
static_cast<double>(_height));
318 unsigned int width = this->ImageWidth();
319 unsigned int height = this->ImageHeight();
346 if (this->followNode)
349 if (this->followWorldFrame)
352 this->followNode->WorldPosition() + this->followOffset;
354 (targetCamPos - this->WorldPosition()) * this->followPGain;
355 this->SetWorldPosition(pos);
361 this->WorldRotation());
362 targetCamPose = this->followNode->WorldPose() * targetCamPose;
365 (targetCamPose.
Pos() - this->WorldPosition()) * this->followPGain;
366 this->SetWorldPosition(pos);
376 if (this->trackWorldFrame)
378 targetPose.
Pos() += this->trackNode->WorldPosition();
382 targetPose = this->trackNode->WorldPose() * targetPose;
393 this->trackPGain, this->WorldRotation(), p.
Rot(),
true);
395 this->SetWorldRotation(q);
411 unsigned int width = this->ImageWidth();
412 unsigned int height = this->ImageHeight();
413 return Image(width, height, format);
423 if (!this->
Scene()->LegacyAutoGpuFlush())
456 return newFrameEvent.Connect(_listener);
464 unsigned int size = this->ImageMemorySize();
465 return new unsigned char *[size];
481 this->SetImageWidth(1);
482 this->SetImageHeight(1);
484 this->SetAspectRatio(0.0);
485 this->SetAntiAliasing(0u);
487 this->SetNearClipPlane(0.01);
488 this->SetFarClipPlane(1000);
496 gzerr <<
"Render window not supported for render engine: " <<
508 double ratio = this->AspectRatio();
509 double fov = this->HFOV().Radian();
512 double _near = this->NearClipPlane();
513 double _far = this->FarClipPlane();
514 double top = _near *
std::tan(0.5*vfov) / f;
515 double height = 2 * top;
516 double width = ratio * height;
517 double left = -0.5 * width;
518 double right = left + width;
519 double bottom = top - height;
521 double invw = 1.0 / (right - left);
522 double invh = 1.0 / (top - bottom);
523 double invd = 1.0 / (_far - _near);
524 double x = 2 * _near * invw;
525 double y = 2 * _near * invh;
526 double a = (right + left) * invw;
527 double b = (top + bottom) * invh;
528 double c = -(_far + _near) * invd;
529 double d = -2 * _far * _near * invd;
540 double width = this->ImageWidth();
541 double height = this->ImageHeight();
542 double left = -width * 0.5;
543 double right = -left;
544 double top = height * 0.5;
545 double bottom = -top;
546 double _near = this->NearClipPlane();
547 double _far = this->FarClipPlane();
549 double invw = 1.0 / (right - left);
550 double invh = 1.0 / (top - bottom);
551 double invd = 1.0 / (_far - _near);
553 result(0, 0) = 2.0 * invw;
554 result(0, 3) = -(right + left) * invw;
555 result(1, 1) = 2.0 * invh;
556 result(1, 3) = -(top + bottom) * invh;
557 result(2, 2) = -2.0 * invd;
558 result(2, 3) = -(_far + _near) * invd;
563 gzerr <<
"Unknown camera projection type: " << this->projectionType
574 this->projectionMatrix = _matrix;
600 this->projectionType = _type;
607 return this->projectionType;
617 double w = m(3, 0) * _pt.X() + m(3, 1) * _pt.Y() + m(3, 2) * _pt.Z()
619 pos.
X() = pos.
X() / w;
620 pos.
Y() = pos.
Y() / w;
622 screenPos.
X() =
static_cast<int>(
623 ((pos.
X() / 2.0) + 0.5) * this->ImageWidth());
624 screenPos.
Y() =
static_cast<int>(
625 (1 - ((pos.
Y() / 2.0) + 0.5)) * this->ImageHeight());
641 gzerr <<
"VisualAt not implemented for the render engine" <<
std::endl;
659 if (this->aspect <= 0.0)
661 return static_cast<double>(this->ImageWidth()) /
662 static_cast<double>(this->ImageHeight());
671 this->aspect = _aspect;
678 return this->antiAliasing;
685 this->antiAliasing = _aa;
692 return this->farClip;
699 this->farClip = _far;
706 return this->nearClip;
713 this->nearClip = _near;
721 this->trackNode = _target;
722 this->trackWorldFrame = _worldFrame;
723 this->trackOffset = _offset;
730 return this->trackNode;
737 return this->trackOffset;
744 this->trackOffset = _offset;
758 return this->trackPGain;
766 this->followNode = _target;
767 this->followWorldFrame = _worldFrame;
768 this->followOffset = _offset;
775 return this->followNode;
782 return this->followOffset;
789 this->followOffset = _offset;
803 return this->followPGain;
810 gzerr <<
"SetMaterial not implemented for current render"
818 gzerr <<
"RenderTextureGLId is not supported by current render"
827 gzerr <<
"RenderTextureMetalId is not supported by current render"