Browse Source

math: add quat::fromeuler static constructor for quaternions.

legacy
Sam Hocevar sam 12 years ago
parent
commit
fceeaf1c74
2 changed files with 23 additions and 0 deletions
  1. +2
    -0
      src/lol/math/vector.h
  2. +21
    -0
      src/math/vector.cpp

+ 2
- 0
src/lol/math/vector.h View File

@@ -923,6 +923,8 @@ template <typename T> struct Quat


static Quat<T> rotate(T angle, T x, T y, T z); static Quat<T> rotate(T angle, T x, T y, T z);
static Quat<T> rotate(T angle, Vec3<T> const &v); static Quat<T> rotate(T angle, Vec3<T> const &v);
static Quat<T> fromeuler(T x, T y, T z);
static Quat<T> fromeuler(Vec3<T> const &v);


inline Quat<T> operator *(Quat<T> const &val) const inline Quat<T> operator *(Quat<T> const &val) const
{ {


+ 21
- 0
src/math/vector.cpp View File

@@ -470,6 +470,27 @@ template<> quat quat::rotate(float angle, float x, float y, float z)
return quat::rotate(angle, vec3(x, y, z)); return quat::rotate(angle, vec3(x, y, z));
} }


template<> quat quat::fromeuler(vec3 const &v)
{
using std::sin;
using std::cos;

vec3 half_angles = (M_PI / 360.0f) * v;
float sx = sin(half_angles.x), cx = cos(half_angles.x);
float sy = sin(half_angles.y), cy = cos(half_angles.y);
float sz = sin(half_angles.z), cz = cos(half_angles.z);

return quat(cx * cy * cz + sx * sy * sz,
sx * cy * cz - cx * sy * sz,
cx * sy * cz + sx * cy * sz,
cx * cy * sz - sx * sy * cz);
}

template<> quat quat::fromeuler(float x, float y, float z)
{
return quat::fromeuler(vec3(x, y, z));
}

template<> mat4 mat4::lookat(vec3 eye, vec3 center, vec3 up) template<> mat4 mat4::lookat(vec3 eye, vec3 center, vec3 up)
{ {
vec3 v3 = normalize(eye - center); vec3 v3 = normalize(eye - center);


Loading…
Cancel
Save