From baa209ceea7e696bc0d21064088bf69dcceccd06 Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Thu, 24 Jan 2019 15:52:03 +0100 Subject: [PATCH] somewhat better working registration system for the lua runtime --- src/core/include/pw/core/matrix.hpp | 3 +- src/core/include/pw/core/vector.hpp | 3 + src/core/tests/pwcore_test_vector.cpp | 6 +- src/geometry/src/primitives.cpp | 46 +++++++------- src/scripting/src/runtime_lua.cpp | 2 - src/scripting/src/runtime_lua.hpp | 9 ++- src/scripting/src/script.cpp | 17 ++++- src/scripting/src/script_core.cpp | 89 ++++++++++++++------------- src/scripting/src/script_scene.cpp | 4 +- src/scripting/src/script_system.cpp | 8 ++- src/scripting/src/script_visual.cpp | 4 +- src/scripts/demos/simple_000.lua | 2 +- src/scripts/tests/test_core.lua | 39 ++++++++---- 13 files changed, 141 insertions(+), 91 deletions(-) diff --git a/src/core/include/pw/core/matrix.hpp b/src/core/include/pw/core/matrix.hpp index f199377..0fcdc4d 100644 --- a/src/core/include/pw/core/matrix.hpp +++ b/src/core/include/pw/core/matrix.hpp @@ -57,7 +57,7 @@ struct matrix_ : matrixbase_> return *this; } - matrix_(std::initializer_list args) + explicit matrix_(std::initializer_list args) { typename std::initializer_list::iterator it = args.begin(); for (;it != args.end();it++) data[it-args.begin()] = *it; @@ -235,7 +235,6 @@ auto operator * (const matrix_& A, // // - template using matrix2x2_ = matrix_<2, 2, T>; template using matrix3x3_ = matrix_<3, 3, T>; template using matrix4x4_ = matrix_<4, 4, T>; diff --git a/src/core/include/pw/core/vector.hpp b/src/core/include/pw/core/vector.hpp index a3c42c7..6b1b08b 100644 --- a/src/core/include/pw/core/vector.hpp +++ b/src/core/include/pw/core/vector.hpp @@ -66,12 +66,15 @@ struct vector3_ : matrix_<3,1,T> { inline const T& x() const { return (*this)[0]; } inline T& x() { return (*this)[0]; } + inline vector3_& set_x(T val) { (*this)[0] = val; return *this;} inline const T& y() const { return (*this)[1]; } inline T& y() { return (*this)[1]; } + inline vector3_& set_y(T val) { (*this)[1] = val; return *this;} inline const T& z() const { return (*this)[2]; } inline T& z() { return (*this)[2]; } + inline vector3_& set_z(T val) { (*this)[2] = val; return *this;} inline auto xy() const { return vector2_( { x(),y() } ); } inline auto homogenous(T w = 1) const { return matrix_<4,1,T>( { x(),y(),z(),w } ); } diff --git a/src/core/tests/pwcore_test_vector.cpp b/src/core/tests/pwcore_test_vector.cpp index c72d8e4..880723c 100644 --- a/src/core/tests/pwcore_test_vector.cpp +++ b/src/core/tests/pwcore_test_vector.cpp @@ -6,11 +6,11 @@ int main(int argc,char **argv) { - pw::vector2_ v2_A = { 3.2, 1.2 }; - pw::vector2_ v2_B = { 3.2, 1.2 }; +// pw::vector2_ v2_A = { 3.2, 1.2 }; +// pw::vector2_ v2_B = { 3.2, 1.2 }; - auto AB_lerp = pw::vector2f::lerp(v2_A,v2_B,0.5); +// auto AB_lerp = pw::vector2f::lerp(v2_A,v2_B,0.5); diff --git a/src/geometry/src/primitives.cpp b/src/geometry/src/primitives.cpp index 70e123c..683260a 100644 --- a/src/geometry/src/primitives.cpp +++ b/src/geometry/src/primitives.cpp @@ -8,32 +8,32 @@ mesh primitives::box(real_t size_x,real_t size_y, real_t size_z) mesh::vertex3array_t vertices; - vertices.push_back( { -size_x / 2,-size_y / 2, size_z / 2 } ); // 0 - vertices.push_back( { size_x / 2,-size_y / 2, size_z / 2 } ); // 1 - vertices.push_back( { size_x / 2, size_y / 2, size_z / 2 } ); // 2 - vertices.push_back( {-size_x / 2, size_y / 2, size_z / 2} ); // 3 +// vertices.push_back( { -size_x / 2,-size_y / 2, size_z / 2 } ); // 0 +// vertices.push_back( { size_x / 2,-size_y / 2, size_z / 2 } ); // 1 +// vertices.push_back( { size_x / 2, size_y / 2, size_z / 2 } ); // 2 +// vertices.push_back( {-size_x / 2, size_y / 2, size_z / 2} ); // 3 - vertices.push_back( {-size_x / 2,-size_y / 2,-size_z / 2} ); // 4 - vertices.push_back( { size_x / 2,-size_y / 2,-size_z / 2}); // 5 - vertices.push_back( { size_x / 2, size_y / 2,-size_z / 2}); // 6 - vertices.push_back( {-size_x / 2, size_y / 2,-size_z / 2}); // 7 +// vertices.push_back( {-size_x / 2,-size_y / 2,-size_z / 2} ); // 4 +// vertices.push_back( { size_x / 2,-size_y / 2,-size_z / 2}); // 5 +// vertices.push_back( { size_x / 2, size_y / 2,-size_z / 2}); // 6 +// vertices.push_back( {-size_x / 2, size_y / 2,-size_z / 2}); // 7 - mesh::indexarray_t indices = { - 0, 1, 2, // 0 - 2, 3, 0, // 1 - 1, 5, 6, // 2 - 6, 2, 1, // 3 - 5, 4, 7, // 4 - 7, 6, 5, // 5 - 4, 0, 3, // 6 - 3, 7, 4, // 7 - 3, 2, 6, // 8 - 6, 7, 3, // 9 - 4, 5, 1, // 10 - 1, 0, 4 // 11 - }; +// mesh::indexarray_t indices = { +// 0, 1, 2, // 0 +// 2, 3, 0, // 1 +// 1, 5, 6, // 2 +// 6, 2, 1, // 3 +// 5, 4, 7, // 4 +// 7, 6, 5, // 5 +// 4, 0, 3, // 6 +// 3, 7, 4, // 7 +// 3, 2, 6, // 8 +// 6, 7, 3, // 9 +// 4, 5, 1, // 10 +// 1, 0, 4 // 11 +// }; - m.set_indices(indices); +// m.set_indices(indices); return m; } diff --git a/src/scripting/src/runtime_lua.cpp b/src/scripting/src/runtime_lua.cpp index c31a556..9446f40 100644 --- a/src/scripting/src/runtime_lua.cpp +++ b/src/scripting/src/runtime_lua.cpp @@ -16,6 +16,4 @@ runtime_lua::runtime_lua() { } - - } diff --git a/src/scripting/src/runtime_lua.hpp b/src/scripting/src/runtime_lua.hpp index c711259..170a8a6 100644 --- a/src/scripting/src/runtime_lua.hpp +++ b/src/scripting/src/runtime_lua.hpp @@ -29,7 +29,14 @@ protected: }; -void register_core_lua(); +#define PW_REGISTER_LUA(name) void register_##name##_lua() { runtime_lua::get().add(#name,register_##name##_function); } + +#define PW_REGISTER_DECL_LUA(name) void register_##name##_lua(); + +#define PW_REGISTER_USE_LUA(name) register_##name##_lua(); + + + } diff --git a/src/scripting/src/script.cpp b/src/scripting/src/script.cpp index 8666e1b..e455c64 100644 --- a/src/scripting/src/script.cpp +++ b/src/scripting/src/script.cpp @@ -5,9 +5,14 @@ #include "pw/core/debug.hpp" - namespace pw { + +PW_REGISTER_DECL_LUA(core) +PW_REGISTER_DECL_LUA(system) +PW_REGISTER_DECL_LUA(scene) +PW_REGISTER_DECL_LUA(visual) + struct script::state { sol::state _state; @@ -41,7 +46,7 @@ script::state::state() // add the script as a user type _namespace.new_usertype("script", - "initialize",&state::load_all, + "load_all",&state::load_all, "load",&state::load ); @@ -58,6 +63,7 @@ void script::state::load_all() { // for (auto m : runtime_lua::get().register_functions()) { m.second(_state,_namespace); + debug::d() << "loading module " << m.first; } } @@ -81,7 +87,12 @@ int script::eval(const std::string &s) void script::initialize() { - register_core_lua(); + + PW_REGISTER_USE_LUA(core) + PW_REGISTER_USE_LUA(system) + PW_REGISTER_USE_LUA(scene) + PW_REGISTER_USE_LUA(visual) + } diff --git a/src/scripting/src/script_core.cpp b/src/scripting/src/script_core.cpp index 725ab2b..7f162a5 100644 --- a/src/scripting/src/script_core.cpp +++ b/src/scripting/src/script_core.cpp @@ -9,46 +9,52 @@ #include "runtime_lua.hpp" -namespace pw { +// seems CRTP magic doesnt work with SOL +namespace sol { + template <> struct is_automagical : std::false_type {}; + template <> struct is_automagical : std::false_type {}; + template <> struct is_automagical : std::false_type {}; +} + +namespace pw { void register_core_function(sol::state& lua,sol::table& ns) { - debug::d() << "registering "; - typedef real_t Scalar; ns.set("pi",pw::pi()); - ns.new_usertype - ( - "vector3", - sol::constructors(), - "x", sol::property(sol::resolve(&vector3::x), [](vector3& v,vector3::value_type val){ v.x() = val;}), - "y", sol::property(sol::resolve(&vector3::y), [](vector3& v,vector3::value_type val){ v.y() = val;}), - "z", sol::property(sol::resolve(&vector3::z), [](vector3& v,vector3::value_type val){ v.z() = val;}), - "cross",&vector3::cross, - "lerp",&vector3::lerp - ); + ns.new_usertype("matrix4x4" + , sol::constructors() + , "row",&matrix4x4::row + ); + ns.new_usertype("vector3" + ,sol::constructors() + ,"x", sol::property(sol::resolve(&vector3::x), [](vector3& v,vector3::value_type val){ v.x() = val;}) + ,"y", sol::property(sol::resolve(&vector3::y), [](vector3& v,vector3::value_type val){ v.y() = val;}) + ,"z", sol::property(sol::resolve(&vector3::z), [](vector3& v,vector3::value_type val){ v.z() = val;}) + ,"cross",&vector3::cross + ,"transposed",&vector3::transposed + ,"lerp",&vector3::lerp + ); - ns.new_usertype - ( - "quaternion", - sol::constructors(), - "x", sol::property(sol::resolve(&quaternion::x), [](quaternion& v,quaternion::value_type val){ v.x() = val;}), - "y", sol::property(sol::resolve(&quaternion::y), [](quaternion& v,quaternion::value_type val){ v.y() = val;}), - "z", sol::property(sol::resolve(&quaternion::z), [](quaternion& v,quaternion::value_type val){ v.z() = val;}), - "w", sol::property(sol::resolve(&quaternion::w), [](quaternion& v,quaternion::value_type val){ v.w() = val;}), - "identity",sol::readonly_property(&quaternion::identity), - "dot",&quaternion::dot, - "inverse",sol::readonly_property(&quaternion::inverse), - "normalized",&quaternion::normalized, - "lerp",&quaternion::lerp, - "slerp",&quaternion::slerp - ); + ns.new_usertype("quaternion" + , sol::constructors() + ,"x", sol::property(sol::resolve(&quaternion::x), [](quaternion& v,quaternion::value_type val){ v.x() = val;}) + ,"y", sol::property(sol::resolve(&quaternion::y), [](quaternion& v,quaternion::value_type val){ v.y() = val;}) + ,"z", sol::property(sol::resolve(&quaternion::z), [](quaternion& v,quaternion::value_type val){ v.z() = val;}) + ,"w", sol::property(sol::resolve(&quaternion::w), [](quaternion& v,quaternion::value_type val){ v.w() = val;}) + ,"identity",sol::readonly_property(&quaternion::identity) + ,"dot",&quaternion::dot + ,"inverse",sol::readonly_property(&quaternion::inverse) + ,"normalized",&quaternion::normalized + ,"lerp",&quaternion::lerp + ,"slerp",&quaternion::slerp + ); ns.new_usertype ("axisangle", @@ -86,23 +92,22 @@ void register_core_function(sol::state& lua,sol::table& ns) "reset",&timer::reset ); - ns.new_usertype - ( - "mesh", - sol::constructors() - ); + auto mesh_type = ns.new_usertype("mesh" + , sol::constructors() + , "topology", sol::property(&mesh::topology,&mesh::set_topology) + , "vertices", sol::property(&mesh::vertices,&mesh::set_vertices) + , "indices", sol::property(&mesh::indices,&mesh::set_indices) + ); + + mesh_type.new_enum("topology" + , "points", mesh::topology_type::points + , "lines", mesh::topology_type::lines + , "line_strip", mesh::topology_type::line_strip + ); - // lua["mesh"].new_enum() - // ( - // "mesh", - // sol::constructors() - // ); } +PW_REGISTER_LUA(core) -void register_core_lua() -{ - runtime_lua::get().add("core",register_core_function); -} } diff --git a/src/scripting/src/script_scene.cpp b/src/scripting/src/script_scene.cpp index f68fa2c..e49d4df 100644 --- a/src/scripting/src/script_scene.cpp +++ b/src/scripting/src/script_scene.cpp @@ -5,7 +5,7 @@ namespace pw { -void lua_register_scene(sol::state& lua,sol::table &ns) +void register_scene_function(sol::state&,sol::table &ns) { ns.new_usertype("node", @@ -21,4 +21,6 @@ void lua_register_scene(sol::state& lua,sol::table &ns) ); } +PW_REGISTER_LUA(scene) + } diff --git a/src/scripting/src/script_system.cpp b/src/scripting/src/script_system.cpp index 8160c72..12cc383 100644 --- a/src/scripting/src/script_system.cpp +++ b/src/scripting/src/script_system.cpp @@ -10,7 +10,7 @@ namespace pw { -void lua_register_system(sol::table &ns) +void register_system_function(sol::state&, sol::table &ns) { ns.new_usertype("window", "update",&window::update, @@ -40,4 +40,10 @@ void lua_register_system(sol::table &ns) } + + + +PW_REGISTER_LUA(system) + + } diff --git a/src/scripting/src/script_visual.cpp b/src/scripting/src/script_visual.cpp index d4a79f6..5d3b66e 100644 --- a/src/scripting/src/script_visual.cpp +++ b/src/scripting/src/script_visual.cpp @@ -6,7 +6,7 @@ namespace pw { -void lua_register_visual(sol::table &ns) +void register_visual_function(sol::state&,sol::table &ns) { ns.new_usertype("pipeline", @@ -16,4 +16,6 @@ void lua_register_visual(sol::table &ns) } +PW_REGISTER_LUA(visual) + } diff --git a/src/scripts/demos/simple_000.lua b/src/scripts/demos/simple_000.lua index f765587..8b47718 100644 --- a/src/scripts/demos/simple_000.lua +++ b/src/scripts/demos/simple_000.lua @@ -1,5 +1,5 @@ -- loading our libraries -pw.script:initialize() +pw.script:load_all() print("hello pixwerx!") diff --git a/src/scripts/tests/test_core.lua b/src/scripts/tests/test_core.lua index 962760b..a1cb76c 100644 --- a/src/scripts/tests/test_core.lua +++ b/src/scripts/tests/test_core.lua @@ -3,31 +3,48 @@ -- -- loading our libraries -pw.script:initialize() - -pw.script:load("core") - +pw.script:load_all() +-- vector3 local v1 = pw.vector3.new(3,2,1) - print("v1 ",v1.x,v1.y,v1.z) +-- quaternion local q = pw.quaternion.new() q = pw.quaternion.identity print("q",q.x,q.y,q.z,q.w) - qi = q.inverse print("q.inverse",qi.x,qi.y,qi.z,qi.w) - local q2 = pw.quaternion.new(0,0,0,1) -- bug! ---qm = pw.quaternion.lerp(q,qi,0.5) ---print("q.m",qm.x,qm.y,qm.z,qm.w) +-- qm = pw.quaternion.lerp(q,qi,0.5) --- axis angle test +-- axisangle local aa = pw.axisangle.new(v1,0.707) - print("aa.axis",aa.axis.x,aa.axis.y,aa.axis.z) print("aa.angle",aa.angle) +-- mesh +local somemesh = pw.mesh.new() + +somemesh.indices:add(0) +somemesh.vertices:add(pw.vector3.new(3,4,5)) + +-- +print(somemesh.vertices) +print(somemesh.indices) + + +for i=1,#somemesh.indices do + print(i," - ", somemesh.indices:get(i)) +end + + +for k,v in ipairs(somemesh.indices) do + print(k,v) +end + +for k,v in ipairs(somemesh.vertices) do + print(k,v) +end