ClanLib  2.3.7
mat4.h
Go to the documentation of this file.
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2011 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Mark Page
28 ** Harry Storbacka
29 */
30 
33 
34 #pragma once
35 
36 #include "../api_core.h"
37 #include "../System/cl_platform.h"
38 #include "mat2.h"
39 #include "mat3.h"
40 #include "angle.h"
41 
42 template<typename Type>
43 class CL_Mat2;
44 
45 template<typename Type>
46 class CL_Mat3;
47 
48 template<typename Type>
49 class CL_Mat4;
50 
51 template<typename Type>
52 class CL_Vec3;
53 
54 class CL_Angle;
55 
60 template<typename Type>
61 class CL_Mat4
62 {
65 
66 public:
69  {
70  for (int i=0; i<16; i++)
71  matrix[i] = 0;
72  }
73 
75  CL_Mat4(const CL_Mat4<Type> &copy)
76  {
77  for (int i=0; i<16; i++)
78  matrix[i] = copy.matrix[i];
79  }
80 
82  CL_Mat4(const CL_Mat2<Type> &copy);
83 
85  CL_Mat4(const CL_Mat3<Type> &copy);
86 
88  CL_Mat4(const float *init_matrix)
89  {
90  for (int i=0; i<16; i++)
91  matrix[i] = (Type) init_matrix[i];
92  }
93 
95  CL_Mat4(const double *init_matrix)
96  {
97  for (int i=0; i<16; i++)
98  matrix[i] = (Type) init_matrix[i];
99  }
100 
102  CL_Mat4(const cl_byte64 *init_matrix)
103  {
104  for (int i=0; i<16; i++)
105  matrix[i] = (Type) init_matrix[i];
106  }
107 
109  CL_Mat4(const cl_byte32 *init_matrix)
110  {
111  for (int i=0; i<16; i++)
112  matrix[i] = (Type) init_matrix[i];
113  }
114 
116  CL_Mat4(const cl_byte16 *init_matrix)
117  {
118  for (int i=0; i<16; i++)
119  matrix[i] = (Type) init_matrix[i];
120  }
121 
123  CL_Mat4(const cl_byte8 *init_matrix)
124  {
125  for (int i=0; i<16; i++)
126  matrix[i] = (Type) init_matrix[i];
127  }
128 
132  static CL_Mat4<Type> null();
133 
136  static CL_Mat4<Type> identity();
137 
142  static CL_Mat4<Type> frustum(Type left, Type right, Type bottom, Type top, Type z_near, Type z_far);
143 
148  static CL_Mat4<Type> perspective(
149  Type field_of_view_y_degrees,
150  Type aspect,
151  Type z_near,
152  Type z_far);
153 
158  static CL_Mat4<Type> ortho(Type left, Type right, Type bottom, Type top, Type z_near, Type z_far);
159 
164  static CL_Mat4<Type> ortho_2d(Type left, Type right, Type bottom, Type top);
165 
175  static CL_Mat4<Type> rotate(const CL_Angle &angle, Type x, Type y, Type z, bool normalize = true);
176 
182  static CL_Mat4<Type> rotate(const CL_Angle &angle_x, const CL_Angle &angle_y, const CL_Angle &angle_z, CL_EulerOrder order);
183 
190  static CL_Mat4<Type> scale(Type x, Type y, Type z);
191 
199  static CL_Mat4<Type> translate(Type x, Type y, Type z);
200 
214  static CL_Mat4<Type> look_at(
215  Type eye_x, Type eye_y, Type eye_z,
216  Type center_x, Type center_y, Type center_z,
217  Type up_x, Type up_y, Type up_z);
218 
226  static CL_Mat4<Type> multiply(const CL_Mat4<Type> &matrix_1, const CL_Mat4<Type> &matrix_2);
227 
235  static CL_Mat4<Type> add(const CL_Mat4<Type> &matrix_1, const CL_Mat4<Type> &matrix_2);
236 
244  static CL_Mat4<Type> subtract(const CL_Mat4<Type> &matrix_1, const CL_Mat4<Type> &matrix_2);
245 
250  static CL_Mat4<Type> adjoint(const CL_Mat4<Type> &matrix);
251 
257  static CL_Mat4<Type> inverse(const CL_Mat4<Type> &matrix);
258 
264 
268 
269 public:
271  Type matrix[16];
272 
274  Type get_origin_x() const { return matrix[12]; }
275 
277  Type get_origin_y() const { return matrix[13]; }
278 
280  Type get_origin_z() const { return matrix[14]; }
281 
286 
291 
295 
296 public:
304  CL_Mat4<Type> &multiply(const CL_Mat4<Type> &mult);
305 
313  CL_Mat4<Type> &add(const CL_Mat4<Type> &add_matrix);
314 
322  CL_Mat4<Type> &subtract(const CL_Mat4<Type> &sub_matrix);
323 
333  CL_Mat4<Type> &scale_self(Type x, Type y, Type z);
334 
345  CL_Mat4<Type> &translate_self(Type x, Type y, Type z);
346 
350  double det() const;
351 
356 
362 
367 
371 
372 public:
374  operator Type const*() const { return matrix; }
375 
377  operator Type *() { return matrix; }
378 
380  Type &operator[](int i) { return matrix[i]; }
381 
383  const Type &operator[](int i) const { return matrix[i]; }
384 
386  Type &operator[](unsigned int i) { return matrix[i]; }
387 
389  const Type &operator[](unsigned int i) const { return matrix[i]; }
390 
392  CL_Mat4<Type> &operator =(const CL_Mat4<Type> &copy) {memcpy(matrix, copy.matrix, sizeof(matrix)); return *this; }
393 
395  CL_Mat4<Type> &operator =(const CL_Mat3<Type> &copy);
396 
398  CL_Mat4<Type> &operator =(const CL_Mat2<Type> &copy);
399 
401  CL_Mat4<Type> operator *(const CL_Mat4<Type> &mult) const { CL_Mat4<Type> result = mult; result.multiply(*this); return result; }
402 
404  CL_Mat4<Type> operator +(const CL_Mat4<Type> &add_matrix) const { CL_Mat4<Type> result = add_matrix; result.add(*this); return result; }
405 
407  CL_Mat4<Type> operator -(const CL_Mat4<Type> &sub_matrix) const { CL_Mat4<Type> result = sub_matrix; result.subtract(*this); return result; }
408 
410  bool operator==(const CL_Mat4<Type> &other)
411  {
412  for (int i=0; i<16; i++)
413  if (matrix[i] != other.matrix[i]) return false;
414  return true;
415  }
416 
418  bool operator!=(const CL_Mat4<Type> &other) { return !((*this) == other); }
419 
423 
424 private:
426 };
427 
431