Merge 'remotes/trunk'
[0ad.git] / source / collada / Maths.cpp
blobe8ec313dc1032074b49402f23e7c35457c832b8f
1 /* Copyright (C) 2009 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #include "precompiled.h"
20 #include "Maths.h"
22 #include "FCollada.h"
25 void DumpMatrix(const FMMatrix44& m)
27 Log(LOG_INFO, "\n[%f %f %f %f]\n[%f %f %f %f]\n[%f %f %f %f]\n[%f %f %f %f]",
28 m.m[0][0], m.m[0][1], m.m[0][2], m.m[0][3],
29 m.m[1][0], m.m[1][1], m.m[1][2], m.m[1][3],
30 m.m[2][0], m.m[2][1], m.m[2][2], m.m[2][3],
31 m.m[3][0], m.m[3][1], m.m[3][2], m.m[3][3]
35 FMMatrix44 DecomposeToScaleMatrix(const FMMatrix44& m)
37 FMVector3 scale, rotation, translation;
38 float inverted;
39 m.Decompose(scale, rotation, translation, inverted);
40 return FMMatrix44::ScaleMatrix(scale);
44 FMMatrix44 operator+ (const FMMatrix44& a, const FMMatrix44& b)
46 FMMatrix44 r;
47 for (int x = 0; x < 4; ++x)
48 for (int y = 0; y < 4; ++y)
49 r[x][y] = a[x][y] + b[x][y];
50 return r;
53 FMMatrix44 operator/ (const FMMatrix44& a, const float b)
55 FMMatrix44 r;
56 for (int x = 0; x < 4; ++x)
57 for (int y = 0; y < 4; ++y)
58 r[x][y] = a[x][y] / b;
59 return r;
62 FMMatrix44 QuatToMatrix(float x, float y, float z, float w)
64 FMMatrix44 r;
66 r[0][0] = 1.0f - (y*y*2 + z*z*2);
67 r[1][0] = x*y*2 - w*z*2;
68 r[2][0] = x*z*2 + w*y*2;
69 r[3][0] = 0;
71 r[0][1] = x*y*2 + w*z*2;
72 r[1][1] = 1.0f - (x*x*2 + z*z*2);
73 r[2][1] = y*z*2 - w*x*2;
74 r[3][1] = 0;
76 r[0][2] = x*z*2 - w*y*2;
77 r[1][2] = y*z*2 + w*x*2;
78 r[2][2] = 1.0f - (x*x*2 + y*y*2);
79 r[3][2] = 0;
81 r[0][3] = 0;
82 r[1][3] = 0;
83 r[2][3] = 0;
84 r[3][3] = 1;
86 return r;