| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |
|
| 31 |
class TransformNode |
| 32 |
{ |
| 33 |
public: |
| 34 |
STRING_CONSTANT( Name, "TransformNode" ); |
| 35 |
|
| 36 |
virtual const Matrix4& localToParent() const = 0; |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
class IdentityTransform : public TransformNode |
| 41 |
{ |
| 42 |
public: |
| 43 |
|
| 44 |
const Matrix4& localToParent() const { |
| 45 |
return g_matrix4_identity; |
| 46 |
} |
| 47 |
}; |
| 48 |
|
| 49 |
|
| 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 |
|
| 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 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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<void()> m_changed; |
| 120 |
Callback<void()> m_apply; |
| 121 |
TransformModifierType m_type; |
| 122 |
public: |
| 123 |
|
| 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 ), |
| 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 |