still plenty of problems with angleaxis and other linear algebra stuff

This commit is contained in:
Hartmut Seichter 2019-01-25 18:01:01 +01:00
parent 9bdc13e3fc
commit 5245ceb112
13 changed files with 146 additions and 166 deletions

View file

@ -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

View file

@ -28,60 +28,73 @@
namespace pw {
template <typename T>
class axisangle_ {
protected:
vector3_<T> _axis;
T _angle;
public:
struct axisangle_ {
typedef T value_type;
typedef vector3_<T> axis_type;
axis_type axis;
T angle;
axisangle_()
: _axis(vector3_<T>::up()),
_angle(0)
: axis(vector3_<T>::up()),
angle(0)
{}
axisangle_(const vector3_<T> &axis,const T &angle)
: _axis(axis)
, _angle(angle)
: axis(axis)
, angle(angle)
{
}
vector3_<T> axis() const { return _axis; }
void set_axis(const vector3_<T> &axis) { _axis = axis; }
T angle() const { return _angle; }
void set_angle(const T &angle) { _angle = angle; }
matrix4x4_<T> to_matrix() const
matrix_<4,4,T> to_matrix() const
{
// TODO ... Somehow buggy!
using std::cos;
using std::sin;
matrix4x4_<T> R; R.set_identity(); // = matrix44<T>::Identity();
// result
matrix_<4,4,T> rot_mat; rot_mat.set_identity();
if (_axis.norm() < std::numeric_limits<T>::epsilon()) return R;
// return as identity
if (axis.norm() < std::numeric_limits<T>::epsilon()) return rot_mat;
const T _fCos = cos(_angle);
const T f_cos = cos(angle);
vector3_<T> _vCos(_axis * (1 - _fCos));
vector3_<T> _vSin(_axis * sin(_angle));
const vector3_<T> cos_axis = axis * (T(1) - f_cos);
const vector3_<T> 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;
}
};

View file

@ -36,12 +36,12 @@ template <typename T>
inline const T pi() { return static_cast<T>(__PW_PI); }
template <typename T>
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 <typename T>
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;
}

View file

@ -109,7 +109,8 @@ struct matrix_ : matrixbase_<T, matrix_<R, C, T>>
}
template<std::size_t Rs,std::size_t Cs,bool RowMajorSlice = RowMajor>
matrix_& set_slice(const matrix_<Rs,Cs,T,RowMajorSlice>& s,std::size_t r,std::size_t c)
matrix_& set_slice(const matrix_<Rs,Cs,T,RowMajorSlice>& 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 <typename T>
struct transform_tools {
inline static
matrix4x4_<T> projection_from_frustum(T Left,T Right,T Bottom,T Top,T zNear,T zFar)
{
matrix4x4_<T> 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<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 );
}
};
}

View file

@ -164,12 +164,12 @@ struct quaternion_ : vector4_<T> {
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_<T>( { aa.axis().x() * sinHalfAngle, // x
aa.axis().y() * sinHalfAngle, // y
aa.axis().z() * sinHalfAngle, // z
cos(aa.angle() * T(0.5)) // w
return quaternion_<T>( { aa.axis.x() * sinHalfAngle, // x
aa.axis.y() * sinHalfAngle, // y
aa.axis.z() * sinHalfAngle, // z
cos(aa.angle * T(0.5)) // w
}
);

View file

@ -96,6 +96,11 @@ struct vector3_ : matrix_<3,1,T> {
inline static vector3_<T> up() { return vector3_<T> ( { T(0), T(1), T(0) } ); }
inline static vector3_<T> down() { return vector3_<T> ( { T(0),-T(1), T(0) } ); }
inline static vector3_<T> x_axis() { return vector3_<T> ( { T(1), T(0), T(0) } ); }
inline static vector3_<T> y_axis() { return vector3_<T> ( { T(0), T(1), T(0) } ); }
inline static vector3_<T> z_axis() { return vector3_<T> ( { T(0), T(0), T(1) } ); }
};
template <typename T>

View file

@ -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)

View file

@ -8,10 +8,26 @@ int main(int argc,char **argv) {
pw::axisangle_<float> aa = pw::axisangle_<float>();
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;
}

View file

@ -6,41 +6,35 @@
int main(int argc,char **argv) {
// pw::vector2_<float> v2_A = { 3.2, 1.2 };
// pw::vector2_<float> v2_B = { 3.2, 1.2 };
pw::vector2_<float> v2_A = { 3.2, 1.2 };
pw::vector2_<float> 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_<float> v4;
pw::vector3f v;
v4.fill(1.5);
// v2 = 0.1f;
// pw::vector4_<float> 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;

View file

@ -1,4 +1,5 @@
#include "pw/scene/camera.hpp"
#include "pw/core/transform_tools.hpp"
namespace pw {

View file

@ -59,8 +59,8 @@ void register_core_function(sol::state& lua,sol::table& ns)
ns.new_usertype<axisangle>
("axisangle",
sol::constructors<axisangle(), axisangle(vector3,Scalar)>(),
"axis",sol::property(&axisangle::axis,&axisangle::set_axis),
"angle",sol::property(&axisangle::angle,&axisangle::set_angle)
"axis",&axisangle::axis,
"angle",&axisangle::angle
);

View file

@ -62,6 +62,12 @@ struct mesh_renderer::impl {
_vbos.clear();
}
void draw()
{
}
};

View file

@ -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