From fceeaf1c74d828d01a82f3be7abecdbc3944e8ca Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 30 Apr 2012 10:23:11 +0000 Subject: [PATCH] math: add quat::fromeuler static constructor for quaternions. --- src/lol/math/vector.h | 2 ++ src/math/vector.cpp | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/lol/math/vector.h b/src/lol/math/vector.h index 9df2077a..86bb5176 100644 --- a/src/lol/math/vector.h +++ b/src/lol/math/vector.h @@ -923,6 +923,8 @@ template struct Quat static Quat rotate(T angle, T x, T y, T z); static Quat rotate(T angle, Vec3 const &v); + static Quat fromeuler(T x, T y, T z); + static Quat fromeuler(Vec3 const &v); inline Quat operator *(Quat const &val) const { diff --git a/src/math/vector.cpp b/src/math/vector.cpp index 0c24448d..3a77ffe4 100644 --- a/src/math/vector.cpp +++ b/src/math/vector.cpp @@ -470,6 +470,27 @@ template<> quat quat::rotate(float angle, float x, float y, float 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) { vec3 v3 = normalize(eye - center);