mat4.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 **    Mark Page
00028 **    Harry Storbacka
00029 */
00030 
00033 
00034 #pragma once
00035 
00036 #include "../api_core.h"
00037 #include "../System/cl_platform.h"
00038 #include "mat2.h"
00039 #include "mat3.h"
00040 #include "angle.h"
00041 
00042 template<typename Type>
00043 class CL_Mat2;
00044 
00045 template<typename Type>
00046 class CL_Mat3;
00047 
00048 template<typename Type>
00049 class CL_Mat4;
00050 
00051 template<typename Type>
00052 class CL_Vec3;
00053 
00054 class CL_Angle;
00055 
00060 template<typename Type>
00061 class CL_Mat4
00062 {
00065 
00066 public:
00068         CL_Mat4()
00069         {
00070                 for (int i=0; i<16; i++)
00071                         matrix[i] = 0;
00072         }
00073 
00075         CL_Mat4(const CL_Mat4<Type> &copy)
00076         {
00077                 for (int i=0; i<16; i++)
00078                         matrix[i] = copy.matrix[i];
00079         }
00080 
00082         CL_Mat4(const CL_Mat2<Type> &copy);
00083 
00085         CL_Mat4(const CL_Mat3<Type> &copy);
00086 
00088         CL_Mat4(const float *init_matrix)
00089         {
00090                 for (int i=0; i<16; i++)
00091                         matrix[i] = (Type) init_matrix[i];
00092         }
00093 
00095         CL_Mat4(const double *init_matrix)
00096         {
00097                 for (int i=0; i<16; i++)
00098                         matrix[i] = (Type) init_matrix[i];
00099         }
00100 
00102         CL_Mat4(const cl_byte64 *init_matrix)
00103         {
00104                 for (int i=0; i<16; i++)
00105                         matrix[i] = (Type) init_matrix[i];
00106         }
00107 
00109         CL_Mat4(const cl_byte32 *init_matrix)
00110         {
00111                 for (int i=0; i<16; i++)
00112                         matrix[i] = (Type) init_matrix[i];
00113         }
00114 
00116         CL_Mat4(const cl_byte16 *init_matrix)
00117         {
00118                 for (int i=0; i<16; i++)
00119                         matrix[i] = (Type) init_matrix[i];
00120         }
00121 
00123         CL_Mat4(const cl_byte8 *init_matrix)
00124         {
00125                 for (int i=0; i<16; i++)
00126                         matrix[i] = (Type) init_matrix[i];
00127         }
00128 
00132         static CL_Mat4<Type> null();
00133 
00136         static CL_Mat4<Type> identity();
00137 
00142         static CL_Mat4<Type> frustum(Type left, Type right, Type bottom, Type top, Type z_near, Type z_far);
00143 
00148         static CL_Mat4<Type> perspective(
00149                 Type field_of_view_y_degrees,
00150                 Type aspect,
00151                 Type z_near,
00152                 Type z_far);
00153 
00158         static CL_Mat4<Type> ortho(Type left, Type right, Type bottom, Type top, Type z_near, Type z_far);
00159 
00164         static CL_Mat4<Type> ortho_2d(Type left, Type right, Type bottom, Type top);
00165 
00175         static CL_Mat4<Type> rotate(const CL_Angle &angle, Type x, Type y, Type z, bool normalize = true);
00176 
00182         static CL_Mat4<Type> rotate(const CL_Angle &angle_x, const CL_Angle &angle_y, const CL_Angle &angle_z, CL_EulerOrder order);
00183 
00190         static CL_Mat4<Type> scale(Type x, Type y, Type z);
00191 
00199         static CL_Mat4<Type> translate(Type x, Type y, Type z);
00200 
00214         static CL_Mat4<Type> look_at(
00215                 Type eye_x, Type eye_y, Type eye_z,
00216                 Type center_x, Type center_y, Type center_z,
00217                 Type up_x, Type up_y, Type up_z);
00218 
00226         static CL_Mat4<Type> multiply(const CL_Mat4<Type> &matrix_1, const CL_Mat4<Type> &matrix_2);
00227 
00235         static CL_Mat4<Type> add(const CL_Mat4<Type> &matrix_1, const CL_Mat4<Type> &matrix_2);
00236 
00244         static CL_Mat4<Type> subtract(const CL_Mat4<Type> &matrix_1, const CL_Mat4<Type> &matrix_2);
00245 
00250         static CL_Mat4<Type> adjoint(const CL_Mat4<Type> &matrix);
00251 
00257         static CL_Mat4<Type> inverse(const CL_Mat4<Type> &matrix);
00258 
00263         static CL_Mat4<Type> transpose(const CL_Mat4<Type> &matrix);
00264 
00268 
00269 public:
00271         Type matrix[16];
00272 
00274         Type get_origin_x() const { return matrix[12]; }
00275 
00277         Type get_origin_y() const { return matrix[13]; }
00278 
00280         Type get_origin_z() const { return matrix[14]; }
00281 
00285         CL_Vec3<Type> get_euler(CL_EulerOrder order) const;
00286 
00290         CL_Vec3<Type> get_transformed_point(const CL_Vec3<Type> &vector) const;
00291 
00295 
00296 public:
00304         CL_Mat4<Type> &multiply(const CL_Mat4<Type> &mult);
00305 
00313         CL_Mat4<Type> &add(const CL_Mat4<Type> &add_matrix);
00314 
00322         CL_Mat4<Type> &subtract(const CL_Mat4<Type> &sub_matrix);
00323 
00333         CL_Mat4<Type> &scale_self(Type x, Type y, Type z);
00334 
00345         CL_Mat4<Type> &translate_self(Type x, Type y, Type z);
00346 
00350         double det() const;
00351 
00355         CL_Mat4<Type> &adjoint();
00356 
00361         CL_Mat4<Type> &inverse();
00362 
00366         CL_Mat4<Type> &transpose();
00367 
00371 
00372 public:
00374         operator Type const*() const { return matrix; }
00375 
00377         operator Type *() { return matrix; }
00378 
00380         Type &operator[](int i) { return matrix[i]; }
00381 
00383         const Type &operator[](int i) const { return matrix[i]; }
00384 
00386         Type &operator[](unsigned int i) { return matrix[i]; }
00387 
00389         const Type &operator[](unsigned int i) const { return matrix[i]; }
00390 
00392         CL_Mat4<Type> &operator =(const CL_Mat4<Type> &copy) {memcpy(matrix, copy.matrix, sizeof(matrix)); return *this; }
00393 
00395         CL_Mat4<Type> &operator =(const CL_Mat3<Type> &copy);
00396 
00398         CL_Mat4<Type> &operator =(const CL_Mat2<Type> &copy);
00399 
00401         CL_Mat4<Type> operator *(const CL_Mat4<Type> &mult) const { CL_Mat4<Type> result = mult; result.multiply(*this); return result; }
00402 
00404         CL_Mat4<Type> operator +(const CL_Mat4<Type> &add_matrix) const { CL_Mat4<Type> result = add_matrix; result.add(*this); return result; }
00405 
00407         CL_Mat4<Type> operator -(const CL_Mat4<Type> &sub_matrix) const { CL_Mat4<Type> result = sub_matrix; result.subtract(*this); return result; }
00408 
00410         bool operator==(const CL_Mat4<Type> &other)
00411         {
00412                 for (int i=0; i<16; i++)
00413                         if (matrix[i] != other.matrix[i]) return false;
00414                 return true;
00415         }
00416 
00418         bool operator!=(const CL_Mat4<Type> &other) { return !((*this) == other); }
00419 
00423 
00424 private:
00426 };
00427 
00428 typedef CL_Mat4<int> CL_Mat4i;
00429 typedef CL_Mat4<float> CL_Mat4f;
00430 typedef CL_Mat4<double> CL_Mat4d;
00431