Graphics/AbstractViewer: Make Project/Unproject const
This commit is contained in:
parent
9f88d8a7e8
commit
2c6b507e62
|
|
@ -30,9 +30,9 @@ namespace Nz
|
||||||
virtual const ViewerInstance& GetViewerInstance() const = 0;
|
virtual const ViewerInstance& GetViewerInstance() const = 0;
|
||||||
virtual const Recti& GetViewport() 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;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,16 +11,17 @@ namespace Nz
|
||||||
{
|
{
|
||||||
AbstractViewer::~AbstractViewer() = default;
|
AbstractViewer::~AbstractViewer() = default;
|
||||||
|
|
||||||
Vector3f AbstractViewer::Project(const Vector3f& worldPos)
|
Vector3f AbstractViewer::Project(const Vector3f& worldPos) const
|
||||||
{
|
{
|
||||||
const Matrix4f& viewProj = GetViewerInstance().GetViewProjMatrix();
|
const Matrix4f& viewProj = GetViewerInstance().GetViewProjMatrix();
|
||||||
Vector2f screenSize = Vector2f(GetRenderTarget().GetSize());
|
|
||||||
|
|
||||||
Vector4f clipspace = viewProj * Vector4f(worldPos, 1.f);
|
Vector4f clipspace = viewProj * Vector4f(worldPos, 1.f);
|
||||||
clipspace.x = clipspace.x / clipspace.w;
|
clipspace.x = clipspace.x / clipspace.w;
|
||||||
clipspace.y = clipspace.y / clipspace.w;
|
clipspace.y = clipspace.y / clipspace.w;
|
||||||
clipspace.z = clipspace.z / clipspace.w;
|
clipspace.z = clipspace.z / clipspace.w;
|
||||||
|
|
||||||
|
Vector2f screenSize = Vector2f(GetRenderTarget().GetSize());
|
||||||
|
|
||||||
Vector3f screenSpace;
|
Vector3f screenSpace;
|
||||||
screenSpace.x = (clipspace.x * 0.5f + 0.5f) * screenSize.x;
|
screenSpace.x = (clipspace.x * 0.5f + 0.5f) * screenSize.x;
|
||||||
screenSpace.y = (clipspace.y * 0.5f + 0.5f) * screenSize.y;
|
screenSpace.y = (clipspace.y * 0.5f + 0.5f) * screenSize.y;
|
||||||
|
|
@ -29,9 +30,8 @@ namespace Nz
|
||||||
return screenSpace;
|
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());
|
Vector2f screenSize = Vector2f(GetRenderTarget().GetSize());
|
||||||
|
|
||||||
// Screen space => clip space
|
// Screen space => clip space
|
||||||
|
|
@ -42,6 +42,8 @@ namespace Nz
|
||||||
clipSpace.w = 1.f;
|
clipSpace.w = 1.f;
|
||||||
|
|
||||||
// Clip space => projection space => view space => world space
|
// Clip space => projection space => view space => world space
|
||||||
|
const Matrix4f& inverseViewProj = GetViewerInstance().GetInvViewProjMatrix();
|
||||||
|
|
||||||
Vector4f unproj = inverseViewProj * clipSpace;
|
Vector4f unproj = inverseViewProj * clipSpace;
|
||||||
return Vector3f(unproj.x, unproj.y, unproj.z) / unproj.w;
|
return Vector3f(unproj.x, unproj.y, unproj.z) / unproj.w;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue