Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check valid camera near far clip distances #1082

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions include/gz/rendering/base/BaseCamera.hh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef GZ_RENDERING_BASE_BASECAMERA_HH_
#define GZ_RENDERING_BASE_BASECAMERA_HH_

#include <cmath>
#include <string>

#include <gz/math/Matrix3.hh>
Expand Down Expand Up @@ -709,6 +710,12 @@ namespace gz
template <class T>
void BaseCamera<T>::SetFarClipPlane(const double _far)
{
if (_far <= 0 || !std::isfinite(_far))
{
gzerr << "Far clip distance must be a finite number greater than 0."
<< std::endl;
return;
}
this->farClip = _far;
}

Expand All @@ -723,6 +730,12 @@ namespace gz
template <class T>
void BaseCamera<T>::SetNearClipPlane(const double _near)
{
if (_near <= 0 || !std::isfinite(_near))
{
gzerr << "Near clip distance must be a finite number greater than 0."
<< std::endl;
return;
}
this->nearClip = _near;
}

Expand Down
4 changes: 2 additions & 2 deletions ogre/src/OgreCamera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,14 @@ void OgreCamera::SetNearClipPlane(const double _near)
{
// this->nearClip = _near;
BaseCamera::SetNearClipPlane(_near);
this->ogreCamera->setNearClipDistance(_near);
this->ogreCamera->setNearClipDistance(this->nearClip);
}

//////////////////////////////////////////////////
void OgreCamera::SetFarClipPlane(const double _far)
{
BaseCamera::SetFarClipPlane(_far);
this->ogreCamera->setFarClipDistance(_far);
this->ogreCamera->setFarClipDistance(this->farClip);
}

//////////////////////////////////////////////////
Expand Down
4 changes: 2 additions & 2 deletions ogre2/src/Ogre2Camera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,14 @@ void Ogre2Camera::SetProjectionType(CameraProjectionType _type)
void Ogre2Camera::SetNearClipPlane(const double _near)
{
BaseCamera::SetNearClipPlane(_near);
this->ogreCamera->setNearClipDistance(_near);
this->ogreCamera->setNearClipDistance(this->nearClip);
}

//////////////////////////////////////////////////
void Ogre2Camera::SetFarClipPlane(const double _far)
{
BaseCamera::SetFarClipPlane(_far);
this->ogreCamera->setFarClipDistance(_far);
this->ogreCamera->setFarClipDistance(this->farClip);
}

//////////////////////////////////////////////////
Expand Down
8 changes: 8 additions & 0 deletions test/common_test/Camera_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,18 @@ TEST_F(CameraTest, ViewProjectionMatrix)
camera->SetNearClipPlane(0.1);
EXPECT_DOUBLE_EQ(0.1, camera->NearClipPlane());

// set invalid clip distance and verify it is not set
camera->SetNearClipPlane(0.0);
EXPECT_DOUBLE_EQ(0.1, camera->NearClipPlane());

EXPECT_GT(camera->FarClipPlane(), 0);
camera->SetFarClipPlane(800);
EXPECT_DOUBLE_EQ(800, camera->FarClipPlane());

// set invalid clip distance and verify it is not set
camera->SetFarClipPlane(-10.0);
EXPECT_DOUBLE_EQ(800, camera->FarClipPlane());

EXPECT_NE(projMatrix, camera->ProjectionMatrix());

// view matrix
Expand Down
Loading