6 Transform::Transform():
7 position(0.0f, 0.0f, 0.0f),
8 euler(Angle::zero(), Angle::zero(), Angle::zero()),
9 scale(1.0f, 1.0f, 1.0f),
13 Transform Transform::from_matrix(const Matrix &matrix)
16 trn.position = matrix.column(3).slice<3>(0);
18 trn.euler.z = Geometry::atan2<float>(matrix(1, 0), matrix(0, 0));
19 Matrix m = Matrix::rotation(-trn.euler.z, Vector3(0.0f, 0.0f, 1.0f))*matrix;
20 trn.euler.y = Geometry::atan2<float>(-m(2, 0), m(0, 0));
21 m = Matrix::rotation(-trn.euler.y, Vector3(0.0f, 1.0f, 0.0f))*m;
22 trn.euler.x = Geometry::atan2<float>(m(2, 1), m(1, 1));
23 m = Matrix::rotation(-trn.euler.x, Vector3(1.0f, 0.0f, 0.0f))*m;
25 trn.scale = Vector3(m(0, 0), m(1, 1), m(2, 2));
27 trn.mask = POSITION|EULER|SCALE;
31 void Transform::set_position(const Vector3 &p)
37 void Transform::set_euler(const AngleVector3 &e)
43 void Transform::set_rotation(const Angle &angle, const Vector3 &axis)
45 euler = from_matrix(Matrix::rotation(angle, axis)).euler;
49 void Transform::set_scale(float s)
51 set_scale(Vector3(s, s, s));
54 void Transform::set_scale(const Vector3 &s)
60 Matrix Transform::to_matrix() const
63 result.translate(position);
64 result.rotate(euler.z, Vector3(0.0f, 0.0f, 1.0f));
65 result.rotate(euler.y, Vector3(0.0f, 1.0f, 0.0f));
66 result.rotate(euler.x, Vector3(1.0f, 0.0f, 0.0f));
72 Transform::Loader::Loader(Transform &t):
73 DataFile::ObjectLoader<Transform>(t)
75 add("position_x", &Loader::position_x);
76 add("position_y", &Loader::position_y);
77 add("position_z", &Loader::position_z);
78 add("position", &Loader::position);
79 add("euler_x", &Loader::euler_x);
80 add("euler_y", &Loader::euler_y);
81 add("euler_z", &Loader::euler_z);
82 add("euler", &Loader::euler);
83 add("rotation", &Loader::rotation);
84 add("scale_x", &Loader::scale_x);
85 add("scale_y", &Loader::scale_y);
86 add("scale_z", &Loader::scale_z);
87 add("scale", &Loader::scale_uniform);
88 add("scale", &Loader::scale);
91 void Transform::Loader::position_x(float x)
94 obj.mask = obj.mask|POSITION_X;
97 void Transform::Loader::position_y(float y)
100 obj.mask = obj.mask|POSITION_Y;
103 void Transform::Loader::position_z(float z)
106 obj.mask = obj.mask|POSITION_Z;
109 void Transform::Loader::position(float x, float y, float z)
111 obj.set_position(Vector3(x, y, z));
114 void Transform::Loader::euler_x(float x)
116 obj.euler.x = Angle::from_degrees(x);
117 obj.mask = obj.mask|EULER_X;
120 void Transform::Loader::euler_y(float y)
122 obj.euler.y = Angle::from_degrees(y);
123 obj.mask = obj.mask|EULER_Y;
126 void Transform::Loader::euler_z(float z)
128 obj.euler.z = Angle::from_degrees(z);
129 obj.mask = obj.mask|EULER_Z;
132 void Transform::Loader::euler(float x, float y, float z)
134 obj.set_euler(AngleVector3(Angle::from_degrees(x), Angle::from_degrees(y), Angle::from_degrees(z)));
137 void Transform::Loader::rotation(float a, float x, float y, float z)
139 obj.set_rotation(Angle::from_degrees(a), Vector3(x, y, z));
142 void Transform::Loader::scale_x(float x)
145 obj.mask = obj.mask|SCALE_X;
148 void Transform::Loader::scale_y(float y)
151 obj.mask = obj.mask|SCALE_Y;
154 void Transform::Loader::scale_z(float z)
157 obj.mask = obj.mask|SCALE_Z;
160 void Transform::Loader::scale_uniform(float s)
165 void Transform::Loader::scale(float x, float y, float z)
167 obj.set_scale(Vector3(x, y, z));