Add lens shift support for the camera and the perspective projection functions

As Blender docs describe it so nicely: Using lens shift is equivalent to rendering an image with a larger FOV and cropping it off-center.
This can be quite useful for architecture renderings, but I guess also has it's use in other situations.
Note: Didn't make the ICameraSceneNode functions pure virtual so users don't have to update their cameras for this
Also some change in serialization - same as in other places by now, do use existing values as defaults values when they are not found instead of resetting them to 0.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@6565 dfc29bdd-3216-0410-991c-e03cc46cb475
This commit is contained in:
cutealien
2023-10-19 16:04:43 +00:00
parent 567f8688e2
commit d4f6d8c17b
5 changed files with 65 additions and 27 deletions

View File

@ -201,6 +201,11 @@ f32 CCameraSceneNode::getFOV() const
return Fovy;
}
core::vector2df CCameraSceneNode::getLensShift() const
{
return LensShift;
}
void CCameraSceneNode::setNearValue(f32 f)
{
@ -231,10 +236,15 @@ void CCameraSceneNode::setFOV(f32 f)
recalculateProjectionMatrix();
}
void CCameraSceneNode::setLensShift(const core::vector2df& shift)
{
LensShift = shift;
recalculateProjectionMatrix();
}
void CCameraSceneNode::recalculateProjectionMatrix()
{
ViewArea.getTransform ( video::ETS_PROJECTION ).buildProjectionMatrixPerspectiveFovLH(Fovy, Aspect, ZNear, ZFar, HasD3DStyleProjectionMatrix);
ViewArea.getTransform ( video::ETS_PROJECTION ).buildProjectionMatrixPerspectiveFovLH(Fovy, Aspect, ZNear, ZFar, HasD3DStyleProjectionMatrix, LensShift.X, LensShift.Y);
IsOrthogonal = false;
}
@ -330,6 +340,7 @@ void CCameraSceneNode::serializeAttributes(io::IAttributes* out, io::SAttributeR
out->addFloat("Aspect", Aspect);
out->addFloat("ZNear", ZNear);
out->addFloat("ZFar", ZFar);
out->addVector2d("LensShift", LensShift);
out->addBool("Binding", TargetAndRotationAreBound);
out->addBool("ReceiveInput", InputReceiverEnabled);
}
@ -339,15 +350,15 @@ void CCameraSceneNode::deserializeAttributes(io::IAttributes* in, io::SAttribute
{
ICameraSceneNode::deserializeAttributes(in, options);
Target = in->getAttributeAsVector3d("Target");
UpVector = in->getAttributeAsVector3d("UpVector");
Fovy = in->getAttributeAsFloat("Fovy");
Aspect = in->getAttributeAsFloat("Aspect");
ZNear = in->getAttributeAsFloat("ZNear");
ZFar = in->getAttributeAsFloat("ZFar");
TargetAndRotationAreBound = in->getAttributeAsBool("Binding");
if ( in->findAttribute("ReceiveInput") )
InputReceiverEnabled = in->getAttributeAsBool("ReceiveInput");
Target = in->getAttributeAsVector3d("Target", Target);
UpVector = in->getAttributeAsVector3d("UpVector", UpVector);
Fovy = in->getAttributeAsFloat("Fovy", Fovy);
Aspect = in->getAttributeAsFloat("Aspect", Aspect);
ZNear = in->getAttributeAsFloat("ZNear", ZNear);
ZFar = in->getAttributeAsFloat("ZFar", ZFar);
LensShift = in->getAttributeAsVector2d("LensShift", LensShift);
TargetAndRotationAreBound = in->getAttributeAsBool("Binding", TargetAndRotationAreBound);
InputReceiverEnabled = in->getAttributeAsBool("ReceiveInput", InputReceiverEnabled);
recalculateProjectionMatrix();
recalculateViewArea();

View File

@ -95,6 +95,9 @@ namespace scene
//! \return Field of view of the camera
virtual f32 getFOV() const IRR_OVERRIDE;
//! Get the horizontal and vertical lens/projection plane shift
virtual core::vector2df getLensShift() const IRR_OVERRIDE;
//! Sets the value of the near clipping plane. (default: 1.0f)
virtual void setNearValue(f32 zn) IRR_OVERRIDE;
@ -107,6 +110,9 @@ namespace scene
//! Sets the field of view (Default: PI / 3.5f)
virtual void setFOV(f32 fovy) IRR_OVERRIDE;
//! Set the horizontal and vertical lens/projection plane shift
virtual void setLensShift(const core::vector2df& shift) IRR_OVERRIDE;
//! PreRender event
virtual void OnRegisterSceneNode() IRR_OVERRIDE;
@ -162,6 +168,7 @@ namespace scene
f32 Aspect; // Aspect ratio.
f32 ZNear; // value of the near view-plane.
f32 ZFar; // Z-value of the far view-plane.
core::vector2df LensShift; // For rendering off-center
SViewFrustum ViewArea;
core::matrix4 Affector;