# rbdl / src / SimpleMath / SimpleMathGL.h

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178``` ```#ifndef _SIMPLEMATHGL_H_ #define _SIMPLEMATHGL_H_ #include "SimpleMath.h" #include namespace SimpleMath { namespace GL { inline Matrix44f RotateMat44 (float rot_deg, float x, float y, float z) { float c = cosf (rot_deg * M_PI / 180.f); float s = sinf (rot_deg * M_PI / 180.f); return Matrix44f ( x * x * (1.0f - c) + c, y * x * (1.0f - c) + z * s, x * z * (1.0f - c) - y * s, 0.f, x * y * (1.0f - c) - z * s, y * y * (1.0f - c) + c, y * z * (1.0f - c) + x * s, 0.f, x * z * (1.0f - c) + y * s, y * z * (1.0f - c) - x * s, z * z * (1.0f - c) + c, 0.f, 0.f, 0.f, 0.f, 1.f ); } inline Matrix44f TranslateMat44 (float x, float y, float z) { return Matrix44f ( 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, x, y, z, 1.f ); } inline Matrix44f ScaleMat44 (float x, float y, float z) { return Matrix44f ( x, 0.f, 0.f, 0.f, 0.f, y, 0.f, 0.f, 0.f, 0.f, z, 0.f, 0.f, 0.f, 0.f, 1.f ); } /** Quaternion * * order: x,y,z,w */ class Quaternion : public Vector4f { public: Quaternion () : Vector4f (0.f, 0.f, 0.f, 1.f) {} Quaternion (const Vector4f vec4) : Vector4f (vec4) {} Quaternion (float x, float y, float z, float w): Vector4f (x, y, z, w) {} /** This function is equivalent to multiplicate their corresponding rotation matrices */ Quaternion operator* (const Quaternion &q) const { return Quaternion ( q[3] * (*this)[0] + q[0] * (*this)[3] + q[1] * (*this)[2] - q[2] * (*this)[1], q[3] * (*this)[1] + q[1] * (*this)[3] + q[2] * (*this)[0] - q[0] * (*this)[2], q[3] * (*this)[2] + q[2] * (*this)[3] + q[0] * (*this)[1] - q[1] * (*this)[0], q[3] * (*this)[3] - q[0] * (*this)[0] - q[1] * (*this)[1] - q[2] * (*this)[2] ); } Quaternion& operator*=(const Quaternion &q) { set ( q[3] * (*this)[0] + q[0] * (*this)[3] + q[1] * (*this)[2] - q[2] * (*this)[1], q[3] * (*this)[1] + q[1] * (*this)[3] + q[2] * (*this)[0] - q[0] * (*this)[2], q[3] * (*this)[2] + q[2] * (*this)[3] + q[0] * (*this)[1] - q[1] * (*this)[0], q[3] * (*this)[3] - q[0] * (*this)[0] - q[1] * (*this)[1] - q[2] * (*this)[2] ); return *this; } static Quaternion fromGLRotate (float angle, float x, float y, float z) { float st = sinf (angle * M_PI / 360.f); return Quaternion ( st * x, st * y, st * z, cosf (angle * M_PI / 360.f) ); } Quaternion normalize() { return Vector4f::normalize(); } Quaternion slerp (float alpha, const Quaternion &quat) const { // check whether one of the two has 0 length float s = sqrt (squaredNorm() * quat.squaredNorm()); // division by 0.f is unhealthy! assert (s != 0.f); float angle = acos (dot(quat) / s); if (angle == 0.f || isnan(angle)) { return *this; } assert(!isnan(angle)); float d = 1.f / sinf (angle); float p0 = sinf ((1.f - alpha) * angle); float p1 = sinf (alpha * angle); if (dot (quat) < 0.f) { return Quaternion( ((*this) * p0 - quat * p1) * d); } return Quaternion( ((*this) * p0 + quat * p1) * d); } Matrix44f toGLMatrix() const { float x = (*this)[0]; float y = (*this)[1]; float z = (*this)[2]; float w = (*this)[3]; return Matrix44f ( 1 - 2*y*y - 2*z*z, 2*x*y + 2*w*z, 2*x*z - 2*w*y, 0.f, 2*x*y - 2*w*z, 1 - 2*x*x - 2*z*z, 2*y*z + 2*w*x, 0.f, 2*x*z + 2*w*y, 2*y*z - 2*w*x, 1 - 2*x*x - 2*y*y, 0.f, 0.f, 0.f, 0.f, 1.f); } Quaternion conjugate() const { return Quaternion ( -(*this)[0], -(*this)[1], -(*this)[2], (*this)[3]); } Vector3f rotate (const Vector3f &vec) const { Vector3f vn (vec); vn.normalize(); Quaternion vec_quat (vn[0], vn[1], vn[2], 0.f), res_quat; res_quat = vec_quat * (*this); res_quat = conjugate() * res_quat; return Vector3f (res_quat[0], res_quat[1], res_quat[2]); } }; // namespace GL } // namespace SimpleMath } /* _SIMPLEMATHGL_H_ */ #endif ```