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

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