Graphics/AbstractViewer: Make Project/Unproject const

This commit is contained in:
SirLynix 2023-10-30 18:36:41 +01:00
parent 9f88d8a7e8
commit 2c6b507e62
2 changed files with 8 additions and 6 deletions

View File

@ -30,9 +30,9 @@ namespace Nz
virtual const ViewerInstance& GetViewerInstance() const = 0;
virtual const Recti& GetViewport() const = 0;
Vector3f Project(const Vector3f& worldPos);
Vector3f Project(const Vector3f& worldPos) const;
Vector3f Unproject(const Vector3f& screenPos);
Vector3f Unproject(const Vector3f& screenPos) const;
};
}

View File

@ -11,16 +11,17 @@ namespace Nz
{
AbstractViewer::~AbstractViewer() = default;
Vector3f AbstractViewer::Project(const Vector3f& worldPos)
Vector3f AbstractViewer::Project(const Vector3f& worldPos) const
{
const Matrix4f& viewProj = GetViewerInstance().GetViewProjMatrix();
Vector2f screenSize = Vector2f(GetRenderTarget().GetSize());
Vector4f clipspace = viewProj * Vector4f(worldPos, 1.f);
clipspace.x = clipspace.x / clipspace.w;
clipspace.y = clipspace.y / clipspace.w;
clipspace.z = clipspace.z / clipspace.w;
Vector2f screenSize = Vector2f(GetRenderTarget().GetSize());
Vector3f screenSpace;
screenSpace.x = (clipspace.x * 0.5f + 0.5f) * screenSize.x;
screenSpace.y = (clipspace.y * 0.5f + 0.5f) * screenSize.y;
@ -29,9 +30,8 @@ namespace Nz
return screenSpace;
}
Vector3f AbstractViewer::Unproject(const Vector3f& screenPos)
Vector3f AbstractViewer::Unproject(const Vector3f& screenPos) const
{
const Matrix4f& inverseViewProj = GetViewerInstance().GetInvViewProjMatrix();
Vector2f screenSize = Vector2f(GetRenderTarget().GetSize());
// Screen space => clip space
@ -42,6 +42,8 @@ namespace Nz
clipSpace.w = 1.f;
// Clip space => projection space => view space => world space
const Matrix4f& inverseViewProj = GetViewerInstance().GetInvViewProjMatrix();
Vector4f unproj = inverseViewProj * clipSpace;
return Vector3f(unproj.x, unproj.y, unproj.z) / unproj.w;
}