diff --git a/include/Nazara/Graphics/AbstractViewer.hpp b/include/Nazara/Graphics/AbstractViewer.hpp index f6fcad623..448a9ecc0 100644 --- a/include/Nazara/Graphics/AbstractViewer.hpp +++ b/include/Nazara/Graphics/AbstractViewer.hpp @@ -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; }; } diff --git a/src/Nazara/Graphics/AbstractViewer.cpp b/src/Nazara/Graphics/AbstractViewer.cpp index 1788793c5..d7ab68bbb 100644 --- a/src/Nazara/Graphics/AbstractViewer.cpp +++ b/src/Nazara/Graphics/AbstractViewer.cpp @@ -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; }