rayzor

annotate src/vmath.cc @ 22:5380ff64e83f

minor changes from dos, and line endings cleanup
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 02 May 2014 14:32:58 +0300
parents d94a69933a71
children
rev   line source
nuclear@1 1 #include "vmathmat.h"
nuclear@1 2 #include "vmath.h"
nuclear@1 3
nuclear@1 4 void Matrix4x4::lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up)
nuclear@1 5 {
nuclear@1 6 Vector3 vk = normalize(targ - pos);
nuclear@1 7 Vector3 vj = normalize(up);
nuclear@1 8 Vector3 vi = normalize(cross(vk, vj));
nuclear@1 9 vj = cross(vi, vk);
nuclear@1 10
nuclear@17 11 Matrix4x4 rm(
nuclear@1 12 vi.x, vi.y, vi.z, 0,
nuclear@1 13 vj.x, vj.y, vj.z, 0,
nuclear@1 14 -vk.x, -vk.y, -vk.z, 0,
nuclear@1 15 0, 0, 0, 1);
nuclear@17 16 Matrix4x4 tm(
nuclear@17 17 1, 0, 0, -pos.x,
nuclear@17 18 0, 1, 0, -pos.y,
nuclear@17 19 0, 0, 1, -pos.z,
nuclear@17 20 0, 0, 0, 1);
nuclear@17 21 *this = *this * (rm * tm);
nuclear@1 22 }
nuclear@12 23
nuclear@12 24 void Matrix4x4::transpose()
nuclear@12 25 {
nuclear@12 26 Matrix4x4 tmp = *this;
nuclear@12 27 for(int i=0; i<4; i++) {
nuclear@12 28 for(int j=0; j<4; j++) {
nuclear@12 29 m[i][j] = tmp[j][i];
nuclear@12 30 }
nuclear@12 31 }
nuclear@12 32 }
nuclear@12 33
nuclear@12 34 float Matrix4x4::determinant() const
nuclear@12 35 {
nuclear@12 36 float det11 = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
nuclear@12 37 (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
nuclear@12 38 (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
nuclear@12 39
nuclear@12 40 float det12 = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
nuclear@12 41 (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
nuclear@12 42 (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
nuclear@12 43
nuclear@12 44 float det13 = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
nuclear@12 45 (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
nuclear@12 46 (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
nuclear@12 47
nuclear@12 48 float det14 = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
nuclear@12 49 (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
nuclear@12 50 (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
nuclear@12 51
nuclear@12 52 return m[0][0] * det11 - m[0][1] * det12 + m[0][2] * det13 - m[0][3] * det14;
nuclear@12 53 }
nuclear@12 54
nuclear@12 55
nuclear@12 56 Matrix4x4 Matrix4x4::adjoint() const
nuclear@12 57 {
nuclear@12 58 Matrix4x4 coef;
nuclear@12 59
nuclear@12 60 coef.m[0][0] = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
nuclear@12 61 (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
nuclear@12 62 (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
nuclear@12 63 coef.m[0][1] = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
nuclear@12 64 (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
nuclear@12 65 (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
nuclear@12 66 coef.m[0][2] = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
nuclear@12 67 (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
nuclear@12 68 (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
nuclear@12 69 coef.m[0][3] = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
nuclear@12 70 (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
nuclear@12 71 (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
nuclear@12 72
nuclear@12 73 coef.m[1][0] = (m[0][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
nuclear@12 74 (m[0][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
nuclear@12 75 (m[0][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
nuclear@12 76 coef.m[1][1] = (m[0][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
nuclear@12 77 (m[0][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
nuclear@12 78 (m[0][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
nuclear@12 79 coef.m[1][2] = (m[0][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
nuclear@12 80 (m[0][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
nuclear@12 81 (m[0][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
nuclear@12 82 coef.m[1][3] = (m[0][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
nuclear@12 83 (m[0][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
nuclear@12 84 (m[0][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
nuclear@12 85
nuclear@12 86 coef.m[2][0] = (m[0][1] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
nuclear@12 87 (m[0][2] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) +
nuclear@12 88 (m[0][3] * (m[1][1] * m[3][2] - m[3][1] * m[1][2]));
nuclear@12 89 coef.m[2][1] = (m[0][0] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
nuclear@12 90 (m[0][2] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
nuclear@12 91 (m[0][3] * (m[1][0] * m[3][2] - m[3][0] * m[1][2]));
nuclear@12 92 coef.m[2][2] = (m[0][0] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) -
nuclear@12 93 (m[0][1] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
nuclear@12 94 (m[0][3] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
nuclear@12 95 coef.m[2][3] = (m[0][0] * (m[1][1] * m[3][2] - m[3][1] * m[1][2])) -
nuclear@12 96 (m[0][1] * (m[1][0] * m[3][2] - m[3][0] * m[1][2])) +
nuclear@12 97 (m[0][2] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
nuclear@12 98
nuclear@12 99 coef.m[3][0] = (m[0][1] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
nuclear@12 100 (m[0][2] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) +
nuclear@12 101 (m[0][3] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]));
nuclear@12 102 coef.m[3][1] = (m[0][0] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
nuclear@12 103 (m[0][2] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
nuclear@12 104 (m[0][3] * (m[1][0] * m[2][2] - m[2][0] * m[1][2]));
nuclear@12 105 coef.m[3][2] = (m[0][0] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) -
nuclear@12 106 (m[0][1] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
nuclear@12 107 (m[0][3] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
nuclear@12 108 coef.m[3][3] = (m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])) -
nuclear@12 109 (m[0][1] * (m[1][0] * m[2][2] - m[2][0] * m[1][2])) +
nuclear@12 110 (m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
nuclear@12 111
nuclear@12 112 coef.transpose();
nuclear@12 113
nuclear@12 114 for(int i=0; i<4; i++) {
nuclear@12 115 for(int j=0; j<4; j++) {
nuclear@12 116 coef.m[i][j] = j % 2 ? -coef.m[i][j] : coef.m[i][j];
nuclear@12 117 if(i % 2) coef.m[i][j] = -coef.m[i][j];
nuclear@12 118 }
nuclear@12 119 }
nuclear@12 120
nuclear@12 121 return coef;
nuclear@12 122 }
nuclear@12 123
nuclear@12 124 Matrix4x4 Matrix4x4::inverse() const
nuclear@12 125 {
nuclear@12 126 Matrix4x4 adj = adjoint();
nuclear@12 127 return adj * (1.0f / determinant());
nuclear@12 128 }