1 | /*
|
---|
2 | -----------------------------------------------------------------------------
|
---|
3 | This source file is part of OGRE
|
---|
4 | (Object-oriented Graphics Rendering Engine)
|
---|
5 | For the latest info, see http://www.ogre3d.org/
|
---|
6 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
23 | -----------------------------------------------------------------------------
|
---|
24 | */
|
---|
25 | #include "OgreStableHeaders.h"
|
---|
26 | #include "OgreMatrix4.h"
|
---|
27 |
|
---|
28 | #include "OgreVector3.h"
|
---|
29 | #include "OgreMatrix3.h"
|
---|
30 |
|
---|
31 | namespace Ogre
|
---|
32 | {
|
---|
33 |
|
---|
34 | const Matrix4 Matrix4::ZERO(
|
---|
35 | 0, 0, 0, 0,
|
---|
36 | 0, 0, 0, 0,
|
---|
37 | 0, 0, 0, 0,
|
---|
38 | 0, 0, 0, 0 );
|
---|
39 |
|
---|
40 | const Matrix4 Matrix4::IDENTITY(
|
---|
41 | 1, 0, 0, 0,
|
---|
42 | 0, 1, 0, 0,
|
---|
43 | 0, 0, 1, 0,
|
---|
44 | 0, 0, 0, 1 );
|
---|
45 |
|
---|
46 | const Matrix4 Matrix4::CLIPSPACE2DTOIMAGESPACE(
|
---|
47 | 0.5, 0, 0, 0.5,
|
---|
48 | 0, -0.5, 0, 0.5,
|
---|
49 | 0, 0, 1, 0,
|
---|
50 | 0, 0, 0, 1);
|
---|
51 |
|
---|
52 | //-----------------------------------------------------------------------
|
---|
53 | inline static Real
|
---|
54 | MINOR(const Matrix4& m, const size_t r0, const size_t r1, const size_t r2,
|
---|
55 | const size_t c0, const size_t c1, const size_t c2)
|
---|
56 | {
|
---|
57 | return m[r0][c0] * (m[r1][c1] * m[r2][c2] - m[r2][c1] * m[r1][c2]) -
|
---|
58 | m[r0][c1] * (m[r1][c0] * m[r2][c2] - m[r2][c0] * m[r1][c2]) +
|
---|
59 | m[r0][c2] * (m[r1][c0] * m[r2][c1] - m[r2][c0] * m[r1][c1]);
|
---|
60 | }
|
---|
61 | //-----------------------------------------------------------------------
|
---|
62 | Matrix4 Matrix4::adjoint() const
|
---|
63 | {
|
---|
64 | return Matrix4( MINOR(*this, 1, 2, 3, 1, 2, 3),
|
---|
65 | -MINOR(*this, 0, 2, 3, 1, 2, 3),
|
---|
66 | MINOR(*this, 0, 1, 3, 1, 2, 3),
|
---|
67 | -MINOR(*this, 0, 1, 2, 1, 2, 3),
|
---|
68 |
|
---|
69 | -MINOR(*this, 1, 2, 3, 0, 2, 3),
|
---|
70 | MINOR(*this, 0, 2, 3, 0, 2, 3),
|
---|
71 | -MINOR(*this, 0, 1, 3, 0, 2, 3),
|
---|
72 | MINOR(*this, 0, 1, 2, 0, 2, 3),
|
---|
73 |
|
---|
74 | MINOR(*this, 1, 2, 3, 0, 1, 3),
|
---|
75 | -MINOR(*this, 0, 2, 3, 0, 1, 3),
|
---|
76 | MINOR(*this, 0, 1, 3, 0, 1, 3),
|
---|
77 | -MINOR(*this, 0, 1, 2, 0, 1, 3),
|
---|
78 |
|
---|
79 | -MINOR(*this, 1, 2, 3, 0, 1, 2),
|
---|
80 | MINOR(*this, 0, 2, 3, 0, 1, 2),
|
---|
81 | -MINOR(*this, 0, 1, 3, 0, 1, 2),
|
---|
82 | MINOR(*this, 0, 1, 2, 0, 1, 2));
|
---|
83 | }
|
---|
84 | //-----------------------------------------------------------------------
|
---|
85 | Real Matrix4::determinant() const
|
---|
86 | {
|
---|
87 | return m[0][0] * MINOR(*this, 1, 2, 3, 1, 2, 3) -
|
---|
88 | m[0][1] * MINOR(*this, 1, 2, 3, 0, 2, 3) +
|
---|
89 | m[0][2] * MINOR(*this, 1, 2, 3, 0, 1, 3) -
|
---|
90 | m[0][3] * MINOR(*this, 1, 2, 3, 0, 1, 2);
|
---|
91 | }
|
---|
92 | //-----------------------------------------------------------------------
|
---|
93 | Matrix4 Matrix4::inverse() const
|
---|
94 | {
|
---|
95 | return adjoint() * (1.0f / determinant());
|
---|
96 | }
|
---|
97 | //-----------------------------------------------------------------------
|
---|
98 | void Matrix4::makeTransform(const Vector3& position, const Vector3& scale, const Quaternion& orientation)
|
---|
99 | {
|
---|
100 | // Ordering:
|
---|
101 | // 1. Scale
|
---|
102 | // 2. Rotate
|
---|
103 | // 3. Translate
|
---|
104 |
|
---|
105 | Matrix3 rot3x3, scale3x3;
|
---|
106 | orientation.ToRotationMatrix(rot3x3);
|
---|
107 | scale3x3 = Matrix3::ZERO;
|
---|
108 | scale3x3[0][0] = scale.x;
|
---|
109 | scale3x3[1][1] = scale.y;
|
---|
110 | scale3x3[2][2] = scale.z;
|
---|
111 |
|
---|
112 | // Set up final matrix with scale, rotation and translation
|
---|
113 | *this = rot3x3 * scale3x3;
|
---|
114 | this->setTrans(position);
|
---|
115 |
|
---|
116 | // No projection term
|
---|
117 | m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
|
---|
118 | }
|
---|
119 | //-----------------------------------------------------------------------
|
---|
120 | void Matrix4::makeInverseTransform(const Vector3& position, const Vector3& scale, const Quaternion& orientation)
|
---|
121 | {
|
---|
122 | // Invert the parameters
|
---|
123 | Vector3 invTranslate = -position;
|
---|
124 | Vector3 invScale(1 / scale.x, 1 / scale.y, 1 / scale.z);
|
---|
125 | Quaternion invRot = orientation.Inverse();
|
---|
126 |
|
---|
127 | // Because we're inverting, order is translation, rotation, scale
|
---|
128 | // So make translation relative to scale & rotation
|
---|
129 | invTranslate *= invScale; // scale
|
---|
130 | invTranslate = invRot * invTranslate; // rotate
|
---|
131 |
|
---|
132 | // Next, make a 3x3 rotation matrix and apply inverse scale
|
---|
133 | Matrix3 rot3x3, scale3x3;
|
---|
134 | invRot.ToRotationMatrix(rot3x3);
|
---|
135 | scale3x3 = Matrix3::ZERO;
|
---|
136 | scale3x3[0][0] = invScale.x;
|
---|
137 | scale3x3[1][1] = invScale.y;
|
---|
138 | scale3x3[2][2] = invScale.z;
|
---|
139 |
|
---|
140 | // Set up final matrix with scale, rotation and translation
|
---|
141 | *this = scale3x3 * rot3x3;
|
---|
142 | this->setTrans(invTranslate);
|
---|
143 |
|
---|
144 | // No projection term
|
---|
145 | m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
|
---|
146 | }
|
---|
147 |
|
---|
148 | }
|
---|