00001 #ifndef OPENTISSUE_UTILITY_GL_GL_TRANSFORM_H 00002 #define OPENTISSUE_UTILITY_GL_GL_TRANSFORM_H 00003 // 00004 // OpenTissue Template Library 00005 // - A generic toolbox for physics-based modeling and simulation. 00006 // Copyright (C) 2008 Department of Computer Science, University of Copenhagen. 00007 // 00008 // OTTL is licensed under zlib: http://opensource.org/licenses/zlib-license.php 00009 // 00010 #include <OpenTissue/configuration.h> 00011 00012 #include <OpenTissue/utility/gl/gl.h> 00013 00014 #include <OpenTissue/core/math/math_vector3.h> 00015 #include <OpenTissue/core/math/math_quaternion.h> 00016 #include <OpenTissue/core/math/math_coordsys.h> 00017 #include <OpenTissue/core/math/math_constants.h> 00018 00019 #include <cmath> 00020 00021 namespace OpenTissue 00022 { 00023 00024 namespace gl 00025 { 00026 00027 template<typename value_type> 00028 inline void Transform( math::Vector3<value_type> const & r) 00029 { 00030 glTranslatef( ( GLfloat ) r( 0 ), ( GLfloat ) r( 1 ), ( GLfloat ) r( 2 ) ); 00031 } 00032 00038 template <typename value_type> 00039 inline void Transform ( math::Quaternion<value_type> const & Q ) 00040 { 00041 using std::acos; 00042 using std::sin; 00043 00044 value_type angle = acos( Q.s() ) * 2.0; 00045 if ( angle ) 00046 { 00047 value_type factor = sin( angle / 2.0 ); 00048 value_type rx = Q.v()( 0 ) / factor; 00049 value_type ry = Q.v()( 1 ) / factor; 00050 value_type rz = Q.v()( 2 ) / factor; 00051 angle = ( angle / math::detail::pi<value_type>() ) * 180; //--- Convert to degrees 00052 glRotatef( ( GLfloat ) angle, ( GLfloat ) rx, ( GLfloat ) ry, ( GLfloat ) rz ); 00053 } 00054 } 00055 00064 template <typename vector3_type, typename quaternion_type> 00065 inline void Transform ( vector3_type const & r, quaternion_type const & Q ) 00066 { 00067 Transform(r); 00068 Transform(Q); 00069 } 00070 00078 template <typename V> 00079 inline void Transform ( math::CoordSys<V> const & C ) 00080 { 00081 Transform(C.T()); 00082 Transform(C.Q()); 00083 } 00084 00085 } // namespace gl 00086 00087 } // namespace OpenTissue 00088 00089 //OPENTISSUE_UTILITY_GL_GL_TRANSFORM_H 00090 #endif