1 | // Copyright (C) 2012-2014 ChaosForge Ltd
|
---|
2 | // http://chaosforge.org/
|
---|
3 | //
|
---|
4 | // This file is part of NV Libraries.
|
---|
5 | // For conditions of distribution and use, see copyright notice in nv.hh
|
---|
6 |
|
---|
7 | #ifndef NV_CORE_TRANSFORM_HH
|
---|
8 | #define NV_CORE_TRANSFORM_HH
|
---|
9 |
|
---|
10 | #include <nv/core/common.hh>
|
---|
11 | #include <nv/core/math.hh>
|
---|
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 ) {}
|
---|
22 | explicit transform( const mat4& a_matrix ) { set( a_matrix ); }
|
---|
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; }
|
---|
27 | void set_orientation( const vec3& axis, float angle )
|
---|
28 | {
|
---|
29 | m_orientation = glm::angleAxis( angle, axis );
|
---|
30 | }
|
---|
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 | }
|
---|
44 | void rotate( const vec3& axis, f32 angle )
|
---|
45 | {
|
---|
46 | quat temp( angle, axis );
|
---|
47 | m_orientation = temp * m_orientation;
|
---|
48 | }
|
---|
49 | void set( const mat4& from )
|
---|
50 | {
|
---|
51 | m_orientation = quat( from );
|
---|
52 | m_position = vec3( from[3] );
|
---|
53 | }
|
---|
54 | mat4 extract() const
|
---|
55 | {
|
---|
56 | mat4 result = glm::mat4_cast( m_orientation );
|
---|
57 | result[3] = vec4( m_position, 1.0f );
|
---|
58 | return result;
|
---|
59 | }
|
---|
60 | transform inverse() const
|
---|
61 | {
|
---|
62 | quat new_orient( glm::inverse( m_orientation ) );
|
---|
63 | // TODO: simplify
|
---|
64 | return transform( -glm::mat3_cast(new_orient) * m_position, new_orient );
|
---|
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 |
|
---|
74 | vec3 transformed( const vec3& v ) const
|
---|
75 | {
|
---|
76 | return m_orientation * v + m_position;
|
---|
77 | }
|
---|
78 | private:
|
---|
79 | vec3 m_position;
|
---|
80 | quat m_orientation;
|
---|
81 | };
|
---|
82 |
|
---|
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 |
|
---|
94 | template <> struct enum_to_type< TRANSFORM > { typedef transform type; };
|
---|
95 | template <> struct type_to_enum< transform > { static const datatype type = TRANSFORM; };
|
---|
96 |
|
---|
97 | template<>
|
---|
98 | inline transform interpolate( const transform& a, const transform& b, float value )
|
---|
99 | {
|
---|
100 | return transform(
|
---|
101 | glm::mix ( a.get_position(), b.get_position(), value ),
|
---|
102 | glm::slerp( a.get_orientation(), b.get_orientation(), value )
|
---|
103 | );
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | #endif // NV_CORE_TRANSFORM_HH
|
---|