quaternion.hpp

Go to the documentation of this file.
00001 /*
00002     RenderStack  Support library for OpenGL 3+
00003     Copyright (C) 2010  Timo Suoranta
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2.1 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00027 #ifndef QUATERNION_HPP
00028 #define QUATERNION_HPP
00029 
00030 #include "renderstack/rs.h"
00031 #include "renderstack/vec3.hpp"
00032 
00033 namespace renderstack {
00034 
00035 class matrix;
00036 
00038 class quaternion 
00039 {
00040 public:
00041     quaternion()
00042     :   x(0.0f)
00043     ,   y(0.0f)
00044     ,   z(0.0f)
00045     ,   w(1.0f)
00046     {
00047     }
00048     quaternion(float x_, float y_, float z_, float w_)
00049     :   x(x_)
00050     ,   y(y_)
00051     ,   z(z_)
00052     ,   w(w_)
00053     {
00054     }
00055     quaternion(quaternion const &other)
00056     :   x(other.x)
00057     ,   y(other.y)
00058     ,   z(other.z)
00059     ,   w(other.w)
00060     {
00061     }
00062     explicit quaternion(matrix const &m);
00063     quaternion(vec3 const &axis, float half_a_sin, float half_a_cos)
00064     :   x(axis.x * half_a_sin)
00065     ,   y(axis.y * half_a_sin)
00066     ,   z(axis.z * half_a_sin)
00067     ,   w(half_a_cos)
00068     {
00069     }
00070 
00071     vec3 hpb        () const;
00072     vec3 view_axis  () const;
00073     vec3 up_axis    () const;
00074     vec3 right_axis () const;
00075 
00076     quaternion operator*    (quaternion const &other) const;
00077     quaternion &operator*=  (quaternion const &other) { return *this = *this * other; }
00078 
00079 public:
00080     float x;
00081     float y;
00082     float z;
00083     float w;
00084 };
00085 
00086 inline quaternion normalize(quaternion const &q)
00087 {
00088     float dot   = q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w;
00089     float t     = 1.5f - 0.5f * dot;
00090     return quaternion(q.x * t, q.y * t, q.z * t, q.w * t);
00091 }
00092 
00093 }
00094 
00095 #endif
00096 
Generated on Sun Apr 11 12:23:08 2010 for RenderStack by  doxygen 1.6.3