[DEV] better vector and matrix4D

This commit is contained in:
Edouard DUPIN 2013-01-03 22:30:13 +01:00
parent ba9e2fca7b
commit 3b5aa7b84f
3 changed files with 65 additions and 17 deletions

View File

@ -130,6 +130,48 @@ etk::Matrix4 etk::matRotate(etk::Vector3D<float> vect, float angleRad)
return tmp;
}
etk::Matrix4 etk::matRotate2(etk::Vector3D<float> vect)
{
return matLookAt(vect, etk::Vector3D<float>(0,0,0), etk::Vector3D<float>(0,1,0));
}
etk::Matrix4 etk::matLookAt(etk::Vector3D<float> eye,
etk::Vector3D<float> center,
etk::Vector3D<float> up)
{
etk::Matrix4 tmp;
etk::Vector3D<float> forward = center - eye;
forward.Normalize();
etk::Vector3D<float> side = forward.CrossProduct(up);
side.Normalize();
etk::Vector3D<float> plane_up = side.CrossProduct(forward);
plane_up.Normalize();
tmp.m_mat[0] = side.x;
tmp.m_mat[1] = plane_up.x;
tmp.m_mat[2] = -forward.x;
tmp.m_mat[3] = 0.0f;
tmp.m_mat[4] = side.y;
tmp.m_mat[5] = plane_up.y;
tmp.m_mat[6] = -forward.y;
tmp.m_mat[7] = 0.0f;
tmp.m_mat[8] = side.z;
tmp.m_mat[9] = plane_up.z;
tmp.m_mat[10] = -forward.z;
tmp.m_mat[11] = 0.0f;
tmp.m_mat[12] = 0.0f;
tmp.m_mat[13] = 0.0f;
tmp.m_mat[14] = 0.0f;
tmp.m_mat[15] = 1.0f;
return tmp;
}
float etk::Matrix4::CoFactor(int32_t row, int32_t col) const
{

View File

@ -297,6 +297,10 @@ namespace etk
Matrix4 matTranslate(etk::Vector3D<float> vect);
Matrix4 matScale(etk::Vector3D<float> vect);
Matrix4 matRotate(etk::Vector3D<float> vect, float angleRad=0.0);
Matrix4 matRotate2(etk::Vector3D<float> vect);
Matrix4 matLookAt(etk::Vector3D<float> eye,
etk::Vector3D<float> center,
etk::Vector3D<float> up);
/**
* @brief Debug operator To display the curent element in a Human redeable information
*/

View File

@ -10,6 +10,7 @@
#define __ETK_MATH_VECTOR3D_H__
#include <etk/types.h>
#include <etk/DebugInternal.h>
#include <math.h>
#include <etk/Stream.h>
@ -294,72 +295,73 @@ namespace etk
};
//rotations
void RotateX(double angle)
void RotateX(float angle)
{
(*this)=GetRotatedX(angle);
};
Vector3D<T> GetRotatedX(double angle) const
Vector3D<T> GetRotatedX(float angle) const
{
if(angle==0.0) {
return (*this);
}
float sinAngle=sinf(M_PI*angle/180);
float cosAngle=cosf(M_PI*angle/180);
float sinAngle=sinf(angle);
float cosAngle=cosf(angle);
return Vector3D<T>( x,
y*cosAngle - z*sinAngle,
y*sinAngle + z*cosAngle);
};
void RotateY(double angle)
void RotateY(float angle)
{
(*this)=GetRotatedY(angle);
};
Vector3D<T> GetRotatedY(double angle) const
Vector3D<T> GetRotatedY(float angle) const
{
if(angle==0.0) {
return (*this);
}
float sinAngle=sinf(M_PI*angle/180);
float cosAngle=cosf(M_PI*angle/180);
float sinAngle=sinf(angle);
float cosAngle=cosf(angle);
return Vector3D<T>( x*cosAngle + z*sinAngle,
y,
-x*sinAngle + z*cosAngle);
};
void RotateZ(double angle)
void RotateZ(float angle)
{
(*this)=GetRotatedZ(angle);
};
Vector3D<T> GetRotatedZ(double angle) const
Vector3D<T> GetRotatedZ(float angle) const
{
if(angle==0.0) {
return (*this);
}
float sinAngle=sinf(M_PI*angle/180);
float cosAngle=cosf(M_PI*angle/180);
float sinAngle=sinf(angle);
float cosAngle=cosf(angle);
return Vector3D<T>( x*cosAngle - y*sinAngle,
x*sinAngle + y*cosAngle,
z);
};
void RotateAxis(double angle, const Vector3D<T> & axis)
void RotateAxis(const Vector3D<T> & axis, float angle)
{
(*this)=GetRotatedAxis(angle, axis);
(*this)=GetRotatedAxis(axis, angle);
TK_DEBUG("Rotate : " << *this);
};
Vector3D<T> GetRotatedAxis(double angle, const Vector3D<T> & axis) const
Vector3D<T> GetRotatedAxis(const Vector3D<T> & axis, float angle) const
{
if(angle==0.0) {
return (*this);
}
Vector3D<T> u=axis.GetNormalized();
Vector3D<T> rotMatrixRow0, rotMatrixRow1, rotMatrixRow2;
float sinAngle=sinf(M_PI*angle/180);
float cosAngle=cosf(M_PI*angle/180);
float sinAngle=sinf(angle);
float cosAngle=cosf(angle);
float MinusCosAngle=1.0f-cosAngle;
rotMatrixRow0.x=(u.x)*(u.x) + cosAngle*(1-(u.x)*(u.x));
rotMatrixRow0.y=(u.x)*(u.y)*(MinusCosAngle) - sinAngle*u.z;