diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 224d3ee..84a6fd8 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -1,22 +1,23 @@ set(hdrs - include/pw/core/debug.hpp include/pw/core/axisangle.hpp include/pw/core/core.hpp + include/pw/core/debug.hpp + include/pw/core/globals.hpp include/pw/core/math.hpp include/pw/core/matrixbase.hpp include/pw/core/matrix.hpp - include/pw/core/vector.hpp include/pw/core/quaternion.hpp - include/pw/core/serialize.hpp include/pw/core/image.hpp include/pw/core/point.hpp include/pw/core/rect.hpp + include/pw/core/serialize.hpp include/pw/core/size.hpp include/pw/core/timer.hpp include/pw/core/mesh.hpp - include/pw/core/globals.hpp include/pw/core/image.hpp + include/pw/core/vector.hpp + include/pw/core/transform_tools.hpp ) set(misc diff --git a/src/core/include/pw/core/axisangle.hpp b/src/core/include/pw/core/axisangle.hpp index 48bfa1c..77744ee 100644 --- a/src/core/include/pw/core/axisangle.hpp +++ b/src/core/include/pw/core/axisangle.hpp @@ -28,60 +28,73 @@ namespace pw { template -class axisangle_ { -protected: - vector3_ _axis; - T _angle; -public: +struct axisangle_ { + + typedef T value_type; + typedef vector3_ axis_type; + + axis_type axis; + T angle; axisangle_() - : _axis(vector3_::up()), - _angle(0) + : axis(vector3_::up()), + angle(0) {} axisangle_(const vector3_ &axis,const T &angle) - : _axis(axis) - , _angle(angle) + : axis(axis) + , angle(angle) { } - vector3_ axis() const { return _axis; } - void set_axis(const vector3_ &axis) { _axis = axis; } - - T angle() const { return _angle; } - void set_angle(const T &angle) { _angle = angle; } - - matrix4x4_ to_matrix() const + matrix_<4,4,T> to_matrix() const { + + // TODO ... Somehow buggy! using std::cos; using std::sin; - matrix4x4_ R; R.set_identity(); // = matrix44::Identity(); + // result + matrix_<4,4,T> rot_mat; rot_mat.set_identity(); - if (_axis.norm() < std::numeric_limits::epsilon()) return R; + // return as identity + if (axis.norm() < std::numeric_limits::epsilon()) return rot_mat; - const T _fCos = cos(_angle); + const T f_cos = cos(angle); - vector3_ _vCos(_axis * (1 - _fCos)); - vector3_ _vSin(_axis * sin(_angle)); + const vector3_ cos_axis = axis * (T(1) - f_cos); + const vector3_ sin_axis = axis * sin(angle); -// R.at(0) = (_axis(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)); + T val = cos_axis.x() * axis.y(); + rot_mat(0,1) = val - sin_axis.z(); + rot_mat(1,0) = val + sin_axis.z(); - // 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)); + val = cos_axis.x() * axis.z(); + rot_mat(0,2) = val + sin_axis.z(); + rot_mat(2,0) = val - sin_axis.z(); - // 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); + val = cos_axis.y() * axis.z(); + rot_mat(1,2) = val - sin_axis.x(); + rot_mat(2,1) = val + sin_axis.x(); - return R; + rot_mat(0,0) = cos_axis[0] * axis[0] + f_cos; + rot_mat(1,1) = cos_axis[1] * axis[1] + f_cos; + rot_mat(2,2) = cos_axis[2] * axis[2] + f_cos; + +// R.at(0) = axis[0] * _vCos(0,0)) + f_cos; +// R.at(4) = axis[0] * _vCos(1,0)) - _vSin(2,0)); +// R.at(8) = axis[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)) + f_cos); +// 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)) + f_cos); + + return rot_mat; } - - - }; diff --git a/src/core/include/pw/core/math.hpp b/src/core/include/pw/core/math.hpp index b9a9efe..750c1da 100644 --- a/src/core/include/pw/core/math.hpp +++ b/src/core/include/pw/core/math.hpp @@ -36,12 +36,12 @@ template inline const T pi() { return static_cast(__PW_PI); } template -inline static T rad_to_deg(const T& angle_in_radian) { +inline T rad_to_deg(const T& angle_in_radian) { return angle_in_radian * __RAD2DEG; } template -inline static T deg_to_rad(const T& angle_in_degree) { +inline T deg_to_rad(const T& angle_in_degree) { return angle_in_degree * __DEG2RAD; } diff --git a/src/core/include/pw/core/matrix.hpp b/src/core/include/pw/core/matrix.hpp index 27cf492..4ca5a19 100644 --- a/src/core/include/pw/core/matrix.hpp +++ b/src/core/include/pw/core/matrix.hpp @@ -109,7 +109,8 @@ struct matrix_ : matrixbase_> } template - matrix_& set_slice(const matrix_& s,std::size_t r,std::size_t c) + matrix_& set_slice(const matrix_& s, + std::size_t r,std::size_t c) { for (std::size_t ri = 0;ri < Rs;ri++) for (std::size_t ci = 0;ci < Cs;ci++) @@ -251,64 +252,6 @@ using matrix4x4f = matrix_<4, 4,float>; using matrix4x4d = matrix_<4, 4,double>; using matrix4x4 = matrix_<4, 4,real_t>; -// -// -// - -template -struct transform_tools { - - inline static - matrix4x4_ projection_from_frustum(T Left,T Right,T Bottom,T Top,T zNear,T zFar) - { - matrix4x4_ frustum; - - frustum.fill(0); - - frustum(0,0) = T(2) * zNear/(Right-Left); - frustum(1,1) = T(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) = -(T(2) * zFar*zNear)/(zFar-zNear); //D - - frustum(2,3) = -T(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()); // 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 ); - } -}; } diff --git a/src/core/include/pw/core/quaternion.hpp b/src/core/include/pw/core/quaternion.hpp index e3dbea5..b2bb56c 100644 --- a/src/core/include/pw/core/quaternion.hpp +++ b/src/core/include/pw/core/quaternion.hpp @@ -164,12 +164,12 @@ struct quaternion_ : vector4_ { using std::sin; using std::cos; - const T sinHalfAngle( sin(aa.angle() * T(0.5) )); + const T sinHalfAngle( sin(aa.angle * T(0.5) )); - return quaternion_( { aa.axis().x() * sinHalfAngle, // x - aa.axis().y() * sinHalfAngle, // y - aa.axis().z() * sinHalfAngle, // z - cos(aa.angle() * T(0.5)) // w + return quaternion_( { aa.axis.x() * sinHalfAngle, // x + aa.axis.y() * sinHalfAngle, // y + aa.axis.z() * sinHalfAngle, // z + cos(aa.angle * T(0.5)) // w } ); diff --git a/src/core/include/pw/core/vector.hpp b/src/core/include/pw/core/vector.hpp index 6b1b08b..39192cc 100644 --- a/src/core/include/pw/core/vector.hpp +++ b/src/core/include/pw/core/vector.hpp @@ -96,6 +96,11 @@ struct vector3_ : matrix_<3,1,T> { inline static vector3_ up() { return vector3_ ( { T(0), T(1), T(0) } ); } inline static vector3_ down() { return vector3_ ( { T(0),-T(1), T(0) } ); } + inline static vector3_ x_axis() { return vector3_ ( { T(1), T(0), T(0) } ); } + inline static vector3_ y_axis() { return vector3_ ( { T(0), T(1), T(0) } ); } + inline static vector3_ z_axis() { return vector3_ ( { T(0), T(0), T(1) } ); } + + }; template diff --git a/src/core/tests/CMakeLists.txt b/src/core/tests/CMakeLists.txt index 7abff2a..895d118 100644 --- a/src/core/tests/CMakeLists.txt +++ b/src/core/tests/CMakeLists.txt @@ -1,29 +1,15 @@ -add_executable(pwcore_test_matrix - pwcore_test_matrix.cpp - ) - -target_link_libraries(pwcore_test_matrix - pwcore) - -add_executable(pwcore_test_vector - pwcore_test_vector.cpp +macro(make_test arg1) + add_executable(${arg1} + ${arg1}.cpp ) - -target_link_libraries(pwcore_test_vector - pwcore) + target_link_libraries(${arg1} + pwcore) +endmacro() -add_executable(pwcore_test_quaternion - pwcore_test_quaternion.cpp - ) - -target_link_libraries(pwcore_test_quaternion - pwcore) - - -add_executable(pwcore_test_axisangle - pwcore_test_axisangle.cpp - ) - -target_link_libraries(pwcore_test_axisangle - pwcore) +make_test(pwcore_test_matrix) +make_test(pwcore_test_vector) +make_test(pwcore_test_axisangle) +make_test(pwcore_test_quaternion) +make_test(pwcore_test_transform_tools) +make_test(pwcore_test_mesh) diff --git a/src/core/tests/pwcore_test_axisangle.cpp b/src/core/tests/pwcore_test_axisangle.cpp index 39864ba..21f95b2 100644 --- a/src/core/tests/pwcore_test_axisangle.cpp +++ b/src/core/tests/pwcore_test_axisangle.cpp @@ -8,10 +8,26 @@ int main(int argc,char **argv) { pw::axisangle_ aa = pw::axisangle_(); - pw::quaternionf qf = pw::quaternionf::from_axisangle(aa); +// pw::quaternionf qf = pw::quaternionf::from_axisangle(aa); +// std::cout << "aa as quaternion as matrix = " << pw::serialize::matrix(qf.to_matrix()) << std::endl; +// std::cout << "aa.matrix() = " << pw::serialize::matrix(qf.to_matrix()) << std::endl; + + std::cout << "x-axis" << std::endl; + + aa.axis = pw::vector3::x_axis(); + aa.angle = pw::deg_to_rad(45.f); + + std::cout << "aa.matrix() = " << std::endl << pw::serialize::matrix(aa.to_matrix()) << std::endl; + + std::cout << "y-axis" << std::endl; + + aa.axis = pw::vector3::y_axis(); + aa.angle = pw::deg_to_rad(45.f); + + std::cout << "aa.matrix() = " << std::endl << pw::serialize::matrix(aa.to_matrix()) << std::endl; + + - std::cout << "aa as quaternion as matrix = " << pw::serialize::matrix(qf.to_matrix()) << std::endl; - std::cout << "aa.matrix() = " << pw::serialize::matrix(qf.to_matrix()) << std::endl; return 0; } diff --git a/src/core/tests/pwcore_test_vector.cpp b/src/core/tests/pwcore_test_vector.cpp index 880723c..6403f58 100644 --- a/src/core/tests/pwcore_test_vector.cpp +++ b/src/core/tests/pwcore_test_vector.cpp @@ -6,41 +6,35 @@ int main(int argc,char **argv) { -// pw::vector2_ v2_A = { 3.2, 1.2 }; -// pw::vector2_ v2_B = { 3.2, 1.2 }; + pw::vector2_ v2_A = { 3.2, 1.2 }; + pw::vector2_ v2_B = { 3.2, 1.2 }; + auto AB_lerp = pw::vector2f::lerp(v2_A,v2_B,0.5); -// auto AB_lerp = pw::vector2f::lerp(v2_A,v2_B,0.5); + pw::vector4_ v4; + pw::vector3f v; + v4.fill(1.5); - - -// v2 = 0.1f; - -// pw::vector4_ v4; -// pw::vector3f v; - -// v4.fill(1.5); - -// std::cout << "v4 = " << pw::serialize::matrix(v4) << std::endl; + std::cout << "v4 = " << pw::serialize::matrix(v4) << std::endl; // std::cout << "rows() : " << v4.rows() << std::endl; // std::cout << "cols() : " << v4.cols() << std::endl; -// std::cout << "ptr() : " << v4.ptr() << std::endl; -// std::cout << "ptr()[0] : " << v4.ptr()[0] << std::endl; -// std::cout << "(0,0) : " << v4(0,0) << std::endl; + std::cout << "ptr() : " << v4.ptr() << std::endl; + std::cout << "ptr()[0] : " << v4.ptr()[0] << std::endl; + std::cout << "(0,0) : " << v4(0,0) << std::endl; -// auto v3 = v4.xyz(); + auto v3 = v4.xyz(); -// auto v3_p = v4.project(); + auto v3_p = v4.project(); -// auto v3_h = v.homogenous(); + auto v3_h = v.homogenous(); -//// auto v3_lerp = vector4f::lerp() +// auto v3_lerp = vector4f::lerp() -// std::cout << "v3 = " << pw::serialize::matrix(v3) << std::endl; + std::cout << "v3 = " << pw::serialize::matrix(v3) << std::endl; -// std::cout << "v3.normalized() = " << pw::serialize::matrix(v3.normalized()) << std::endl; + std::cout << "v3.normalized() = " << pw::serialize::matrix(v3.normalized()) << std::endl; return 0; diff --git a/src/scene/src/camera.cpp b/src/scene/src/camera.cpp index d211710..b1325da 100644 --- a/src/scene/src/camera.cpp +++ b/src/scene/src/camera.cpp @@ -1,4 +1,5 @@ #include "pw/scene/camera.hpp" +#include "pw/core/transform_tools.hpp" namespace pw { diff --git a/src/scripting/src/script_core.cpp b/src/scripting/src/script_core.cpp index 7f162a5..f733fea 100644 --- a/src/scripting/src/script_core.cpp +++ b/src/scripting/src/script_core.cpp @@ -59,8 +59,8 @@ void register_core_function(sol::state& lua,sol::table& ns) ns.new_usertype ("axisangle", sol::constructors(), - "axis",sol::property(&axisangle::axis,&axisangle::set_axis), - "angle",sol::property(&axisangle::angle,&axisangle::set_angle) + "axis",&axisangle::axis, + "angle",&axisangle::angle ); diff --git a/src/visual/src/mesh_renderer.cpp b/src/visual/src/mesh_renderer.cpp index a4051ef..fff5c06 100644 --- a/src/visual/src/mesh_renderer.cpp +++ b/src/visual/src/mesh_renderer.cpp @@ -62,6 +62,12 @@ struct mesh_renderer::impl { _vbos.clear(); } + + void draw() + { + + } + }; diff --git a/src/visual/src/pipeline.cpp b/src/visual/src/pipeline.cpp index 87e5817..6132f5f 100644 --- a/src/visual/src/pipeline.cpp +++ b/src/visual/src/pipeline.cpp @@ -38,8 +38,8 @@ struct triangle_renderer mesh::vertex3array_t vertices = { - {0.0f, 0.5f, 0.0f} - ,{0.5f, -0.5f, 0.0f} + { 0.0f, 0.5f, 0.0f} + ,{ 0.5f, -0.5f, 0.0f} ,{-0.5f, -0.5f, 0.0f} }; @@ -64,18 +64,26 @@ struct triangle_renderer glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, vertex_size_bytes , amesh.vertices().data(), GL_STATIC_DRAW); - glGenVertexArrays(1, &vao); + glGenVertexArrays(1,&vao); glBindVertexArray(vao); + glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vbo); glVertexAttribPointer(0, vertex_stride, GL_FLOAT, GL_FALSE, 0, nullptr); - const char* vertex_shader = - "#version 400\n" - "in vec3 vp;" - "void main() {" - " gl_Position = vec4(vp, 1.0);" - "}"; + const char* vertex_shader = R"( + #version 400 + in vec3 vp; + void main() { + gl_Position = vec4(vp, 1.0); + } + )"; + +// "#version 400\n" +// "in vec3 vp;" +// "void main() {" +// " gl_Position = vec4(vp, 1.0);" +// "}"; const char *fragment_shader = R"(#version 400 @@ -219,8 +227,15 @@ void pipeline::impl::draw() glClearColor(1,1,1,1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // + // draw pass + // + tr.draw(); + + // reset