Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/OgreMain/src/OgreMatrix4.cpp @ 3

Last change on this file since 3 was 3, checked in by anonymous, 17 years ago

=update

File size: 9.0 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4(Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29#include "OgreStableHeaders.h"
30#include "OgreMatrix4.h"
31
32#include "OgreVector3.h"
33#include "OgreMatrix3.h"
34
35namespace Ogre
36{
37
38    const Matrix4 Matrix4::ZERO(
39        0, 0, 0, 0,
40        0, 0, 0, 0,
41        0, 0, 0, 0,
42        0, 0, 0, 0 );
43
44    const Matrix4 Matrix4::IDENTITY(
45        1, 0, 0, 0,
46        0, 1, 0, 0,
47        0, 0, 1, 0,
48        0, 0, 0, 1 );
49
50    const Matrix4 Matrix4::CLIPSPACE2DTOIMAGESPACE(
51        0.5,    0,  0, 0.5, 
52          0, -0.5,  0, 0.5, 
53          0,    0,  1,   0,
54          0,    0,  0,   1);
55
56    //-----------------------------------------------------------------------
57    inline static Real
58        MINOR(const Matrix4& m, const size_t r0, const size_t r1, const size_t r2, 
59                                                                const size_t c0, const size_t c1, const size_t c2)
60    {
61        return m[r0][c0] * (m[r1][c1] * m[r2][c2] - m[r2][c1] * m[r1][c2]) -
62            m[r0][c1] * (m[r1][c0] * m[r2][c2] - m[r2][c0] * m[r1][c2]) +
63            m[r0][c2] * (m[r1][c0] * m[r2][c1] - m[r2][c0] * m[r1][c1]);
64    }
65    //-----------------------------------------------------------------------
66    Matrix4 Matrix4::adjoint() const
67    {
68        return Matrix4( MINOR(*this, 1, 2, 3, 1, 2, 3),
69            -MINOR(*this, 0, 2, 3, 1, 2, 3),
70            MINOR(*this, 0, 1, 3, 1, 2, 3),
71            -MINOR(*this, 0, 1, 2, 1, 2, 3),
72
73            -MINOR(*this, 1, 2, 3, 0, 2, 3),
74            MINOR(*this, 0, 2, 3, 0, 2, 3),
75            -MINOR(*this, 0, 1, 3, 0, 2, 3),
76            MINOR(*this, 0, 1, 2, 0, 2, 3),
77
78            MINOR(*this, 1, 2, 3, 0, 1, 3),
79            -MINOR(*this, 0, 2, 3, 0, 1, 3),
80            MINOR(*this, 0, 1, 3, 0, 1, 3),
81            -MINOR(*this, 0, 1, 2, 0, 1, 3),
82
83            -MINOR(*this, 1, 2, 3, 0, 1, 2),
84            MINOR(*this, 0, 2, 3, 0, 1, 2),
85            -MINOR(*this, 0, 1, 3, 0, 1, 2),
86            MINOR(*this, 0, 1, 2, 0, 1, 2));
87    }
88    //-----------------------------------------------------------------------
89    Real Matrix4::determinant() const
90    {
91        return m[0][0] * MINOR(*this, 1, 2, 3, 1, 2, 3) -
92            m[0][1] * MINOR(*this, 1, 2, 3, 0, 2, 3) +
93            m[0][2] * MINOR(*this, 1, 2, 3, 0, 1, 3) -
94            m[0][3] * MINOR(*this, 1, 2, 3, 0, 1, 2);
95    }
96    //-----------------------------------------------------------------------
97    Matrix4 Matrix4::inverse() const
98    {
99        Real m00 = m[0][0], m01 = m[0][1], m02 = m[0][2], m03 = m[0][3];
100        Real m10 = m[1][0], m11 = m[1][1], m12 = m[1][2], m13 = m[1][3];
101        Real m20 = m[2][0], m21 = m[2][1], m22 = m[2][2], m23 = m[2][3];
102        Real m30 = m[3][0], m31 = m[3][1], m32 = m[3][2], m33 = m[3][3];
103
104        Real v0 = m20 * m31 - m21 * m30;
105        Real v1 = m20 * m32 - m22 * m30;
106        Real v2 = m20 * m33 - m23 * m30;
107        Real v3 = m21 * m32 - m22 * m31;
108        Real v4 = m21 * m33 - m23 * m31;
109        Real v5 = m22 * m33 - m23 * m32;
110
111        Real t00 = + (v5 * m11 - v4 * m12 + v3 * m13);
112        Real t10 = - (v5 * m10 - v2 * m12 + v1 * m13);
113        Real t20 = + (v4 * m10 - v2 * m11 + v0 * m13);
114        Real t30 = - (v3 * m10 - v1 * m11 + v0 * m12);
115
116        Real invDet = 1 / (t00 * m00 + t10 * m01 + t20 * m02 + t30 * m03);
117
118        Real d00 = t00 * invDet;
119        Real d10 = t10 * invDet;
120        Real d20 = t20 * invDet;
121        Real d30 = t30 * invDet;
122
123        Real d01 = - (v5 * m01 - v4 * m02 + v3 * m03) * invDet;
124        Real d11 = + (v5 * m00 - v2 * m02 + v1 * m03) * invDet;
125        Real d21 = - (v4 * m00 - v2 * m01 + v0 * m03) * invDet;
126        Real d31 = + (v3 * m00 - v1 * m01 + v0 * m02) * invDet;
127
128        v0 = m10 * m31 - m11 * m30;
129        v1 = m10 * m32 - m12 * m30;
130        v2 = m10 * m33 - m13 * m30;
131        v3 = m11 * m32 - m12 * m31;
132        v4 = m11 * m33 - m13 * m31;
133        v5 = m12 * m33 - m13 * m32;
134
135        Real d02 = + (v5 * m01 - v4 * m02 + v3 * m03) * invDet;
136        Real d12 = - (v5 * m00 - v2 * m02 + v1 * m03) * invDet;
137        Real d22 = + (v4 * m00 - v2 * m01 + v0 * m03) * invDet;
138        Real d32 = - (v3 * m00 - v1 * m01 + v0 * m02) * invDet;
139
140        v0 = m21 * m10 - m20 * m11;
141        v1 = m22 * m10 - m20 * m12;
142        v2 = m23 * m10 - m20 * m13;
143        v3 = m22 * m11 - m21 * m12;
144        v4 = m23 * m11 - m21 * m13;
145        v5 = m23 * m12 - m22 * m13;
146
147        Real d03 = - (v5 * m01 - v4 * m02 + v3 * m03) * invDet;
148        Real d13 = + (v5 * m00 - v2 * m02 + v1 * m03) * invDet;
149        Real d23 = - (v4 * m00 - v2 * m01 + v0 * m03) * invDet;
150        Real d33 = + (v3 * m00 - v1 * m01 + v0 * m02) * invDet;
151
152        return Matrix4(
153            d00, d01, d02, d03,
154            d10, d11, d12, d13,
155            d20, d21, d22, d23,
156            d30, d31, d32, d33);
157    }
158    //-----------------------------------------------------------------------
159    Matrix4 Matrix4::inverseAffine(void) const
160    {
161        assert(isAffine());
162
163        Real m10 = m[1][0], m11 = m[1][1], m12 = m[1][2];
164        Real m20 = m[2][0], m21 = m[2][1], m22 = m[2][2];
165
166        Real t00 = m22 * m11 - m21 * m12;
167        Real t10 = m20 * m12 - m22 * m10;
168        Real t20 = m21 * m10 - m20 * m11;
169
170        Real m00 = m[0][0], m01 = m[0][1], m02 = m[0][2];
171
172        Real invDet = 1 / (m00 * t00 + m01 * t10 + m02 * t20);
173
174        t00 *= invDet; t10 *= invDet; t20 *= invDet;
175
176        m00 *= invDet; m01 *= invDet; m02 *= invDet;
177
178        Real r00 = t00;
179        Real r01 = m02 * m21 - m01 * m22;
180        Real r02 = m01 * m12 - m02 * m11;
181
182        Real r10 = t10;
183        Real r11 = m00 * m22 - m02 * m20;
184        Real r12 = m02 * m10 - m00 * m12;
185
186        Real r20 = t20;
187        Real r21 = m01 * m20 - m00 * m21;
188        Real r22 = m00 * m11 - m01 * m10;
189
190        Real m03 = m[0][3], m13 = m[1][3], m23 = m[2][3];
191
192        Real r03 = - (r00 * m03 + r01 * m13 + r02 * m23);
193        Real r13 = - (r10 * m03 + r11 * m13 + r12 * m23);
194        Real r23 = - (r20 * m03 + r21 * m13 + r22 * m23);
195
196        return Matrix4(
197            r00, r01, r02, r03,
198            r10, r11, r12, r13,
199            r20, r21, r22, r23,
200              0,   0,   0,   1);
201    }
202    //-----------------------------------------------------------------------
203    void Matrix4::makeTransform(const Vector3& position, const Vector3& scale, const Quaternion& orientation)
204    {
205        // Ordering:
206        //    1. Scale
207        //    2. Rotate
208        //    3. Translate
209
210        Matrix3 rot3x3, scale3x3;
211        orientation.ToRotationMatrix(rot3x3);
212        scale3x3 = Matrix3::ZERO;
213        scale3x3[0][0] = scale.x;
214        scale3x3[1][1] = scale.y;
215        scale3x3[2][2] = scale.z;
216
217        // Set up final matrix with scale, rotation and translation
218        *this = rot3x3 * scale3x3;
219        this->setTrans(position);
220
221        // No projection term
222        m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
223    }
224    //-----------------------------------------------------------------------
225    void Matrix4::makeInverseTransform(const Vector3& position, const Vector3& scale, const Quaternion& orientation)
226    {
227        // Invert the parameters
228        Vector3 invTranslate = -position;
229        Vector3 invScale(1 / scale.x, 1 / scale.y, 1 / scale.z);
230        Quaternion invRot = orientation.Inverse();
231
232        // Because we're inverting, order is translation, rotation, scale
233        // So make translation relative to scale & rotation
234        invTranslate *= invScale; // scale
235        invTranslate = invRot * invTranslate; // rotate
236
237        // Next, make a 3x3 rotation matrix and apply inverse scale
238        Matrix3 rot3x3, scale3x3;
239        invRot.ToRotationMatrix(rot3x3);
240        scale3x3 = Matrix3::ZERO;
241        scale3x3[0][0] = invScale.x;
242        scale3x3[1][1] = invScale.y;
243        scale3x3[2][2] = invScale.z;
244
245        // Set up final matrix with scale, rotation and translation
246        *this = scale3x3 * rot3x3;
247        this->setTrans(invTranslate);
248
249        // No projection term
250        m[3][0] = 0; m[3][1] = 0; m[3][2] = 0; m[3][3] = 1;
251    }
252
253}
Note: See TracBrowser for help on using the repository browser.