rayzor

annotate src/vmathmat.h @ 1:a826bf0fb169

fixed line endings
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 05 Apr 2014 09:05:26 +0300
parents 2a5340a6eee4
children d94a69933a71
rev   line source
nuclear@1 1 #ifndef VMATH_MATRIX_H_
nuclear@1 2 #define VMATH_MATRIX_H_
nuclear@1 3
nuclear@1 4 #include <math.h>
nuclear@1 5
nuclear@1 6 #ifndef M_PI
nuclear@1 7 #define M_PI 3.141592653
nuclear@1 8 #endif
nuclear@1 9
nuclear@1 10 class Vector3;
nuclear@1 11
nuclear@1 12 class Matrix4x4 {
nuclear@1 13 public:
nuclear@1 14 float m[4][4];
nuclear@1 15
nuclear@1 16 Matrix4x4()
nuclear@1 17 {
nuclear@1 18 set_identity();
nuclear@1 19 }
nuclear@1 20
nuclear@1 21 Matrix4x4(float m00, float m01, float m02, float m03,
nuclear@1 22 float m10, float m11, float m12, float m13,
nuclear@1 23 float m20, float m21, float m22, float m23,
nuclear@1 24 float m30, float m31, float m32, float m33)
nuclear@1 25 {
nuclear@1 26 m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03;
nuclear@1 27 m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13;
nuclear@1 28 m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23;
nuclear@1 29 m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33;
nuclear@1 30 }
nuclear@1 31
nuclear@1 32 inline void set_identity();
nuclear@1 33 inline void translate(float x, float y, float z);
nuclear@1 34 inline void rotate(float angle, float x, float y, float z);
nuclear@1 35 inline void scale(float x, float y, float z);
nuclear@1 36 inline void perspective(float vfov, float aspect, float znear, float zfar);
nuclear@1 37 inline void lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up);
nuclear@1 38
nuclear@1 39 float *operator [](int idx) { return m[idx]; }
nuclear@1 40 const float *operator [](int idx) const { return m[idx]; }
nuclear@1 41 };
nuclear@1 42
nuclear@1 43 inline Matrix4x4 operator *(const Matrix4x4 &a, const Matrix4x4 &b)
nuclear@1 44 {
nuclear@1 45 Matrix4x4 res;
nuclear@1 46 for(int i=0; i<4; i++) {
nuclear@1 47 for(int j=0; j<4; j++) {
nuclear@1 48 res[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] +
nuclear@1 49 a[i][2] * b[2][j] + a[i][3] * b[3][j];
nuclear@1 50 }
nuclear@1 51 }
nuclear@1 52 return res;
nuclear@1 53 }
nuclear@1 54
nuclear@1 55 inline void Matrix4x4::set_identity()
nuclear@1 56 {
nuclear@1 57 m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1.0;
nuclear@1 58 m[0][1] = m[0][2] = m[0][3] = m[1][2] = m[1][3] = m[2][3] = 0.0;
nuclear@1 59 m[1][0] = m[2][0] = m[3][0] = m[2][1] = m[3][1] = m[3][2] = 0.0;
nuclear@1 60 }
nuclear@1 61
nuclear@1 62 inline void Matrix4x4::translate(float x, float y, float z)
nuclear@1 63 {
nuclear@1 64 Matrix4x4 m(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1);
nuclear@1 65 *this = *this * m;
nuclear@1 66 }
nuclear@1 67
nuclear@1 68 inline void Matrix4x4::rotate(float angle, float x, float y, float z)
nuclear@1 69 {
nuclear@1 70 float sina = (float)sin(angle);
nuclear@1 71 float cosa = (float)cos(angle);
nuclear@1 72 float rcosa = 1.0f - cosa;
nuclear@1 73 float nxsq = x * x;
nuclear@1 74 float nysq = y * y;
nuclear@1 75 float nzsq = z * z;
nuclear@1 76
nuclear@1 77 Matrix4x4 m;
nuclear@1 78 m[0][0] = nxsq + (1.0f - nxsq) * cosa;
nuclear@1 79 m[0][1] = x * y * rcosa - z * sina;
nuclear@1 80 m[0][2] = x * z * rcosa + y * sina;
nuclear@1 81
nuclear@1 82 m[1][0] = x * y * rcosa + z * sina;
nuclear@1 83 m[1][1] = nysq + (1.0f - nysq) * cosa;
nuclear@1 84 m[1][2] = y * z * rcosa - x * sina;
nuclear@1 85
nuclear@1 86 m[2][0] = x * z * rcosa - y * sina;
nuclear@1 87 m[2][1] = y * z * rcosa + x * sina;
nuclear@1 88 m[2][2] = nzsq + (1.0f - nzsq) * cosa;
nuclear@1 89
nuclear@1 90 *this = *this * m;
nuclear@1 91 }
nuclear@1 92
nuclear@1 93 inline void Matrix4x4::scale(float x, float y, float z)
nuclear@1 94 {
nuclear@1 95 Matrix4x4 m(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1);
nuclear@1 96 *this = *this * m;
nuclear@1 97 }
nuclear@1 98
nuclear@1 99 inline void Matrix4x4::perspective(float vfov, float aspect, float znear, float zfar)
nuclear@1 100 {
nuclear@1 101 float f = 1.0f / tan(vfov * 0.5f);
nuclear@1 102 float dz = znear - zfar;
nuclear@1 103
nuclear@1 104 Matrix4x4 m;
nuclear@1 105 m[0][0] = f / aspect;
nuclear@1 106 m[1][1] = f;
nuclear@1 107 m[2][2] = (zfar + znear) / dz;
nuclear@1 108 m[3][2] = -1.0f;
nuclear@1 109 m[2][3] = 2.0f * zfar * znear / dz;
nuclear@1 110 m[3][3] = 0.0f;
nuclear@1 111
nuclear@1 112 *this = *this * m;
nuclear@1 113 }
nuclear@1 114
nuclear@1 115
nuclear@1 116 #endif // VMATH_MATRIX_H_