From 33abfd834ad5c9541796ef3f8a4999539a2f91f8 Mon Sep 17 00:00:00 2001 From: Roz K Date: Thu, 29 Dec 2022 08:11:12 +0100 Subject: [PATCH] Add mat4_orbit. --- __init__.py | 8 ++++++++ cpp/math.cpp | 14 +++++++++++++- cpp/math.hpp | 12 ++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/__init__.py b/__init__.py index 6b4909d..3b2dc86 100644 --- a/__init__.py +++ b/__init__.py @@ -182,6 +182,14 @@ mat4_lookat.argtypes = ( _vec3_p, # position _vec3_p) # lookat +mat4_orbit = _engine.rk_mat4_orbit +mat4_orbit.argtypes = ( + _mat4_p, # ret + _vec3_p, # origin + ctypes.c_float, # yaw + ctypes.c_float, # pitch + ctypes.c_float) # distance + mat4_mul_vec3 = _engine.rk_mat4_mul_vec3 mat4_mul_vec3.argtypes = ( _vec3_p, # ret diff --git a/cpp/math.cpp b/cpp/math.cpp index 192941b..1c7a7fa 100644 --- a/cpp/math.cpp +++ b/cpp/math.cpp @@ -64,7 +64,19 @@ void rk_mat4_lookat( rk_mat4 & ret, rk_vec3 const & position, rk_vec3 const & lookat) { - ret = glm::lookAtRH(position, lookat, glm::vec3(0.0f, 0.0f, 1.0f)); + ret = glm::lookAtRH(position, lookat, vec3_up); +} + +void rk_mat4_orbit( + rk_mat4 & ret, + rk_vec3 const & origin, + float const yaw, + float const pitch, + float const distance) { + ret = glm::translate( + glm::lookAtRH(glm::vec3(0.f, -distance, 0.f), vec3_origin, vec3_up) * + glm::mat4(glm::angleAxis(pitch, vec3_right) * glm::angleAxis(yaw, vec3_up)), + origin); } void rk_mat4_mul_vec3( diff --git a/cpp/math.hpp b/cpp/math.hpp index 2bd03ac..f352ddb 100644 --- a/cpp/math.hpp +++ b/cpp/math.hpp @@ -26,6 +26,11 @@ typedef glm::vec4 rk_vec4; typedef glm::mat3 rk_mat3; typedef glm::mat4 rk_mat4; +#define vec3_right (rk_vec3(1.f, 0.f, 0.f)) +#define vec3_forward (rk_vec3(0.f, 1.f, 0.f)) +#define vec3_up (rk_vec3(0.f, 0.f, 1.f)) +#define vec3_origin (rk_vec3(0.f, 0.f, 0.f)) + RK_EXPORT void rk_vec3_rotate( rk_vec3 & ret, rk_vec3 const & vec3, @@ -64,6 +69,13 @@ RK_EXPORT void rk_mat4_lookat( rk_vec3 const & position, rk_vec3 const & lookat); +RK_EXPORT void rk_mat4_orbit( + rk_mat4 & ret, + rk_vec3 const & origin, + float const yaw, + float const pitch, + float const distance); + RK_EXPORT void rk_mat4_mul_vec3( rk_vec3 & ret, rk_mat4 const & a,