mirror of
https://github.com/johndoe6345789/SDL3CPlusPlus.git
synced 2026-04-24 13:44:58 +00:00
glm stuff
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -5,6 +5,10 @@
|
||||
#include <assimp/postprocess.h>
|
||||
#include <assimp/scene.h>
|
||||
#include <btBulletDynamicsCommon.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
@@ -220,6 +224,24 @@ bool TryLoadMeshPayload(const CubeScript* script,
|
||||
return true;
|
||||
}
|
||||
|
||||
glm::vec3 ToVec3(const std::array<float, 3>& value) {
|
||||
return glm::vec3(value[0], value[1], value[2]);
|
||||
}
|
||||
|
||||
glm::quat ToQuat(const std::array<float, 4>& 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<float, 3> translation = CubeScript::ReadVector3(L, 1);
|
||||
std::array<float, 4> 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<float, 16> 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();
|
||||
|
||||
Reference in New Issue
Block a user