about summary refs log tree commit diff
path: root/src/base/tl/matrix.hpp
blob: b723eaa7d324622650281deb4298794dc1ecc221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef TL_FILE_MATRIX_HPP
#define TL_FILE_MATRIX_HPP

/*
 
 Looks like OpenGL 
 
 Column Major

        / m[0][0]  m[1][0]  m[2][0]  m[3][0] \     / v[0] \
        |                                    |     |      |
		| m[0][1]  m[1][1]  m[2][1]  m[3][1] |     | v[1] |
        |                                    |     |      |
 M(v) = | m[0][2]  m[1][2]  m[2][2]  m[3][2] |  X  | v[2] |
        |                                    |     |      |
        \ m[0][3]  m[1][3]  m[2][3]  m[3][3] /     \ v[3] /
 
 v[0] = x
 v[1] = y
 v[2] = z
 v[3] = w or 1
 
   +y
   |
   |
   |_____ +x
   /
  /
 +z
 
 right = +x
 up = +y
 forward = -z

*/

template<class T>
class matrix4_base
{
public:
	// [col][row]
	T m[4][4];

	//
	inline matrix4_base()
	{}

	inline vector3_base<T> get_column3(const int i) const { return vector3_base<T>(m[i][0], m[i][1], m[i][2]); }
	inline vector3_base<T> get_column4(const int i) const { return vector4_base<T>(m[i][0], m[i][1], m[i][2], m[i][3]); }
	inline vector3_base<T> get_row3(const int i) const { return vector3_base<T>(m[0][i], m[1][i], m[2][i]); }
	inline vector4_base<T> get_row4(const int i) const { return vector4_base<T>(m[0][i], m[1][i], m[2][i], m[3][i]); }

	inline vector3_base<T> get_right() const { return get_row3(0); }
	inline vector3_base<T> get_up() const { return get_row3(1); }
	inline vector3_base<T> get_forward() const { return -get_row3(2); }
	inline vector3_base<T> get_translation() const { return get_column3(3); }

	//
	void unit()
	{
		m[0][1] = m[0][2] = m[0][3] = 0;
		m[1][0] = m[1][2] = m[1][3] = 0;
		m[2][0] = m[2][1] = m[2][3] = 0;
		m[3][0] = m[3][1] = m[3][2] = 0;

		m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1;
	}

	//
	vector3_base<T> operator *(const vector3_base<T> &v) const
	{
		vector3_base<T> r(0,0,0);

		r.x += v.x*m[0][0] + v.y*m[1][0] + v.z*m[2][0] + m[3][0];
		r.y += v.x*m[0][1] + v.y*m[1][1] + v.z*m[2][1] + m[3][1];
		r.z += v.x*m[0][2] + v.y*m[1][2] + v.z*m[2][2] + m[3][2];
		return r;
	}

	//
	vector4_base<T> operator *(const vector4_base<T> &v) const
	{
		vector4_base<T> r(0,0,0,0);

		r.x += v.x*m[0][0] + v.y*m[1][0] + v.z*m[2][0] + v.w*m[3][0];
		r.y += v.x*m[0][1] + v.y*m[1][1] + v.z*m[2][1] + v.w*m[3][1];
		r.z += v.x*m[0][2] + v.y*m[1][2] + v.z*m[2][2] + v.w*m[3][2];
		r.w += v.x*m[0][3] + v.y*m[1][3] + v.z*m[2][3] + v.w*m[3][3];
		return r;
	}
	//
	matrix4_base operator *(const matrix4_base &other) const
	{
		matrix4_base r;

		for(int i = 0; i < 4; i++)
			for(int j = 0; j < 4; j++)
			{
				r.m[i][j] = 0;
				for(int a = 0; a < 4; a++)
					r.m[i][j] += m[a][j] * other.m[i][a];
			}
		return r;
	}
	
	
	//
	// THIS PART IS KINDA UGLY BECAUSE MAT4 IS NOT IMMUTABLE
	//
	
	inline void set_row(const vector3_base<T>& v, const int row)
	{
		m[0][row] = v.x;
		m[1][row] = v.y;
		m[2][row] = v.z;
	}

	inline void set_column(const vector3_base<T>& v, const int col)
	{
		m[col][0] = v.x;
		m[col][1] = v.y;
		m[col][2] = v.z;
	}

	inline void set_translation(const vector3_base<T>& v) { set_column(v,3); }	

	//
	void rot_x(T angle)
	{
		T sina = (T)sin(angle);
		T cosa = (T)cos(angle);

		unit();
		m[1][1] = cosa; m[2][1] =-sina;
		m[1][2] = sina; m[2][2] = cosa;
	}

	//
	void rot_y(T angle)
	{
		T sina = (T)sin(-angle);
		T cosa = (T)cos(-angle);

		unit();
		m[0][0] = cosa; m[2][0] =-sina;
		m[0][2] = sina; m[2][2] = cosa;
	}

	//
	void rot_z(T angle)
	{
		T sina = (T)sin(angle);
		T cosa = (T)cos(angle);

		unit();
		m[0][0] = cosa; m[1][0] =-sina;
		m[0][1] = sina; m[1][1] = cosa;
	}
};

typedef matrix4_base<float> mat4;

#endif