diff --git a/CMakeLists.txt b/CMakeLists.txt index c591dfc..1fd53d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,6 +78,7 @@ find_package(CLI11 CONFIG REQUIRED) find_package(rapidjson CONFIG REQUIRED) find_package(assimp CONFIG REQUIRED) find_package(Bullet CONFIG REQUIRED) +find_package(glm CONFIG REQUIRED) if(BUILD_SDL3_APP) add_executable(sdl3_app @@ -94,7 +95,7 @@ add_executable(sdl3_app src/script/cube_script.cpp ) target_include_directories(sdl3_app PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src") - target_link_libraries(sdl3_app PRIVATE sdl::sdl Vulkan::Vulkan lua::lua CLI11::CLI11 rapidjson assimp::assimp Bullet::Bullet) + target_link_libraries(sdl3_app PRIVATE sdl::sdl Vulkan::Vulkan lua::lua CLI11::CLI11 rapidjson assimp::assimp Bullet::Bullet glm::glm) target_compile_definitions(sdl3_app PRIVATE SDL_MAIN_HANDLED) file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/shaders" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}") @@ -108,5 +109,5 @@ add_executable(cube_script_tests src/script/cube_script.cpp ) target_include_directories(cube_script_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src") -target_link_libraries(cube_script_tests PRIVATE lua::lua assimp::assimp Bullet::Bullet) +target_link_libraries(cube_script_tests PRIVATE lua::lua assimp::assimp Bullet::Bullet glm::glm) add_test(NAME cube_script_tests COMMAND cube_script_tests) diff --git a/scripts/cube_logic.lua b/scripts/cube_logic.lua index 1f79c36..f3a055c 100644 --- a/scripts/cube_logic.lua +++ b/scripts/cube_logic.lua @@ -122,25 +122,6 @@ local cube_state = { } local physics_last_time = 0.0 -local function quaternion_to_matrix(q) - local x, y, z, w = q[1], q[2], q[3], q[4] - local xx = x * x - local yy = y * y - local zz = z * z - local xy = x * y - local xz = x * z - local yz = y * z - local wx = w * x - local wy = w * y - local wz = w * z - return { - 1.0 - 2.0 * yy - 2.0 * zz, 2.0 * xy + 2.0 * wz, 2.0 * xz - 2.0 * wy, 0.0, - 2.0 * xy - 2.0 * wz, 1.0 - 2.0 * xx - 2.0 * zz, 2.0 * yz + 2.0 * wx, 0.0, - 2.0 * xz + 2.0 * wy, 2.0 * yz - 2.0 * wx, 1.0 - 2.0 * xx - 2.0 * yy, 0.0, - 0.0, 0.0, 0.0, 1.0, - } -end - local function initialize_physics() if type(physics_create_box) ~= "function" then error("physics_create_box() is unavailable") @@ -269,13 +250,7 @@ end local function create_physics_cube(shader_key) local function compute_model_matrix(time) sync_physics(time) - local offset = math3d.translation( - cube_state.position[1], - cube_state.position[2], - cube_state.position[3] - ) - local rotation_matrix = quaternion_to_matrix(cube_state.rotation) - return math3d.multiply(offset, rotation_matrix) + return glm_matrix_from_transform(cube_state.position, cube_state.rotation) end return { diff --git a/src/script/cube_script.cpp b/src/script/cube_script.cpp index 8085a50..5bc0f6f 100644 --- a/src/script/cube_script.cpp +++ b/src/script/cube_script.cpp @@ -5,6 +5,10 @@ #include #include #include +#include +#include +#include +#include #include #include @@ -220,6 +224,24 @@ bool TryLoadMeshPayload(const CubeScript* script, return true; } +glm::vec3 ToVec3(const std::array& value) { + return glm::vec3(value[0], value[1], value[2]); +} + +glm::quat ToQuat(const std::array& value) { + // Lua exposes {x, y, z, w} + return glm::quat(value[3], value[0], value[1], value[2]); +} + +void PushMatrix(lua_State* L, const glm::mat4& matrix) { + lua_newtable(L); + const float* ptr = glm::value_ptr(matrix); + for (int i = 0; i < 16; ++i) { + lua_pushnumber(L, ptr[i]); + lua_rawseti(L, -2, i + 1); + } +} + int PushMeshToLua(lua_State* L, const MeshPayload& payload) { lua_newtable(L); // mesh @@ -347,6 +369,16 @@ int LuaPhysicsGetTransform(lua_State* L) { return 1; } +int LuaGlmMatrixFromTransform(lua_State* L) { + std::array translation = CubeScript::ReadVector3(L, 1); + std::array rotation = CubeScript::ReadQuaternion(L, 2); + glm::vec3 pos = ToVec3(translation); + glm::quat quat = ToQuat(rotation); + glm::mat4 matrix = glm::translate(glm::mat4(1.0f), pos) * glm::mat4_cast(quat); + PushMatrix(L, matrix); + return 1; +} + std::array IdentityMatrix() { return {1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, @@ -378,8 +410,8 @@ CubeScript::CubeScript(const std::filesystem::path& scriptPath, bool debugEnable lua_pushcclosure(L_, &LuaPhysicsGetTransform, 1); lua_setglobal(L_, "physics_get_transform"); lua_pushlightuserdata(L_, this); - lua_pushcclosure(L_, &LuaLoadMeshFromFile, 1); - lua_setglobal(L_, "load_mesh_from_file"); + lua_pushcclosure(L_, &LuaGlmMatrixFromTransform, 1); + lua_setglobal(L_, "glm_matrix_from_transform"); lua_pushboolean(L_, debugEnabled_); lua_setglobal(L_, "lua_debug"); auto scriptDir = scriptPath.parent_path();