summaryrefslogtreecommitdiffhomepage
path: root/src/raymath.h
diff options
context:
space:
mode:
authorraysan5 <[email protected]>2020-09-01 21:08:45 +0200
committerraysan5 <[email protected]>2020-09-01 21:08:45 +0200
commit05cdaf7d0153c8847a651334265eafd67fff21b2 (patch)
treef941d212c770de8e8fb724c0b9b861587ffbb92d /src/raymath.h
parenta1422ba1c16c74a39793f95f73f1228eeca71f38 (diff)
downloadraylib-05cdaf7d0153c8847a651334265eafd67fff21b2.tar.gz
raylib-05cdaf7d0153c8847a651334265eafd67fff21b2.zip
MOVED: rlUnproject() [rlgl] -> Vector3Unproject() [raymath]
Diffstat (limited to 'src/raymath.h')
-rw-r--r--src/raymath.h23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/raymath.h b/src/raymath.h
index 3faa1d25..3e5e1973 100644
--- a/src/raymath.h
+++ b/src/raymath.h
@@ -1477,4 +1477,27 @@ RMDEF Quaternion QuaternionTransform(Quaternion q, Matrix mat)
return result;
}
+// Projects a Vector3 from screen space into object space
+RMDEF Vector3 Vector3Unproject(Vector3 source, Matrix projection, Matrix view)
+{
+ Vector3 result = { 0.0f, 0.0f, 0.0f };
+
+ // Calculate unproject matrix (multiply view patrix by projection matrix) and invert it
+ Matrix matViewProj = MatrixMultiply(view, projection);
+ matViewProj = MatrixInvert(matViewProj);
+
+ // Create quaternion from source point
+ Quaternion quat = { source.x, source.y, source.z, 1.0f };
+
+ // Multiply quat point by unproject matrix
+ quat = QuaternionTransform(quat, matViewProj);
+
+ // Normalized world points in vectors
+ result.x = quat.x/quat.w;
+ result.y = quat.y/quat.w;
+ result.z = quat.z/quat.w;
+
+ return result;
+}
+
#endif // RAYMATH_H