diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 490122e..91a1209 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -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 ) diff --git a/src/core/include/pw/core/matrix.hpp b/src/core/include/pw/core/matrix.hpp index 4ca5a19..359f5bf 100644 --- a/src/core/include/pw/core/matrix.hpp +++ b/src/core/include/pw/core/matrix.hpp @@ -31,7 +31,6 @@ namespace pw { - template struct matrix_ : matrixbase_> { @@ -240,17 +239,17 @@ template using matrix2x2_ = matrix_<2, 2, T>; template using matrix3x3_ = matrix_<3, 3, T>; template 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_; +using matrix2x2d = matrix2x2_; +using matrix2x2 = matrix2x2_; -using matrix3x3f = matrix_<3, 3,float>; -using matrix3x3d = matrix_<3, 3,double>; -using matrix3x3 = matrix_<3, 3,real_t>; +using matrix3x3f = matrix3x3_; +using matrix3x3d = matrix3x3_; +using matrix3x3 = matrix3x3_; -using matrix4x4f = matrix_<4, 4,float>; -using matrix4x4d = matrix_<4, 4,double>; -using matrix4x4 = matrix_<4, 4,real_t>; +using matrix4x4f = matrix4x4_; +using matrix4x4d = matrix4x4_; +using matrix4x4 = matrix4x4_; diff --git a/src/core/include/pw/core/transform_tools.hpp b/src/core/include/pw/core/matrix_transform.hpp similarity index 73% rename from src/core/include/pw/core/transform_tools.hpp rename to src/core/include/pw/core/matrix_transform.hpp index 999469c..da53132 100644 --- a/src/core/include/pw/core/transform_tools.hpp +++ b/src/core/include/pw/core/matrix_transform.hpp @@ -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 namespace pw { template -struct transform_tools { +struct matrix_transform { inline static matrix_<4,4,T> scale_matrix(const vector3_& 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(2) / (right-left); + ortho(1,1) = static_cast(2) / (top-bottom); + ortho(2,2) = -static_cast(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_ &position, @@ -106,7 +113,7 @@ struct transform_tools { matrix_<4,4,T> view_matrix; view_matrix.set_identity(); const vector3_ los = (target - position).normalized(); // line of sight - const vector3_ sid = los.cross(up).normalized(); + const vector3_ sid = los.cross(up).normalized(); const vector3_ upd = sid.cross(los).normalized(); // set base vectors @@ -117,7 +124,6 @@ struct transform_tools { return view_matrix; } - }; } diff --git a/src/core/include/pw/core/point.hpp b/src/core/include/pw/core/point.hpp index 1d4401d..dd02833 100644 --- a/src/core/include/pw/core/point.hpp +++ b/src/core/include/pw/core/point.hpp @@ -40,11 +40,12 @@ struct point_ { }; -typedef point_ point; +using point = point_; + +using pointf = point_; +using pointd = point_; +using pointi = point_; -typedef point_ pointi; -typedef point_ pointf; -typedef point_ pointd; } diff --git a/src/core/include/pw/core/rectangle.hpp b/src/core/include/pw/core/rectangle.hpp index 4926842..afdea20 100644 --- a/src/core/include/pw/core/rectangle.hpp +++ b/src/core/include/pw/core/rectangle.hpp @@ -23,8 +23,8 @@ #ifndef PW_CORE_RECT_HPP #define PW_CORE_RECT_HPP -#include #include +#include namespace pw { @@ -38,7 +38,7 @@ struct rectangle_ { rectangle_(point_ const & p,size_ const & s) : size(s), position(p) {} - bool contains(const point_& p) const + bool contains(const point_& 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_ rectangle; - -typedef rectangle_ rectanglei; -typedef rectangle_ rectanglef; -typedef rectangle_ rectangled; +using rectangle = rectangle_; +using rectanglef = rectangle_; +using rectangled = rectangle_; } diff --git a/src/core/include/pw/core/timer.hpp b/src/core/include/pw/core/time.hpp similarity index 96% rename from src/core/include/pw/core/timer.hpp rename to src/core/include/pw/core/time.hpp index 7d5f4ef..e8127c1 100644 --- a/src/core/include/pw/core/timer.hpp +++ b/src/core/include/pw/core/time.hpp @@ -32,13 +32,13 @@ namespace pw { /** * @brief A simple timer */ -class timer { +class time { public: typedef std::chrono::time_point tick_t; - timer(); /// c'tor - ~timer(); /// d'tor + time(); /// c'tor + ~time(); /// d'tor void reset(); /// reset the timer diff --git a/src/core/src/timer.cpp b/src/core/src/time.cpp similarity index 87% rename from src/core/src/timer.cpp rename to src/core/src/time.cpp index f6c5c44..2e56aac 100644 --- a/src/core/src/timer.cpp +++ b/src/core/src/time.cpp @@ -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 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(); } } diff --git a/src/core/tests/pwcore_test_mesh.cpp b/src/core/tests/pwcore_test_mesh.cpp index a84e739..d7134e4 100644 --- a/src/core/tests/pwcore_test_mesh.cpp +++ b/src/core/tests/pwcore_test_mesh.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include @@ -17,7 +17,7 @@ int main(int argc,char **argv) { std::cout << pw::serialize::matrix(v.transposed()) << std::endl; } - auto scale = pw::transform_tools::scale_matrix({2,2,2}); + auto scale = pw::matrix_transform::scale_matrix({2,2,2}); amesh.apply(scale); diff --git a/src/core/tests/pwcore_test_transform_tools.cpp b/src/core/tests/pwcore_test_transform_tools.cpp index d1bfd6c..1a4c0b0 100644 --- a/src/core/tests/pwcore_test_transform_tools.cpp +++ b/src/core/tests/pwcore_test_transform_tools.cpp @@ -1,15 +1,15 @@ #include #include -#include +#include #include int main(int argc,char **argv) { - auto perspective_mat = pw::transform_tools::perspective_projection(45.f,1.3f,10,100); - auto ortho_mat = pw::transform_tools::orthogonal_projection(-1,1,1,-1,10,100); - auto lookat_mat = pw::transform_tools::look_at(pw::vector3(0,0,5),pw::vector3(0,0,0),pw::vector3(0,1,0)); + auto perspective_mat = pw::matrix_transform::perspective_projection(45.f,1.3f,10,100); + auto ortho_mat = pw::matrix_transform::orthographic_frustum(-1,1,1,-1,10,100); + auto lookat_mat = pw::matrix_transform::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; diff --git a/src/io/src/image_io.cpp b/src/io/src/image_io.cpp index f659ab7..92d41af 100644 --- a/src/io/src/image_io.cpp +++ b/src/io/src/image_io.cpp @@ -8,6 +8,8 @@ #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" +#include + namespace pw { struct image_io::impl diff --git a/src/scene/src/camera.cpp b/src/scene/src/camera.cpp index 112584d..1797cd9 100644 --- a/src/scene/src/camera.cpp +++ b/src/scene/src/camera.cpp @@ -1,5 +1,5 @@ #include "pw/scene/camera.hpp" -#include "pw/core/transform_tools.hpp" +#include "pw/core/matrix_transform.hpp" namespace pw { @@ -9,7 +9,7 @@ camera::camera(node &host) , _near_plane(0.2f) , _far_plane(1000) { - set_projection(transform_tools::perspective_projection(_fov,1,_near_plane,_far_plane)); + set_projection(matrix_transform::perspective_projection(_fov,1,_near_plane,_far_plane)); } void camera::set_projection(const matrix4x4 &projection) diff --git a/src/scripting/src/script_core.cpp b/src/scripting/src/script_core.cpp index a9bf558..5dfc151 100644 --- a/src/scripting/src/script_core.cpp +++ b/src/scripting/src/script_core.cpp @@ -4,7 +4,7 @@ #include "pw/core/debug.hpp" #include "pw/core/size.hpp" #include "pw/core/point.hpp" -#include "pw/core/timer.hpp" +#include "pw/core/time.hpp" #include "pw/core/mesh.hpp" #include "pw/core/image.hpp" @@ -97,11 +97,11 @@ void register_core_function(sol::state& lua,sol::table& ns) ); - ns.new_usertype("timer", - "now",sol::readonly_property(&timer::now), - "elapsed",sol::readonly_property(&timer::elapsed), - "reset",&timer::reset - ); + ns.new_usertype