matrix.hpp

Go to the documentation of this file.
00001 /*
00002     RenderStack  Support library for OpenGL 3+
00003     Copyright (C) 2010  Timo Suoranta
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2.1 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 */
00019 
00027 #ifndef MATRIX_HPP
00028 #define MATRIX_HPP
00029 
00030 #include "renderstack/rs.h"
00031 #include "renderstack/vec3.hpp"
00032 #include "renderstack/vec4.hpp"
00033 #include <exception>
00034 
00035 namespace renderstack {
00036 
00037 class matrix_not_invertible_exception : public std::exception
00038 {
00039 };
00040 
00041 class quaternion;
00042 
00044 class matrix
00045 {
00046 public:
00047     matrix(){}
00048     matrix(matrix const &other)
00049     {
00050         m[0][0] = other.m[0][0]; m[1][0] = other.m[1][0]; m[2][0] = other.m[2][0]; m[3][0] = other.m[3][0];
00051         m[0][1] = other.m[0][1]; m[1][1] = other.m[1][1]; m[2][1] = other.m[2][1]; m[3][1] = other.m[3][1];
00052         m[0][2] = other.m[0][2]; m[1][2] = other.m[1][2]; m[2][2] = other.m[2][2]; m[3][2] = other.m[3][2];
00053         m[0][3] = other.m[0][3]; m[1][3] = other.m[1][3]; m[2][3] = other.m[2][3]; m[3][3] = other.m[3][3];
00054     }
00055     explicit matrix(quaternion const &q);
00056 
00057     matrix &operator=(matrix const &other)
00058     {
00059         m[0][0] = other.m[0][0]; m[1][0] = other.m[1][0]; m[2][0] = other.m[2][0]; m[3][0] = other.m[3][0];
00060         m[0][1] = other.m[0][1]; m[1][1] = other.m[1][1]; m[2][1] = other.m[2][1]; m[3][1] = other.m[3][1];
00061         m[0][2] = other.m[0][2]; m[1][2] = other.m[1][2]; m[2][2] = other.m[2][2]; m[3][2] = other.m[3][2];
00062         m[0][3] = other.m[0][3]; m[1][3] = other.m[1][3]; m[2][3] = other.m[2][3]; m[3][3] = other.m[3][3];
00063 
00064         return *this;
00065     }
00066 
00067     matrix(
00068         float m00, float m10, float m20, float m30,
00069         float m01, float m11, float m21, float m31,
00070         float m02, float m12, float m22, float m32,
00071         float m03, float m13, float m23, float m33
00072     )
00073     {
00074         m[0][0] = m00; m[1][0] = m10; m[2][0] = m20; m[3][0] = m30;
00075         m[0][1] = m01; m[1][1] = m11; m[2][1] = m21; m[3][1] = m31;
00076         m[0][2] = m02; m[1][2] = m12; m[2][2] = m22; m[3][2] = m32;
00077         m[0][3] = m03; m[1][3] = m13; m[2][3] = m23; m[3][3] = m33;
00078     }
00079     matrix(
00080         vec4 const &row0,
00081         vec4 const &row1,
00082         vec4 const &row2,
00083         vec4 const &row3
00084     )
00085     {
00086         m[0][0] = row0.x; m[1][0] = row0.y; m[2][0] = row0.z; m[3][0] = row0.w;
00087         m[0][1] = row1.x; m[1][1] = row1.y; m[2][1] = row1.z; m[3][1] = row1.w;
00088         m[0][2] = row2.x; m[1][2] = row2.y; m[2][2] = row2.z; m[3][2] = row2.w;
00089         m[0][3] = row3.x; m[1][3] = row3.y; m[2][3] = row3.z; m[3][3] = row3.w;
00090     }
00091 
00092     vec3    transform_vector            (vec3 const &v) const;
00093     vec4    transform_vector            (vec4 const &v) const;
00094     vec3    uniform_transform_direction (vec3 const &v) const;
00095 
00096     matrix  operator*                   (matrix const &other) const;
00097     matrix  &operator*=                 (matrix const &other) { return *this = *this * other; }
00098 
00099     vec3    column3                     (int column) const;
00100 
00101     vec4    column                      (int column) const;
00102     vec4    row                         (int row) const;
00103 
00104     void    set_row                     (int row, vec4 const &v);
00105     void    set_column                  (int col, vec4 const &v);
00106 
00107 public:
00108     float m[4][4];
00109 
00110 public:
00111     static matrix identity()
00112     {
00113         return matrix(
00114             1.0f, 0.0f, 0.0f, 0.0f,
00115             0.0f, 1.0f, 0.0f, 0.0f,
00116             0.0f, 0.0f, 1.0f, 0.0f,
00117             0.0f, 0.0f, 0.0f, 1.0f
00118         );
00119     };
00120 };
00121 
00122 void    make_matrix_quaternion_xyz           (matrix &m, matrix &mi, quaternion const &q, vec3 const &xyz);
00123 void    make_matrix_quaternion_xyz_scale     (matrix &m, matrix &mi, quaternion const &q, vec3 const &xyz, vec3 const &scaling);
00124 
00125 matrix  transpose                            (matrix const &m);
00126 matrix  inverse                              (matrix const &m);
00127 
00128 matrix  make_matrix_frustum_centered         (float width, float height, float nearv, float farv);
00129 matrix  make_matrix_frustum                  (float left, float right, float bottom, float top, float nearv, float farv);
00130 matrix  make_matrix_perspective_vertical     (float fovy, float aspect, float near_val, float far_val);
00131 matrix  make_matrix_perspective_horizontal   (float fovx, float aspect, float near_val, float far_val);
00132 matrix  make_matrix_orthogonal_centered      (float width, float height, float near_val, float far_val);
00133 matrix  make_matrix_orthogonal               (float left, float right, float bottom, float top, float near_val, float far_val);
00134 
00135 matrix  make_matrix_translate                (vec3 const &translation);
00136 matrix  make_matrix_scale                    (vec3 const &scaling);
00137 matrix  make_matrix_translate_scale          (vec3 const &translation, vec3 const &scaling);
00138 
00139 void    make_matrix_hpb_xyz                  (matrix &m, matrix &mi, vec3 const &hpb, vec3 const &translation);
00140 void    make_matrix_hpb_xyz_scale            (matrix &m, matrix &mi, vec3 const &hpb, vec3 const &translation, vec3 const &scaling);
00141 
00142 }
00143 
00144 #endif
00145 
Generated on Sun Apr 11 12:23:08 2010 for RenderStack by  doxygen 1.6.3