2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
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.
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.
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
22 #if !defined ( INCLUDED_TRANSFORMLIB_H )
23 #define INCLUDED_TRANSFORMLIB_H
25 #include "generic/constant.h"
26 #include "math/matrix.h"
27 #include "math/quaternion.h"
30 /// \brief A transform node.
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;
39 /// \brief A transform node which has no effect.
40 class IdentityTransform : public TransformNode
43 /// \brief Returns the identity matrix.
44 const Matrix4& localToParent() const {
45 return g_matrix4_identity;
49 /// \brief A transform node which stores a generic transformation matrix.
50 class MatrixTransform : public TransformNode
52 Matrix4 m_localToParent;
54 MatrixTransform() : m_localToParent( g_matrix4_identity ){
57 Matrix4& localToParent(){
58 return m_localToParent;
60 /// \brief Returns the stored local->parent transform.
61 const Matrix4& localToParent() const {
62 return m_localToParent;
67 #include "generic/callback.h"
69 typedef Vector3 Translation;
70 typedef Quaternion Rotation;
71 typedef Vector3 Scale;
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();
84 typedef bool TransformModifierType;
85 const TransformModifierType TRANSFORM_PRIMITIVE = false;
86 const TransformModifierType TRANSFORM_COMPONENT = true;
88 /// \brief A transformable scene-graph instance.
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
95 /// Freezing the transform on an instance will cause its transform
96 /// to be permanently applied to the geometry of the node.
100 STRING_CONSTANT( Name, "Transformable" );
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;
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 );
114 class TransformModifier : public Transformable
116 Translation m_translation;
119 Callback<void()> m_changed;
120 Callback<void()> m_apply;
121 TransformModifierType m_type;
124 TransformModifier( const Callback<void()>& changed, const Callback<void()>& apply ) :
125 m_translation( c_translation_identity ),
126 m_rotation( c_quaternion_identity ),
127 m_scale( c_scale_identity ),
128 m_changed( changed ),
130 m_type( TRANSFORM_PRIMITIVE ){
132 void setType( TransformModifierType type ){
135 TransformModifierType getType() const {
138 void setTranslation( const Translation& value ){
139 m_translation = value;
142 void setRotation( const Rotation& value ){
146 void setScale( const Scale& value ){
150 void freezeTransform(){
151 if ( m_translation != c_translation_identity
152 || m_rotation != c_rotation_identity
153 || m_scale != c_scale_identity ) {
155 m_translation = c_translation_identity;
156 m_rotation = c_rotation_identity;
157 m_scale = c_scale_identity;
161 const Translation& getTranslation() const {
162 return m_translation;
164 const Rotation& getRotation() const {
167 const Scale& getScale() const {
170 Matrix4 calculateTransform() const {
171 return matrix4_transform_for_components( getTranslation(), getRotation(), getScale() );