]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/transformlib.h
Merge remote-tracking branch 'github/master'
[xonotic/netradiant.git] / libs / transformlib.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #if !defined ( INCLUDED_TRANSFORMLIB_H )
23 #define INCLUDED_TRANSFORMLIB_H
24
25 #include "generic/constant.h"
26 #include "math/matrix.h"
27 #include "math/quaternion.h"
28
29
30 /// \brief A transform node.
31 class TransformNode
32 {
33 public:
34 STRING_CONSTANT( Name, "TransformNode" );
35 /// \brief Returns the transform which maps the node's local-space into the local-space of its parent node.
36 virtual const Matrix4& localToParent() const  = 0;
37 };
38
39 /// \brief A transform node which has no effect.
40 class IdentityTransform : public TransformNode
41 {
42 public:
43 /// \brief Returns the identity matrix.
44 const Matrix4& localToParent() const {
45         return g_matrix4_identity;
46 }
47 };
48
49 /// \brief A transform node which stores a generic transformation matrix.
50 class MatrixTransform : public TransformNode
51 {
52 Matrix4 m_localToParent;
53 public:
54 MatrixTransform() : m_localToParent( g_matrix4_identity ){
55 }
56
57 Matrix4& localToParent(){
58         return m_localToParent;
59 }
60 /// \brief Returns the stored local->parent transform.
61 const Matrix4& localToParent() const {
62         return m_localToParent;
63 }
64 };
65
66
67 #include "generic/callback.h"
68
69 typedef Vector3 Translation;
70 typedef Quaternion Rotation;
71 typedef Vector3 Scale;
72
73 inline Matrix4 matrix4_transform_for_components( const Translation& translation, const Rotation& rotation, const Scale& scale ){
74         Matrix4 result( matrix4_rotation_for_quaternion_quantised( rotation ) );
75         vector4_to_vector3( result.x() ) *= scale.x();
76         vector4_to_vector3( result.y() ) *= scale.y();
77         vector4_to_vector3( result.z() ) *= scale.z();
78         result.tx() = translation.x();
79         result.ty() = translation.y();
80         result.tz() = translation.z();
81         return result;
82 }
83
84 typedef bool TransformModifierType;
85 const TransformModifierType TRANSFORM_PRIMITIVE = false;
86 const TransformModifierType TRANSFORM_COMPONENT = true;
87
88 /// \brief A transformable scene-graph instance.
89 ///
90 /// A transformable instance may be translated, rotated or scaled.
91 /// The state of the instanced node's geometrical representation
92 /// will be the product of its geometry and the transforms of each
93 /// of its instances, applied in the order they appear in a graph
94 /// traversal.
95 /// Freezing the transform on an instance will cause its transform
96 /// to be permanently applied to the geometry of the node.
97 class Transformable
98 {
99 public:
100 STRING_CONSTANT( Name, "Transformable" );
101
102 virtual void setType( TransformModifierType type ) = 0;
103 virtual void setTranslation( const Translation& value ) = 0;
104 virtual void setRotation( const Rotation& value ) = 0;
105 virtual void setScale( const Scale& value ) = 0;
106 virtual void freezeTransform() = 0;
107 };
108
109 const Translation c_translation_identity = Translation( 0, 0, 0 );
110 const Rotation c_rotation_identity = c_quaternion_identity;
111 const Scale c_scale_identity = Scale( 1, 1, 1 );
112
113
114 class TransformModifier : public Transformable
115 {
116 Translation m_translation;
117 Rotation m_rotation;
118 Scale m_scale;
119 Callback m_changed;
120 Callback m_apply;
121 TransformModifierType m_type;
122 public:
123
124 TransformModifier( const Callback& changed, const Callback& apply ) :
125         m_translation( c_translation_identity ),
126         m_rotation( c_quaternion_identity ),
127         m_scale( c_scale_identity ),
128         m_changed( changed ),
129         m_apply( apply ),
130         m_type( TRANSFORM_PRIMITIVE ){
131 }
132 void setType( TransformModifierType type ){
133         m_type = type;
134 }
135 TransformModifierType getType() const {
136         return m_type;
137 }
138 void setTranslation( const Translation& value ){
139         m_translation = value;
140         m_changed();
141 }
142 void setRotation( const Rotation& value ){
143         m_rotation = value;
144         m_changed();
145 }
146 void setScale( const Scale& value ){
147         m_scale = value;
148         m_changed();
149 }
150 void freezeTransform(){
151         if ( m_translation != c_translation_identity
152                  || m_rotation != c_rotation_identity
153                  || m_scale != c_scale_identity ) {
154                 m_apply();
155                 m_translation = c_translation_identity;
156                 m_rotation = c_rotation_identity;
157                 m_scale = c_scale_identity;
158                 m_changed();
159         }
160 }
161 const Translation& getTranslation() const {
162         return m_translation;
163 }
164 const Rotation& getRotation() const {
165         return m_rotation;
166 }
167 const Scale& getScale() const {
168         return m_scale;
169 }
170 Matrix4 calculateTransform() const {
171         return matrix4_transform_for_components( getTranslation(), getRotation(), getScale() );
172 }
173 };
174
175
176 #endif