diff --git a/CMakeLists.txt b/CMakeLists.txt index c23cca3..f304ab2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,6 @@ +# +# CMake build system for pixwerx +# cmake_minimum_required(VERSION 3.8) project(pixwerx) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3de2253..0016544 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,6 +3,7 @@ add_subdirectory(deps) add_subdirectory(core) add_subdirectory(scene) +#add_subdirectory(system) add_subdirectory(ui) add_subdirectory(scripting) add_subdirectory(engine) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 1a20e78..2ec0ae7 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -1,4 +1,38 @@ -add_subdirectory(src) +set(hdrs + include/pw/core/axisangle.hpp + include/pw/core/core.hpp + include/pw/core/math.hpp + include/pw/core/matrixbase.hpp + include/pw/core/matrix.hpp + include/pw/core/vector.hpp + include/pw/core/quaternion.hpp + include/pw/core/serialize.hpp + include/pw/core/image.hpp + include/pw/core/globals.hpp + ) + +set(srcs + src/core.cpp + src/serialize.cpp + ) + +add_library(pwcore + STATIC + ${hdrs} + ${srcs} + ) + +target_include_directories( + pwcore + PUBLIC + include + ) + +target_link_libraries(pwcore) + + + +#add_subdirectory(src) add_subdirectory(tests) diff --git a/src/core/include/pw/core/axisangle.hpp b/src/core/include/pw/core/axisangle.hpp index 9fbf8c3..52bedb7 100644 --- a/src/core/include/pw/core/axisangle.hpp +++ b/src/core/include/pw/core/axisangle.hpp @@ -12,7 +12,10 @@ protected: T _angle; public: - axisangle() {} + axisangle() + : _axis(vector3::up()), + _angle(0) + {} axisangle(const vector3 &axis,const T &angle) : _axis(axis) diff --git a/src/core/include/pw/core/math.hpp b/src/core/include/pw/core/math.hpp index 2281117..6c828dd 100644 --- a/src/core/include/pw/core/math.hpp +++ b/src/core/include/pw/core/math.hpp @@ -10,8 +10,6 @@ const static double __PW_PI = 3.141592653589793238462643383279502884197169399375 template inline const T pi() { return static_cast(__PW_PI); } - - template inline static double rad_to_deg(const T& angle_in_radian) { return static_cast(angle_in_radian * T(180.) / pi()); diff --git a/src/core/include/pw/core/quaternion.hpp b/src/core/include/pw/core/quaternion.hpp index 7bc57d4..d2ec8bd 100644 --- a/src/core/include/pw/core/quaternion.hpp +++ b/src/core/include/pw/core/quaternion.hpp @@ -37,135 +37,135 @@ namespace pw { template class quaternion { - static const T _sqrt90; + static const T _sqrt90; public: - typedef vector4 coefficient_type; - typedef T value_type; + typedef vector4 coefficient_type; + typedef T value_type; - quaternion() { *this = identity(); } + quaternion() { *this = identity(); } - quaternion(const T& x,const T& y,const T& z,const T& w) - : _q(coefficient_type(x,y,z,w)) {} + quaternion(const T& x,const T& y,const T& z,const T& w) + : _q(coefficient_type(x,y,z,w)) {} - quaternion(const coefficient_type& vec) { *this = vec; } + quaternion(const coefficient_type& vec) { *this = vec; } - inline quaternion& operator = (const coefficient_type& vec) { _q = vec; return *this; } + inline quaternion& operator = (const coefficient_type& vec) { _q = vec; return *this; } - inline void set(const T& x,const T& y,const T& z,const T& w) { - _q.set(x,y,z,w); - } + inline void set(const T& x,const T& y,const T& z,const T& w) { + _q.set(x,y,z,w); + } - inline void set_x(const T& v) { x() = v; } - inline void set_y(const T& v) { y() = v; } - inline void set_z(const T& v) { z() = v; } - inline void set_w(const T& v) { w() = v; } + inline void set_x(const T& v) { x() = v; } + inline void set_y(const T& v) { y() = v; } + inline void set_z(const T& v) { z() = v; } + inline void set_w(const T& v) { w() = v; } - inline const coefficient_type& as_vector() const { return _q; } + inline const coefficient_type& as_vector() const { return _q; } - inline T& x() { return _q.x(); } - inline T& y() { return _q.x(); } - inline T& z() { return _q.z(); } - inline T& w() { return _q.w(); } + inline T& x() { return _q.x(); } + inline T& y() { return _q.x(); } + inline T& z() { return _q.z(); } + inline T& w() { return _q.w(); } - inline const T& x() const { return _q.z(); } - inline const T& y() const { return _q.y(); } - inline const T& z() const { return _q.z(); } - inline const T& w() const { return _q.w(); } + inline const T& x() const { return _q.z(); } + inline const T& y() const { return _q.y(); } + inline const T& z() const { return _q.z(); } + inline const T& w() const { return _q.w(); } - //! multiplication - inline const quaternion operator * (const quaternion& rhs) const { - return quaternion( - rhs.w()*x() + rhs.x()*w() + rhs.y()*z() - rhs.z()*y(), - rhs.w()*y() - rhs.x()*z() + rhs.y()*w() + rhs.z()*x(), - rhs.w()*z() + rhs.x()*y() - rhs.y()*x() + rhs.z()*w(), - rhs.w()*w() - rhs.x()*x() - rhs.y()*y() - rhs.z()*z() - ); - } + //! multiplication + inline const quaternion operator * (const quaternion& rhs) const { + return quaternion( + rhs.w()*x() + rhs.x()*w() + rhs.y()*z() - rhs.z()*y(), + rhs.w()*y() - rhs.x()*z() + rhs.y()*w() + rhs.z()*x(), + rhs.w()*z() + rhs.x()*y() - rhs.y()*x() + rhs.z()*w(), + rhs.w()*w() - rhs.x()*x() - rhs.y()*y() - rhs.z()*z() + ); + } - //! multiply with scalar - inline const quaternion operator * (const T& s) const { - return quaternion(x()*s,y()*s,z()*s,w()*s); - } + //! multiply with scalar + inline const quaternion operator * (const T& s) const { + return quaternion(x()*s,y()*s,z()*s,w()*s); + } - //! addition - inline const quaternion operator + (const quaternion& rhs) const { - return quaternion(coefficient_type(this->_q + rhs._q)); - } + //! addition + inline const quaternion operator + (const quaternion& rhs) const { + return quaternion(coefficient_type(this->_q + rhs._q)); + } - //! addition - inline const quaternion operator - (const quaternion& rhs) const { - return quaternion(this->_q - rhs._q); - } + //! addition + inline const quaternion operator - (const quaternion& rhs) const { + return quaternion(this->_q - rhs._q); + } - //! squared norm - inline const T squared_norm() const { return _q.squared_norm(); } + //! squared norm + inline const T squared_norm() const { return _q.squared_norm(); } - //! norm - inline const T norm() const { return _q.norm(); } + //! norm + inline const T norm() const { return _q.norm(); } - //! dot product - inline const T dot(const quaternion& other) const { return _q.dot(other._q); } + //! dot product + inline const T dot(const quaternion& other) const { return _q.dot(other._q); } - //! conjugate - inline quaternion conjugate() const { return quaternion(-x(),-y(),-z(),w()); } + //! conjugate + inline quaternion conjugate() const { return quaternion(-x(),-y(),-z(),w()); } - //! compute inverse - inline quaternion inverse() const { - const T one_over_squared_norm = T(1) / squared_norm(); - return conjugate() * one_over_squared_norm; - } + //! compute inverse + inline quaternion inverse() const { + const T one_over_squared_norm = T(1) / squared_norm(); + return conjugate() * one_over_squared_norm; + } - //! compute normalized - inline quaternion normalized() const { - return quaternion(_q.normalized()); - } + //! compute normalized + inline quaternion normalized() const { + return quaternion(_q.normalized()); + } - inline void normalize() { *this = this->normalized(); } + inline void normalize() { *this = this->normalized(); } - //! conversion from a matrix - inline static const quaternion from_matrix(const matrix<4,4,T> &m); + //! conversion from a matrix + inline static const quaternion from_matrix(const matrix<4,4,T> &m); - //! conversion to a matrix - const matrix<4,4,T> to_matrix() const; + //! conversion to a matrix + const matrix<4,4,T> to_matrix() const; - //! return identiy quaternion - static const quaternion identity(); + //! return identiy quaternion + static const quaternion identity(); - static const quaternion rotate_180_degree_around_x(); ///< rotate 180 degree around X axis - static const quaternion rotate_180_degree_around_y(); ///< rotate 180 degree around Y axis - static const quaternion rotate_180_degree_around_z(); ///< rotate 180 degree around Z axis + static const quaternion rotate_180_degree_around_x(); ///< rotate 180 degree around X axis + static const quaternion rotate_180_degree_around_y(); ///< rotate 180 degree around Y axis + static const quaternion rotate_180_degree_around_z(); ///< rotate 180 degree around Z axis - static const quaternion rotate_90_degree_around_x(bool negative = false); - static const quaternion rotate_90_degree_around_y(bool negative = false); - static const quaternion rotate_90_degree_around_z(bool negative = false); + static const quaternion rotate_90_degree_around_x(bool negative = false); + static const quaternion rotate_90_degree_around_y(bool negative = false); + static const quaternion rotate_90_degree_around_z(bool negative = false); - template - static const quaternion from_axisangle(const AxisAngleType &aa) { + template + static const quaternion from_axisangle(const AxisAngleType &aa) { - using std::sin; - using std::cos; + using std::sin; + using std::cos; - const T sinHalfAngle(sin(aa.angle() * T(0.5) )); + const T sinHalfAngle(sin(aa.angle() * T(0.5) )); - return quaternion(aa.axis().x() * sinHalfAngle, // x - aa.axis().y() * sinHalfAngle, // y - aa.axis().z() * sinHalfAngle, // z - cos(aa.angle() * 0.5) // w - ); + return quaternion(aa.axis().x() * sinHalfAngle, // x + aa.axis().y() * sinHalfAngle, // y + aa.axis().z() * sinHalfAngle, // z + cos(aa.angle() * 0.5) // w + ); - } + } - static const quaternion lerp(const quaternion& qa,const quaternion& qb,const T& t); - static const quaternion normalized_lerp(const quaternion& qa,const quaternion& qb,const T& t); - static const quaternion slerp(const quaternion& qa,const quaternion& qb,const T& t); + static const quaternion lerp(const quaternion& qa,const quaternion& qb,const T& t); + static const quaternion normalized_lerp(const quaternion& qa,const quaternion& qb,const T& t); + static const quaternion slerp(const quaternion& qa,const quaternion& qb,const T& t); protected: - coefficient_type _q; + coefficient_type _q; }; template @@ -175,138 +175,138 @@ const T quaternion::_sqrt90 = std::sqrt(0.5); template const quaternion quaternion::from_matrix(const matrix<4,4,T> &m) { - using std::sqrt; + using std::sqrt; - T wtemp = sqrt(T(1) + m.at(0,0) + m.at(1,1) + m.at(2,2)) / T(2.0); + T wtemp = sqrt(T(1) + m.at(0,0) + m.at(1,1) + m.at(2,2)) / T(2.0); - const T w4 = T(4.0) * wtemp; - return quaternion( - (m.at(2,1) - m.at(1,2)) / w4, - (m.at(0,2) - m.at(2,0)) / w4, - (m.at(1,0) - m.at(0,1)) / w4, - wtemp); + const T w4 = T(4.0) * wtemp; + return quaternion( + (m.at(2,1) - m.at(1,2)) / w4, + (m.at(0,2) - m.at(2,0)) / w4, + (m.at(1,0) - m.at(0,1)) / w4, + wtemp); } template const matrix<4,4,T> quaternion::to_matrix() const { - matrix<4,4,T> m; m.set_identity(); + matrix<4,4,T> m; m.set_identity(); - T xx = x() * x(); - T xy = x() * y(); - T xz = x() * z(); - T xw = x() * w(); + T xx = x() * x(); + T xy = x() * y(); + T xz = x() * z(); + T xw = x() * w(); - T yy = y() * y(); - T yz = y() * z(); - T yw = y() * w(); + T yy = y() * y(); + T yz = y() * z(); + T yw = y() * w(); - T zz = z() * z(); - T zw = z() * w(); + T zz = z() * z(); + T zw = z() * w(); - m.at(0,0) = 1 - 2 * ( yy + zz ); - m.at(0,1) = 2 * ( xy - zw ); - m.at(0,2) = 2 * ( xz + yw ); + m.at(0,0) = 1 - 2 * ( yy + zz ); + m.at(0,1) = 2 * ( xy - zw ); + m.at(0,2) = 2 * ( xz + yw ); - m.at(1,0) = 2 * ( xy + zw ); - m.at(1,1) = 1 - 2 * ( xx + zz ); - m.at(1,2) = 2 * ( yz - xw ); + m.at(1,0) = 2 * ( xy + zw ); + m.at(1,1) = 1 - 2 * ( xx + zz ); + m.at(1,2) = 2 * ( yz - xw ); - m.at(2,0) = 2 * ( xz - yw ); - m.at(2,1) = 2 * ( yz + xw ); - m.at(2,2) = 1 - 2 * ( xx + yy ); + m.at(2,0) = 2 * ( xz - yw ); + m.at(2,1) = 2 * ( yz + xw ); + m.at(2,2) = 1 - 2 * ( xx + yy ); - return m; + return m; } template const quaternion quaternion::identity() { - return quaternion(0,0,0,1); + return quaternion(0,0,0,1); } template const quaternion quaternion::rotate_180_degree_around_x() { - return quaternion(1,0,0,0); + return quaternion(1,0,0,0); } template const quaternion quaternion::rotate_180_degree_around_y() { - return quaternion(0,1,0,0); + return quaternion(0,1,0,0); } template const quaternion quaternion::rotate_180_degree_around_z() { - return quaternion(0,0,1,0); + return quaternion(0,0,1,0); } template const quaternion quaternion::rotate_90_degree_around_x(bool negative/* = false*/) { - return quaternion((negative) ? - _sqrt90 : _sqrt90,0,0,_sqrt90); + return quaternion((negative) ? - _sqrt90 : _sqrt90,0,0,_sqrt90); } template const quaternion quaternion::rotate_90_degree_around_y(bool negative/* = false*/) { - return quaternion(0, (negative) ? -_sqrt90 : _sqrt90,0,_sqrt90); + return quaternion(0, (negative) ? -_sqrt90 : _sqrt90,0,_sqrt90); } template const quaternion quaternion::rotate_90_degree_around_z(bool negative/* = false*/) { - return quaternion(0,0,(negative) ? -_sqrt90 : _sqrt90, _sqrt90); + return quaternion(0,0,(negative) ? -_sqrt90 : _sqrt90, _sqrt90); } template const quaternion quaternion::lerp(const quaternion& qa,const quaternion& qb,const T& t) { - return quaternion(qa + (qb - qa) * t); + return quaternion(qa + (qb - qa) * t); } template const quaternion quaternion::normalized_lerp(const quaternion& qa,const quaternion& qb,const T& t) { - return quaternion::lerp(qa,qb,t).normalized(); + return quaternion::lerp(qa,qb,t).normalized(); } template const quaternion quaternion::slerp(const quaternion& qa,const quaternion& qb,const T& t) { - using std::abs; - using std::sqrt; - using std::acos; + using std::abs; + using std::sqrt; + using std::acos; - // quaternion to return - quaternion qm; - // Calculate angle between them. - double 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; - } + // quaternion to return + quaternion qm; + // Calculate angle between them. + double 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. - double halfTheta = acos(cosHalfTheta); - double 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 (::std::abs(sinHalfTheta) < 0.001){ // fabs is floating point absolute - qm.w() = (qa.w() * 0.5 + qb.w() * 0.5); - qm.x() = (qa.x() * 0.5 + qb.x() * 0.5); - qm.y() = (qa.y() * 0.5 + qb.y() * 0.5); - qm.z() = (qa.z() * 0.5 + qb.z() * 0.5); - return qm; - } - double ratioA = sin((1 - t) * halfTheta) / sinHalfTheta; - double 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); + // Calculate temporary values. + double halfTheta = acos(cosHalfTheta); + double 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 (::std::abs(sinHalfTheta) < 0.001){ // fabs is floating point absolute + qm.w() = (qa.w() * 0.5 + qb.w() * 0.5); + qm.x() = (qa.x() * 0.5 + qb.x() * 0.5); + qm.y() = (qa.y() * 0.5 + qb.y() * 0.5); + qm.z() = (qa.z() * 0.5 + qb.z() * 0.5); + return qm; + } + double ratioA = sin((1 - t) * halfTheta) / sinHalfTheta; + double 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; + return qm; } // diff --git a/src/core/include/pw/core/vector.hpp b/src/core/include/pw/core/vector.hpp index d96c808..0808732 100644 --- a/src/core/include/pw/core/vector.hpp +++ b/src/core/include/pw/core/vector.hpp @@ -113,6 +113,12 @@ public: #endif + static vector3 forward() { return vector3 ( 0, 0,-1); } + static vector3 backward() { return vector3( 0, 0, 1); } + static vector3 right() { return vector3 ( 1, 0, 0); } + static vector3 left() { return vector3 (-1, 0, 0); } + static vector3 up() { return vector3 ( 0, 1, 0); } + static vector3 down() { return vector3 ( 0,-1, 0); } }; @@ -120,8 +126,7 @@ public: // Vec2x ----------------------------------------------------------------------- -template class vector2 : public vector<2,T> -{ +template class vector2 : public vector<2,T> { public: vector2() {} diff --git a/src/core/src/CMakeLists.txt b/src/core/src/CMakeLists.txt index 8c6e730..e69de29 100644 --- a/src/core/src/CMakeLists.txt +++ b/src/core/src/CMakeLists.txt @@ -1,38 +0,0 @@ - -set(hdrs -# ../include/pw/core/context.hpp - ../include/pw/core/axisangle.hpp - ../include/pw/core/core.hpp -# ../include/pw/core/script.hpp -# ../include/pw/core/scripting.hpp - ../include/pw/core/math.hpp - ../include/pw/core/matrixbase.hpp - ../include/pw/core/matrix.hpp - ../include/pw/core/vector.hpp - ../include/pw/core/quaternion.hpp - ../include/pw/core/serialize.hpp - ../include/pw/core/image.hpp - ../include/pw/core/globals.hpp - ) - -set(srcs -# script.cpp -# context.cpp - core.cpp - serialize.cpp - ) - -add_library(pwcore - STATIC - ${hdrs} - ${srcs} - ) - -target_include_directories( - pwcore - PUBLIC - ../include - ) - -target_link_libraries(pwcore) - diff --git a/src/core/src/core.cpp b/src/core/src/core.cpp index 1632142..1e4b5a1 100644 --- a/src/core/src/core.cpp +++ b/src/core/src/core.cpp @@ -17,8 +17,6 @@ void test_matrixbase() { m.set_identity(); - - } diff --git a/src/scene/CMakeLists.txt b/src/scene/CMakeLists.txt index fdfb8f4..dadb76b 100644 --- a/src/scene/CMakeLists.txt +++ b/src/scene/CMakeLists.txt @@ -1,2 +1,37 @@ -add_subdirectory(src) + +set(hdrs + include/pw/scene/component.hpp + include/pw/scene/node.hpp + include/pw/scene/nodepath.hpp + include/pw/scene/transform.hpp + include/pw/scene/traverser.hpp + ) + +set(srcs + src/node.cpp + src/nodepath.cpp + src/component.cpp + src/transform.cpp + src/traverser.cpp + ) + +add_library(pwscene + STATIC + ${hdrs} + ${srcs} + ) + +target_include_directories( + pwscene + PUBLIC + include + ) + +target_include_directories( + pwscene + PUBLIC + ) + +target_link_libraries(pwscene pwcore) + add_subdirectory(tests) diff --git a/src/scene/include/pw/scene/node.hpp b/src/scene/include/pw/scene/node.hpp index 4b68a73..6e399eb 100644 --- a/src/scene/include/pw/scene/node.hpp +++ b/src/scene/include/pw/scene/node.hpp @@ -9,8 +9,10 @@ namespace pw { +class traverser; + /** - * @brief nodes are representing the hierarchical structore fo the scene graph + * @brief nodes represent the structure for the scene graph */ class node { public: @@ -27,6 +29,10 @@ public: //! copy c'tor node(const node& node); + + //! wrapper for scripting interface + std::shared_ptr shared(); + //! cloning interface virtual node* clone(const unsigned short& copymode) const; @@ -36,11 +42,8 @@ public: //! return list of parents inline const ptr_array& parents() const { return _parents; } - //! check if this is the root node - inline bool is_root() const { return parents().empty(); } - //! add a child node - void add_child(node *anode); + std::shared_ptr add_child(std::shared_ptr anode); //! get the list of children inline const ref_array& children() const { return _children; } @@ -51,10 +54,37 @@ public: //! check if this node is a leaf node inline bool is_leaf() const { return children().empty(); } + //! check if this is the root node + inline bool is_root() const { return parents().empty(); } + + //! d'tor virtual ~node(); + //! list of components const component::array& components() const; + //! add a node as parent node + void add_parent(node *anode); + + //! visitor to collect data from the graph + struct visitor { + enum direction { + up, + down + } direction; + + visitor(enum direction d = direction::down) : direction(d) {} + + virtual void enter(node* n) = 0; + virtual void leave(node* n) = 0; + }; + + void visit(visitor &visitor); + + void ascend(visitor &visitor); + + void descend(visitor &visitor); + protected: void register_component(component* c); diff --git a/src/scene/include/pw/scene/nodepath.hpp b/src/scene/include/pw/scene/nodepath.hpp new file mode 100644 index 0000000..b2b726c --- /dev/null +++ b/src/scene/include/pw/scene/nodepath.hpp @@ -0,0 +1,24 @@ +#ifndef PW_SCENE_NODEPATH_HPP +#define PW_SCENE_NODEPATH_HPP + +#include + +namespace pw { + +class nodepath { +public: + + nodepath() {} + + void update(node* n); + +protected: + + node::ptr_array _path; + +}; + +} + + +#endif // NODEPATH_HPP diff --git a/src/scene/include/pw/scene/traverser.hpp b/src/scene/include/pw/scene/traverser.hpp new file mode 100644 index 0000000..000e00e --- /dev/null +++ b/src/scene/include/pw/scene/traverser.hpp @@ -0,0 +1,30 @@ +#ifndef PW_SCENE_TRAVERSER_HPP +#define PW_SCENE_TRAVERSER_HPP + +namespace pw { + +class node; + +//class traverser { +//public: + +// enum direction { +// up, +// down +// }; + +// traverser(direction dir = direction::down); + +// void enter(node* node); + +// void leave(node* node); + +//protected: + +// direction _direction; + +//}; + +} + +#endif // PW_SCENE_TRAVERSER_HPP diff --git a/src/scene/src/CMakeLists.txt b/src/scene/src/CMakeLists.txt index 98c75e9..139597f 100644 --- a/src/scene/src/CMakeLists.txt +++ b/src/scene/src/CMakeLists.txt @@ -1,32 +1,2 @@ -set(hdrs - ../include/pw/scene/component.hpp - ../include/pw/scene/node.hpp - ../include/pw/scene/transform.hpp - ) -set(srcs - node.cpp - nodepath.cpp - component.cpp - transform.cpp - ) - -add_library(pwscene - STATIC - ${hdrs} - ${srcs} - ) - -target_include_directories( - pwscene - PUBLIC - ../include - ) - -target_include_directories( - pwscene - PUBLIC - ) - -target_link_libraries(pwscene pwcore) diff --git a/src/scene/src/component.cpp b/src/scene/src/component.cpp index 168f161..06a32fe 100644 --- a/src/scene/src/component.cpp +++ b/src/scene/src/component.cpp @@ -16,7 +16,16 @@ namespace pw { component::component(node *n) : _node(n) { - if (_node != nullptr) _node->register_component(this); + if (_node != nullptr) { + + for (const auto c : _node->components()) { + if (typeid (c.get()).name() == typeid (this).name()) { + // error + return; + } + } + _node->register_component(this); + } } component::component(const component &other) diff --git a/src/scene/src/node.cpp b/src/scene/src/node.cpp index cd5b54c..7fe54bb 100644 --- a/src/scene/src/node.cpp +++ b/src/scene/src/node.cpp @@ -22,10 +22,44 @@ void node::set_name(const std::string &name) _name = name; } -void node::add_child(node *anode) +std::shared_ptr node::add_child(std::shared_ptr anode) { - _children.push_back(std::shared_ptr(anode)); - anode->_parents.push_back(this); + anode->add_parent(this); + _children.push_back(anode); + return anode; +} + +void node::add_parent(node *anode) +{ + _parents.push_back(anode); +} + +void node::visit(visitor& visitor) +{ + visitor.enter(this); + + if (visitor.direction == node::visitor::direction::up) + this->ascend(visitor); + else { + this->descend(visitor); + } + + visitor.leave(this); +} + +void node::ascend(visitor& visitor) +{ + for (auto p : this->parents()) p->visit(visitor); +} + +void node::descend(visitor& visitor) +{ + for (auto c : this->children()) c->visit(visitor); +} + +std::shared_ptr node::shared() +{ + return std::shared_ptr(this); } node::~node() @@ -34,6 +68,13 @@ node::~node() _parents.clear(); } + + + +// +// +// + void node::register_component(component *c) { _components.push_back(std::shared_ptr(c)); } @@ -54,4 +95,5 @@ const component::array& node::components() const + } diff --git a/src/scene/src/nodepath.cpp b/src/scene/src/nodepath.cpp index 65ee44a..d8d1e02 100644 --- a/src/scene/src/nodepath.cpp +++ b/src/scene/src/nodepath.cpp @@ -1,6 +1,11 @@ +#include "pw/scene/nodepath.hpp" +#include "pw/scene/node.hpp" + namespace pw { - +void nodepath::update(node *n) +{ +} } diff --git a/src/scene/src/traverser.cpp b/src/scene/src/traverser.cpp new file mode 100644 index 0000000..3f362d8 --- /dev/null +++ b/src/scene/src/traverser.cpp @@ -0,0 +1,34 @@ +#include "pw/scene/traverser.hpp" +#include "pw/scene/node.hpp" + +#include +#include + +namespace pw { + +//traverser::traverser(direction dir) +//{ +//} + +//void traverser::run(node *root_node) +//{ +// std::stack node_stack; +// node_stack.push(root_node); + +// node *n = root_node; + +// while (!node_stack.empty()) { + +// if (n->is_leaf()) { +// std::cout << n->name() << std::endl; +// node_stack.pop(); +// // save +// } else { +// for +//// n = n->children().front().get(); +// } + +// } +//} + +} diff --git a/src/scene/tests/CMakeLists.txt b/src/scene/tests/CMakeLists.txt index 0497955..4e66de9 100644 --- a/src/scene/tests/CMakeLists.txt +++ b/src/scene/tests/CMakeLists.txt @@ -4,3 +4,11 @@ add_executable(pwscene_test_node target_link_libraries(pwscene_test_node pwcore pwscene) + + +add_executable(pwscene_test_traverser + pwscene_test_traverser.cpp + ) + +target_link_libraries(pwscene_test_traverser + pwcore pwscene) diff --git a/src/scene/tests/pwscene_test_node.cpp b/src/scene/tests/pwscene_test_node.cpp index a559696..0dbf5e2 100644 --- a/src/scene/tests/pwscene_test_node.cpp +++ b/src/scene/tests/pwscene_test_node.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include @@ -13,14 +14,14 @@ int main(int argc,char **argv) { // check { - n->add_child(new node("sub")); + n->add_child(std::make_shared("sub")); } std::cout << "n name: " << n->name() << std::endl; std::cout << "n leaf: " << n->is_leaf() << std::endl; std::cout << "n root: " << n->is_root() << std::endl; - std::cout << "s name: " << n->children()[0]->name() << std::endl; + std::cout << "s name: " << n->children().front()->name() << std::endl; std::cout << "s leaf: " << n->children()[0]->is_leaf() << std::endl; std::cout << "s root: " << n->children()[0]->is_root() << std::endl; @@ -33,5 +34,7 @@ int main(int argc,char **argv) { std::cout << "n components: " << n->components().size() << std::endl; + + return 0; } diff --git a/src/scene/tests/pwscene_test_traverser.cpp b/src/scene/tests/pwscene_test_traverser.cpp new file mode 100644 index 0000000..64144ab --- /dev/null +++ b/src/scene/tests/pwscene_test_traverser.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +//#include + +#include + +struct test_visitor : pw::node::visitor { + virtual void enter(pw::node *n) override; + virtual void leave(pw::node *n) override; +}; + +void test_visitor::enter(pw::node *n) +{ + std::cout << n->name() << " " << n->is_leaf() << std::endl; +} + +void test_visitor::leave(pw::node *n) +{ +} + +#include + +int main(int argc,char **argv) { + + using namespace pw; + + node::ref root(std::make_shared("root")); + root->add_child(std::make_shared("sub-1")); + + node::ref sub_2_1 = std::make_shared("sub-2-1"); + + root->add_child(std::make_shared("sub-2"))->add_child(std::make_shared("sub-2-1")); + root->add_child(std::make_shared("sub-3"))->add_child(sub_2_1); + + std::cout << "sub-2-1 parent count: " << sub_2_1->parents().size() << std::endl; + + test_visitor tv; + + root->visit(tv); + + return 0; +} diff --git a/src/scripting/CMakeLists.txt b/src/scripting/CMakeLists.txt index febd4f0..452b32b 100644 --- a/src/scripting/CMakeLists.txt +++ b/src/scripting/CMakeLists.txt @@ -1 +1,32 @@ -add_subdirectory(src) +#add_subdirectory(src) + + +set(hdrs + include/pw/scripting/script.hpp + include/pw/scripting/scripting.hpp + ) + +set(srcs + src/script.cpp + ) + +add_library(pwscripting + STATIC + ${hdrs} + ${srcs} + ) + +target_include_directories( + pwscripting + PUBLIC + include + ) + +target_include_directories( + pwscripting + PRIVATE + ${CMAKE_SOURCE_DIR}/src/deps/lua-5.3.4/src + ${CMAKE_SOURCE_DIR}/src/deps/sol + ) + +target_link_libraries(pwscripting lualib pwcore pwui pwscene) diff --git a/src/scripting/include/pw/scripting/script.hpp b/src/scripting/include/pw/scripting/script.hpp index 609d5e5..5eb8924 100644 --- a/src/scripting/include/pw/scripting/script.hpp +++ b/src/scripting/include/pw/scripting/script.hpp @@ -28,6 +28,4 @@ protected: } - - #endif diff --git a/src/scripting/src/CMakeLists.txt b/src/scripting/src/CMakeLists.txt index 15019a3..e69de29 100644 --- a/src/scripting/src/CMakeLists.txt +++ b/src/scripting/src/CMakeLists.txt @@ -1,30 +0,0 @@ - -set(hdrs - ../include/pw/scripting/script.hpp - ../include/pw/scripting/scripting.hpp - ) - -set(srcs - script.cpp - ) - -add_library(pwscripting - STATIC - ${hdrs} - ${srcs} - ) - -target_include_directories( - pwscripting - PUBLIC - ../include - ) - -target_include_directories( - pwscripting - PRIVATE - ${CMAKE_SOURCE_DIR}/src/deps/lua-5.3.4/src - ${CMAKE_SOURCE_DIR}/src/deps/sol - ) - -target_link_libraries(pwscripting lua pwcore) diff --git a/src/scripting/src/script.cpp b/src/scripting/src/script.cpp index fc7fc20..40b1374 100644 --- a/src/scripting/src/script.cpp +++ b/src/scripting/src/script.cpp @@ -6,75 +6,118 @@ #include "pw/core/quaternion.hpp" #include "pw/core/axisangle.hpp" +#include "pw/scene/node.hpp" + +#include "pw/ui/window.hpp" + struct lua_state : public pw::script::state { - pw::scripting::state _state; - pw::scripting::table _namespace; + pw::scripting::state _state; + pw::scripting::table _namespace; - void load_modules(); + void load_modules(); - lua_state() { + lua_state() { - // create global pw namespace - _namespace = _state.create_named_table("pw"); + // create global pw namespace + _namespace = _state.create_named_table("pw"); - // add the script as a user type - _namespace.new_usertype("script", - "initialize",&lua_state::load_modules - ); + // add the script as a user type + _namespace.new_usertype("script", + "initialize",&lua_state::load_modules + ); - // add this instance as "script" - _namespace.set("script",this); - } + // add this instance as "script" + _namespace.set("script",this); + } - virtual int run(const std::string &s); + virtual int run(const std::string &s); }; int lua_state::run(const std::string &s) { - return _state.script(s.c_str()).get(); + return _state.script(s.c_str()).get(); } void lua_state::load_modules() { - // open all libraries - _state.open_libraries(); + // open all libraries + _state.open_libraries(); - typedef double Scalar; + typedef double Scalar; - _namespace.set("pi",pw::pi()); + _namespace.set("pi",pw::pi()); - using namespace pw; + using namespace pw; - _namespace.new_usertype("vector3", - "set",&vector3d::set, - "x", scripting::property(scripting::resolve(&vector3d::x), &vector3d::set_x), - "y", scripting::property(scripting::resolve(&vector3d::y), &vector3d::set_y), - "z", scripting::property(scripting::resolve(&vector3d::z), &vector3d::set_z), - // "v",&vector3d::values, - "clone",&vector3d::clone - ); + _namespace.new_usertype("vector3", + sol::constructors(), + "set",&vector3d::set, + "x", scripting::property(scripting::resolve(&vector3d::x), &vector3d::set_x), + "y", scripting::property(scripting::resolve(&vector3d::y), &vector3d::set_y), + "z", scripting::property(scripting::resolve(&vector3d::z), &vector3d::set_z), + "norm",&vector3d::norm, + "cross",&vector3d::cross, + "dot",&vector3d::dot, + // sol::meta_function::addition, sol::resolve(::operator+), + // sol::meta_function::subtraction, &vector3d::operator- + // "v",&vector3d::values, + "clone",&vector3d::clone + ); - _namespace.new_usertype("quaternion", - "set",&quaterniond::set, - "x", scripting::property(scripting::resolve(&quaterniond::x), &quaterniond::set_x), - "y", scripting::property(scripting::resolve(&quaterniond::y), &quaterniond::set_y), - "z", scripting::property(scripting::resolve(&quaterniond::z), &quaterniond::set_z), - "w", scripting::property(scripting::resolve(&quaterniond::w), &quaterniond::set_w), - "dot",&quaterniond::dot, - "inverse",&quaterniond::inverse, - "normalized",&quaterniond::normalized, - "lerp",&quaterniond::lerp - // "v",&vector3d::values, - // "clone",&vector3d::clone - ); + _namespace.new_usertype("quaternion", + sol::constructors(), + "set",&quaterniond::set, + "x", scripting::property(scripting::resolve(&quaterniond::x), &quaterniond::set_x), + "y", scripting::property(scripting::resolve(&quaterniond::y), &quaterniond::set_y), + "z", scripting::property(scripting::resolve(&quaterniond::z), &quaterniond::set_z), + "w", scripting::property(scripting::resolve(&quaterniond::w), &quaterniond::set_w), + "dot",&quaterniond::dot, + "inverse",&quaterniond::inverse, + "normalized",&quaterniond::normalized, + "lerp",&quaterniond::lerp + // "v",&vector3d::values, + // "clone",&vector3d::clone + ); - _namespace.new_usertype("axisangle", - "axis",scripting::property(&axisangled::axis,&axisangled::set_axis), - "angle",scripting::property(&axisangled::angle,&axisangled::set_angle) - ); + _namespace.new_usertype("axisangle", + sol::constructors(), + "axis",scripting::property(&axisangled::axis,&axisangled::set_axis), + "angle",scripting::property(&axisangled::angle,&axisangled::set_angle) + ); + + + + _namespace.new_usertype("window", + "update",&window::update, + "set_title",&window::set_title, + "set_size",&window::set_size + ); + + + _namespace.new_usertype("node", + sol::constructors(), + "add_child",&node::add_child, + "shared",&node::shared, + "children",&node::children, + "child_count",&node::child_count, + "create", []() -> std::shared_ptr { + auto ptr = std::make_shared(); + return ptr; + }, + // "share",scripting::property(scripting::resolve(std::make_shared)) + "name",scripting::property(&node::name,&node::set_name) + ); + + + + // _namespace.set("something", std::shared_ptr(new node())); + + _namespace["my_func"] = []() -> std::shared_ptr { + return std::make_shared(); +}; } @@ -82,17 +125,17 @@ void lua_state::load_modules() { pw::script::script() { - _state.reset(new lua_state()); + _state.reset(new lua_state()); } pw::script::~script() { - _state = nullptr; + _state = nullptr; } int pw::script::run(const std::string &s) { - return _state->run(s); + return _state->run(s); } diff --git a/src/scripts/demos/simple_000.lua b/src/scripts/demos/simple_000.lua index d5a072f..cbce571 100644 --- a/src/scripts/demos/simple_000.lua +++ b/src/scripts/demos/simple_000.lua @@ -3,10 +3,10 @@ pw.script:initialize() print("hello pixwerx!") -local v = pw.vector3.new() -v:set(0,1,2) +local v1 = pw.vector3.new(3,2,1) +v1:set(0,1,2) -print("v",v.x,v.y,v.z) +print("v1 ",v1.x,v1.y,v1.z) ---- objects need to be cloned @@ -23,18 +23,44 @@ print("v",v.x,v.y,v.z) 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) print("q.m",qm.x,qm.y,qm.z,qm.w) ---local aa = pw.axisangle.new() ----- assign vector to axis -----aa.axis = v +-- axis angle test +local aa = pw.axisangle.new(v1,0.707) ---print("aa",aa.axis.x) +print("aa.axis",aa.axis.x,aa.axis.y,aa.axis.z) +print("aa.angle",aa.angle) + + +local n_1 = pw.node.new("node-1") +local n_2 = pw.node.new("node-2") + +print("node 1: ", n_1.name) +print("node 2: ", n_2.name) + +print(pw.node.create()) + +print("node children ",n_1:children()) + +--print(n_1:shared()) +--print(pw.my_func()) + +--n_1:add_child() + +--local w = pw.window.new() + +--while w:update() +--do +-- -- print("update") +--end --local scene = pw:scene.new() diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index febd4f0..228a2cb 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -1 +1,23 @@ -add_subdirectory(src) +#add_subdirectory(src) + +set(hdrs + include/pw/ui/window.hpp + ) + +set(srcs + src/window.cpp + ) + +add_library(pwui + STATIC + ${hdrs} + ${srcs} + ) + +target_include_directories( + pwui + PUBLIC + include + ) + +target_link_libraries(pwui pwcore glfw glad) diff --git a/src/ui/src/CMakeLists.txt b/src/ui/src/CMakeLists.txt index f367385..e69de29 100644 --- a/src/ui/src/CMakeLists.txt +++ b/src/ui/src/CMakeLists.txt @@ -1,22 +0,0 @@ -set(hdrs - ../include/pw/ui/window.hpp - ) - -set(srcs - window.cpp - ) - - -add_library(pwui - STATIC - ${hdrs} - ${srcs} - ) - -target_include_directories( - pwui - PUBLIC - ../include - ) - -target_link_libraries(pwui pwcore glfw glad) diff --git a/src/ui/src/window.cpp b/src/ui/src/window.cpp index 9f483a9..587153e 100644 --- a/src/ui/src/window.cpp +++ b/src/ui/src/window.cpp @@ -2,14 +2,16 @@ //#include "glad/glad.h" -pw::window::window() { +pw::window::window() +{ + glfwInit(); - _window = glfwCreateWindow(640, 480, "pixwerxs", NULL, NULL); + _window = glfwCreateWindow(640, 480, "pixwerxs", nullptr, nullptr); #if 0 - glfwMakeContextCurrent(_window); + glfwMakeContextCurrent(_window); - gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); + gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); #endif @@ -17,45 +19,41 @@ pw::window::window() { pw::window::~window() { - glfwDestroyWindow(_window); + glfwDestroyWindow(_window); } bool pw::window::update() { - if (!glfwWindowShouldClose(_window)) { + if (!glfwWindowShouldClose(_window)) { - // do other stuff + // do other stuff #if 0 - glClearColor(1,0,0,1); - glClear(GL_COLOR_BUFFER_BIT); + glClearColor(1,0,0,1); + glClear(GL_COLOR_BUFFER_BIT); #endif - glfwSwapBuffers(_window); - glfwPollEvents(); + glfwSwapBuffers(_window); + glfwPollEvents(); - return true; - } + return true; + } - return false; + return false; } void pw::window::set_title(const char *t) { - glfwSetWindowTitle(_window,t); + glfwSetWindowTitle(_window,t); } void pw::window::set_size(int w,int h) { - glfwSetWindowSize(_window,w,h); + glfwSetWindowSize(_window,w,h); } //void pw::window::load(sol::table &ns) //{ -// glfwInit(); +// + -// ns.new_usertype("window", -// "update",&window::update, -// "set_title",&window::set_title, -// "set_size",&window::set_size -// ); //}