Browse Source

math: implement rotate() for mat3 in addition to mat4.

legacy
Sam Hocevar sam 12 years ago
parent
commit
3e3b254423
2 changed files with 39 additions and 9 deletions
  1. +15
    -3
      src/lol/math/vector.h
  2. +24
    -6
      src/math/vector.cpp

+ 15
- 3
src/lol/math/vector.h View File

@@ -1561,15 +1561,27 @@ template <typename T> struct Mat4
/* Helpers for transformation matrices */ /* Helpers for transformation matrices */
static Mat4<T> translate(T x, T y, T z); static Mat4<T> translate(T x, T y, T z);
static Mat4<T> translate(Vec3<T> v); static Mat4<T> translate(Vec3<T> v);
static Mat4<T> rotate(T angle, T x, T y, T z);
static Mat4<T> rotate(T angle, Vec3<T> v);
static Mat4<T> rotate(Quat<T> q);


static inline Mat4<T> translate(Mat4<T> const &mat, Vec3<T> v) static inline Mat4<T> translate(Mat4<T> const &mat, Vec3<T> v)
{ {
return translate(v) * mat; return translate(v) * mat;
} }


static inline Mat4<T> rotate(T angle, T x, T y, T z)
{
return Mat4<T>(Mat3<T>::rotate(angle, x, y, z), (T)1);
}

static inline Mat4<T> rotate(T angle, Vec3<T> v)
{
return Mat4<T>(Mat3<T>::rotate(angle, v), (T)1);
}

static inline Mat4<T> rotate(Quat<T> q)
{
return Mat4<T>(Mat3<T>::rotate(q), (T)1);
}

static inline Mat4<T> rotate(Mat4<T> &mat, T angle, Vec3<T> v) static inline Mat4<T> rotate(Mat4<T> &mat, T angle, Vec3<T> v)
{ {
return rotate(angle, v) * mat; return rotate(angle, v) * mat;


+ 24
- 6
src/math/vector.cpp View File

@@ -320,7 +320,25 @@ template<> mat4 mat4::translate(vec3 v)
return translate(v.x, v.y, v.z); return translate(v.x, v.y, v.z);
} }


template<> mat4 mat4::rotate(float angle, float x, float y, float z)
template<> mat2 mat2::rotate(float angle)
{
angle *= (M_PI / 180.0f);

float st = sinf(angle);
float ct = cosf(angle);

mat2 ret;

ret[0][0] = ct;
ret[0][1] = st;

ret[1][0] = -st;
ret[1][1] = ct;

return ret;
}

template<> mat3 mat3::rotate(float angle, float x, float y, float z)
{ {
angle *= (M_PI / 180.0f); angle *= (M_PI / 180.0f);


@@ -337,7 +355,7 @@ template<> mat4 mat4::rotate(float angle, float x, float y, float z)
float mty = (1.0f - ct) * y; float mty = (1.0f - ct) * y;
float mtz = (1.0f - ct) * z; float mtz = (1.0f - ct) * z;


mat4 ret(1.0f);
mat3 ret;


ret[0][0] = x * mtx + ct; ret[0][0] = x * mtx + ct;
ret[0][1] = x * mty + st * z; ret[0][1] = x * mty + st * z;
@@ -354,19 +372,19 @@ template<> mat4 mat4::rotate(float angle, float x, float y, float z)
return ret; return ret;
} }


template<> mat4 mat4::rotate(float angle, vec3 v)
template<> mat3 mat3::rotate(float angle, vec3 v)
{ {
return rotate(angle, v.x, v.y, v.z); return rotate(angle, v.x, v.y, v.z);
} }


template<> mat4 mat4::rotate(quat q)
template<> mat3 mat3::rotate(quat q)
{ {
mat4 ret(1.0f);
float n = norm(q); float n = norm(q);


if (!n) if (!n)
return ret;
return mat3(1.0f);


mat3 ret;
float s = 2.0f / n; float s = 2.0f / n;


ret[0][0] = 1.0f - s * (q.y * q.y + q.z * q.z); ret[0][0] = 1.0f - s * (q.y * q.y + q.z * q.z);


Loading…
Cancel
Save