From 803af8c37a022a5cf615c764062c9cb9080c7476 Mon Sep 17 00:00:00 2001 From: Hartmut Seichter Date: Tue, 22 Jan 2019 15:21:04 +0100 Subject: [PATCH] cleanup before reworking the scripting engine layout: --- src/core/include/pw/core/quaternion.hpp | 55 ++++++++++++++-- src/core/tests/pwcore_test_quaternion.cpp | 4 +- src/engine/CMakeLists.txt | 9 ++- src/scripting/src/script_core.cpp | 80 ++++++++++++----------- src/scripts/demos/simple_000.lua | 62 ------------------ src/scripts/tests/test_core.lua | 28 ++++++++ 6 files changed, 130 insertions(+), 108 deletions(-) create mode 100644 src/scripts/tests/test_core.lua diff --git a/src/core/include/pw/core/quaternion.hpp b/src/core/include/pw/core/quaternion.hpp index 51a62de..e3dbea5 100644 --- a/src/core/include/pw/core/quaternion.hpp +++ b/src/core/include/pw/core/quaternion.hpp @@ -36,16 +36,18 @@ struct quaternion_ : vector4_ { typedef vector4_ base_type; + using typename base_type::value_type; using base_type::base_type; using base_type::x; using base_type::y; using base_type::z; using base_type::w; using base_type::lerp; - using base_type::operator*; - using base_type::operator/; +// using base_type::operator*; +// using base_type::operator/; - quaternion_(const base_type& other) : base_type(other) {} + + quaternion_(const base_type& other) : base_type(other) {} inline const quaternion_ operator * (const quaternion_& rhs) const { return quaternion_( @@ -68,6 +70,10 @@ struct quaternion_ : vector4_ { return conjugate() / this->norm(); } + inline static quaternion_ identity() { + return quaternion_({0,0,0,1}); + } + const matrix4x4_ to_matrix() const { matrix4x4_ m; m.set_identity(); @@ -110,10 +116,49 @@ struct quaternion_ : vector4_ { wtemp); } - static const quaternion_ normalized_lerp(const quaternion_ &a,const quaternion_ &b,const T &t) { - return quaternion_(lerp(a,b,t).normalized()); + static const quaternion_ normalized_lerp(const quaternion_ &a,const quaternion_ &b,const T &t) { + return quaternion_(lerp(a,b,t).normalized()); } + const quaternion_ slerp(const quaternion_& qa,const quaternion_& qb,const T& t) + { + using std::abs; + using std::sqrt; + using std::acos; + + // quaternion to return + quaternion_ qm; + // Calculate angle between them. + T cosHalfTheta = qa.w() * qb.w() + qa.x() * qb.x() + qa.y() * qb.y() + qa.z() * qb.z(); + // if qa=qb or qa=-qb then theta = 0 and we can return qa + if (abs(cosHalfTheta) >= T(1)) { + return qa; + } + + // Calculate temporary values. + const T halfTheta = acos(cosHalfTheta); + const T sinHalfTheta = sqrt(1.0 - cosHalfTheta * cosHalfTheta); + // if theta = 180 degrees then result is not fully defined + // we could rotate around any axis normal to qa or qb + if (abs(sinHalfTheta) < 0.001){ // fabs is floating point absolute + qm.w() = (qa.w() * T(0.5) + qb.w() * T(0.5)); + qm.x() = (qa.x() * T(0.5) + qb.x() * T(0.5)); + qm.y() = (qa.y() * T(0.5) + qb.y() * T(0.5)); + qm.z() = (qa.z() * T(0.5) + qb.z() * T(0.5)); + return qm; + } + const T ratioA = sin((value_type(1) - t) * halfTheta) / sinHalfTheta; + const T ratioB = sin(t * halfTheta) / sinHalfTheta; + //calculate Quaternion. + qm.w() = (qa.w() * ratioA + qb.w() * ratioB); + qm.x() = (qa.x() * ratioA + qb.x() * ratioB); + qm.y() = (qa.y() * ratioA + qb.y() * ratioB); + qm.z() = (qa.z() * ratioA + qb.z() * ratioB); + + return qm; + } + + static const quaternion_ from_axisangle(const axisangle_ &aa) { using std::sin; diff --git a/src/core/tests/pwcore_test_quaternion.cpp b/src/core/tests/pwcore_test_quaternion.cpp index f7608c8..e04f906 100644 --- a/src/core/tests/pwcore_test_quaternion.cpp +++ b/src/core/tests/pwcore_test_quaternion.cpp @@ -15,10 +15,10 @@ int main(int argc,char **argv) { std::cout << "qf.conjugate() = " << pw::serialize::matrix(qf.conjugate()) << std::endl; pw::quaternionf qc = qf.conjugate(); - std::cout << "qf.conjugate() (qc) = " << pw::serialize::matrix(qc) << std::endl; + std::cout << "qf.conjugate() (qc) = " << pw::serialize::matrix(qc.to_matrix()) << std::endl; pw::quaternionf qi = qf.inverse(); - std::cout << "qf.inverse() (qi) = " << pw::serialize::matrix(qi) << std::endl; + std::cout << "qf.inverse() (qi) = " << pw::serialize::matrix(qi.to_matrix()) << std::endl; pw::quaternionf qmid = pw::quaternionf::normalized_lerp(qi,qf,0.5f); // std::cout << "qmid.dot() (half between qf and qi) = " << pw::rad_to_deg(quaternionf::angle_between()) << std::endl; diff --git a/src/engine/CMakeLists.txt b/src/engine/CMakeLists.txt index b3361eb..962b622 100644 --- a/src/engine/CMakeLists.txt +++ b/src/engine/CMakeLists.txt @@ -1,11 +1,16 @@ -set(scripts +set(scripts_demo ${CMAKE_SOURCE_DIR}/src/scripts/demos/simple_000.lua ) +set(scripts_test + ${CMAKE_SOURCE_DIR}/src/scripts/tests/test_core.lua + ) + add_executable(pixwerx WIN32 MACOSX_BUNDLE pixwerx.cpp - ${scripts} + ${scripts_demo} + ${scripts_test} ) target_include_directories(pixwerx diff --git a/src/scripting/src/script_core.cpp b/src/scripting/src/script_core.cpp index 109dc86..d26e81a 100644 --- a/src/scripting/src/script_core.cpp +++ b/src/scripting/src/script_core.cpp @@ -7,7 +7,7 @@ #include "pw/core/size.hpp" #include "pw/core/point.hpp" #include "pw/core/timer.hpp" - +#include "pw/core/mesh.hpp" namespace pw { @@ -19,46 +19,41 @@ void script_core::load(sol::table &ns) ns.set("pi",pw::pi()); - ns.new_usertype( - "vector3", - sol::constructors(), - "x", sol::property(sol::resolve(&vector3::x), [](vector3::value_type v){ x() = v}) - ); + 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("vector3", -// sol::constructors(), -// "x",&vector3::x -//// "set",&vector3::set, -//// "x", scripting::property(scripting::resolve(&vector3::x), &vector3::set_x), -//// "y", scripting::property(scripting::resolve(&vector3::y), &vector3::set_y), -//// "z", scripting::property(scripting::resolve(&vector3::z), &vector3::set_z), -//// "norm",&vector3::norm, -//// "cross",&vector3::cross, -//// "dot",&vector3::dot -// ); -// ns.new_usertype("quaternion", -// sol::constructors(), -// "set",&quaternion::set, -// "x", scripting::property(scripting::resolve(&quaternion::x), &quaternion::set_x), -// "y", scripting::property(scripting::resolve(&quaternion::y), &quaternion::set_y), -// "z", scripting::property(scripting::resolve(&quaternion::z), &quaternion::set_z), -// "w", scripting::property(scripting::resolve(&quaternion::w), &quaternion::set_w), -// "dot",&quaternion::dot, -// "inverse",scripting::readonly_property(&quaternion::inverse), -// "normalized",&quaternion::normalized, -// "lerp",&quaternion::lerp, -// "slerp",&quaternion::slerp -// // "v",&vector3d::values, -// // "clone",&vector3d::clone -// ); + 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",scripting::readonly_property(&quaternion::inverse), + "normalized",&quaternion::normalized, + "lerp",&quaternion::lerp, + "slerp",&quaternion::slerp + ); - ns.new_usertype("axisangle", - sol::constructors(), - "axis",scripting::property(&axisangle::axis,&axisangle::set_axis), - "angle",scripting::property(&axisangle::angle,&axisangle::set_angle) - ); + ns.new_usertype + ("axisangle", + sol::constructors(), + "axis",scripting::property(&axisangle::axis,&axisangle::set_axis), + "angle",scripting::property(&axisangle::angle,&axisangle::set_angle) + ); ns.new_usertype("size", @@ -89,6 +84,17 @@ void script_core::load(sol::table &ns) "reset",&timer::reset ); + ns.new_usertype + ( + "mesh", + sol::constructors() + ); + +// ns.new_enum +// ( +// "mesh", +// sol::constructors() +// ); } diff --git a/src/scripts/demos/simple_000.lua b/src/scripts/demos/simple_000.lua index 2145f44..f765587 100644 --- a/src/scripts/demos/simple_000.lua +++ b/src/scripts/demos/simple_000.lua @@ -3,68 +3,6 @@ pw.script:initialize() print("hello pixwerx!") -local v1 = pw.vector3.new(3,2,1) -v1:set(0,1,2) - -print("v1 ",v1.x,v1.y,v1.z) - - ----- objects need to be cloned -----local v2 = v:clone() ---local v2 = v - ----- manipulate stuff ---v.x = 0.2 ---v.y = pw.pi - - ---print("v : ", v:v()) ---print("v2: ", v2:v()) - -local q = pw.quaternion.new() -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) - -qm = pw.quaternion.lerp(q,qi,0.5) -print("q.m",qm.x,qm.y,qm.z,qm.w) - --- axis angle test -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) - -local n_1 = pw.node.create() -n_1.name = "root" - -print("node 1: ", n_1.name) - ---print(pw.node.create()) - -n_1:add_child(pw.node.create()).name = "one" -n_1:add_child(pw.node.create()).name = "two" -n_1:add_child(pw.node.create()).name = "three" -n_1:add_child(pw.node.create()).name = "four" -n_1:add_child(pw.node.create()).name = "five" - ---n_1:add_child(n_2:shared()) - -print("node 1 - child count ",n_1.child_count) - --- stuff -for i = 1,n_1.child_count do - print(i,n_1.children[i],n_1.children[i].name) -end - ---print(n_1:shared()) ---print(pw.my_func()) - ---n_1:add_child() - local w = pw.window.new() w.visible = false diff --git a/src/scripts/tests/test_core.lua b/src/scripts/tests/test_core.lua new file mode 100644 index 0000000..c766e7d --- /dev/null +++ b/src/scripts/tests/test_core.lua @@ -0,0 +1,28 @@ +-- loading our libraries +pw.script:initialize() + +print("hello pixwerx!") + +local v1 = pw.vector3.new(3,2,1) + +print("v1 ",v1.x,v1.y,v1.z) + +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) + +-- axis angle test +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) +