[395] | 1 | // Copyright (C) 2012-2015 ChaosForge Ltd
|
---|
[230] | 2 | // http://chaosforge.org/
|
---|
| 3 | //
|
---|
[395] | 4 | // This file is part of Nova libraries.
|
---|
| 5 | // For conditions of distribution and use, see copying.txt file in root folder.
|
---|
[230] | 6 |
|
---|
[319] | 7 | #ifndef NV_CORE_TRANSFORM_HH
|
---|
| 8 | #define NV_CORE_TRANSFORM_HH
|
---|
[230] | 9 |
|
---|
[395] | 10 | #include <nv/common.hh>
|
---|
[368] | 11 | #include <nv/stl/math.hh>
|
---|
[230] | 12 |
|
---|
| 13 | namespace nv
|
---|
| 14 | {
|
---|
| 15 |
|
---|
| 16 | class transform
|
---|
| 17 | {
|
---|
| 18 | public:
|
---|
| 19 | transform() {}
|
---|
| 20 | explicit transform( const vec3& a_position ) : m_position( a_position ) {}
|
---|
| 21 | explicit transform( const quat& a_orientation ) : m_orientation( a_orientation ) {}
|
---|
[282] | 22 | explicit transform( const mat4& a_matrix ) { set( a_matrix ); }
|
---|
[230] | 23 | transform( const vec3& a_position, const quat& a_orientation ) : m_position( a_position ), m_orientation( a_orientation ) {}
|
---|
| 24 |
|
---|
| 25 | void set_position( const vec3& a_position ) { m_position = a_position; }
|
---|
| 26 | void set_orientation( const quat& a_orientation ) { m_orientation = a_orientation; }
|
---|
[397] | 27 | void set_orientation( float radians, const vec3& axis )
|
---|
[398] | 28 | { // use math::radians if degrees!
|
---|
[397] | 29 | m_orientation = glm::angleAxis( radians, axis );
|
---|
| 30 | }
|
---|
[230] | 31 |
|
---|
| 32 | const vec3& get_position() const { return m_position; }
|
---|
| 33 | const quat& get_orientation() const { return m_orientation; }
|
---|
| 34 |
|
---|
| 35 | public:
|
---|
| 36 | void move( const vec3& heading, float distance )
|
---|
| 37 | {
|
---|
| 38 | m_position += glm::normalize( heading ) * distance;
|
---|
| 39 | }
|
---|
| 40 | void translate( const vec3& absolute )
|
---|
| 41 | {
|
---|
| 42 | m_position += absolute;
|
---|
| 43 | }
|
---|
[397] | 44 | // void rotate( const vec3& axis, f32 angle )
|
---|
[398] | 45 | // { // use math::radians if degrees!
|
---|
[397] | 46 | // quat temp( angle, axis );
|
---|
| 47 | // m_orientation = temp * m_orientation;
|
---|
| 48 | // }
|
---|
[230] | 49 | void set( const mat4& from )
|
---|
| 50 | {
|
---|
| 51 | m_orientation = quat( from );
|
---|
| 52 | m_position = vec3( from[3] );
|
---|
| 53 | }
|
---|
| 54 | mat4 extract() const
|
---|
| 55 | {
|
---|
[398] | 56 | mat4 result = mat4_cast( m_orientation );
|
---|
[230] | 57 | result[3] = vec4( m_position, 1.0f );
|
---|
| 58 | return result;
|
---|
| 59 | }
|
---|
[259] | 60 | transform inverse() const
|
---|
| 61 | {
|
---|
| 62 | quat new_orient( glm::inverse( m_orientation ) );
|
---|
| 63 | // TODO: simplify
|
---|
[398] | 64 | return transform( -mat3_cast(new_orient) * m_position, new_orient );
|
---|
[259] | 65 | }
|
---|
| 66 |
|
---|
| 67 | transform& operator*=(const transform& rhs)
|
---|
| 68 | {
|
---|
| 69 | m_position = m_position + m_orientation * rhs.m_position;
|
---|
| 70 | m_orientation = m_orientation * rhs.m_orientation;
|
---|
| 71 | return *this;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[261] | 74 | vec3 transformed( const vec3& v ) const
|
---|
[259] | 75 | {
|
---|
| 76 | return m_orientation * v + m_position;
|
---|
| 77 | }
|
---|
[230] | 78 | private:
|
---|
| 79 | vec3 m_position;
|
---|
| 80 | quat m_orientation;
|
---|
| 81 | };
|
---|
| 82 |
|
---|
[259] | 83 | inline transform operator*(transform lhs, const transform& rhs)
|
---|
| 84 | {
|
---|
| 85 | lhs *= rhs;
|
---|
| 86 | return lhs;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | inline vec3 operator*(const vec3 lhs, const transform& rhs)
|
---|
| 90 | {
|
---|
| 91 | return rhs.transformed( lhs );
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[282] | 94 | template <> struct enum_to_type< TRANSFORM > { typedef transform type; };
|
---|
| 95 | template <> struct type_to_enum< transform > { static const datatype type = TRANSFORM; };
|
---|
[259] | 96 |
|
---|
[241] | 97 | template<>
|
---|
| 98 | inline transform interpolate( const transform& a, const transform& b, float value )
|
---|
[230] | 99 | {
|
---|
| 100 | return transform(
|
---|
| 101 | glm::mix ( a.get_position(), b.get_position(), value ),
|
---|
[398] | 102 | glm::slerp( a.get_orientation(), b.get_orientation(), value )
|
---|
[230] | 103 | );
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[319] | 107 | #endif // NV_CORE_TRANSFORM_HH
|
---|