source: trunk/nv/core/transform.hh @ 485

Last change on this file since 485 was 485, checked in by epyon, 9 years ago
  • massive update (need to start doing atomics again) :/
File size: 2.9 KB
Line 
1// Copyright (C) 2012-2015 ChaosForge Ltd
2// http://chaosforge.org/
3//
4// This file is part of Nova libraries.
5// For conditions of distribution and use, see copying.txt file in root folder.
6
7#ifndef NV_CORE_TRANSFORM_HH
8#define NV_CORE_TRANSFORM_HH
9
10#include <nv/common.hh>
11#include <nv/stl/math.hh>
12
13namespace 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( float radians, const vec3& axis )
28                {
29                        m_orientation = math::angle_axis( radians, 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 += math::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 = math::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( math::inverse( m_orientation ) );
63                        // TODO: simplify
64                        return transform( -math::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
79                transform scaled( float v ) const
80                {
81                        return transform( v * m_position, v * m_orientation );
82                }
83
84        private:
85                vec3 m_position;
86                quat m_orientation;
87        };
88
89        inline transform operator*(transform lhs, const transform& rhs)
90        {
91                lhs *= rhs;
92                return lhs;
93        }
94
95        inline vec3 operator*(const vec3 lhs, const transform& rhs)
96        {
97                return rhs.transformed( lhs );
98        }
99
100        inline transform operator*( float value, const transform& lhs )
101        {
102                return lhs.scaled( value );
103        }
104
105        template <> struct enum_to_type< TRANSFORM > { typedef transform type; };
106        template <> struct type_to_enum< transform > { static const datatype type = TRANSFORM; };
107
108}
109
110#endif // NV_CORE_TRANSFORM_HH
Note: See TracBrowser for help on using the repository browser.