From: Mikko Rasa Date: Mon, 24 Oct 2016 20:55:58 +0000 (+0300) Subject: Fix Camera::unproject X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=9ea7e740503b09298ae814b1bf521312d1744db2;p=libs%2Fgl.git Fix Camera::unproject It was lacking un-perspective division. There's also a Vector3 overload now. --- diff --git a/source/camera.cpp b/source/camera.cpp index ba8ff718..ae35f632 100644 --- a/source/camera.cpp +++ b/source/camera.cpp @@ -99,10 +99,15 @@ Vector3 Camera::project(const Vector3 &p) const Vector4 Camera::unproject(const Vector4 &p) const { Vector4 r = invert(proj_matrix)*Vector4(p.x, p.y, p.z, 1.0f); - r = object_matrix*Vector4(r.x, r.y, r.z, p.w); + r = object_matrix*Vector4(r.x/r.w, r.y/r.w, r.z/r.w, p.w); return r; } +Vector3 Camera::unproject(const Vector3 &p) const +{ + return unproject(Vector4(p.x, p.y, p.z, 1.0f)).slice<3>(0); +} + void Camera::apply() const { MatrixStack::projection() = proj_matrix; diff --git a/source/camera.h b/source/camera.h index f814e9d7..44d3bf33 100644 --- a/source/camera.h +++ b/source/camera.h @@ -66,6 +66,7 @@ public: Vector3 project(const Vector4 &) const; Vector3 project(const Vector3 &) const; Vector4 unproject(const Vector4 &) const; + Vector3 unproject(const Vector3 &) const; void apply() const;