slowly getting tracktion again
This commit is contained in:
parent
a5dea1ede1
commit
dd23fa811a
29 changed files with 685 additions and 380 deletions
|
@ -1,3 +1,6 @@
|
||||||
|
#
|
||||||
|
# CMake build system for pixwerx
|
||||||
|
#
|
||||||
cmake_minimum_required(VERSION 3.8)
|
cmake_minimum_required(VERSION 3.8)
|
||||||
|
|
||||||
project(pixwerx)
|
project(pixwerx)
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
add_subdirectory(deps)
|
add_subdirectory(deps)
|
||||||
add_subdirectory(core)
|
add_subdirectory(core)
|
||||||
add_subdirectory(scene)
|
add_subdirectory(scene)
|
||||||
|
#add_subdirectory(system)
|
||||||
add_subdirectory(ui)
|
add_subdirectory(ui)
|
||||||
add_subdirectory(scripting)
|
add_subdirectory(scripting)
|
||||||
add_subdirectory(engine)
|
add_subdirectory(engine)
|
||||||
|
|
|
@ -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)
|
add_subdirectory(tests)
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,10 @@ protected:
|
||||||
T _angle;
|
T _angle;
|
||||||
public:
|
public:
|
||||||
|
|
||||||
axisangle() {}
|
axisangle()
|
||||||
|
: _axis(vector3<T>::up()),
|
||||||
|
_angle(0)
|
||||||
|
{}
|
||||||
|
|
||||||
axisangle(const vector3<T> &axis,const T &angle)
|
axisangle(const vector3<T> &axis,const T &angle)
|
||||||
: _axis(axis)
|
: _axis(axis)
|
||||||
|
|
|
@ -10,8 +10,6 @@ const static double __PW_PI = 3.141592653589793238462643383279502884197169399375
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline const T pi() { return static_cast<T>(__PW_PI); }
|
inline const T pi() { return static_cast<T>(__PW_PI); }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
inline static double rad_to_deg(const T& angle_in_radian) {
|
inline static double rad_to_deg(const T& angle_in_radian) {
|
||||||
return static_cast<T>(angle_in_radian * T(180.) / pi<T>());
|
return static_cast<T>(angle_in_radian * T(180.) / pi<T>());
|
||||||
|
|
|
@ -37,135 +37,135 @@ namespace pw {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class quaternion {
|
class quaternion {
|
||||||
|
|
||||||
static const T _sqrt90;
|
static const T _sqrt90;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
typedef vector4<T> coefficient_type;
|
typedef vector4<T> coefficient_type;
|
||||||
typedef T value_type;
|
typedef T value_type;
|
||||||
|
|
||||||
quaternion() { *this = identity(); }
|
quaternion() { *this = identity(); }
|
||||||
|
|
||||||
quaternion(const T& x,const T& y,const T& z,const T& w)
|
quaternion(const T& x,const T& y,const T& z,const T& w)
|
||||||
: _q(coefficient_type(x,y,z,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) {
|
inline void set(const T& x,const T& y,const T& z,const T& w) {
|
||||||
_q.set(x,y,z,w);
|
_q.set(x,y,z,w);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void set_x(const T& v) { x() = v; }
|
inline void set_x(const T& v) { x() = v; }
|
||||||
inline void set_y(const T& v) { y() = v; }
|
inline void set_y(const T& v) { y() = v; }
|
||||||
inline void set_z(const T& v) { z() = v; }
|
inline void set_z(const T& v) { z() = v; }
|
||||||
inline void set_w(const T& v) { w() = 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& x() { return _q.x(); }
|
||||||
inline T& y() { return _q.x(); }
|
inline T& y() { return _q.x(); }
|
||||||
inline T& z() { return _q.z(); }
|
inline T& z() { return _q.z(); }
|
||||||
inline T& w() { return _q.w(); }
|
inline T& w() { return _q.w(); }
|
||||||
|
|
||||||
inline const T& x() const { return _q.z(); }
|
inline const T& x() const { return _q.z(); }
|
||||||
inline const T& y() const { return _q.y(); }
|
inline const T& y() const { return _q.y(); }
|
||||||
inline const T& z() const { return _q.z(); }
|
inline const T& z() const { return _q.z(); }
|
||||||
inline const T& w() const { return _q.w(); }
|
inline const T& w() const { return _q.w(); }
|
||||||
|
|
||||||
|
|
||||||
//! multiplication
|
//! multiplication
|
||||||
inline const quaternion operator * (const quaternion& rhs) const {
|
inline const quaternion operator * (const quaternion& rhs) const {
|
||||||
return quaternion(
|
return quaternion(
|
||||||
rhs.w()*x() + rhs.x()*w() + rhs.y()*z() - rhs.z()*y(),
|
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()*y() - rhs.x()*z() + rhs.y()*w() + rhs.z()*x(),
|
||||||
rhs.w()*z() + rhs.x()*y() - rhs.y()*x() + rhs.z()*w(),
|
rhs.w()*z() + rhs.x()*y() - rhs.y()*x() + rhs.z()*w(),
|
||||||
rhs.w()*w() - rhs.x()*x() - rhs.y()*y() - rhs.z()*z()
|
rhs.w()*w() - rhs.x()*x() - rhs.y()*y() - rhs.z()*z()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
//! multiply with scalar
|
//! multiply with scalar
|
||||||
inline const quaternion operator * (const T& s) const {
|
inline const quaternion operator * (const T& s) const {
|
||||||
return quaternion(x()*s,y()*s,z()*s,w()*s);
|
return quaternion(x()*s,y()*s,z()*s,w()*s);
|
||||||
}
|
}
|
||||||
|
|
||||||
//! addition
|
//! addition
|
||||||
inline const quaternion operator + (const quaternion& rhs) const {
|
inline const quaternion operator + (const quaternion& rhs) const {
|
||||||
return quaternion(coefficient_type(this->_q + rhs._q));
|
return quaternion(coefficient_type(this->_q + rhs._q));
|
||||||
}
|
}
|
||||||
|
|
||||||
//! addition
|
//! addition
|
||||||
inline const quaternion operator - (const quaternion& rhs) const {
|
inline const quaternion operator - (const quaternion& rhs) const {
|
||||||
return quaternion(this->_q - rhs._q);
|
return quaternion(this->_q - rhs._q);
|
||||||
}
|
}
|
||||||
|
|
||||||
//! squared norm
|
//! squared norm
|
||||||
inline const T squared_norm() const { return _q.squared_norm(); }
|
inline const T squared_norm() const { return _q.squared_norm(); }
|
||||||
|
|
||||||
//! norm
|
//! norm
|
||||||
inline const T norm() const { return _q.norm(); }
|
inline const T norm() const { return _q.norm(); }
|
||||||
|
|
||||||
//! dot product
|
//! dot product
|
||||||
inline const T dot(const quaternion& other) const { return _q.dot(other._q); }
|
inline const T dot(const quaternion& other) const { return _q.dot(other._q); }
|
||||||
|
|
||||||
//! conjugate
|
//! conjugate
|
||||||
inline quaternion conjugate() const { return quaternion(-x(),-y(),-z(),w()); }
|
inline quaternion conjugate() const { return quaternion(-x(),-y(),-z(),w()); }
|
||||||
|
|
||||||
//! compute inverse
|
//! compute inverse
|
||||||
inline quaternion inverse() const {
|
inline quaternion inverse() const {
|
||||||
const T one_over_squared_norm = T(1) / squared_norm();
|
const T one_over_squared_norm = T(1) / squared_norm();
|
||||||
return conjugate() * one_over_squared_norm;
|
return conjugate() * one_over_squared_norm;
|
||||||
}
|
}
|
||||||
|
|
||||||
//! compute normalized
|
//! compute normalized
|
||||||
inline quaternion normalized() const {
|
inline quaternion normalized() const {
|
||||||
return quaternion(_q.normalized());
|
return quaternion(_q.normalized());
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void normalize() { *this = this->normalized(); }
|
inline void normalize() { *this = this->normalized(); }
|
||||||
|
|
||||||
//! conversion from a matrix
|
//! conversion from a matrix
|
||||||
inline static const quaternion from_matrix(const matrix<4,4,T> &m);
|
inline static const quaternion from_matrix(const matrix<4,4,T> &m);
|
||||||
|
|
||||||
//! conversion to a matrix
|
//! conversion to a matrix
|
||||||
const matrix<4,4,T> to_matrix() const;
|
const matrix<4,4,T> to_matrix() const;
|
||||||
|
|
||||||
//! return identiy quaternion
|
//! return identiy quaternion
|
||||||
static const quaternion<T> identity();
|
static const quaternion<T> identity();
|
||||||
|
|
||||||
static const quaternion<T> rotate_180_degree_around_x(); ///< rotate 180 degree around X axis
|
static const quaternion<T> rotate_180_degree_around_x(); ///< rotate 180 degree around X axis
|
||||||
static const quaternion<T> rotate_180_degree_around_y(); ///< rotate 180 degree around Y axis
|
static const quaternion<T> rotate_180_degree_around_y(); ///< rotate 180 degree around Y axis
|
||||||
static const quaternion<T> rotate_180_degree_around_z(); ///< rotate 180 degree around Z axis
|
static const quaternion<T> rotate_180_degree_around_z(); ///< rotate 180 degree around Z axis
|
||||||
|
|
||||||
static const quaternion<T> rotate_90_degree_around_x(bool negative = false);
|
static const quaternion<T> rotate_90_degree_around_x(bool negative = false);
|
||||||
static const quaternion<T> rotate_90_degree_around_y(bool negative = false);
|
static const quaternion<T> rotate_90_degree_around_y(bool negative = false);
|
||||||
static const quaternion<T> rotate_90_degree_around_z(bool negative = false);
|
static const quaternion<T> rotate_90_degree_around_z(bool negative = false);
|
||||||
|
|
||||||
template <typename AxisAngleType>
|
template <typename AxisAngleType>
|
||||||
static const quaternion<T> from_axisangle(const AxisAngleType &aa) {
|
static const quaternion<T> from_axisangle(const AxisAngleType &aa) {
|
||||||
|
|
||||||
using std::sin;
|
using std::sin;
|
||||||
using std::cos;
|
using std::cos;
|
||||||
|
|
||||||
const T sinHalfAngle(sin(aa.angle() * T(0.5) ));
|
const T sinHalfAngle(sin(aa.angle() * T(0.5) ));
|
||||||
|
|
||||||
return quaternion<T>(aa.axis().x() * sinHalfAngle, // x
|
return quaternion<T>(aa.axis().x() * sinHalfAngle, // x
|
||||||
aa.axis().y() * sinHalfAngle, // y
|
aa.axis().y() * sinHalfAngle, // y
|
||||||
aa.axis().z() * sinHalfAngle, // z
|
aa.axis().z() * sinHalfAngle, // z
|
||||||
cos(aa.angle() * 0.5) // w
|
cos(aa.angle() * 0.5) // w
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const quaternion<T> lerp(const quaternion& qa,const quaternion& qb,const T& t);
|
static const quaternion<T> lerp(const quaternion& qa,const quaternion& qb,const T& t);
|
||||||
static const quaternion<T> normalized_lerp(const quaternion& qa,const quaternion& qb,const T& t);
|
static const quaternion<T> normalized_lerp(const quaternion& qa,const quaternion& qb,const T& t);
|
||||||
static const quaternion<T> slerp(const quaternion& qa,const quaternion& qb,const T& t);
|
static const quaternion<T> slerp(const quaternion& qa,const quaternion& qb,const T& t);
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
coefficient_type _q;
|
coefficient_type _q;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
@ -175,138 +175,138 @@ const T quaternion<T>::_sqrt90 = std::sqrt(0.5);
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const quaternion<T> quaternion<T>::from_matrix(const matrix<4,4,T> &m) {
|
const quaternion<T> quaternion<T>::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;
|
const T w4 = T(4.0) * wtemp;
|
||||||
return quaternion<T>(
|
return quaternion<T>(
|
||||||
(m.at(2,1) - m.at(1,2)) / w4,
|
(m.at(2,1) - m.at(1,2)) / w4,
|
||||||
(m.at(0,2) - m.at(2,0)) / w4,
|
(m.at(0,2) - m.at(2,0)) / w4,
|
||||||
(m.at(1,0) - m.at(0,1)) / w4,
|
(m.at(1,0) - m.at(0,1)) / w4,
|
||||||
wtemp);
|
wtemp);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const matrix<4,4,T> quaternion<T>::to_matrix() const {
|
const matrix<4,4,T> quaternion<T>::to_matrix() const {
|
||||||
|
|
||||||
matrix<4,4,T> m; m.set_identity();
|
matrix<4,4,T> m; m.set_identity();
|
||||||
|
|
||||||
T xx = x() * x();
|
T xx = x() * x();
|
||||||
T xy = x() * y();
|
T xy = x() * y();
|
||||||
T xz = x() * z();
|
T xz = x() * z();
|
||||||
T xw = x() * w();
|
T xw = x() * w();
|
||||||
|
|
||||||
T yy = y() * y();
|
T yy = y() * y();
|
||||||
T yz = y() * z();
|
T yz = y() * z();
|
||||||
T yw = y() * w();
|
T yw = y() * w();
|
||||||
|
|
||||||
T zz = z() * z();
|
T zz = z() * z();
|
||||||
T zw = z() * w();
|
T zw = z() * w();
|
||||||
|
|
||||||
m.at(0,0) = 1 - 2 * ( yy + zz );
|
m.at(0,0) = 1 - 2 * ( yy + zz );
|
||||||
m.at(0,1) = 2 * ( xy - zw );
|
m.at(0,1) = 2 * ( xy - zw );
|
||||||
m.at(0,2) = 2 * ( xz + yw );
|
m.at(0,2) = 2 * ( xz + yw );
|
||||||
|
|
||||||
m.at(1,0) = 2 * ( xy + zw );
|
m.at(1,0) = 2 * ( xy + zw );
|
||||||
m.at(1,1) = 1 - 2 * ( xx + zz );
|
m.at(1,1) = 1 - 2 * ( xx + zz );
|
||||||
m.at(1,2) = 2 * ( yz - xw );
|
m.at(1,2) = 2 * ( yz - xw );
|
||||||
|
|
||||||
m.at(2,0) = 2 * ( xz - yw );
|
m.at(2,0) = 2 * ( xz - yw );
|
||||||
m.at(2,1) = 2 * ( yz + xw );
|
m.at(2,1) = 2 * ( yz + xw );
|
||||||
m.at(2,2) = 1 - 2 * ( xx + yy );
|
m.at(2,2) = 1 - 2 * ( xx + yy );
|
||||||
|
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const quaternion<T> quaternion<T>::identity()
|
const quaternion<T> quaternion<T>::identity()
|
||||||
{
|
{
|
||||||
return quaternion<T>(0,0,0,1);
|
return quaternion<T>(0,0,0,1);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const quaternion<T> quaternion<T>::rotate_180_degree_around_x()
|
const quaternion<T> quaternion<T>::rotate_180_degree_around_x()
|
||||||
{
|
{
|
||||||
return quaternion<T>(1,0,0,0);
|
return quaternion<T>(1,0,0,0);
|
||||||
}
|
}
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const quaternion<T> quaternion<T>::rotate_180_degree_around_y()
|
const quaternion<T> quaternion<T>::rotate_180_degree_around_y()
|
||||||
{
|
{
|
||||||
return quaternion<T>(0,1,0,0);
|
return quaternion<T>(0,1,0,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const quaternion<T> quaternion<T>::rotate_180_degree_around_z()
|
const quaternion<T> quaternion<T>::rotate_180_degree_around_z()
|
||||||
{
|
{
|
||||||
return quaternion<T>(0,0,1,0);
|
return quaternion<T>(0,0,1,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const quaternion<T> quaternion<T>::rotate_90_degree_around_x(bool negative/* = false*/)
|
const quaternion<T> quaternion<T>::rotate_90_degree_around_x(bool negative/* = false*/)
|
||||||
{
|
{
|
||||||
return quaternion<T>((negative) ? - _sqrt90 : _sqrt90,0,0,_sqrt90);
|
return quaternion<T>((negative) ? - _sqrt90 : _sqrt90,0,0,_sqrt90);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const quaternion<T> quaternion<T>::rotate_90_degree_around_y(bool negative/* = false*/)
|
const quaternion<T> quaternion<T>::rotate_90_degree_around_y(bool negative/* = false*/)
|
||||||
{
|
{
|
||||||
return quaternion<T>(0, (negative) ? -_sqrt90 : _sqrt90,0,_sqrt90);
|
return quaternion<T>(0, (negative) ? -_sqrt90 : _sqrt90,0,_sqrt90);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const quaternion<T> quaternion<T>::rotate_90_degree_around_z(bool negative/* = false*/)
|
const quaternion<T> quaternion<T>::rotate_90_degree_around_z(bool negative/* = false*/)
|
||||||
{
|
{
|
||||||
return quaternion<T>(0,0,(negative) ? -_sqrt90 : _sqrt90, _sqrt90);
|
return quaternion<T>(0,0,(negative) ? -_sqrt90 : _sqrt90, _sqrt90);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const quaternion<T> quaternion<T>::lerp(const quaternion<T>& qa,const quaternion<T>& qb,const T& t) {
|
const quaternion<T> quaternion<T>::lerp(const quaternion<T>& qa,const quaternion<T>& qb,const T& t) {
|
||||||
return quaternion<T>(qa + (qb - qa) * t);
|
return quaternion<T>(qa + (qb - qa) * t);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const quaternion<T> quaternion<T>::normalized_lerp(const quaternion<T>& qa,const quaternion<T>& qb,const T& t) {
|
const quaternion<T> quaternion<T>::normalized_lerp(const quaternion<T>& qa,const quaternion<T>& qb,const T& t) {
|
||||||
return quaternion<T>::lerp(qa,qb,t).normalized();
|
return quaternion<T>::lerp(qa,qb,t).normalized();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
const quaternion<T> quaternion<T>::slerp(const quaternion<T>& qa,const quaternion<T>& qb,const T& t)
|
const quaternion<T> quaternion<T>::slerp(const quaternion<T>& qa,const quaternion<T>& qb,const T& t)
|
||||||
{
|
{
|
||||||
using std::abs;
|
using std::abs;
|
||||||
using std::sqrt;
|
using std::sqrt;
|
||||||
using std::acos;
|
using std::acos;
|
||||||
|
|
||||||
// quaternion to return
|
// quaternion to return
|
||||||
quaternion qm;
|
quaternion qm;
|
||||||
// Calculate angle between them.
|
// Calculate angle between them.
|
||||||
double cosHalfTheta = qa.w() * qb.w() + qa.x() * qb.x() + qa.y() * qb.y() + qa.z() * qb.z();
|
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 qa=qb or qa=-qb then theta = 0 and we can return qa
|
||||||
if (abs(cosHalfTheta) >= T(1.)) {
|
if (abs(cosHalfTheta) >= T(1.)) {
|
||||||
return qa;
|
return qa;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate temporary values.
|
// Calculate temporary values.
|
||||||
double halfTheta = acos(cosHalfTheta);
|
double halfTheta = acos(cosHalfTheta);
|
||||||
double sinHalfTheta = sqrt(1.0 - cosHalfTheta * cosHalfTheta);
|
double sinHalfTheta = sqrt(1.0 - cosHalfTheta * cosHalfTheta);
|
||||||
// if theta = 180 degrees then result is not fully defined
|
// if theta = 180 degrees then result is not fully defined
|
||||||
// we could rotate around any axis normal to qa or qb
|
// we could rotate around any axis normal to qa or qb
|
||||||
if (::std::abs(sinHalfTheta) < 0.001){ // fabs is floating point absolute
|
if (::std::abs(sinHalfTheta) < 0.001){ // fabs is floating point absolute
|
||||||
qm.w() = (qa.w() * 0.5 + qb.w() * 0.5);
|
qm.w() = (qa.w() * 0.5 + qb.w() * 0.5);
|
||||||
qm.x() = (qa.x() * 0.5 + qb.x() * 0.5);
|
qm.x() = (qa.x() * 0.5 + qb.x() * 0.5);
|
||||||
qm.y() = (qa.y() * 0.5 + qb.y() * 0.5);
|
qm.y() = (qa.y() * 0.5 + qb.y() * 0.5);
|
||||||
qm.z() = (qa.z() * 0.5 + qb.z() * 0.5);
|
qm.z() = (qa.z() * 0.5 + qb.z() * 0.5);
|
||||||
return qm;
|
return qm;
|
||||||
}
|
}
|
||||||
double ratioA = sin((1 - t) * halfTheta) / sinHalfTheta;
|
double ratioA = sin((1 - t) * halfTheta) / sinHalfTheta;
|
||||||
double ratioB = sin(t * halfTheta) / sinHalfTheta;
|
double ratioB = sin(t * halfTheta) / sinHalfTheta;
|
||||||
//calculate Quaternion.
|
//calculate Quaternion.
|
||||||
qm.w() = (qa.w() * ratioA + qb.w() * ratioB);
|
qm.w() = (qa.w() * ratioA + qb.w() * ratioB);
|
||||||
qm.x() = (qa.x() * ratioA + qb.x() * ratioB);
|
qm.x() = (qa.x() * ratioA + qb.x() * ratioB);
|
||||||
qm.y() = (qa.y() * ratioA + qb.y() * ratioB);
|
qm.y() = (qa.y() * ratioA + qb.y() * ratioB);
|
||||||
qm.z() = (qa.z() * ratioA + qb.z() * ratioB);
|
qm.z() = (qa.z() * ratioA + qb.z() * ratioB);
|
||||||
|
|
||||||
return qm;
|
return qm;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
|
@ -113,6 +113,12 @@ public:
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
static vector3<T> forward() { return vector3<T> ( 0, 0,-1); }
|
||||||
|
static vector3<T> backward() { return vector3<T>( 0, 0, 1); }
|
||||||
|
static vector3<T> right() { return vector3<T> ( 1, 0, 0); }
|
||||||
|
static vector3<T> left() { return vector3<T> (-1, 0, 0); }
|
||||||
|
static vector3<T> up() { return vector3<T> ( 0, 1, 0); }
|
||||||
|
static vector3<T> down() { return vector3<T> ( 0,-1, 0); }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -120,8 +126,7 @@ public:
|
||||||
|
|
||||||
// Vec2x -----------------------------------------------------------------------
|
// Vec2x -----------------------------------------------------------------------
|
||||||
|
|
||||||
template <class T> class vector2 : public vector<2,T>
|
template <class T> class vector2 : public vector<2,T> {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
vector2() {}
|
vector2() {}
|
||||||
|
|
|
@ -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)
|
|
||||||
|
|
|
@ -17,8 +17,6 @@ void test_matrixbase() {
|
||||||
|
|
||||||
m.set_identity();
|
m.set_identity();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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)
|
add_subdirectory(tests)
|
||||||
|
|
|
@ -9,8 +9,10 @@
|
||||||
|
|
||||||
namespace pw {
|
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 {
|
class node {
|
||||||
public:
|
public:
|
||||||
|
@ -27,6 +29,10 @@ public:
|
||||||
//! copy c'tor
|
//! copy c'tor
|
||||||
node(const node& node);
|
node(const node& node);
|
||||||
|
|
||||||
|
|
||||||
|
//! wrapper for scripting interface
|
||||||
|
std::shared_ptr<node> shared();
|
||||||
|
|
||||||
//! cloning interface
|
//! cloning interface
|
||||||
virtual node* clone(const unsigned short& copymode) const;
|
virtual node* clone(const unsigned short& copymode) const;
|
||||||
|
|
||||||
|
@ -36,11 +42,8 @@ public:
|
||||||
//! return list of parents
|
//! return list of parents
|
||||||
inline const ptr_array& parents() const { return _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
|
//! add a child node
|
||||||
void add_child(node *anode);
|
std::shared_ptr<node> add_child(std::shared_ptr<node> anode);
|
||||||
|
|
||||||
//! get the list of children
|
//! get the list of children
|
||||||
inline const ref_array& children() const { return _children; }
|
inline const ref_array& children() const { return _children; }
|
||||||
|
@ -51,10 +54,37 @@ public:
|
||||||
//! check if this node is a leaf node
|
//! check if this node is a leaf node
|
||||||
inline bool is_leaf() const { return children().empty(); }
|
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();
|
virtual ~node();
|
||||||
|
|
||||||
|
//! list of components
|
||||||
const component::array& components() const;
|
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:
|
protected:
|
||||||
|
|
||||||
void register_component(component* c);
|
void register_component(component* c);
|
||||||
|
|
24
src/scene/include/pw/scene/nodepath.hpp
Normal file
24
src/scene/include/pw/scene/nodepath.hpp
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
#ifndef PW_SCENE_NODEPATH_HPP
|
||||||
|
#define PW_SCENE_NODEPATH_HPP
|
||||||
|
|
||||||
|
#include <pw/scene/node.hpp>
|
||||||
|
|
||||||
|
namespace pw {
|
||||||
|
|
||||||
|
class nodepath {
|
||||||
|
public:
|
||||||
|
|
||||||
|
nodepath() {}
|
||||||
|
|
||||||
|
void update(node* n);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
node::ptr_array _path;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif // NODEPATH_HPP
|
30
src/scene/include/pw/scene/traverser.hpp
Normal file
30
src/scene/include/pw/scene/traverser.hpp
Normal file
|
@ -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
|
|
@ -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)
|
|
||||||
|
|
|
@ -16,7 +16,16 @@ namespace pw {
|
||||||
component::component(node *n)
|
component::component(node *n)
|
||||||
: _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)
|
component::component(const component &other)
|
||||||
|
|
|
@ -22,10 +22,44 @@ void node::set_name(const std::string &name)
|
||||||
_name = name;
|
_name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
void node::add_child(node *anode)
|
std::shared_ptr<node> node::add_child(std::shared_ptr<node> anode)
|
||||||
{
|
{
|
||||||
_children.push_back(std::shared_ptr<node>(anode));
|
anode->add_parent(this);
|
||||||
anode->_parents.push_back(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> node::shared()
|
||||||
|
{
|
||||||
|
return std::shared_ptr<node>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
node::~node()
|
node::~node()
|
||||||
|
@ -34,6 +68,13 @@ node::~node()
|
||||||
_parents.clear();
|
_parents.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
void node::register_component(component *c) {
|
void node::register_component(component *c) {
|
||||||
_components.push_back(std::shared_ptr<component>(c));
|
_components.push_back(std::shared_ptr<component>(c));
|
||||||
}
|
}
|
||||||
|
@ -54,4 +95,5 @@ const component::array& node::components() const
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
|
#include "pw/scene/nodepath.hpp"
|
||||||
|
#include "pw/scene/node.hpp"
|
||||||
|
|
||||||
namespace pw {
|
namespace pw {
|
||||||
|
|
||||||
|
void nodepath::update(node *n)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
34
src/scene/src/traverser.cpp
Normal file
34
src/scene/src/traverser.cpp
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
#include "pw/scene/traverser.hpp"
|
||||||
|
#include "pw/scene/node.hpp"
|
||||||
|
|
||||||
|
#include <stack>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace pw {
|
||||||
|
|
||||||
|
//traverser::traverser(direction dir)
|
||||||
|
//{
|
||||||
|
//}
|
||||||
|
|
||||||
|
//void traverser::run(node *root_node)
|
||||||
|
//{
|
||||||
|
// std::stack<node*> 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();
|
||||||
|
// }
|
||||||
|
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
|
}
|
|
@ -4,3 +4,11 @@ add_executable(pwscene_test_node
|
||||||
|
|
||||||
target_link_libraries(pwscene_test_node
|
target_link_libraries(pwscene_test_node
|
||||||
pwcore pwscene)
|
pwcore pwscene)
|
||||||
|
|
||||||
|
|
||||||
|
add_executable(pwscene_test_traverser
|
||||||
|
pwscene_test_traverser.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(pwscene_test_traverser
|
||||||
|
pwcore pwscene)
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <pw/core/serialize.hpp>
|
#include <pw/core/serialize.hpp>
|
||||||
#include <pw/scene/node.hpp>
|
#include <pw/scene/node.hpp>
|
||||||
#include <pw/scene/transform.hpp>
|
#include <pw/scene/transform.hpp>
|
||||||
|
#include <pw/scene/nodepath.hpp>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
@ -13,14 +14,14 @@ int main(int argc,char **argv) {
|
||||||
|
|
||||||
// check
|
// check
|
||||||
{
|
{
|
||||||
n->add_child(new node("sub"));
|
n->add_child(std::make_shared<node>("sub"));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "n name: " << n->name() << std::endl;
|
std::cout << "n name: " << n->name() << std::endl;
|
||||||
std::cout << "n leaf: " << n->is_leaf() << std::endl;
|
std::cout << "n leaf: " << n->is_leaf() << std::endl;
|
||||||
std::cout << "n root: " << n->is_root() << 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 leaf: " << n->children()[0]->is_leaf() << std::endl;
|
||||||
std::cout << "s root: " << n->children()[0]->is_root() << 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;
|
std::cout << "n components: " << n->components().size() << std::endl;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
45
src/scene/tests/pwscene_test_traverser.cpp
Normal file
45
src/scene/tests/pwscene_test_traverser.cpp
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
#include <pw/core/vector.hpp>
|
||||||
|
#include <pw/core/serialize.hpp>
|
||||||
|
#include <pw/scene/node.hpp>
|
||||||
|
#include <pw/scene/transform.hpp>
|
||||||
|
#include <pw/scene/nodepath.hpp>
|
||||||
|
//#include <pw/scene/traverser.hpp>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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 <iostream>
|
||||||
|
|
||||||
|
int main(int argc,char **argv) {
|
||||||
|
|
||||||
|
using namespace pw;
|
||||||
|
|
||||||
|
node::ref root(std::make_shared<node>("root"));
|
||||||
|
root->add_child(std::make_shared<node>("sub-1"));
|
||||||
|
|
||||||
|
node::ref sub_2_1 = std::make_shared<node>("sub-2-1");
|
||||||
|
|
||||||
|
root->add_child(std::make_shared<node>("sub-2"))->add_child(std::make_shared<node>("sub-2-1"));
|
||||||
|
root->add_child(std::make_shared<node>("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;
|
||||||
|
}
|
|
@ -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)
|
||||||
|
|
|
@ -28,6 +28,4 @@ protected:
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -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)
|
|
|
@ -6,75 +6,118 @@
|
||||||
#include "pw/core/quaternion.hpp"
|
#include "pw/core/quaternion.hpp"
|
||||||
#include "pw/core/axisangle.hpp"
|
#include "pw/core/axisangle.hpp"
|
||||||
|
|
||||||
|
#include "pw/scene/node.hpp"
|
||||||
|
|
||||||
|
#include "pw/ui/window.hpp"
|
||||||
|
|
||||||
|
|
||||||
struct lua_state : public pw::script::state {
|
struct lua_state : public pw::script::state {
|
||||||
|
|
||||||
pw::scripting::state _state;
|
pw::scripting::state _state;
|
||||||
pw::scripting::table _namespace;
|
pw::scripting::table _namespace;
|
||||||
|
|
||||||
void load_modules();
|
void load_modules();
|
||||||
|
|
||||||
lua_state() {
|
lua_state() {
|
||||||
|
|
||||||
// create global pw namespace
|
// create global pw namespace
|
||||||
_namespace = _state.create_named_table("pw");
|
_namespace = _state.create_named_table("pw");
|
||||||
|
|
||||||
// add the script as a user type
|
// add the script as a user type
|
||||||
_namespace.new_usertype<lua_state>("script",
|
_namespace.new_usertype<lua_state>("script",
|
||||||
"initialize",&lua_state::load_modules
|
"initialize",&lua_state::load_modules
|
||||||
);
|
);
|
||||||
|
|
||||||
// add this instance as "script"
|
// add this instance as "script"
|
||||||
_namespace.set("script",this);
|
_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)
|
int lua_state::run(const std::string &s)
|
||||||
{
|
{
|
||||||
return _state.script(s.c_str()).get<int>();
|
return _state.script(s.c_str()).get<int>();
|
||||||
}
|
}
|
||||||
|
|
||||||
void lua_state::load_modules() {
|
void lua_state::load_modules() {
|
||||||
|
|
||||||
// open all libraries
|
// open all libraries
|
||||||
_state.open_libraries();
|
_state.open_libraries();
|
||||||
|
|
||||||
typedef double Scalar;
|
typedef double Scalar;
|
||||||
|
|
||||||
_namespace.set("pi",pw::pi<Scalar>());
|
_namespace.set("pi",pw::pi<Scalar>());
|
||||||
|
|
||||||
using namespace pw;
|
using namespace pw;
|
||||||
|
|
||||||
_namespace.new_usertype<vector3d>("vector3",
|
_namespace.new_usertype<vector3d>("vector3",
|
||||||
"set",&vector3d::set,
|
sol::constructors<vector3d(), vector3d(Scalar,Scalar,Scalar)>(),
|
||||||
"x", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::x), &vector3d::set_x),
|
"set",&vector3d::set,
|
||||||
"y", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::y), &vector3d::set_y),
|
"x", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::x), &vector3d::set_x),
|
||||||
"z", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::z), &vector3d::set_z),
|
"y", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::y), &vector3d::set_y),
|
||||||
// "v",&vector3d::values,
|
"z", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::z), &vector3d::set_z),
|
||||||
"clone",&vector3d::clone
|
"norm",&vector3d::norm,
|
||||||
);
|
"cross",&vector3d::cross,
|
||||||
|
"dot",&vector3d::dot,
|
||||||
|
// sol::meta_function::addition, sol::resolve<vector3d(const vector3d&, const vector3d&)>(::operator+),
|
||||||
|
// sol::meta_function::subtraction, &vector3d::operator-
|
||||||
|
// "v",&vector3d::values,
|
||||||
|
"clone",&vector3d::clone
|
||||||
|
);
|
||||||
|
|
||||||
_namespace.new_usertype<quaterniond>("quaternion",
|
_namespace.new_usertype<quaterniond>("quaternion",
|
||||||
"set",&quaterniond::set,
|
sol::constructors<quaterniond(), vector3d(Scalar,Scalar,Scalar,Scalar)>(),
|
||||||
"x", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::x), &quaterniond::set_x),
|
"set",&quaterniond::set,
|
||||||
"y", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::y), &quaterniond::set_y),
|
"x", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::x), &quaterniond::set_x),
|
||||||
"z", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::z), &quaterniond::set_z),
|
"y", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::y), &quaterniond::set_y),
|
||||||
"w", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::w), &quaterniond::set_w),
|
"z", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::z), &quaterniond::set_z),
|
||||||
"dot",&quaterniond::dot,
|
"w", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::w), &quaterniond::set_w),
|
||||||
"inverse",&quaterniond::inverse,
|
"dot",&quaterniond::dot,
|
||||||
"normalized",&quaterniond::normalized,
|
"inverse",&quaterniond::inverse,
|
||||||
"lerp",&quaterniond::lerp
|
"normalized",&quaterniond::normalized,
|
||||||
// "v",&vector3d::values,
|
"lerp",&quaterniond::lerp
|
||||||
// "clone",&vector3d::clone
|
// "v",&vector3d::values,
|
||||||
);
|
// "clone",&vector3d::clone
|
||||||
|
);
|
||||||
|
|
||||||
_namespace.new_usertype<axisangled>("axisangle",
|
_namespace.new_usertype<axisangled>("axisangle",
|
||||||
"axis",scripting::property(&axisangled::axis,&axisangled::set_axis),
|
sol::constructors<axisangled(), axisangled(vector3d,Scalar)>(),
|
||||||
"angle",scripting::property(&axisangled::angle,&axisangled::set_angle)
|
"axis",scripting::property(&axisangled::axis,&axisangled::set_axis),
|
||||||
);
|
"angle",scripting::property(&axisangled::angle,&axisangled::set_angle)
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
_namespace.new_usertype<window>("window",
|
||||||
|
"update",&window::update,
|
||||||
|
"set_title",&window::set_title,
|
||||||
|
"set_size",&window::set_size
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
_namespace.new_usertype<node>("node",
|
||||||
|
sol::constructors<node(), node(std::string)>(),
|
||||||
|
"add_child",&node::add_child,
|
||||||
|
"shared",&node::shared,
|
||||||
|
"children",&node::children,
|
||||||
|
"child_count",&node::child_count,
|
||||||
|
"create", []() -> std::shared_ptr<node> {
|
||||||
|
auto ptr = std::make_shared<node>();
|
||||||
|
return ptr;
|
||||||
|
},
|
||||||
|
// "share",scripting::property(scripting::resolve<std::shared_ptr<node>(std::make_shared<node>))
|
||||||
|
"name",scripting::property(&node::name,&node::set_name)
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// _namespace.set("something", std::shared_ptr<node>(new node()));
|
||||||
|
|
||||||
|
_namespace["my_func"] = []() -> std::shared_ptr<node> {
|
||||||
|
return std::make_shared<node>();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -82,17 +125,17 @@ void lua_state::load_modules() {
|
||||||
|
|
||||||
pw::script::script()
|
pw::script::script()
|
||||||
{
|
{
|
||||||
_state.reset(new lua_state());
|
_state.reset(new lua_state());
|
||||||
}
|
}
|
||||||
|
|
||||||
pw::script::~script()
|
pw::script::~script()
|
||||||
{
|
{
|
||||||
_state = nullptr;
|
_state = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int pw::script::run(const std::string &s)
|
int pw::script::run(const std::string &s)
|
||||||
{
|
{
|
||||||
return _state->run(s);
|
return _state->run(s);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,10 +3,10 @@ pw.script:initialize()
|
||||||
|
|
||||||
print("hello pixwerx!")
|
print("hello pixwerx!")
|
||||||
|
|
||||||
local v = pw.vector3.new()
|
local v1 = pw.vector3.new(3,2,1)
|
||||||
v:set(0,1,2)
|
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
|
---- objects need to be cloned
|
||||||
|
@ -23,18 +23,44 @@ print("v",v.x,v.y,v.z)
|
||||||
|
|
||||||
local q = pw.quaternion.new()
|
local q = pw.quaternion.new()
|
||||||
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)
|
||||||
|
|
||||||
qm = pw.quaternion.lerp(q,qi)
|
qm = pw.quaternion.lerp(q,qi)
|
||||||
print("q.m",qm.x,qm.y,qm.z,qm.w)
|
print("q.m",qm.x,qm.y,qm.z,qm.w)
|
||||||
|
|
||||||
--local aa = pw.axisangle.new()
|
|
||||||
|
|
||||||
---- assign vector to axis
|
-- axis angle test
|
||||||
----aa.axis = v
|
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()
|
--local scene = pw:scene.new()
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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)
|
|
|
@ -2,14 +2,16 @@
|
||||||
|
|
||||||
//#include "glad/glad.h"
|
//#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
|
#if 0
|
||||||
glfwMakeContextCurrent(_window);
|
glfwMakeContextCurrent(_window);
|
||||||
|
|
||||||
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
|
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -17,45 +19,41 @@ pw::window::window() {
|
||||||
|
|
||||||
pw::window::~window() {
|
pw::window::~window() {
|
||||||
|
|
||||||
glfwDestroyWindow(_window);
|
glfwDestroyWindow(_window);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool pw::window::update()
|
bool pw::window::update()
|
||||||
{
|
{
|
||||||
if (!glfwWindowShouldClose(_window)) {
|
if (!glfwWindowShouldClose(_window)) {
|
||||||
|
|
||||||
// do other stuff
|
// do other stuff
|
||||||
#if 0
|
#if 0
|
||||||
glClearColor(1,0,0,1);
|
glClearColor(1,0,0,1);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
glfwSwapBuffers(_window);
|
glfwSwapBuffers(_window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void pw::window::set_title(const char *t)
|
void pw::window::set_title(const char *t)
|
||||||
{
|
{
|
||||||
glfwSetWindowTitle(_window,t);
|
glfwSetWindowTitle(_window,t);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pw::window::set_size(int w,int h) {
|
void pw::window::set_size(int w,int h) {
|
||||||
glfwSetWindowSize(_window,w,h);
|
glfwSetWindowSize(_window,w,h);
|
||||||
}
|
}
|
||||||
|
|
||||||
//void pw::window::load(sol::table &ns)
|
//void pw::window::load(sol::table &ns)
|
||||||
//{
|
//{
|
||||||
// glfwInit();
|
//
|
||||||
|
|
||||||
|
|
||||||
// ns.new_usertype<window>("window",
|
|
||||||
// "update",&window::update,
|
|
||||||
// "set_title",&window::set_title,
|
|
||||||
// "set_size",&window::set_size
|
|
||||||
// );
|
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue