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

CustomPSSMShadowCamera: support custom projection matrix #3249

Merged
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
7 changes: 2 additions & 5 deletions gazebo/rendering/Camera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -813,11 +813,8 @@ void Camera::SetClipDist()

if (this->camera)
{
if (!this->cameraUsingIntrinsics)
{
this->camera->setNearClipDistance(clipElem->Get<double>("near"));
this->camera->setFarClipDistance(clipElem->Get<double>("far"));
}
this->camera->setNearClipDistance(clipElem->Get<double>("near"));
this->camera->setFarClipDistance(clipElem->Get<double>("far"));
this->camera->setRenderingDistance(clipElem->Get<double>("far"));
}
else
Expand Down
32 changes: 32 additions & 0 deletions gazebo/rendering/CustomPSSMShadowCameraSetup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,18 @@ void CustomPSSMShadowCameraSetup::calculateShadowMappingMatrix(
}
}

//////////////////////////////////////////////////
// Build a simple perspective projection matrix using only near and far clipping
// planes.
static Ogre::Matrix4 buildSimplePerspectiveMatrix(
const Ogre::Real _near, const Ogre::Real _far)
{
return Ogre::Matrix4(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, -(_far + _near) / (_far - _near), -2 * _far * _near / (_far - _near),
0, 0, -1, 0);
}

//////////////////////////////////////////////////
Ogre::Matrix4 CustomPSSMShadowCameraSetup::buildViewMatrix(
const Ogre::Vector3 &_pos, const Ogre::Vector3 &_dir,
Expand Down Expand Up @@ -611,8 +623,24 @@ void CustomPSSMShadowCameraSetup::getShadowCamera(const Ogre::SceneManager *_sm,
Ogre::Camera *cam = const_cast<Ogre::Camera *>(_cam);
Ogre::Real oldNear = _cam->getNearClipDistance();
Ogre::Real oldFar = _cam->getFarClipDistance();
Ogre::Matrix4 oldCustomProjMat = _cam->getProjectionMatrix();
cam->setNearClipDistance(nearDist);
cam->setFarClipDistance(farDist);
if (cam->isCustomProjectionMatrixEnabled())
{
// setNearClipDistance() and setFarClipDistance() will cause a regular
// camera projection matrix to be rebuilt, but they do nothing if the camera
// is using a custom matrix. Ideally, we would build the necessary matrix
// from scratch here, but we do not have access to the required camera
// intrinsics. We work around this problem by modifying the existing matrix,
// resulting in a new custom matrix with altered near and far clip planes
// that is almost identical to one built from scratch (typically some
// elements differ in the forth or fifth digit).
Ogre::Matrix4 oldNearFarMat = buildSimplePerspectiveMatrix(oldNear, oldFar);
Ogre::Matrix4 newNearFarMat = buildSimplePerspectiveMatrix(nearDist, farDist);
cam->setCustomProjectionMatrix(true, oldCustomProjMat *
oldNearFarMat.inverse() * newNearFarMat);
}

// Replaced LiSPSMShadowCameraSetup::getShadowCamera() with
// FocusedShadowCameraSetup::getShadowCamera(). This is the same solution
Expand All @@ -624,6 +652,10 @@ void CustomPSSMShadowCameraSetup::getShadowCamera(const Ogre::SceneManager *_sm,
// restore near/far
cam->setNearClipDistance(oldNear);
cam->setFarClipDistance(oldFar);
if (cam->isCustomProjectionMatrixEnabled())
{
cam->setCustomProjectionMatrix(true, oldCustomProjMat);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like we're resetting the custom projection matrix back to the old matrix. So we only want to modify the custom projection matrix temporarily (before calling the clip distance methods)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. In the case of non-custom projection matrices, lines 625 and 626 build a new temporary matrix and lines 650 and 651 rebuild the original matrix. Those lines have no effect on a custom matrix, so I found a different way to build a temporary matrix and restore the original.

Line 619 points out that this is all very "hacky," so the original author of this code probably would have preferred a cleaner solution but didn't want all the refactoring that it would have required.

}
}

//////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion gazebo/rendering/CustomPSSMShadowCameraSetup.hh
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ namespace gazebo

/// \brief lightly modified
/// FocusedShadowCameraSetup::calculateShadowMappingMatrix().
void calculateShadowMappingMatrix(const Ogre::SceneManager &_sm,
public: void calculateShadowMappingMatrix(const Ogre::SceneManager &_sm,
const Ogre::Camera &_cam, const Ogre::Light &_light,
Ogre::Matrix4 *_out_view, Ogre::Matrix4 *_outProj,
Ogre::Camera *_outCam) const;
Expand Down