MatrixAlg.h
1 /*
2  Copyright (C) 2008 Alex Diener
3 
4  This software is provided 'as-is', without any express or implied
5  warranty. In no event will the authors be held liable for any damages
6  arising from the use of this software.
7 
8  Permission is granted to anyone to use this software for any purpose,
9  including commercial applications, and to alter it and redistribute it
10  freely, subject to the following restrictions:
11 
12  1. The origin of this software must not be misrepresented; you must not
13  claim that you wrote the original software. If you use this software
14  in a product, an acknowledgment in the product documentation would be
15  appreciated but is not required.
16  2. Altered source versions must be plainly marked as such, and must not be
17  misrepresented as being the original software.
18  3. This notice may not be removed or altered from any source distribution.
19 
20  Alex Diener adiener@sacredsoftware.net
21 */
22 
23 #ifndef __MATRIX_H__
24 #define __MATRIX_H__
25 
26 #include "Quaternion.h"
27 #include "Vector.h"
28 
29 namespace VoxelFarm
30 {
31  namespace Algebra
32  {
33  //typedef struct Matrix Matrix;
34 
35  struct Vector;
36 
38  struct Matrix
39  {
40  float m[16];
41  };
42 
44  void Matrix_loadIdentity(Matrix * matrix);
47 
49  Matrix Matrix_withValues(float m0, float m4, float m8, float m12,
50  float m1, float m5, float m9, float m13,
51  float m2, float m6, float m10, float m14,
52  float m3, float m7, float m11, float m15);
54  Matrix Matrix_fromDirectionVectors(struct Vector right, struct Vector up, struct Vector front);
55 
57  void Matrix_multiply(Matrix * matrix1, Matrix matrix2);
59  Matrix Matrix_multiplied(Matrix matrix1, Matrix matrix2);
60 
62  void Matrix_translate(Matrix * matrix1, float x, float y, float z);
64  Matrix Matrix_translated(Matrix matrix1, float x, float y, float z);
65 
67  void Matrix_scale(Matrix * matrix, float x, float y, float z);
69  Matrix Matrix_scaled(Matrix matrix, float x, float y, float z);
70 
72  void Matrix_rotate(Matrix * matrix, struct Vector axis, float angle);
74  Matrix Matrix_rotated(Matrix matrix, struct Vector axis, float angle);
75 
77  void Matrix_shearX(Matrix * matrix, float y, float z);
79  Matrix Matrix_shearedX(Matrix matrix, float y, float z);
80 
82  void Matrix_shearY(Matrix * matrix, float x, float z);
84  Matrix Matrix_shearedY(Matrix matrix, float x, float z);
85 
87  void Matrix_shearZ(Matrix * matrix, float x, float y);
89  Matrix Matrix_shearedZ(Matrix matrix, float x, float y);
90 
92  void Matrix_applyPerspective(Matrix * matrix, float fovY, float aspect, float zNear, float zFar);
94  Matrix Matrix_perspective(Matrix matrix, float fovY, float aspect, float zNear, float zFar);
95 
97  void Matrix_transpose(Matrix * matrix);
100 
102  float Matrix_determinant(Matrix matrix);
103 
105  void Matrix_invert(Matrix * matrix);
107  Matrix Matrix_inverted(Matrix matrix);
108 
110  inline struct Vector Matrix_multiplyVector(Matrix matrix, struct Vector vector)
111  {
112  Vector result;
113  result.x = ((matrix.m[0] * vector.x) + (matrix.m[4] * vector.y) + (matrix.m[8] * vector.z) + matrix.m[12]);
114  result.y = ((matrix.m[1] * vector.x) + (matrix.m[5] * vector.y) + (matrix.m[9] * vector.z) + matrix.m[13]);
115  result.z = ((matrix.m[2] * vector.x) + (matrix.m[6] * vector.y) + (matrix.m[10] * vector.z) + matrix.m[14]);
116  return result;
117  }
118  }
119 }
120 
121 #endif
Contains all classes and functions for the VoxelFarm engine.
void Matrix_invert(Matrix *matrix)
Inverts a matrix.
void Matrix_transpose(Matrix *matrix)
Transposes a matrix.
A 3D Vector.
Definition: Vector.h:34
void Matrix_scale(Matrix *matrix, float x, float y, float z)
Scales matrix.
Matrix Matrix_translated(Matrix matrix1, float x, float y, float z)
Returns translated matrix.
Matrix Matrix_scaled(Matrix matrix, float x, float y, float z)
Returns scales matrix.
void Matrix_applyPerspective(Matrix *matrix, float fovY, float aspect, float zNear, float zFar)
Applies perspective transformation to matrix.
void Matrix_shearZ(Matrix *matrix, float x, float y)
Applies a shear transofmration to matrix on Z.
Matrix Matrix_identity()
Returns identity matrix.
void Matrix_shearX(Matrix *matrix, float y, float z)
Applies a shear transofmration to matrix on X.
Matrix Matrix_fromDirectionVectors(struct Vector right, struct Vector up, struct Vector front)
Creates matrix from direction vectors.
struct Vector Matrix_multiplyVector(Matrix matrix, struct Vector vector)
Returns vector from multiplication of matrix to another vector.
Definition: MatrixAlg.h:110
Matrix Matrix_transposed(Matrix matrix)
Returns matrix transpose.
Matrix Matrix_shearedZ(Matrix matrix, float x, float y)
Returns a shear transofmration to matrix on Z.
Matrix Matrix_shearedX(Matrix matrix, float y, float z)
Returns a shear transofmration to matrix on X.
Matrix Matrix_multiplied(Matrix matrix1, Matrix matrix2)
Returns result of matrix multiplication.
float Matrix_determinant(Matrix matrix)
Returns matrix determinant.
A Matrix for 3D operations.
Definition: MatrixAlg.h:38
void Matrix_translate(Matrix *matrix1, float x, float y, float z)
Translates matrix.
Matrix Matrix_rotated(Matrix matrix, struct Vector axis, float angle)
Returns rotated matrix.
Matrix Matrix_inverted(Matrix matrix)
Reurns inverted matrix.
void Matrix_shearY(Matrix *matrix, float x, float z)
Applies a shear transofmration to matrix on Y.
Matrix Matrix_perspective(Matrix matrix, float fovY, float aspect, float zNear, float zFar)
Returns the result of applying perspective transformation to a matrix.
void Matrix_rotate(Matrix *matrix, struct Vector axis, float angle)
Rotates matrix.
void Matrix_multiply(Matrix *matrix1, Matrix matrix2)
Mutiplies matrices.
void Matrix_loadIdentity(Matrix *matrix)
Loads identity matrix.
Matrix Matrix_withValues(float m0, float m4, float m8, float m12, float m1, float m5, float m9, float m13, float m2, float m6, float m10, float m14, float m3, float m7, float m11, float m15)
Initializes matrix with values.
Matrix Matrix_shearedY(Matrix matrix, float x, float z)
Returns a shear transofmration to matrix on Y.