Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/ogre/Tools/3dsmaxExport/LEXIExporter/SharedUtilities/Sources/MathVector4.cpp @ 45

Last change on this file since 45 was 6, checked in by anonymous, 18 years ago

=…

File size: 2.6 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of LEXIExporter
4
5Copyright 2006 NDS Limited
6
7Author(s):
8Bo Krohn
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-----------------------------------------------------------------------------
24*/
25
26/////////////////////////////////////////////////////
27//
28//  Vector4 class
29//
30/////////////////////////////////////////////////////
31
32#include "stdafx.h"
33#include "MathVector4.h"
34
35//
36
37const CVec4 CVec4::_zero(0.0f, 0.0f, 0.0f, 0.0f);
38const CVec4 CVec4::_x(1.0f, 0.0f, 0.0f, 0.0f);
39const CVec4 CVec4::_y(0.0f, 1.0f, 0.0f, 0.0f);
40const CVec4 CVec4::_z(0.0f, 0.0f, 1.0f, 0.0f);
41const CVec4 CVec4::_w(0.0f, 0.0f, 0.0f, 1.0f);
42
43//
44
45void CVec4::fromMatrix(const CMatrix& m)
46{
47        float s;
48        float trace = m.mat[0][0] + m.mat[1][1] + m.mat[2][2];
49
50        if( trace >= 0.0f ) {
51
52                s = sqrtf( trace + 1.0f );
53
54                w = s * 0.5f;
55
56                s = 0.5f / s;
57
58                x = ( m.mat[2][1] - m.mat[1][2] ) * s;
59                y = ( m.mat[0][2] - m.mat[2][0] ) * s;
60                z = ( m.mat[1][0] - m.mat[0][1] ) * s;
61
62        } 
63        else {
64
65                unsigned h = 0;
66
67                if( m.mat[1][1] > m.mat[0][0] ) h = 1;
68                if( m.mat[2][2] > m.mat[h][h] ) h = 2;
69
70                switch( h ) {
71
72                  case 0:
73                        s    = sqrtf( ( m.mat[0][0] - ( m.mat[1][1] + m.mat[2][2] ) ) + m.mat[3][3] );
74                        x = s * 0.5f;
75                        s    = 0.5f / s;
76                        y = ( m.mat[0][1] + m.mat[1][0] ) * s;
77                        z = ( m.mat[2][0] + m.mat[0][2] ) * s;
78                        w = ( m.mat[2][1] - m.mat[1][2] ) * s;
79                        break;
80
81                  case 1:
82                        s    = sqrtf( ( m.mat[1][1] - ( m.mat[2][2] + m.mat[0][0] ) ) + m.mat[3][3] );
83                        y = s * 0.5f;
84                        s    = 0.5f  / s;
85                        z = ( m.mat[1][2] + m.mat[2][1] ) * s;
86                        x = ( m.mat[0][1] + m.mat[1][0] ) * s;
87                        w = ( m.mat[0][2] - m.mat[2][0] ) * s;
88                        break;
89
90                  case 2:
91                        s    = sqrtf( ( m.mat[2][2] - ( m.mat[0][0] + m.mat[1][1] ) ) + m.mat[3][3] );
92                        z = s * 0.5f;
93                        s    = 0.5f / s;
94                        x = ( m.mat[2][0] + m.mat[0][2] ) * s;
95                        y = ( m.mat[1][2] + m.mat[2][1] ) * s;
96                        w = ( m.mat[1][0] - m.mat[0][1] ) * s;
97                        break;
98                }
99        }
100}
101
102//
103
Note: See TracBrowser for help on using the repository browser.