major overhaul for Lua API and additions for using texture coordinates

Signed-off-by: Hartmut Seichter <hartmut@technotecture.com>
This commit is contained in:
Hartmut Seichter 2021-02-02 23:23:40 +01:00
parent 939195b851
commit 749bb67c6c
10 changed files with 242 additions and 142 deletions

View file

@ -31,7 +31,7 @@ vector3 table_to_vector3(sol::table t)
}
template <typename elementType>
std::vector<elementType> convert_sequence(sol::table t)
std::vector<elementType> convert_sequence(sol::state& lua,sol::table t)
{
const std::size_t sz = t.size();
std::vector<elementType> res(sz);
@ -46,46 +46,54 @@ std::vector<elementType> convert_sequence(sol::table t)
void register_core_function(sol::state& lua,sol::table& ns)
{
typedef real_t Scalar;
ns.set("pi",pw::pi<Scalar>());
ns.set("pi",pw::pi<real_t>());
ns.new_usertype<matrix4x4>("matrix4x4"
, sol::call_constructor,sol::constructors<matrix4x4()>()
, "row",&matrix4x4::row
, "column",&matrix4x4::column
, "set_identity",&matrix4x4::set_identity
, "inverse",&matrix4x4::inverse
, "identity",sol::readonly_property(matrix4x4::identity)
// , "set",[](sol::this_state s,sol::table t) { for (int i = 0;i < t.size();i++) s.Ldata[i] = t[i-1]; })
// , "get",[]() ->sol::table { return sol::table(); }
);
ns.new_usertype<vector2>("vector2"
,sol::call_constructor,sol::constructors<vector2(),vector2(Scalar,Scalar)>()
,"x", sol::property(sol::resolve<const vector2::value_type&() const>(&vector2::x), [](vector2& v,vector2::value_type val){ v.x() = val;})
,"y", sol::property(sol::resolve<const vector2::value_type&() const>(&vector2::y), [](vector2& v,vector2::value_type val){ v.y() = val;})
ns.new_usertype<matrix4x4>("matrix4x4",
sol::call_constructor,sol::constructors<matrix4x4()>(),
"row",&matrix4x4::row,
"column",&matrix4x4::column,
"inverse",sol::readonly_property(&matrix4x4::inverse),
"identity",sol::readonly_property(matrix4x4::identity),
sol::meta_function::multiplication,[](const matrix4x4& a,const vector4& b) { return vector4(a * b); }
);
ns.new_usertype<vector2>("vector2",
sol::call_constructor,sol::constructors<vector2(),vector2(real_t,real_t)>(),
"x", sol::property(sol::resolve<const vector2::value_type&() const>(&vector2::x), [](vector2& v,vector2::value_type val){ v.x() = val;}),
"y", sol::property(sol::resolve<const vector2::value_type&() const>(&vector2::y), [](vector2& v,vector2::value_type val){ v.y() = val;}),
"dot",&vector2::dot,
sol::meta_function::addition,[](const vector2& a,const vector2& b){ return vector2(a + b); },
sol::meta_function::subtraction,[](const vector2& a,const vector2& b){ return vector2(a - b); },
"data",sol::property([](vector2& s) { return std::ref(s.data);} ),
"table",sol::property([](const vector2& s){ return sol::as_table(std::array<vector2::value_type,2>{s.x(),s.y()}); },
[](vector2& s,const sol::table& t) { s.data[0] = t[1]; s.data[1] = t[2];} )
);
ns.new_usertype<vector3>("vector3"
,sol::constructors<vector3(),vector3(Scalar,Scalar,Scalar)>()
,sol::call_constructor,sol::constructors<vector3(),vector3(real_t,real_t,real_t)>()
,"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
,"normalize",&vector3::normalize
,"norm",sol::readonly_property(&vector3::norm)
,"squared_norm",sol::readonly_property(&vector3::squared_norm)
,"dot",&vector3::dot
,"transposed",sol::readonly_property(&vector3::transposed)
,"normalized",sol::readonly_property(&vector3::normalized)
,"lerp",&vector3::lerp
,"forward",&vector3::forward
,"backward",&vector3::backward
,"left",&vector3::left
,"right",&vector3::right
,"up",&vector3::up
,"down",&vector3::down
,sol::meta_function::addition,&vector3::operator+
,sol::meta_function::subtraction,&vector3::operator-
,sol::meta_function::multiplication,&vector3::operator*
,"forward",sol::readonly_property(&vector3::forward)
,"backward",sol::readonly_property(&vector3::backward)
,"left",sol::readonly_property(&vector3::left)
,"right",sol::readonly_property(&vector3::right)
,"up",sol::readonly_property(&vector3::up)
,"down",sol::readonly_property(&vector3::down)
,sol::meta_function::addition,[](const vector3& a,const vector3& b){ return vector3(a + b); }
,sol::meta_function::subtraction,[](const vector3& a,const vector3& b){ return vector3(a - b); }
,"data",sol::property([](vector3& s) { return std::ref(s.data);} )
,"table",sol::property([](const vector3& s){ return sol::as_table(std::array<vector3::value_type,3>{s.x(),s.y(),s.z()}); },
[](vector3& s,const sol::table& t) { s.data[0] = t[1]; s.data[1] = t[2]; s.data[2] = t[3];} )
);
ns.new_usertype<vector4>("vector4"
@ -94,50 +102,51 @@ void register_core_function(sol::state& lua,sol::table& ns)
,"y", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::y), [](vector4& v,vector4::value_type val){ v.y() = val;})
,"z", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::z), [](vector4& v,vector4::value_type val){ v.z() = val;})
,"w", sol::property(sol::resolve<const vector4::value_type&() const>(&vector4::w), [](vector4& v,vector4::value_type val){ v.w() = val;})
,"project",&vector4::project
,"project",sol::readonly_property(&vector4::project)
,sol::meta_function::multiplication,[](const matrix4x4& a,const vector4& b) { return vector4(a * b); }
);
ns.new_usertype<quaternion>("quaternion"
,sol::constructors<quaternion(), quaternion(quaternion::value_type,quaternion::value_type,quaternion::value_type,quaternion::value_type)>()
,"x", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::x), [](quaternion& v,quaternion::value_type val){ v.x() = val;})
,"y", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::y), [](quaternion& v,quaternion::value_type val){ v.y() = val;})
,"z", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::z), [](quaternion& v,quaternion::value_type val){ v.z() = val;})
,"w", sol::property(sol::resolve<const quaternion::value_type&() const>(&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
,"matrix",&quaternion::to_matrix
);
ns.new_usertype<quaternion>("quaternion",
sol::call_constructor,sol::constructors<quaternion(), quaternion(quaternion::value_type,quaternion::value_type,quaternion::value_type,quaternion::value_type)>(),
"x", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::x), [](quaternion& v,quaternion::value_type val){ v.x() = val;}),
"y", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::y), [](quaternion& v,quaternion::value_type val){ v.y() = val;}),
"z", sol::property(sol::resolve<const quaternion::value_type&() const>(&quaternion::z), [](quaternion& v,quaternion::value_type val){ v.z() = val;}),
"w", sol::property(sol::resolve<const quaternion::value_type&() const>(&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",sol::readonly_property([](const quaternion& q){ return quaternion{ q.normalized() };}),
"lerp",[](const quaternion& a,const quaternion& b,quaternion::value_type t) { return quaternion{ quaternion::lerp(a,b,t) }; },
"slerp",[](const quaternion& a,const quaternion& b,quaternion::value_type t) { return quaternion{ quaternion::slerp(a,b,t) }; },
"to_matrix",sol::readonly_property(&quaternion::to_matrix)
);
ns.new_usertype<axisangle>("axisangle",
sol::constructors<axisangle(), axisangle(vector3,Scalar)>(),
"axis",&axisangle::axis,
"angle",&axisangle::angle,
sol::call_constructor,sol::constructors<axisangle(), axisangle(vector3,axisangle::value_type)>(),
"axis",sol::property(&axisangle::axis,&axisangle::axis),
"angle",sol::property(&axisangle::angle,&axisangle::angle),
"from_matrix",&axisangle::from_matrix,
"to_matrix",&axisangle::to_matrix
);
ns.new_usertype<size>("size"
, sol::call_constructor, sol::constructors<size(),size(Scalar,Scalar)>()
, sol::call_constructor, sol::constructors<size(),size(size::value_type,size::value_type)>()
, "width",&size::width
, "height",&size::height
, "cast_to_float",&size::cast<float>
);
ns.new_usertype<sizef>("sizef"
, sol::call_constructor, sol::constructors<sizef(),sizef(Scalar,Scalar)>()
, sol::call_constructor, sol::constructors<sizef(),sizef(sizef::value_type,sizef::value_type)>()
, "width",&sizef::width
, "height",&sizef::height
, "cast_to_int",&sizef::cast<int>
);
ns.new_usertype<point>("point",
sol::constructors<point(),point(Scalar,Scalar)>(),
sol::call_constructor,sol::constructors<point(),point(point::value_type,point::value_type)>(),
"x",&point::x,
"y",&point::y
);
@ -157,15 +166,33 @@ void register_core_function(sol::state& lua,sol::table& ns)
"reset",&time::reset
);
ns.new_usertype<geometry>("geometry"
,sol::constructors<geometry(),geometry(geometry::primitive_topology_type,vector3_array,geometry::indices_t)>()
,"primitive_topology", sol::property(&geometry::primitive_topology,&geometry::set_primitive_topology)
// ,"vertices", sol::property(&geometry::ref_vertices,&geometry::ref_vertices)
,"vertices", sol::property(&geometry::vertices,&geometry::set_vertices)
,"indices", sol::property(&geometry::ref_indices,&geometry::ref_indices)
// ,"texture_coordinates", sol::property(&geometry::ref_texture_coordinates,&geometry::ref_texture_coordinates)
,"compute_normals", &geometry::compute_normals
);
ns.new_usertype<geometry>("geometry",
sol::call_constructor,sol::constructors<geometry(),geometry(geometry::primitive_topology_type,vector3_array,geometry::indices_t)>(),
"primitive_topology", sol::property(&geometry::primitive_topology,&geometry::set_primitive_topology),
"compute_normals", &geometry::compute_normals,
"indices",sol::property( [](const geometry& g){ return sol::as_table(g.indices());},
[](geometry& g,sol::table t){
std::vector<uint32_t> vals;
for (std::size_t i = 0; i < t.size();i++) vals.push_back(t[i+1]);
g.set_indices(vals); }),
"vertices",sol::property( [](const geometry& g){ return sol::as_table(g.vertices());},
[](geometry &g,sol::nested<vector3_array> val) {
g.set_vertices(val.value());
}
),
"normals",sol::property( [](const geometry& g){ return sol::as_table(g.vertices());},
[](geometry& g,sol::table t){
vector3_array vals;
for (std::size_t i = 0; i < t.size();i++) vals.push_back(t[i+1]);
g.set_normals(vals); }
),
"texture_coordinates",sol::property(
[](const geometry& g){ return sol::as_table(g.texture_coordinates());},
[](geometry &g,sol::nested<std::vector<vector2_array>> val)
{
g.set_texture_coordinates(val.value());
})
);
ns.new_enum<false>("primitive_topology_type"