// // Copyright (c) 2010 Linaro Limited // // All rights reserved. This program and the accompanying materials // are made available under the terms of the MIT License which accompanies // this distribution, and is available at // http://www.opensource.org/licenses/mit-license.php // // Contributors: // Jesse Barker - original implementation. // #ifndef MAT_H_ #define MAT_H_ #include <stdexcept> #include <iostream> #include <iomanip> #include "vec.h" #ifndef USE_EXCEPTIONS // If we're not throwing exceptions, we'll need the logger to make sure the // caller is informed of errors. #include "log.h" #endif // USE_EXCEPTIONS namespace LibMatrix { // Proxy class for providing the functionality of a doubly-dimensioned array // representation of matrices. Each matrix class defines its operator[] // to return an ArrayProxy. The ArrayProxy then returns the appropriate item // from its operator[]. template<typename T, unsigned int dimension> class ArrayProxy { … }; // Programming interfaces to all matrix objects are represented row-centric // (i.e. C/C++ style references to the data appear as matrix[row][column]). // However, the internal data representation is column-major, so when using // the raw data access member to treat the data as a singly-dimensioned array, // it does not have to be transposed. // // A template class for creating, managing and operating on a 2x2 matrix // of any type you like (intended for built-in types, but as long as it // supports the basic arithmetic and assignment operators, any type should // work). template<typename T> class tmat2 { … }; // Multiply a scalar and a matrix just like the member operator, but allow // the scalar to be the left-hand operand. template<typename T> const tmat2<T> operator*(const T& lhs, const tmat2<T>& rhs) { … } // Multiply a copy of a vector and a matrix (matrix is right-hand operand). // Return the copy. template<typename T> const tvec2<T> operator*(const tvec2<T>& lhs, const tmat2<T>& rhs) { … } // Multiply a copy of a vector and a matrix (matrix is left-hand operand). // Return the copy. template<typename T> const tvec2<T> operator*(const tmat2<T>& lhs, const tvec2<T>& rhs) { … } // Compute the outer product of two vectors. Return the resultant matrix. template<typename T> const tmat2<T> outer(const tvec2<T>& a, const tvec2<T>& b) { … } // A template class for creating, managing and operating on a 3x3 matrix // of any type you like (intended for built-in types, but as long as it // supports the basic arithmetic and assignment operators, any type should // work). template<typename T> class tmat3 { … }; // Multiply a scalar and a matrix just like the member operator, but allow // the scalar to be the left-hand operand. template<typename T> const tmat3<T> operator*(const T& lhs, const tmat3<T>& rhs) { … } // Multiply a copy of a vector and a matrix (matrix is right-hand operand). // Return the copy. template<typename T> const tvec3<T> operator*(const tvec3<T>& lhs, const tmat3<T>& rhs) { … } // Multiply a copy of a vector and a matrix (matrix is left-hand operand). // Return the copy. template<typename T> const tvec3<T> operator*(const tmat3<T>& lhs, const tvec3<T>& rhs) { … } // Compute the outer product of two vectors. Return the resultant matrix. template<typename T> const tmat3<T> outer(const tvec3<T>& a, const tvec3<T>& b) { … } // A template class for creating, managing and operating on a 4x4 matrix // of any type you like (intended for built-in types, but as long as it // supports the basic arithmetic and assignment operators, any type should // work). template<typename T> class tmat4 { … }; // Multiply a scalar and a matrix just like the member operator, but allow // the scalar to be the left-hand operand. template<typename T> const tmat4<T> operator*(const T& lhs, const tmat4<T>& rhs) { … } // Multiply a copy of a vector and a matrix (matrix is right-hand operand). // Return the copy. template<typename T> const tvec4<T> operator*(const tvec4<T>& lhs, const tmat4<T>& rhs) { … } // Multiply a copy of a vector and a matrix (matrix is left-hand operand). // Return the copy. template<typename T> const tvec4<T> operator*(const tmat4<T>& lhs, const tvec4<T>& rhs) { … } // Compute the outer product of two vectors. Return the resultant matrix. template<typename T> const tmat4<T> outer(const tvec4<T>& a, const tvec4<T>& b) { … } // // Convenience typedefs. These are here to present a homogeneous view of these // objects with respect to shader source. // mat2; mat3; mat4; dmat2; dmat3; dmat4; imat2; imat3; imat4; umat2; umat3; umat4; bmat2; bmat3; bmat4; namespace Mat4 { // // Some functions to generate transformation matrices that used to be provided // by OpenGL. // mat4 translate(float x, float y, float z); mat4 scale(float x, float y, float z); mat4 rotate(float angle, float x, float y, float z); mat4 frustum(float left, float right, float bottom, float top, float near, float far); mat4 ortho(float left, float right, float bottom, float top, float near, float far); mat4 perspective(float fovy, float aspect, float zNear, float zFar); mat4 lookAt(float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ); } // namespace Mat4 } // namespace LibMatrix #endif // MAT_H_