| 1 | /* |
|---|
| 2 | ----------------------------------------------------------------------------- |
|---|
| 3 | This source file is part of LEXIExporter |
|---|
| 4 | |
|---|
| 5 | Copyright 2006 NDS Limited |
|---|
| 6 | |
|---|
| 7 | Author(s): |
|---|
| 8 | Bo Krohn |
|---|
| 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 | |
|---|
| 26 | ///////////////////////////////////////////////////// |
|---|
| 27 | // |
|---|
| 28 | // Vector4 class |
|---|
| 29 | // |
|---|
| 30 | ///////////////////////////////////////////////////// |
|---|
| 31 | |
|---|
| 32 | #include "stdafx.h" |
|---|
| 33 | #include "MathVector4.h" |
|---|
| 34 | |
|---|
| 35 | // |
|---|
| 36 | |
|---|
| 37 | const CVec4 CVec4::_zero(0.0f, 0.0f, 0.0f, 0.0f); |
|---|
| 38 | const CVec4 CVec4::_x(1.0f, 0.0f, 0.0f, 0.0f); |
|---|
| 39 | const CVec4 CVec4::_y(0.0f, 1.0f, 0.0f, 0.0f); |
|---|
| 40 | const CVec4 CVec4::_z(0.0f, 0.0f, 1.0f, 0.0f); |
|---|
| 41 | const CVec4 CVec4::_w(0.0f, 0.0f, 0.0f, 1.0f); |
|---|
| 42 | |
|---|
| 43 | // |
|---|
| 44 | |
|---|
| 45 | void 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 | |
|---|