some small refactorings
This commit is contained in:
parent
d8fac9045b
commit
8eda3df225
23 changed files with 314 additions and 268 deletions
|
@ -15,11 +15,11 @@ set(hdrs
|
|||
include/pw/core/rectangle.hpp
|
||||
include/pw/core/serialize.hpp
|
||||
include/pw/core/size.hpp
|
||||
include/pw/core/timer.hpp
|
||||
include/pw/core/time.hpp
|
||||
include/pw/core/mesh.hpp
|
||||
include/pw/core/image.hpp
|
||||
include/pw/core/vector.hpp
|
||||
include/pw/core/transform_tools.hpp
|
||||
include/pw/core/matrix_transform.hpp
|
||||
)
|
||||
|
||||
set(misc
|
||||
|
@ -29,13 +29,13 @@ set(misc
|
|||
)
|
||||
|
||||
set(srcs
|
||||
# src/buffer.cpp
|
||||
# src/buffer.cpp
|
||||
src/image.cpp
|
||||
src/debug.cpp
|
||||
src/mesh.cpp
|
||||
src/core.cpp
|
||||
src/serialize.cpp
|
||||
src/timer.cpp
|
||||
src/time.cpp
|
||||
src/image.cpp
|
||||
)
|
||||
|
||||
|
|
|
@ -31,7 +31,6 @@
|
|||
|
||||
namespace pw {
|
||||
|
||||
|
||||
template <std::size_t R,std::size_t C, typename T, bool RowMajor = false>
|
||||
struct matrix_ : matrixbase_<T, matrix_<R, C, T>>
|
||||
{
|
||||
|
@ -240,17 +239,17 @@ template <typename T> using matrix2x2_ = matrix_<2, 2, T>;
|
|||
template <typename T> using matrix3x3_ = matrix_<3, 3, T>;
|
||||
template <typename T> using matrix4x4_ = matrix_<4, 4, T>;
|
||||
|
||||
using matrix2x2f = matrix_<2, 2,float>;
|
||||
using matrix2x2d = matrix_<2, 2,double>;
|
||||
using matrix2x2 = matrix_<2, 2,real_t>;
|
||||
using matrix2x2f = matrix2x2_<float>;
|
||||
using matrix2x2d = matrix2x2_<double>;
|
||||
using matrix2x2 = matrix2x2_<real_t>;
|
||||
|
||||
using matrix3x3f = matrix_<3, 3,float>;
|
||||
using matrix3x3d = matrix_<3, 3,double>;
|
||||
using matrix3x3 = matrix_<3, 3,real_t>;
|
||||
using matrix3x3f = matrix3x3_<float>;
|
||||
using matrix3x3d = matrix3x3_<double>;
|
||||
using matrix3x3 = matrix3x3_<real_t>;
|
||||
|
||||
using matrix4x4f = matrix_<4, 4,float>;
|
||||
using matrix4x4d = matrix_<4, 4,double>;
|
||||
using matrix4x4 = matrix_<4, 4,real_t>;
|
||||
using matrix4x4f = matrix4x4_<float>;
|
||||
using matrix4x4d = matrix4x4_<double>;
|
||||
using matrix4x4 = matrix4x4_<real_t>;
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/*
|
||||
/*
|
||||
* Copyright (c) 1999-2019 Hartmut Seichter
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
|
@ -20,15 +20,15 @@
|
|||
* SOFTWARE.
|
||||
*
|
||||
*/
|
||||
#ifndef PW_CORE_TRANSFORM_TOOLS_HPP
|
||||
#define PW_CORE_TRANSFORM_TOOLS_HPP
|
||||
#ifndef PW_CORE_MATRIX_TRANSFORM_HPP
|
||||
#define PW_CORE_MATRIX_TRANSFORM_HPP
|
||||
|
||||
#include <pw/core/vector.hpp>
|
||||
|
||||
namespace pw {
|
||||
|
||||
template <typename T>
|
||||
struct transform_tools {
|
||||
struct matrix_transform {
|
||||
|
||||
inline static
|
||||
matrix_<4,4,T> scale_matrix(const vector3_<T>& s)
|
||||
|
@ -39,7 +39,7 @@ struct transform_tools {
|
|||
}
|
||||
|
||||
inline static
|
||||
matrix_<4,4,T> projection_from_frustum(const T &left,const T &right,
|
||||
matrix_<4,4,T> perspective_frustum(const T &left,const T &right,
|
||||
const T &bottom,const T &top,
|
||||
const T &z_near,const T &z_far)
|
||||
{
|
||||
|
@ -63,40 +63,47 @@ struct transform_tools {
|
|||
const T &aspect_ratio,
|
||||
const T &z_near,const T &z_far)
|
||||
{
|
||||
const T tan_half = tan(field_of_view / T(2));
|
||||
const T right = aspect_ratio * tan_half * z_near;
|
||||
const T left = -right;
|
||||
const T top = aspect_ratio * tan_half;
|
||||
const T bottom = -top;
|
||||
const auto tan_half = tan(field_of_view / T(2));
|
||||
const auto right = aspect_ratio * tan_half * z_near;
|
||||
const auto left = -right;
|
||||
const auto top = aspect_ratio * tan_half;
|
||||
const auto bottom = -top;
|
||||
|
||||
return projection_from_frustum(left,right,
|
||||
return perspective_frustum(left,right,
|
||||
bottom,top,
|
||||
z_near,z_far);
|
||||
}
|
||||
|
||||
|
||||
inline static
|
||||
matrix_<4,4,T> orthogonal_projection(T left, T right,
|
||||
matrix_<4,4,T> orthographic_frustum(T left, T right,
|
||||
T bottom,T top,
|
||||
T z_near, T z_far)
|
||||
{
|
||||
|
||||
matrix_<4,4,T> ortho;
|
||||
matrix_<4,4,T> ortho; ortho.fill(0);
|
||||
|
||||
ortho.fill(0);
|
||||
ortho(0,0) = 2 / (right-left);
|
||||
ortho(1,1) = 2 / (top-bottom);
|
||||
ortho(2,2) = -2 / (z_far-z_near);
|
||||
ortho(0,0) = static_cast<T>(2) / (right-left);
|
||||
ortho(1,1) = static_cast<T>(2) / (top-bottom);
|
||||
ortho(2,2) = -static_cast<T>(2) / (z_far-z_near);
|
||||
|
||||
ortho(0,3) = -(right+left)/(right-left);
|
||||
ortho(1,3) = -(top+bottom)/(top-bottom);
|
||||
ortho(2,3) = -(z_far+z_near)/(z_far-z_near);
|
||||
ortho(3,0) = -(right+left)/(right-left);
|
||||
ortho(3,1) = -(top+bottom)/(top-bottom);
|
||||
ortho(3,2) = -(z_far+z_near)/(z_far-z_near);
|
||||
|
||||
ortho(3,3) = 1;
|
||||
|
||||
return ortho;
|
||||
}
|
||||
|
||||
inline static
|
||||
matrix_<4,4,T> orthographic_projection(T size,T z_near, T z_far)
|
||||
{
|
||||
return orthographic_frustum(-size / 2, size / 2,
|
||||
-size / 2, size / 2,
|
||||
z_near,z_far);
|
||||
}
|
||||
|
||||
|
||||
inline static
|
||||
matrix_<4,4,T> look_at(const vector3_<T> &position,
|
||||
|
@ -106,7 +113,7 @@ struct transform_tools {
|
|||
matrix_<4,4,T> view_matrix; view_matrix.set_identity();
|
||||
|
||||
const vector3_<T> los = (target - position).normalized(); // line of sight
|
||||
const vector3_<T> sid = los.cross(up).normalized();
|
||||
const vector3_<T> sid = los.cross(up).normalized();
|
||||
const vector3_<T> upd = sid.cross(los).normalized();
|
||||
|
||||
// set base vectors
|
||||
|
@ -117,7 +124,6 @@ struct transform_tools {
|
|||
|
||||
return view_matrix;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
|
@ -40,11 +40,12 @@ struct point_ {
|
|||
|
||||
};
|
||||
|
||||
typedef point_<real_t> point;
|
||||
using point = point_<real_t>;
|
||||
|
||||
using pointf = point_<float>;
|
||||
using pointd = point_<double>;
|
||||
using pointi = point_<int>;
|
||||
|
||||
typedef point_<int> pointi;
|
||||
typedef point_<float> pointf;
|
||||
typedef point_<float> pointd;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -23,8 +23,8 @@
|
|||
#ifndef PW_CORE_RECT_HPP
|
||||
#define PW_CORE_RECT_HPP
|
||||
|
||||
#include <pw/core/size.hpp>
|
||||
#include <pw/core/point.hpp>
|
||||
#include <pw/core/size.hpp>
|
||||
|
||||
namespace pw {
|
||||
|
||||
|
@ -38,7 +38,7 @@ struct rectangle_ {
|
|||
|
||||
rectangle_(point_<T_> const & p,size_<T_> const & s) : size(s), position(p) {}
|
||||
|
||||
bool contains(const point_<T_>& p) const
|
||||
bool contains(const point_<T_>& p) const noexcept
|
||||
{
|
||||
return p.x >= position.x && p.x <= position.x + size.width &&
|
||||
p.y >= position.y && p.y <= position.y + size.height;
|
||||
|
@ -49,11 +49,9 @@ struct rectangle_ {
|
|||
|
||||
};
|
||||
|
||||
typedef rectangle_<real_t> rectangle;
|
||||
|
||||
typedef rectangle_<int> rectanglei;
|
||||
typedef rectangle_<float> rectanglef;
|
||||
typedef rectangle_<double> rectangled;
|
||||
using rectangle = rectangle_<real_t>;
|
||||
using rectanglef = rectangle_<float>;
|
||||
using rectangled = rectangle_<double>;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -32,13 +32,13 @@ namespace pw {
|
|||
/**
|
||||
* @brief A simple timer
|
||||
*/
|
||||
class timer {
|
||||
class time {
|
||||
public:
|
||||
|
||||
typedef std::chrono::time_point<std::chrono::high_resolution_clock> tick_t;
|
||||
|
||||
timer(); /// c'tor
|
||||
~timer(); /// d'tor
|
||||
time(); /// c'tor
|
||||
~time(); /// d'tor
|
||||
|
||||
void reset(); /// reset the timer
|
||||
|
|
@ -20,34 +20,34 @@
|
|||
* SOFTWARE.
|
||||
*
|
||||
*/
|
||||
#include "pw/core/timer.hpp"
|
||||
#include "pw/core/time.hpp"
|
||||
|
||||
namespace pw {
|
||||
|
||||
timer::timer()
|
||||
time::time()
|
||||
{
|
||||
reset();
|
||||
}
|
||||
|
||||
timer::~timer()
|
||||
time::~time()
|
||||
{
|
||||
}
|
||||
|
||||
void timer::reset()
|
||||
void time::reset()
|
||||
{
|
||||
_start = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
double timer::elapsed() const
|
||||
double time::elapsed() const
|
||||
{
|
||||
std::chrono::duration<double> elapsed_seconds = std::chrono::high_resolution_clock::now() - _start;
|
||||
return elapsed_seconds.count();
|
||||
}
|
||||
|
||||
double timer::now()
|
||||
double time::now()
|
||||
{
|
||||
static timer global_timer;
|
||||
return global_timer.elapsed();
|
||||
static time global_time;
|
||||
return global_time.elapsed();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
#include <pw/core/vector.hpp>
|
||||
#include <pw/core/serialize.hpp>
|
||||
#include <pw/core/transform_tools.hpp>
|
||||
#include <pw/core/matrix_transform.hpp>
|
||||
#include <pw/core/mesh.hpp>
|
||||
#include <pw/core/axisangle.hpp>
|
||||
|
||||
|
@ -17,7 +17,7 @@ int main(int argc,char **argv) {
|
|||
std::cout << pw::serialize::matrix(v.transposed()) << std::endl;
|
||||
}
|
||||
|
||||
auto scale = pw::transform_tools<float>::scale_matrix({2,2,2});
|
||||
auto scale = pw::matrix_transform<float>::scale_matrix({2,2,2});
|
||||
|
||||
amesh.apply(scale);
|
||||
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
|
||||
#include <pw/core/vector.hpp>
|
||||
#include <pw/core/serialize.hpp>
|
||||
#include <pw/core/transform_tools.hpp>
|
||||
#include <pw/core/matrix_transform.hpp>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc,char **argv) {
|
||||
|
||||
auto perspective_mat = pw::transform_tools<float>::perspective_projection(45.f,1.3f,10,100);
|
||||
auto ortho_mat = pw::transform_tools<float>::orthogonal_projection(-1,1,1,-1,10,100);
|
||||
auto lookat_mat = pw::transform_tools<float>::look_at(pw::vector3(0,0,5),pw::vector3(0,0,0),pw::vector3(0,1,0));
|
||||
auto perspective_mat = pw::matrix_transform<float>::perspective_projection(45.f,1.3f,10,100);
|
||||
auto ortho_mat = pw::matrix_transform<float>::orthographic_frustum(-1,1,1,-1,10,100);
|
||||
auto lookat_mat = pw::matrix_transform<float>::look_at(pw::vector3(0,0,5),pw::vector3(0,0,0),pw::vector3(0,1,0));
|
||||
|
||||
|
||||
std::cout << pw::serialize::matrix(perspective_mat) << std::endl;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue