added camera implementation and plenty of other rendering related stuff - splitted out the initialization of the render context

This commit is contained in:
Hartmut Seichter 2018-12-30 23:36:53 +01:00
parent ae37273021
commit f7043fc0cb
43 changed files with 23280 additions and 15161 deletions

Binary file not shown.

View file

@ -3,7 +3,10 @@
add_subdirectory(deps)
add_subdirectory(core)
add_subdirectory(scene)
#add_subdirectory(system)
add_subdirectory(ui)
add_subdirectory(system)
#add_subdirectory(ui)
add_subdirectory(scripting)
add_subdirectory(visual)
add_subdirectory(engine)

View file

@ -9,6 +9,7 @@ set(hdrs
include/pw/core/quaternion.hpp
include/pw/core/serialize.hpp
include/pw/core/image.hpp
include/pw/core/size.hpp
include/pw/core/globals.hpp
)

View file

@ -3,11 +3,14 @@
#include <cstddef>
#include <memory>
#include <string>
namespace pw {
using std::shared_ptr;
typedef float real_t;
}
#endif

View file

@ -2,11 +2,10 @@
#define PW_CORE_IMAGE_HPP
#include <pw/core/globals.hpp>
#include <pw/core/referenced.hpp>
namespace pw {
class image : public referenced<image> {
class image {
};

View file

@ -37,95 +37,95 @@ namespace pw {
template <unsigned int R, unsigned int C, typename T>
class matrix : public matrixbase<T> {
T m[R*C];
T m[R*C];
public:
using typename matrixbase<T>::value_type;
using typename matrixbase<T>::size_type;
using typename matrixbase<T>::value_type;
using typename matrixbase<T>::size_type;
matrix();
matrix();
matrix(const matrix& mtc);
matrix(const matrix& mtc);
matrix& operator = (const matrix& other);
matrix& operator = (const matrix& other);
matrix transposed() const;
matrix transposed() const;
inline matrix& operator *= (const T& b) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) *= b; return *this; }
inline matrix& operator /= (const T& b) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) /= b; return *this; }
inline matrix& operator += (const T& b) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) += b; return *this; }
inline matrix& operator -= (const T& b) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) -= b; return *this; }
inline matrix& operator *= (const T& b) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) *= b; return *this; }
inline matrix& operator /= (const T& b) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) /= b; return *this; }
inline matrix& operator += (const T& b) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) += b; return *this; }
inline matrix& operator -= (const T& b) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) -= b; return *this; }
inline matrix& operator += (const matrix& other) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) += other.at(i); return *this; }
inline matrix& operator -= (const matrix& other) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) -= other.at(i); return *this; }
inline matrix& operator += (const matrix& other) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) += other.at(i); return *this; }
inline matrix& operator -= (const matrix& other) { for (unsigned int i = this->cells(); i--> 0;) this->at(i) -= other.at(i); return *this; }
inline const matrix normalized() const {
const T one_over_n = T(1) / this->norm();
return *this * one_over_n;
}
inline const matrix normalized() const {
const T one_over_n = T(1) / this->norm();
return *this * one_over_n;
}
inline const matrix
get_inverse() const
{
matrix resMat;
inline const matrix
get_inverse() const
{
matrix resMat;
for ( unsigned int r = 0; r < C; ++r) {
for ( unsigned int j = 0; j < R; ++j) {
short sgn = ( (r+j)%2) ? -1 : 1;
matrix<R-1,C-1,T> minor;
this->get_minor(minor,r,j);
resMat.at(r,j) = minor.determinant() * sgn;
}
}
for ( unsigned int r = 0; r < C; ++r) {
for ( unsigned int j = 0; j < R; ++j) {
short sgn = ( (r+j)%2) ? -1 : 1;
matrix<R-1,C-1,T> minor;
this->get_minor(minor,r,j);
resMat.at(r,j) = minor.determinant() * sgn;
}
}
resMat = resMat.transposed();
resMat *= (static_cast<T>(1)/this->determinant());
return resMat;
}
resMat = resMat.transposed();
resMat *= (static_cast<T>(1)/this->determinant());
return resMat;
}
inline
matrix& invert() {
*this = this->get_inverse();
return *this;
}
inline
matrix& invert() {
*this = this->get_inverse();
return *this;
}
void get_minor(matrix<R-1,C-1,T>& res, unsigned int r0, unsigned int c0) const;
void get_minor(matrix<R-1,C-1,T>& res, unsigned int r0, unsigned int c0) const;
T determinant() const;
T determinant() const;
T squared_norm() const;
T squared_norm() const;
T norm() const;
T norm() const;
matrix<R,C,T>& operator *= (const matrix<R,C,T>& rhs);
matrix<R,C,T>& operator *= (const matrix<R,C,T>& rhs);
matrix<R,C,T>& copy_from_data(const T* src) { for (unsigned int i = 0; i < R*C; ++i) { (*this).at(i) = src[i]; } return *this; }
matrix<R,C,T>& copy_from_data(const T* src) { for (unsigned int i = 0; i < R*C; ++i) { (*this).at(i) = src[i]; } return *this; }
matrix<R,C,T> operator * (const matrix<R,C,T>& rhs) const {
return mul(*this,rhs);
}
matrix<R,C,T> operator * (const matrix<R,C,T>& rhs) const {
return mul(*this,rhs);
}
const matrix<C,R,T> reshape() const {
matrix<C,R,T> m;
for (unsigned int r = 0; r < R; ++r)
for (unsigned int c = 0; c < C; ++c)
m(r,c) = (*this)(c,r);
return m;
}
const matrix<C,R,T> reshape() const {
matrix<C,R,T> m;
for (unsigned int r = 0; r < R; ++r)
for (unsigned int c = 0; c < C; ++c)
m(r,c) = (*this)(c,r);
return m;
}
const matrix<R,1,T> get_column(unsigned int col) const {
matrix<R,1,T> c; for (unsigned int r = 0; r < R; ++r) c(r,0) = (this)(r,col);
return c;
}
const matrix<R,1,T> get_column(unsigned int col) const {
matrix<R,1,T> c; for (unsigned int r = 0; r < R; ++r) c(r,0) = (this)(r,col);
return c;
}
const matrix<1,C,T> get_row(unsigned int row) const {
matrix<1,C,T> r; for (unsigned int c = 0; c < C; ++c) r(0,c) = (this)(row,c);
return r;
}
const matrix<1,C,T> get_row(unsigned int row) const {
matrix<1,C,T> r; for (unsigned int c = 0; c < C; ++c) r(0,c) = (this)(row,c);
return r;
}
void normalize();
void normalize();
};
/////////////////////////////////////////////////////////////////////////////
@ -133,49 +133,49 @@ public:
template <unsigned int aR, unsigned int aC, typename T>
inline matrix<aR,aC,T> operator * (const matrix<aR,aC,T>& a, const T& b)
{
matrix<aR,aC,T> res;
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) * b;
return res;
matrix<aR,aC,T> res;
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) * b;
return res;
}
template <unsigned int aR, unsigned int aC, typename T>
inline matrix<aR,aC,T> operator / (const matrix<aR,aC,T>& a, const T& b)
{
matrix<aR,aC,T> res; T oneOverB(1./b);
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) * oneOverB;
return res;
matrix<aR,aC,T> res; T oneOverB(1./b);
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) * oneOverB;
return res;
}
template <unsigned int aR, unsigned int aC, typename T>
inline matrix<aR,aC,T> operator + (const matrix<aR,aC,T>& a, const T& b)
{
matrix<aR,aC,T> res;
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) + b;
return res;
matrix<aR,aC,T> res;
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) + b;
return res;
}
template <unsigned int aR, unsigned int aC, typename T>
inline matrix<aR,aC,T> operator - (const matrix<aR,aC,T>& a, const T& b)
{
matrix<aR,aC,T> res;
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) - b;
return res;
matrix<aR,aC,T> res;
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) - b;
return res;
}
template <unsigned int R, unsigned int C, typename T>
inline matrix<R,C,T> operator + (const matrix<R,C,T>& a, const matrix<R,C,T>& b)
{
matrix<R,C,T> res;
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) + b.at(i);
return res;
matrix<R,C,T> res;
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) + b.at(i);
return res;
}
template <unsigned int R, unsigned int C, typename T>
inline matrix<R,C,T> operator - (const matrix<R,C,T>& a, const matrix<R,C,T>& b)
{
matrix<R,C,T> res;
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) - b.at(i);
return res;
matrix<R,C,T> res;
for (unsigned int i = res.cells(); i--> 0;) res.at(i) = a.at(i) - b.at(i);
return res;
}
/////////////////////////////////////////////////////////////////////////////
@ -184,21 +184,21 @@ template <unsigned int aR,unsigned int aCbR, unsigned int bC, typename T>
matrix<aR,bC,T> static inline
mul(const matrix<aR,aCbR,T>& A, const matrix<aCbR,bC,T>& B)
{
// aC == bR
// set all null
matrix<aR,bC,T> res;
res.fill(0);
// aC == bR
// set all null
matrix<aR,bC,T> res;
res.fill(0);
// compute all resulting cells
for (unsigned int r = 0; r < aR; ++r) {
for (unsigned int c = 0; c < bC; ++c) {
// building inner product
for (unsigned int iI = 0; iI < aCbR;iI++) {
res.at(r,c) += A.at(r,iI) * B.at(iI,c);
}
}
}
return res;
// compute all resulting cells
for (unsigned int r = 0; r < aR; ++r) {
for (unsigned int c = 0; c < bC; ++c) {
// building inner product
for (unsigned int iI = 0; iI < aCbR;iI++) {
res.at(r,c) += A.at(r,iI) * B.at(iI,c);
}
}
}
return res;
}
@ -208,108 +208,108 @@ mul(const matrix<aR,aCbR,T>& A, const matrix<aCbR,bC,T>& B)
template <unsigned int R, unsigned int C,typename T>
matrix<R,C,T>& matrix<R,C,T>::operator *= (const matrix& rhs)
{
*this = mul(*this,rhs);
return *this;
*this = mul(*this,rhs);
return *this;
}
/////////////////////////////////////////////////////////////////////////////
template <unsigned int R, unsigned int C,typename T>
matrix<R,C,T>::matrix()
: matrixbase<T>(R,C,&m[0],true)
: matrixbase<T>(R,C,&m[0],true)
{
}
template <unsigned int R, unsigned int C,typename T>
matrix<R,C,T>::matrix(const matrix &mtc) :
matrixbase<T>(R,C,&m[0],true)
matrixbase<T>(R,C,&m[0],true)
{
*this = mtc;
*this = mtc;
}
template <unsigned int R, unsigned int C,typename T>
matrix<R,C,T> &matrix<R,C,T>::operator = (const matrix<R,C,T> &other)
{
if (this != &other)
for (unsigned int r = 0; r < R;++r)
for (unsigned int c = 0; c < C;++c)
(*this).at(r,c) = other.at(r,c);
return *this;
if (this != &other)
for (unsigned int r = 0; r < R;++r)
for (unsigned int c = 0; c < C;++c)
(*this).at(r,c) = other.at(r,c);
return *this;
}
template <unsigned int R, unsigned int C,typename T>
matrix<R,C,T> matrix<R,C,T>::transposed() const
{
matrix<C,R,T> res;
for (unsigned int r = this->rows();r-->0;)
for (unsigned int c = this->cols();c-->0;)
res.at(c,r) = this->at(r,c);
return res;
matrix<C,R,T> res;
for (unsigned int r = this->rows();r-->0;)
for (unsigned int c = this->cols();c-->0;)
res.at(c,r) = this->at(r,c);
return res;
}
template <unsigned int R, unsigned int C,typename T>
void matrix<R,C,T>::get_minor(matrix<R-1,C-1,T>& res,unsigned int r0, unsigned int c0) const
{
unsigned int r = 0;
for (unsigned int ri = 0; ri < R; ri++)
{
unsigned int c = 0;
if (ri == r0) continue;
for (unsigned int ci = 0; ci < C; ci++)
{
if (ci == c0) continue;
res.data()[r*(C-1)+c] = this->data()[ri*C + ci];//(*this)(ri,ci);
c++;
}
r++;
}
unsigned int r = 0;
for (unsigned int ri = 0; ri < R; ri++)
{
unsigned int c = 0;
if (ri == r0) continue;
for (unsigned int ci = 0; ci < C; ci++)
{
if (ci == c0) continue;
res.data()[r*(C-1)+c] = this->data()[ri*C + ci];//(*this)(ri,ci);
c++;
}
r++;
}
}
template <unsigned int R, unsigned int C,typename T> inline
T matrix<R,C,T>::determinant() const
{
T res(0);
T res(0);
matrix<R-1,C-1,T> minor;
matrix<R-1,C-1,T> minor;
// using Laplace Expansion at compile time
for (unsigned int c = 0; c < C; c++) {
this->get_minor(minor,0,c);
res += ((c % 2 == 0) ? m[c] : -m[c]) * minor.determinant();
}
// using Laplace Expansion at compile time
for (unsigned int c = 0; c < C; c++) {
this->get_minor(minor,0,c);
res += ((c % 2 == 0) ? m[c] : -m[c]) * minor.determinant();
}
return res;
return res;
}
template <unsigned int R, unsigned int C,typename T> inline
T matrix<R,C,T>::squared_norm() const
{
T res(0);
T res(0);
for (unsigned int r = 0; r < R; ++r)
for (unsigned int c = 0; c < C; ++c)
res += ((*this).at(r,c) * (*this).at(r,c));
return res;
for (unsigned int r = 0; r < R; ++r)
for (unsigned int c = 0; c < C; ++c)
res += ((*this).at(r,c) * (*this).at(r,c));
return res;
}
template <unsigned int R, unsigned int C,typename T> inline
T matrix<R,C,T>::norm() const
{
using std::sqrt;
using std::sqrt;
return sqrt(this->squared_norm());
return sqrt(this->squared_norm());
}
template <unsigned int R, unsigned int C,typename T> inline
void matrix<R,C,T>::normalize()
{
T n = norm();
if (n > 0) {
for (unsigned int r = 0; r < R; ++r)
for (unsigned int c = 0; c < C; ++c)
(*this)(r,c) /= n;
}
T n = norm();
if (n > 0) {
for (unsigned int r = 0; r < R; ++r)
for (unsigned int c = 0; c < C; ++c)
(*this)(r,c) /= n;
}
}
/////////////////////////////////////////////////////////////////////////////
@ -323,143 +323,166 @@ class matrix44 : public matrix<4,4,T>
{
public:
using matrix<4,4,T>::matrix;
using matrix<4,4,T>::matrix;
matrix44(const matrix<4,4,T>& i)
{
*this = i;
}
matrix44(const matrix<4,4,T>& i)
{
*this = i;
}
matrix44& operator = (const matrix<4,4,T>& rhs) {
if (this != &rhs){
this->at(0,0) = rhs.at(0,0);this->at(0,1) = rhs.at(0,1);this->at(0,2) = rhs.at(0,2);this->at(0,3) = rhs.at(0,3);
this->at(1,0) = rhs.at(1,0);this->at(1,1) = rhs.at(1,1);this->at(1,2) = rhs.at(1,2);this->at(1,3) = rhs.at(1,3);
this->at(2,0) = rhs.at(2,0);this->at(2,1) = rhs.at(2,1);this->at(2,2) = rhs.at(2,2);this->at(2,3) = rhs.at(2,3);
this->at(3,0) = rhs.at(3,0);this->at(3,1) = rhs.at(3,1);this->at(3,2) = rhs.at(3,2);this->at(3,3) = rhs.at(3,3);
}
matrix44& operator = (const matrix<4,4,T>& rhs) {
if (this != &rhs){
this->at(0,0) = rhs.at(0,0);this->at(0,1) = rhs.at(0,1);this->at(0,2) = rhs.at(0,2);this->at(0,3) = rhs.at(0,3);
this->at(1,0) = rhs.at(1,0);this->at(1,1) = rhs.at(1,1);this->at(1,2) = rhs.at(1,2);this->at(1,3) = rhs.at(1,3);
this->at(2,0) = rhs.at(2,0);this->at(2,1) = rhs.at(2,1);this->at(2,2) = rhs.at(2,2);this->at(2,3) = rhs.at(2,3);
this->at(3,0) = rhs.at(3,0);this->at(3,1) = rhs.at(3,1);this->at(3,2) = rhs.at(3,2);this->at(3,3) = rhs.at(3,3);
}
return *this;
}
return *this;
}
inline static
matrix<4,4,T> projection_from_frustum(T Left,T Right,T Bottom,T Top,T zNear,T zFar)
{
matrix<4,4,T> frustum;
frustum.fill(0);
frustum.at(0,0) = 2 * zNear/(Right-Left);
frustum.at(1,1) = 2 * zNear/(Top-Bottom);
frustum.at(0,2) = (Right+Left)/(Right-Left); //A
frustum.at(1,2) = (Top+Bottom)/(Top-Bottom); //B
frustum.at(2,2) = - (zFar+zNear)/(zFar-zNear); //C
frustum.at(3,2) = -(2 * zFar*zNear)/(zFar-zNear); //D
frustum.at(2,3) = -1;
return frustum;
}
inline static
matrix<4,4,T> orthogonal_projection(T Left, T Right,
T Bottom,T Top,
T Near, T Far)
{
matrix<4,4,T> ortho;
ortho.fill(0);
ortho(0,0) = 2 / (Right-Left);
ortho(1,1) = 2 / (Top-Bottom);
ortho(2,2) = -2 / (Far-Near);
ortho(0,3) = -(Right+Left)/(Right-Left);
ortho(1,3) = -(Top+Bottom)/(Top-Bottom);
ortho(2,3) = -(Far+Near)/(Far-Near);
ortho(3,3) = 1;
return ortho;
}
inline static
matrix<4,4,T> perspective_projection(T fovY, T aspectRatio, T zNear, T zFar)
{
const T height = zNear * tan(fovY/T(360) * pi<T>()); // half height of near plane
const T width = height * aspectRatio; // half width of near plane
return projection_from_frustum(-width, width, -height, height, zNear, zFar );
}
#if TACIT_PIXEL_STUFF_NEEDS_TO_MOVE_TO_SCENE
matrix<4,4,T>&
translate(const T& v1,const T& v2,const T& v3)
{
this->at(12) += v1;
this->at(13) += v2;
this->at(14) += v3;
return *this;
}
matrix<4,4,T>&
translate(const T& v1,const T& v2,const T& v3)
{
this->at(12) += v1;
this->at(13) += v2;
this->at(14) += v3;
return *this;
}
matrix<4,4,T>&
setTranslation(const T& v1,const T& v2,const T& v3)
{
this->identity();
this->at(12) = v1;
this->at(13) = v2;
this->at(14) = v3;
return *this;
}
matrix<4,4,T>&
setTranslation(const T& v1,const T& v2,const T& v3)
{
this->identity();
this->at(12) = v1;
this->at(13) = v2;
this->at(14) = v3;
return *this;
}
matrix<4,4,T>&
setScale(const T& v1,const T& v2,const T& v3)
{
this->identity();
(*this)(0,0) = v1;
(*this)(1,1) = v2;
(*this)(2,2) = v3;
return *this;
}
matrix<4,4,T>&
setScale(const T& v1,const T& v2,const T& v3)
{
this->identity();
(*this)(0,0) = v1;
(*this)(1,1) = v2;
(*this)(2,2) = v3;
return *this;
}
matrix<4,4,T>&
scale(const T& v1,const T& v2,const T& v3)
{
(*this)(0,0) *= v1;
(*this)(1,1) *= v2;
(*this)(2,2) *= v3;
return *this;
}
matrix<4,4,T>&
scale(const T& v1,const T& v2,const T& v3)
{
(*this)(0,0) *= v1;
(*this)(1,1) *= v2;
(*this)(2,2) *= v3;
return *this;
}
static
matrix<4,4,T>
Translation(const T& v1,const T& v2,const T& v3)
{
matrix44<T> res = matrix44<T>::Identity(); res.setTranslation(v1,v2,v3);
return res;
}
static
matrix<4,4,T>
Translation(const T& v1,const T& v2,const T& v3)
{
matrix44<T> res = matrix44<T>::Identity(); res.setTranslation(v1,v2,v3);
return res;
}
inline static
matrix<4,4,T>
AngleAxis(const T& radianRotation,const matrix<3,1,T>& vec);
inline static
matrix<4,4,T> OrthogonalProjection(T Left, T Right,
T Bottom,T Top,
T Near, T Far)
{
matrix<4,4,T> ortho;
ortho.fill(0);
ortho(0,0) = 2 / (Right-Left);
ortho(1,1) = 2 / (Top-Bottom);
ortho(2,2) = -2 / (Far-Near);
ortho(0,3) = -(Right+Left)/(Right-Left);
ortho(1,3) = -(Top+Bottom)/(Top-Bottom);
ortho(2,3) = -(Far+Near)/(Far-Near);
ortho(3,3) = 1;
return ortho;
}
inline static
matrix<4,4,T> Frustum(T Left,T Right,T Bottom,T Top,T zNear,T zFar)
{
matrix<4,4,T> frustum;
frustum.fill(0);
frustum(0,0) = 2 * zNear/(Right-Left);
frustum(1,1) = 2 * zNear/(Top-Bottom);
frustum(0,2) = (Right+Left)/(Right-Left); //A
frustum(1,2) = (Top+Bottom)/(Top-Bottom); //B
frustum(2,2) = - (zFar+zNear)/(zFar-zNear); //C
frustum(3,2) = -(2 * zFar*zNear)/(zFar-zNear); //D
frustum(2,3) = -1;
return frustum;
}
inline static
matrix<4,4,T>
AngleAxis(const T& radianRotation,const matrix<3,1,T>& vec);
inline static
matrix<4,4,T> PerspectiveProjection(T fovY, T aspectRatio, T zNear, T zFar)
{
T height = zNear * tan(fovY/T(360) * Pi); // half height of near plane
T width = height * aspectRatio; // half width of near plane
return Frustum(-width, width, -height, height, zNear, zFar );
}
inline static
matrix<4,4,T> Frustum(T Left,T Right,T Bottom,T Top,T zNear,T zFar)
{
matrix<4,4,T> frustum;
inline static
matrix<4,4,T> LookAt(const matrix<3,1,T>& eye,
const matrix<3,1,T>& target,
const matrix<3,1,T>& up);
frustum.fill(0);
frustum(0,0) = 2 * zNear/(Right-Left);
frustum(1,1) = 2 * zNear/(Top-Bottom);
frustum(0,2) = (Right+Left)/(Right-Left); //A
frustum(1,2) = (Top+Bottom)/(Top-Bottom); //B
frustum(2,2) = - (zFar+zNear)/(zFar-zNear); //C
frustum(3,2) = -(2 * zFar*zNear)/(zFar-zNear); //D
frustum(2,3) = -1;
return frustum;
}
matrix<4,4,T>&
rotate(const matrix<3,1,T>& vec, const T& rotation)
{
matrix44<T> rot = matrix44<T>::AngleAxis(rotation,vec); *this *= rot;
return *this;
}
inline static
matrix<4,4,T> LookAt(const matrix<3,1,T>& eye,
const matrix<3,1,T>& target,
const matrix<3,1,T>& up);
matrix<4,4,T>&
rotate(const matrix<3,1,T>& vec, const T& rotation)
{
matrix44<T> rot = matrix44<T>::AngleAxis(rotation,vec); *this *= rot;
return *this;
}
#endif
@ -475,13 +498,13 @@ public:
template <> inline
float matrix<1,1,float>::determinant() const
{
return this->at(0);
return this->at(0);
}
template <> inline
double matrix<1,1,double>::determinant() const
{
return this->at(0);
return this->at(0);
}
@ -491,83 +514,83 @@ template <typename T>
class matrix31 : public matrix<3,1,T> {
public:
using matrix<3,1,T>::operator =;
using matrix<3,1,T>::operator =;
inline static
matrix<3,1,T> Cross(const matrix<3,1,T>& vec1, const matrix<3,1,T>& vec2)
{
matrix<3,1,T> res;
inline static
matrix<3,1,T> Cross(const matrix<3,1,T>& vec1, const matrix<3,1,T>& vec2)
{
matrix<3,1,T> res;
res.at(0) = vec1.at(1) * vec2.at(2) - vec2.at(1) * vec1.at(2);
res.at(1) = vec1.at(2) * vec2.at(0) - vec2.at(2) * vec1.at(0);
res.at(2) = vec1.at(0) * vec2.at(1) - vec2.at(0) * vec1.at(1);
res.at(0) = vec1.at(1) * vec2.at(2) - vec2.at(1) * vec1.at(2);
res.at(1) = vec1.at(2) * vec2.at(0) - vec2.at(2) * vec1.at(0);
res.at(2) = vec1.at(0) * vec2.at(1) - vec2.at(0) * vec1.at(1);
return res;
}
return res;
}
};
template <typename T>
matrix<4,4,T>
matrix44<T>::AngleAxis(const T &radianRotation, const matrix<3,1,T> &vec)
{
matrix44<T> R = matrix44<T>::Identity();
matrix44<T> R = matrix44<T>::Identity();
if (vec.norm() < std::numeric_limits<T>::epsilon()) return R;
if (vec.norm() < std::numeric_limits<T>::epsilon()) return R;
T _fCos = (T) cos (radianRotation);
T _fCos = (T) cos (radianRotation);
matrix<3,1,T> _vCos(vec * (1 - _fCos));
matrix<3,1,T> _vSin(vec * (T)sin(radianRotation));
matrix<3,1,T> _vCos(vec * (1 - _fCos));
matrix<3,1,T> _vSin(vec * (T)sin(radianRotation));
R.at(0) = (T) ((vec(0,0) * _vCos(0,0)) + _fCos);
R.at(4) = (T) ((vec(0,0) * _vCos(1,0)) - _vSin(2,0));
R.at(8) = (T) ((vec(0,0) * _vCos(2,0)) + _vSin(1,0));
R.at(0) = (T) ((vec(0,0) * _vCos(0,0)) + _fCos);
R.at(4) = (T) ((vec(0,0) * _vCos(1,0)) - _vSin(2,0));
R.at(8) = (T) ((vec(0,0) * _vCos(2,0)) + _vSin(1,0));
R.at(1) = (T) ((vec(1,0) * _vCos(0,0)) + _vSin(2,0));
R.at(5) = (T) ((vec(1,0) * _vCos(1,0)) + _fCos);
R.at(9) = (T) ((vec(1,0) * _vCos(2,0)) - _vSin(0,0));
R.at(1) = (T) ((vec(1,0) * _vCos(0,0)) + _vSin(2,0));
R.at(5) = (T) ((vec(1,0) * _vCos(1,0)) + _fCos);
R.at(9) = (T) ((vec(1,0) * _vCos(2,0)) - _vSin(0,0));
R.at(2) = (T) ((vec(2,0) * _vCos(0,0)) - _vSin(1,0));
R.at(6) = (T) ((vec(2,0) * _vCos(1,0)) + _vSin(0,0));
R.at(10)= (T) ((vec(2,0) * _vCos(2,0)) + _fCos);
R.at(2) = (T) ((vec(2,0) * _vCos(0,0)) - _vSin(1,0));
R.at(6) = (T) ((vec(2,0) * _vCos(1,0)) + _vSin(0,0));
R.at(10)= (T) ((vec(2,0) * _vCos(2,0)) + _fCos);
return R;
return R;
}
template <typename T>
matrix<4,4,T>
matrix44<T>::LookAt(const matrix<3,1,T> &eye, const matrix<3,1,T> &target, const matrix<3,1,T> &up)
{
matrix<4,4,T> lookat = matrix<4,4,T>::Identity();
matrix<4,4,T> lookat = matrix<4,4,T>::Identity();
matrix<3,1,T> L; L = target - eye;
L.normalize();
matrix<3,1,T> S = matrix31<T>::Cross(L,up);
S.normalize();
matrix<3,1,T> Ud = matrix31<T>::Cross(S,L);
Ud.normalize();
matrix<3,1,T> L; L = target - eye;
L.normalize();
matrix<3,1,T> S = matrix31<T>::Cross(L,up);
S.normalize();
matrix<3,1,T> Ud = matrix31<T>::Cross(S,L);
Ud.normalize();
lookat(0,0) = S.at(0);
lookat(0,1) = S.at(1);
lookat(0,2) = S.at(2);
lookat(0,3) = T(0);
lookat(0,0) = S.at(0);
lookat(0,1) = S.at(1);
lookat(0,2) = S.at(2);
lookat(0,3) = T(0);
lookat(1,0) = Ud.at(0);
lookat(1,1) = Ud.at(1);
lookat(1,2) = Ud.at(2);
lookat(1,3) = T(0);
lookat(1,0) = Ud.at(0);
lookat(1,1) = Ud.at(1);
lookat(1,2) = Ud.at(2);
lookat(1,3) = T(0);
lookat(2,0) = -L.at(0);
lookat(2,1) = -L.at(1);
lookat(2,2) = -L.at(2);
lookat(3,2) = T(0);
lookat(2,0) = -L.at(0);
lookat(2,1) = -L.at(1);
lookat(2,2) = -L.at(2);
lookat(3,2) = T(0);
lookat(3,0) = eye.at(0);
lookat(3,1) = eye.at(1);
lookat(3,2) = eye.at(2);
lookat(3,3) = 1;
lookat(3,0) = eye.at(0);
lookat(3,1) = eye.at(1);
lookat(3,2) = eye.at(2);
lookat(3,3) = 1;
return lookat;
return lookat;
}

View file

@ -0,0 +1,50 @@
/*
* Copyright (C) 1999-2017 Hartmut Seichter
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PW_CORE_SIZE_HPP
#define PW_CORE_SIZE_HPP
#include <pw/core/globals.hpp>
namespace pw {
template <typename T_>
struct size {
T_ dim[2] = { 0, 0};
size(T_ w,T_ h) : dim( { w, h }) {}
const T_ width() { return dim[0]; }
const T_ height() { return dim[1]; }
};
typedef size<int> sizei;
typedef size<float> sizef;
}
#endif

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -16,4 +16,4 @@ target_include_directories(pixwerx
${CMAKE_SOURCE_DIR}/src/scripting/include
)
target_link_libraries(pixwerx pwcore pwui pwscripting)
target_link_libraries(pixwerx pwcore pwsystem pwscripting)

View file

@ -1,5 +1,6 @@
set(hdrs
set(hdrs
include/pw/scene/camera.hpp
include/pw/scene/component.hpp
include/pw/scene/node.hpp
include/pw/scene/nodepath.hpp
@ -10,6 +11,7 @@ set(hdrs
set(srcs
src/node.cpp
src/nodepath.cpp
src/camera.cpp
src/component.cpp
src/transform.cpp
src/traverser.cpp

View file

@ -0,0 +1,48 @@
#ifndef PW_SCENE_CAMERA_HPP
#define PW_SCENE_CAMERA_HPP
#include <pw/core/matrix.hpp>
#include <pw/scene/component.hpp>
namespace pw {
class camera : public component {
public:
enum clearflags {
color,
depth,
none
};
enum projection {
orthographic,
perspective
};
using component::component;
camera();
void set_projection(const matrix44d &projection);
const matrix44d& projection() const;
protected:
double _fov;
double _near_plane;
double _far_plane;
double _ortho_size = 2;
private:
matrix44d _projection;
};
}
#endif

View file

@ -23,13 +23,9 @@ public:
//! only very few components can be attached multiple times
virtual bool singular() const { return true; }
explicit component(node* n = nullptr);
component();
component(const component& other);
virtual ~component();
protected:
node* _node;
};
}

View file

@ -1,3 +1,27 @@
/*
* Copyright (C) 1999-2017 Hartmut Seichter
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PW_SCENE_NODE_HPP
#define PW_SCENE_NODE_HPP
@ -9,8 +33,6 @@
namespace pw {
class traverser;
/**
* @brief nodes represent the structure for the scene graph
*/
@ -19,7 +41,7 @@ public:
typedef shared_ptr<node> ref;
typedef std::vector<ref> ref_array;
typedef std::vector<node*> ptr_array;
typedef std::vector<node*> ptr_array;
friend class component;
@ -29,21 +51,20 @@ public:
//! copy c'tor
node(const node& node);
//! wrapper for scripting interface
std::shared_ptr<node> shared();
//! d'tor
virtual ~node();
//! cloning interface
virtual node* clone(const unsigned short& copymode) const;
virtual ref clone(const unsigned short& copymode) const;
std::string name() const;
void set_name(const std::string &name);
std::string name() const; //!< get name
void set_name(const std::string &name); //!< set name
//! return list of parents
inline const ptr_array& parents() const { return _parents; }
//! add a child node
std::shared_ptr<node> add_child(std::shared_ptr<node> anode);
ref add_child(ref anode);
//! get the list of children
inline const ref_array& children() const { return _children; }
@ -57,45 +78,29 @@ public:
//! 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);
//! add a node component
void add_component(component::ref c);
//! 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);
//! remove a node component
void remove_component(component::ref c);
protected:
void register_component(component* c);
void unregister_component(component* c);
ref_array _children;
ptr_array _parents;
component::array _components;
std::string _name;
private:
//! add a node as parent node
void add_parent(node *anode);
};
}

View file

@ -25,10 +25,10 @@ protected:
matrix44d _global;
matrix44d _global_inverse;
std::vector<ref> _path;
};
}
#endif

View file

@ -1,29 +1,32 @@
#ifndef PW_SCENE_TRAVERSER_HPP
#define PW_SCENE_TRAVERSER_HPP
#include <pw/scene/node.hpp>
#include <functional>
namespace pw {
class node;
class traverser {
//class traverser {
//public:
public:
// enum direction {
// up,
// down
// };
enum direction {
up,
down
};
// traverser(direction dir = direction::down);
typedef std::function<void(node*)> edge_function;
// void enter(node* node);
direction _direction = direction::up;
// void leave(node* node);
edge_function on_enter,on_leave;
//protected:
void visit(node* node);
// direction _direction;
void bfs(node::ref root);
//};
};
}

25
src/scene/src/camera.cpp Normal file
View file

@ -0,0 +1,25 @@
#include "pw/scene/camera.hpp"
namespace pw {
camera::camera()
: _fov(60.0)
, _near_plane(0.2)
, _far_plane(1000)
{
set_projection(matrix44d::perspective_projection(_fov,1,_near_plane,_far_plane));
}
void camera::set_projection(const matrix44d& projection)
{
this->_projection = projection;
}
const matrix44d &camera::projection() const
{
return _projection;
}
}

View file

@ -11,30 +11,29 @@
namespace pw {
component::component(node *n)
: _node(n)
component::component()
// : _node(n)
{
if (_node != nullptr) {
// if (_node != nullptr) {
for (const auto c : _node->components()) {
if (typeid (c.get()).name() == typeid (this).name()) {
// error
return;
}
}
_node->register_component(this);
}
// 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)
: _node(other._node)
// : _node(other._node)
{
}
component::~component() {
if (_node != nullptr) _node->unregister_component(this);
component::~component()
{
// if (_node != nullptr) _node->unregister_component(this);
}

View file

@ -3,7 +3,7 @@
namespace pw {
node::node(const std::string &name)
: _name(name)
: _name(name)
{
}
@ -14,22 +14,22 @@ node::node(const node &node)
{
}
node *node::clone(const unsigned short &copymode) const
node::ref node::clone(const unsigned short &copymode) const
{
return nullptr;
return nullptr;
}
std::string node::name() const
{
return _name;
return _name;
}
void node::set_name(const std::string &name)
{
_name = name;
_name = name;
}
std::shared_ptr<node> node::add_child(std::shared_ptr<node> anode)
node::ref node::add_child(ref anode)
{
anode->add_parent(this);
_children.push_back(anode);
@ -41,63 +41,55 @@ void node::add_parent(node *anode)
_parents.push_back(anode);
}
void node::visit(visitor& visitor)
{
visitor.enter(this);
//void node::visit(visitor& visitor)
//{
// visitor.enter(this);
if (visitor.direction == node::visitor::direction::up)
this->ascend(visitor);
else {
this->descend(visitor);
}
// if (visitor.direction == node::visitor::direction::up)
// this->ascend(visitor);
// else {
// this->descend(visitor);
// }
visitor.leave(this);
}
// visitor.leave(this);
//}
void node::ascend(visitor& visitor)
{
for (auto p : this->parents()) p->visit(visitor);
}
//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);
}
//void node::descend(visitor& visitor)
//{
// for (auto c : this->children()) c->visit(visitor);
//}
node::~node()
{
_children.clear();
_parents.clear();
_children.clear();
_parents.clear();
}
//
// components
//
//
void node::register_component(component *c) {
_components.push_back(std::shared_ptr<component>(c));
}
void node::unregister_component(component *c)
void node::add_component(component::ref c)
{
// component::array::iterator it = _components.end();
_components.push_back(c);
}
// while ((it = std::find(_components.begin(),_components.end(),std::shared_ptr<component>(c))) != _components.end()) {
// _components.erase(it);
// }
void node::remove_component(component::ref c)
{
component::array::iterator it = _components.end();
while ((it = std::find(_components.begin(),_components.end(),std::shared_ptr<component>(c))) != _components.end()) {
_components.erase(it);
}
}
const component::array& node::components() const
{
return _components;
return _components;
}

View file

@ -19,8 +19,8 @@ void transform::set_global(const matrix44d &global)
}
void transform::update() {
void transform::update()
{
}
}

View file

@ -3,32 +3,50 @@
#include <stack>
#include <iostream>
#include <queue>
namespace pw {
//traverser::traverser(direction dir)
//{
//}
//void traverser::run(node *root_node)
//{
// std::stack<node*> node_stack;
// node_stack.push(root_node);
traverser t = traverser();
// node *n = root_node;
void traverser::visit(node *node)
{
// enter node
on_enter(node);
// visit
if (direction::up == _direction)
for (auto p : node->parents()) this->visit(p);
else
for (auto c : node->children()) this->visit(c.get());
// while (!node_stack.empty()) {
//leave node
on_leave(node);
}
// if (n->is_leaf()) {
// std::cout << n->name() << std::endl;
// node_stack.pop();
// // save
// } else {
// for
//// n = n->children().front().get();
// }
void traverser::bfs(node::ref root)
{
std::stack<node::ref> q;
q.push(root);
// }
//}
node::ref_array leafs;
while (!q.empty())
{
node::ref n = q.top();
std::cout << "d:" << q.size() << " l:" << n->is_leaf() << " n:" << n->name();
// visit n
q.pop();
// add all children
for(auto c : n->children()) q.push(c);
std::cout << std::endl;
}
}
}

View file

@ -27,9 +27,9 @@ int main(int argc,char **argv) {
// thats the only and most ugly way to register a component
new component(n.get());
// new component(n.get());
new transform(n.get());
n->add_component(std::make_shared<transform>());
std::cout << "n components: " << n->components().size() << std::endl;

View file

@ -3,23 +3,23 @@
#include <pw/scene/node.hpp>
#include <pw/scene/transform.hpp>
#include <pw/scene/nodepath.hpp>
//#include <pw/scene/traverser.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;
};
//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::enter(pw::node *n)
//{
// std::cout << n->name() << " " << n->is_leaf() << std::endl;
//}
void test_visitor::leave(pw::node *n)
{
}
//void test_visitor::leave(pw::node *n)
//{
//}
#include <iostream>
@ -37,9 +37,10 @@ int main(int argc,char **argv) {
std::cout << "sub-2-1 parent count: " << sub_2_1->parents().size() << std::endl;
test_visitor tv;
root->visit(tv);
traverser tv;
tv.bfs(root);
return 0;
}

View file

@ -1,5 +1,3 @@
#add_subdirectory(src)
set(hdrs
include/pw/scripting/script.hpp
@ -8,6 +6,12 @@ set(hdrs
set(srcs
src/script.cpp
src/script_core.hpp
src/script_core.cpp
src/script_system.hpp
src/script_system.cpp
src/script_scene.hpp
src/script_scene.cpp
)
add_library(pwscripting
@ -16,17 +20,14 @@ add_library(pwscripting
${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
${CMAKE_SOURCE_DIR}/src/deps/sol2-2.20.6
PUBLIC
include
)
target_link_libraries(pwscripting lualib pwcore pwui pwscene)
target_link_libraries(pwscripting lualib pwcore pwsystem pwscene)

View file

@ -1,7 +1,7 @@
#ifndef PW_SCRIPTING_SCRIPTING_HPP
#define PW_SCRIPTING_SCRIPTING_HPP
#include "sol.hpp"
#include "sol/sol.hpp"
#include <lua.hpp>
namespace pw {

View file

@ -2,19 +2,18 @@
#include "pw/scripting/scripting.hpp"
#include "pw/core/vector.hpp"
#include "pw/core/quaternion.hpp"
#include "pw/core/axisangle.hpp"
#include "pw/scene/node.hpp"
#include "pw/ui/window.hpp"
#include "script_core.hpp"
#include "script_system.hpp"
#include "script_scene.hpp"
namespace pw {
struct lua_state : public pw::script::state {
struct lua_state : public script::state {
pw::scripting::state _state;
pw::scripting::table _namespace;
scripting::state _state;
scripting::table _namespace;
void load_modules();
@ -46,88 +45,32 @@ void lua_state::load_modules() {
// open all libraries
_state.open_libraries();
typedef double Scalar;
_namespace.set("pi",pw::pi<Scalar>());
using namespace pw;
_namespace.new_usertype<vector3d>("vector3",
sol::constructors<vector3d(), vector3d(Scalar,Scalar,Scalar)>(),
"set",&vector3d::set,
"x", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::x), &vector3d::set_x),
"y", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::y), &vector3d::set_y),
"z", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::z), &vector3d::set_z),
"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",
sol::constructors<quaterniond(), vector3d(Scalar,Scalar,Scalar,Scalar)>(),
"set",&quaterniond::set,
"x", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::x), &quaterniond::set_x),
"y", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::y), &quaterniond::set_y),
"z", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::z), &quaterniond::set_z),
"w", scripting::property(scripting::resolve<const Scalar&() const>(&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<axisangled>("axisangle",
sol::constructors<axisangled(), axisangled(vector3d,Scalar)>(),
"axis",scripting::property(&axisangled::axis,&axisangled::set_axis),
"angle",scripting::property(&axisangled::angle,&axisangled::set_angle)
);
_namespace.new_usertype<window>("window",
"update",&window::update,
"title",sol::writeonly_property(&window::set_title),
"set_size",&window::set_size
);
_namespace.new_usertype<node>("node",
sol::constructors<node(), node(std::string)>(),
"add_child",&node::add_child,
"children",sol::readonly_property(&node::children),
"child_count",sol::readonly_property(&node::child_count),
"create", []() -> std::shared_ptr<node> { return std::make_shared<node>(); },
"is_leaf", sol::readonly_property(&node::is_leaf),
"is_root", sol::readonly_property(&node::is_root),
// "share",scripting::property(scripting::resolve<std::shared_ptr<node>(std::make_shared<node>))
"name",scripting::property(&node::name,&node::set_name)
);
script_core::load(_namespace);
script_system::load(_namespace);
script_scene::load(_namespace);
}
pw::script::script()
script::script()
{
_state.reset(new lua_state());
}
pw::script::~script()
script::~script()
{
_state = nullptr;
}
int pw::script::run(const std::string &s)
int script::run(const std::string &s)
{
return _state->run(s);
}
}

View file

@ -0,0 +1,59 @@
#include "script_core.hpp"
#include "pw/core/vector.hpp"
#include "pw/core/quaternion.hpp"
#include "pw/core/axisangle.hpp"
namespace pw {
void script_core::load(sol::table &ns)
{
typedef double Scalar;
ns.set("pi",pw::pi<Scalar>());
ns.new_usertype<vector3d>("vector3",
sol::constructors<vector3d(), vector3d(Scalar,Scalar,Scalar)>(),
"set",&vector3d::set,
"x", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::x), &vector3d::set_x),
"y", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::y), &vector3d::set_y),
"z", scripting::property(scripting::resolve<const Scalar&() const>(&vector3d::z), &vector3d::set_z),
"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
);
ns.new_usertype<quaterniond>("quaternion",
sol::constructors<quaterniond(), quaterniond(Scalar,Scalar,Scalar,Scalar)>(),
"set",&quaterniond::set,
"x", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::x), &quaterniond::set_x),
"y", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::y), &quaterniond::set_y),
"z", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::z), &quaterniond::set_z),
"w", scripting::property(scripting::resolve<const Scalar&() const>(&quaterniond::w), &quaterniond::set_w),
"dot",&quaterniond::dot,
"inverse",scripting::readonly_property(&quaterniond::inverse),
"normalized",&quaterniond::normalized,
"lerp",&quaterniond::lerp
// "v",&vector3d::values,
// "clone",&vector3d::clone
);
ns.new_usertype<axisangled>("axisangle",
sol::constructors<axisangled(), axisangled(vector3d,Scalar)>(),
"axis",scripting::property(&axisangled::axis,&axisangled::set_axis),
"angle",scripting::property(&axisangled::angle,&axisangled::set_angle)
);
}
}

View file

@ -0,0 +1,17 @@
#ifndef PW_SCRIPTING_PRIVATE_CORE_HPP
#define PW_SCRIPTING_PRIVATE_CORE_HPP
#include <pw/scripting/scripting.hpp>
namespace pw {
struct script_core {
static void load(scripting::table& ns);
};
}
#endif

View file

@ -0,0 +1,23 @@
#include "script_scene.hpp"
#include <pw/scene/node.hpp>
namespace pw {
void script_scene::load(sol::table &ns)
{
ns.new_usertype<node>("node",
sol::constructors<node(), node(std::string)>(),
"add_child",&node::add_child,
"children",sol::readonly_property(&node::children),
"child_count",sol::readonly_property(&node::child_count),
"create", []() -> std::shared_ptr<node> { return std::make_shared<node>(); },
"is_leaf", sol::readonly_property(&node::is_leaf),
"is_root", sol::readonly_property(&node::is_root),
// "share",scripting::property(scripting::resolve<std::shared_ptr<node>(std::make_shared<node>))
"name",scripting::property(&node::name,&node::set_name)
);
}
}

View file

@ -0,0 +1,17 @@
#ifndef PW_SCRIPTING_PRIVATE_SCENE_HPP
#define PW_SCRIPTING_PRIVATE_SCENE_HPP
#include <pw/scripting/scripting.hpp>
namespace pw {
struct script_scene {
static void load(scripting::table& ns);
};
}
#endif

View file

@ -0,0 +1,16 @@
#include "script_system.hpp"
#include "pw/system/window.hpp"
namespace pw {
void script_system::load(sol::table &ns)
{
ns.new_usertype<window>("window",
"update",&window::update,
"title",sol::writeonly_property(&window::set_title),
"set_size",&window::set_size
);
}
}

View file

@ -0,0 +1,17 @@
#ifndef PW_SCRIPTING_PRIVATE_SYSTEM_HPP
#define PW_SCRIPTING_PRIVATE_SYSTEM_HPP
#include <pw/scripting/scripting.hpp>
namespace pw {
struct script_system {
static void load(scripting::table& ns);
};
}
#endif

View file

@ -24,12 +24,12 @@ print("v1 ",v1.x,v1.y,v1.z)
local q = pw.quaternion.new()
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)
local q2 = pw.quaternion.new(0,0,0,1)
qm = pw.quaternion.lerp(q,qi)
qm = pw.quaternion.lerp(q,qi,0.5)
print("q.m",qm.x,qm.y,qm.z,qm.w)
@ -71,6 +71,8 @@ local w = pw.window.new()
w.title = "pixwerx 1.0"
w:set_size(1280,768)
while w:update()
do
-- print("update")

24
src/system/CMakeLists.txt Normal file
View file

@ -0,0 +1,24 @@
set(hdrs
include/pw/system/window.hpp
)
set(srcs
src/window.cpp
)
add_library(pwsystem
STATIC
${hdrs}
${srcs}
)
target_include_directories(
pwsystem
PUBLIC
include
)
target_link_libraries(pwsystem pwcore pwvisual glfw glad)
#add_subdirectory(tests)

View file

@ -0,0 +1,33 @@
#ifndef PW_WINDOW_HPP
#define PW_WINDOW_HPP
#include <pw/core/globals.hpp>
namespace pw {
class window {
public:
window();
~window();
bool update();
void set_title(const std::string& title);
void set_size(int w, int h);
// context* get_context();
int get_heigth();
int get_width();
protected:
struct impl;
std::unique_ptr<impl> _impl;
};
}
#endif

143
src/system/src/window.cpp Normal file
View file

@ -0,0 +1,143 @@
#include "pw/system/window.hpp"
#include "GLFW/glfw3.h"
//#include "glad/glad.h"
#include "pw/visual/context.hpp"
#include <iostream>
namespace pw {
struct window_context : context {
virtual bool make_current() override;
virtual void resize() override;
// virtual context::size size() override;
virtual void flush() override;
};
bool window_context::make_current()
{
}
void window_context::resize()
{
}
void window_context::flush()
{
}
struct window::impl {
GLFWwindow *_window = nullptr;
// window_context _context;
static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
window::impl* impl = static_cast<window::impl*>(glfwGetWindowUserPointer(window));
// impl->on_resize(width,height);
// std::cout << "framebuffer " << width << "x" << height << std::endl;
// glViewport(0, 0, width, height);
}
impl()
{
glfwInit();
_window = glfwCreateWindow(640, 480, "pixwerxs", nullptr, nullptr);
glfwSetWindowUserPointer(_window,this);
glfwSetFramebufferSizeCallback(_window, window::impl::framebuffer_size_callback);
#if 0
glfwMakeContextCurrent(_window);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
#endif
}
~impl()
{
glfwDestroyWindow(_window);
}
bool update()
{
if (!glfwWindowShouldClose(_window)) {
glfwPollEvents();
// do other stuff
#if 0
glClearColor(1,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
#endif
glfwSwapBuffers(_window);
return true;
}
return false;
}
void set_title(const std::string& title)
{
glfwSetWindowTitle(_window,title.c_str());
}
void set_size(int w,int h)
{
glfwSetWindowSize(_window,w,h);
}
};
window::window()
: _impl(std::make_unique<window::impl>())
{
}
window::~window()
{
}
bool window::update()
{
return _impl->update();
}
void window::set_title(const std::string& title)
{
_impl->set_title(title);
}
void window::set_size(int w,int h)
{
_impl->set_size(w,h);
}
int window::get_width()
{
// int w,h;
// glfwGetWindowSize(_window,&w,&h);
// return w;
}
int window::get_heigth()
{
// int w,h;
// glfwGetWindowSize(_window,&w,&h);
// return h;
}
}

View file

@ -1,43 +0,0 @@
#ifndef PW_WINDOW_HPP
#define PW_WINDOW_HPP
//#include "glad/glad.h"
#include "GLFW/glfw3.h"
//#include "sol.hpp"
namespace pw {
class window;
class context {
GLFWwindow* _window;
public:
context(window& other);
~context();
};
class window {
GLFWwindow* _window;
public:
window();
~window();
bool update();
void set_title(const char* t);
void set_size(int w, int h);
context* get_context();
// static void load(sol::table &ns);
};
}
#endif

View file

@ -1,66 +0,0 @@
#include "pw/ui/window.hpp"
//#include "glad/glad.h"
pw::window::window()
{
glfwInit();
_window = glfwCreateWindow(640, 480, "pixwerxs", nullptr, nullptr);
#if 0
glfwMakeContextCurrent(_window);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
#endif
}
pw::window::~window() {
glfwDestroyWindow(_window);
}
bool pw::window::update()
{
if (!glfwWindowShouldClose(_window)) {
// do other stuff
#if 0
glClearColor(1,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
#endif
glfwSwapBuffers(_window);
glfwPollEvents();
return true;
}
return false;
}
void pw::window::set_title(const char *t)
{
glfwSetWindowTitle(_window,t);
}
void pw::window::set_size(int w,int h) {
glfwSetWindowSize(_window,w,h);
}
std::pair<int,int> pw::window::get_size()
{
int x,y;
glfwGetWindowSize(_window,&x,&y);
return std::make_pair(x,y);
}
//void pw::window::load(sol::table &ns)
//{
//
//}

26
src/visual/CMakeLists.txt Normal file
View file

@ -0,0 +1,26 @@
set(hdrs
include/pw/visual/renderer.hpp
include/pw/visual/context.hpp
)
set(srcs
src/renderer.cpp
src/context.cpp
)
add_library(pwvisual
STATIC
${hdrs}
${srcs}
)
target_include_directories(
pwvisual
PUBLIC
include
)
target_link_libraries(pwvisual pwscene)
#add_subdirectory(tests)

View file

@ -0,0 +1,25 @@
#ifndef PW_VISUAL_CONTEXT_HPP
#define PW_VISUAL_CONTEXT_HPP
#include <pw/scene/component.hpp>
namespace pw {
class context {
public:
struct size {
int width,height;
};
virtual bool make_current() = 0;
virtual void resize() = 0;
virtual size size() = 0;
virtual void flush() = 0;
virtual ~context() = default;
};
}
#endif

View file

@ -0,0 +1,19 @@
#ifndef PW_VISUAL_RENDERER_HPP
#define PW_VISUAL_RENDERER_HPP
#include <pw/scene/component.hpp>
namespace pw {
class context;
class renderer {
void render(context& context);
};
}
#endif

View file

View file

@ -0,0 +1,9 @@
#include "pw/visual/renderer.hpp"
namespace pw {
void renderer::render(pw::context &context)
{
}
}