somewhat better working registration system for the lua runtime

This commit is contained in:
Hartmut Seichter 2019-01-24 15:52:03 +01:00
parent 6ee3732994
commit baa209ceea
13 changed files with 141 additions and 91 deletions

View file

@ -57,7 +57,7 @@ struct matrix_ : matrixbase_<T, matrix_<R, C, T>>
return *this; return *this;
} }
matrix_(std::initializer_list<T> args) explicit matrix_(std::initializer_list<T> args)
{ {
typename std::initializer_list<T>::iterator it = args.begin(); typename std::initializer_list<T>::iterator it = args.begin();
for (;it != args.end();it++) data[it-args.begin()] = *it; for (;it != args.end();it++) data[it-args.begin()] = *it;
@ -235,7 +235,6 @@ auto operator * (const matrix_<R, Ca, T>& A,
// //
// //
template <typename T> using matrix2x2_ = matrix_<2, 2, T>; template <typename T> using matrix2x2_ = matrix_<2, 2, T>;
template <typename T> using matrix3x3_ = matrix_<3, 3, T>; template <typename T> using matrix3x3_ = matrix_<3, 3, T>;
template <typename T> using matrix4x4_ = matrix_<4, 4, T>; template <typename T> using matrix4x4_ = matrix_<4, 4, T>;

View file

@ -66,12 +66,15 @@ struct vector3_ : matrix_<3,1,T> {
inline const T& x() const { return (*this)[0]; } inline const T& x() const { return (*this)[0]; }
inline T& x() { 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 const T& y() const { return (*this)[1]; }
inline T& y() { 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 const T& z() const { return (*this)[2]; }
inline T& z() { 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 xy() const { return vector2_( { x(),y() } ); }
inline auto homogenous(T w = 1) const { return matrix_<4,1,T>( { x(),y(),z(),w } ); } inline auto homogenous(T w = 1) const { return matrix_<4,1,T>( { x(),y(),z(),w } ); }

View file

@ -6,11 +6,11 @@
int main(int argc,char **argv) { int main(int argc,char **argv) {
pw::vector2_<float> v2_A = { 3.2, 1.2 }; // pw::vector2_<float> v2_A = { 3.2, 1.2 };
pw::vector2_<float> v2_B = { 3.2, 1.2 }; // pw::vector2_<float> 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);

View file

@ -8,32 +8,32 @@ mesh primitives::box(real_t size_x,real_t size_y, real_t size_z)
mesh::vertex3array_t vertices; 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 } ); // 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 } ); // 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 } ); // 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} ); // 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} ); // 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}); // 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}); // 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}); // 7
mesh::indexarray_t indices = { // mesh::indexarray_t indices = {
0, 1, 2, // 0 // 0, 1, 2, // 0
2, 3, 0, // 1 // 2, 3, 0, // 1
1, 5, 6, // 2 // 1, 5, 6, // 2
6, 2, 1, // 3 // 6, 2, 1, // 3
5, 4, 7, // 4 // 5, 4, 7, // 4
7, 6, 5, // 5 // 7, 6, 5, // 5
4, 0, 3, // 6 // 4, 0, 3, // 6
3, 7, 4, // 7 // 3, 7, 4, // 7
3, 2, 6, // 8 // 3, 2, 6, // 8
6, 7, 3, // 9 // 6, 7, 3, // 9
4, 5, 1, // 10 // 4, 5, 1, // 10
1, 0, 4 // 11 // 1, 0, 4 // 11
}; // };
m.set_indices(indices); // m.set_indices(indices);
return m; return m;
} }

View file

@ -16,6 +16,4 @@ runtime_lua::runtime_lua()
{ {
} }
} }

View file

@ -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();
} }

View file

@ -5,9 +5,14 @@
#include "pw/core/debug.hpp" #include "pw/core/debug.hpp"
namespace pw { 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 { struct script::state {
sol::state _state; sol::state _state;
@ -41,7 +46,7 @@ script::state::state()
// add the script as a user type // add the script as a user type
_namespace.new_usertype<state>("script", _namespace.new_usertype<state>("script",
"initialize",&state::load_all, "load_all",&state::load_all,
"load",&state::load "load",&state::load
); );
@ -58,6 +63,7 @@ void script::state::load_all() {
// //
for (auto m : runtime_lua::get().register_functions()) { for (auto m : runtime_lua::get().register_functions()) {
m.second(_state,_namespace); m.second(_state,_namespace);
debug::d() << "loading module " << m.first;
} }
} }
@ -81,7 +87,12 @@ int script::eval(const std::string &s)
void script::initialize() 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)
} }

View file

@ -9,46 +9,52 @@
#include "runtime_lua.hpp" #include "runtime_lua.hpp"
namespace pw {
// seems CRTP magic doesnt work with SOL
namespace sol {
template <> struct is_automagical<pw::matrix4x4> : std::false_type {};
template <> struct is_automagical<pw::vector3> : std::false_type {};
template <> struct is_automagical<pw::quaternion> : std::false_type {};
}
namespace pw {
void register_core_function(sol::state& lua,sol::table& ns) void register_core_function(sol::state& lua,sol::table& ns)
{ {
debug::d() << "registering ";
typedef real_t Scalar; typedef real_t Scalar;
ns.set("pi",pw::pi<Scalar>()); ns.set("pi",pw::pi<Scalar>());
ns.new_usertype<vector3>
(
"vector3",
sol::constructors<vector3(),vector3(vector3::value_type,vector3::value_type,vector3::value_type)>(),
"x", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::x), [](vector3& v,vector3::value_type val){ v.x() = val;}),
"y", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::y), [](vector3& v,vector3::value_type val){ v.y() = val;}),
"z", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::z), [](vector3& v,vector3::value_type val){ v.z() = val;}),
"cross",&vector3::cross,
"lerp",&vector3::lerp
);
ns.new_usertype<matrix4x4>("matrix4x4"
, sol::constructors<matrix4x4()>()
, "row",&matrix4x4::row
);
ns.new_usertype<vector3>("vector3"
,sol::constructors<vector3(),vector3(Scalar,Scalar,Scalar)>()
,"x", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::x), [](vector3& v,vector3::value_type val){ v.x() = val;})
,"y", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::y), [](vector3& v,vector3::value_type val){ v.y() = val;})
,"z", sol::property(sol::resolve<const vector3::value_type&() const>(&vector3::z), [](vector3& v,vector3::value_type val){ v.z() = val;})
,"cross",&vector3::cross
,"transposed",&vector3::transposed
,"lerp",&vector3::lerp
);
ns.new_usertype<quaternion> ns.new_usertype<quaternion>("quaternion"
( , sol::constructors<quaternion(), quaternion(quaternion::value_type,quaternion::value_type,quaternion::value_type,quaternion::value_type)>()
"quaternion", ,"x", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::x), [](quaternion& v,quaternion::value_type val){ v.x() = val;})
sol::constructors<quaternion(), quaternion(quaternion::value_type,quaternion::value_type,quaternion::value_type,quaternion::value_type)>(), ,"y", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::y), [](quaternion& v,quaternion::value_type val){ v.y() = val;})
"x", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::x), [](quaternion& v,quaternion::value_type val){ v.x() = val;}), ,"z", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::z), [](quaternion& v,quaternion::value_type val){ v.z() = val;})
"y", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::y), [](quaternion& v,quaternion::value_type val){ v.y() = val;}), ,"w", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::w), [](quaternion& v,quaternion::value_type val){ v.w() = val;})
"z", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::z), [](quaternion& v,quaternion::value_type val){ v.z() = val;}), ,"identity",sol::readonly_property(&quaternion::identity)
"w", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::w), [](quaternion& v,quaternion::value_type val){ v.w() = val;}), ,"dot",&quaternion::dot
"identity",sol::readonly_property(&quaternion::identity), ,"inverse",sol::readonly_property(&quaternion::inverse)
"dot",&quaternion::dot, ,"normalized",&quaternion::normalized
"inverse",sol::readonly_property(&quaternion::inverse), ,"lerp",&quaternion::lerp
"normalized",&quaternion::normalized, ,"slerp",&quaternion::slerp
"lerp",&quaternion::lerp, );
"slerp",&quaternion::slerp
);
ns.new_usertype<axisangle> ns.new_usertype<axisangle>
("axisangle", ("axisangle",
@ -86,23 +92,22 @@ void register_core_function(sol::state& lua,sol::table& ns)
"reset",&timer::reset "reset",&timer::reset
); );
ns.new_usertype<mesh> auto mesh_type = ns.new_usertype<mesh>("mesh"
( , sol::constructors<mesh()>()
"mesh", , "topology", sol::property(&mesh::topology,&mesh::set_topology)
sol::constructors<mesh()>() , "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<mesh()>()
// );
} }
PW_REGISTER_LUA(core)
void register_core_lua()
{
runtime_lua::get().add("core",register_core_function);
}
} }

View file

@ -5,7 +5,7 @@
namespace pw { 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>("node", ns.new_usertype<node>("node",
@ -21,4 +21,6 @@ void lua_register_scene(sol::state& lua,sol::table &ns)
); );
} }
PW_REGISTER_LUA(scene)
} }

View file

@ -10,7 +10,7 @@
namespace pw { namespace pw {
void lua_register_system(sol::table &ns) void register_system_function(sol::state&, sol::table &ns)
{ {
ns.new_usertype<window>("window", ns.new_usertype<window>("window",
"update",&window::update, "update",&window::update,
@ -40,4 +40,10 @@ void lua_register_system(sol::table &ns)
} }
PW_REGISTER_LUA(system)
} }

View file

@ -6,7 +6,7 @@
namespace pw { namespace pw {
void lua_register_visual(sol::table &ns) void register_visual_function(sol::state&,sol::table &ns)
{ {
ns.new_usertype<pipeline>("pipeline", ns.new_usertype<pipeline>("pipeline",
@ -16,4 +16,6 @@ void lua_register_visual(sol::table &ns)
} }
PW_REGISTER_LUA(visual)
} }

View file

@ -1,5 +1,5 @@
-- loading our libraries -- loading our libraries
pw.script:initialize() pw.script:load_all()
print("hello pixwerx!") print("hello pixwerx!")

View file

@ -3,31 +3,48 @@
-- --
-- loading our libraries -- loading our libraries
pw.script:initialize() pw.script:load_all()
pw.script:load("core")
-- vector3
local v1 = pw.vector3.new(3,2,1) local v1 = pw.vector3.new(3,2,1)
print("v1 ",v1.x,v1.y,v1.z) print("v1 ",v1.x,v1.y,v1.z)
-- quaternion
local q = pw.quaternion.new() local q = pw.quaternion.new()
q = pw.quaternion.identity q = pw.quaternion.identity
print("q",q.x,q.y,q.z,q.w) print("q",q.x,q.y,q.z,q.w)
qi = q.inverse qi = q.inverse
print("q.inverse",qi.x,qi.y,qi.z,qi.w) print("q.inverse",qi.x,qi.y,qi.z,qi.w)
local q2 = pw.quaternion.new(0,0,0,1) local q2 = pw.quaternion.new(0,0,0,1)
-- bug! -- bug!
--qm = pw.quaternion.lerp(q,qi,0.5) -- qm = pw.quaternion.lerp(q,qi,0.5)
--print("q.m",qm.x,qm.y,qm.z,qm.w)
-- axis angle test -- axisangle
local aa = pw.axisangle.new(v1,0.707) local aa = pw.axisangle.new(v1,0.707)
print("aa.axis",aa.axis.x,aa.axis.y,aa.axis.z) print("aa.axis",aa.axis.x,aa.axis.y,aa.axis.z)
print("aa.angle",aa.angle) 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