rayzor

view src/vmathmat.h @ 0:2a5340a6eee4

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