Browse Source

Implement float4x4::rotate().

legacy
Sam Hocevar sam 13 years ago
parent
commit
e1d6dbebce
2 changed files with 33 additions and 0 deletions
  1. +32
    -0
      src/matrix.cpp
  2. +1
    -0
      src/matrix.h

+ 32
- 0
src/matrix.cpp View File

@@ -116,3 +116,35 @@ template<> float4x4 float4x4::translate(float x, float y, float z)
return ret;
}

template<> float4x4 float4x4::rotate(float theta, float x, float y, float z)
{
float st = sinf(theta);
float ct = cosf(theta);

float len = sqrtf(x * x + y * y + z * z);
float invlen = len ? 1.0f / len : 0.0f;
x *= invlen;
y *= invlen;
z *= invlen;

float mtx = (1.0f - ct) * x;
float mty = (1.0f - ct) * y;
float mtz = (1.0f - ct) * z;

float4x4 ret(1.0f);

ret[0][0] = x * mtx + ct;
ret[0][1] = x * mty + st * z;
ret[0][2] = x * mtz - st * y;

ret[1][0] = y * mtx - st * z;
ret[1][1] = y * mty + ct;
ret[1][2] = y * mtz + st * x;

ret[2][0] = z * mtx + st * y;
ret[2][1] = z * mty - st * x;
ret[2][2] = z * mtz + ct;

return ret;
}


+ 1
- 0
src/matrix.h View File

@@ -199,6 +199,7 @@ template <typename T> struct Mat4
static Mat4<T> frustum(T left, T right, T bottom, T top, T near, T far);
static Mat4<T> perspective(T theta, T width, T height, T near, T far);
static Mat4<T> translate(T x, T y, T z);
static Mat4<T> rotate(T theta, T x, T y, T z);

inline Mat4<T> operator +(Mat4<T> const val) const
{


Loading…
Cancel
Save