This commit is contained in:
Richard Ward
2025-12-18 19:13:31 +00:00
parent 7d83fc3ae9
commit 3ca9ce773d
8 changed files with 218 additions and 161 deletions

View File

@@ -6,11 +6,38 @@
namespace sdl3cpp::script {
namespace {
std::array<float, 16> IdentityMatrix() {
return {1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f};
}
} // namespace
CubeScript::CubeScript(const std::filesystem::path& scriptPath) : L_(luaL_newstate()) {
if (!L_) {
throw std::runtime_error("Failed to create Lua state");
}
luaL_openlibs(L_);
auto scriptDir = scriptPath.parent_path();
if (!scriptDir.empty()) {
lua_getglobal(L_, "package");
if (lua_istable(L_, -1)) {
lua_getfield(L_, -1, "path");
const char* currentPath = lua_tostring(L_, -1);
std::string newPath = scriptDir.string() + "/?.lua;";
if (currentPath) {
newPath += currentPath;
}
lua_pop(L_, 1);
lua_pushstring(L_, newPath.c_str());
lua_setfield(L_, -2, "path");
}
lua_pop(L_, 1);
}
if (luaL_dofile(L_, scriptPath.string().c_str()) != LUA_OK) {
std::string message = LuaErrorMessage(L_);
lua_pop(L_, 1);
@@ -97,7 +124,7 @@ std::array<float, 16> CubeScript::ComputeModelMatrix(int functionRef, float time
lua_getglobal(L_, "compute_model_matrix");
if (!lua_isfunction(L_, -1)) {
lua_pop(L_, 1);
return core::IdentityMatrix();
return IdentityMatrix();
}
} else {
lua_rawgeti(L_, LUA_REGISTRYINDEX, functionRef);
@@ -119,6 +146,27 @@ std::array<float, 16> CubeScript::ComputeModelMatrix(int functionRef, float time
return matrix;
}
std::array<float, 16> CubeScript::GetViewProjectionMatrix(float aspect) {
lua_getglobal(L_, "get_view_projection");
if (!lua_isfunction(L_, -1)) {
lua_pop(L_, 1);
throw std::runtime_error("Lua function 'get_view_projection' is missing");
}
lua_pushnumber(L_, aspect);
if (lua_pcall(L_, 1, 1, 0) != LUA_OK) {
std::string message = LuaErrorMessage(L_);
lua_pop(L_, 1);
throw std::runtime_error("Lua get_view_projection failed: " + message);
}
if (!lua_istable(L_, -1)) {
lua_pop(L_, 1);
throw std::runtime_error("'get_view_projection' did not return a table");
}
std::array<float, 16> matrix = ReadMatrix(L_, -1);
lua_pop(L_, 1);
return matrix;
}
std::vector<core::Vertex> CubeScript::ReadVertexArray(lua_State* L, int index) {
int absIndex = lua_absindex(L, index);
if (!lua_istable(L, absIndex)) {

View File

@@ -9,7 +9,7 @@
#include <lua.hpp>
#include "core/math.hpp"
#include "core/vertex.hpp"
namespace sdl3cpp::script {
@@ -32,6 +32,7 @@ public:
std::vector<SceneObject> LoadSceneObjects();
std::array<float, 16> ComputeModelMatrix(int functionRef, float time);
std::array<float, 16> GetViewProjectionMatrix(float aspect);
std::unordered_map<std::string, ShaderPaths> LoadShaderPathsMap();
private: