nomlib
Loading...
Searching...
No Matches
Matrix.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#pragma once
30
31#include "export.hpp"
32#include "nomlib/config.hpp"
33// #include "nomlib/math/math_helpers.hpp"
34#include "nomlib/math/math_helpers.hpp"
35
36#include <iostream>
37
38#include "nomlib/math/Vector3.hpp"
39#include "nomlib/math/Vector4.hpp"
40
41class Vector3;
42
43namespace nom {
44
45class Matrix4
46{
47 public:
48 Matrix4(void);
49 Matrix4(float elements[16]);
50 ~Matrix4(void);
51
52 float values[16];
53
54 //Set all matrix values to zero
55 void ToZero();
56 //Sets matrix to identity matrix (1.0 down the diagonal)
57 void ToIdentity();
58
59 //Gets the OpenGL position vector (floats 12,13, and 14)
60 Vector3 GetPositionVector() const;
61 //Sets the OpenGL position vector (floats 12,13, and 14)
62 void SetPositionVector(const Vector3 in);
63
64 //Gets the scale vector (floats 1,5, and 10)
65 Vector3 GetScalingVector() const;
66 //Sets the scale vector (floats 1,5, and 10)
67 void SetScalingVector(const Vector3 &in);
68
69 //Creates a rotation matrix that rotates by 'degrees' around the 'axis'
70 //Analogous to glRotatef
71 static Matrix4 Rotation(float degrees, const Vector3 &axis);
72
73 //Creates a scaling matrix (puts the 'scale' vector down the diagonal)
74 //Analogous to glScalef
75 static Matrix4 Scale(const Vector3 &scale);
76
77 //Creates a translation matrix (identity, with 'translation' vector at
78 //floats 12, 13, and 14. Analogous to glTranslatef
79 static Matrix4 Translation(const Vector3 &translation);
80
81 //Creates a perspective matrix, with 'znear' and 'zfar' as the near and
82 //far planes, using 'aspect' and 'fov' as the aspect ratio and vertical
83 //field of vision, respectively.
84 static Matrix4 Perspective(float znear, float zfar, float aspect, float fov);
85
86 //Creates an orthographic matrix with 'znear' and 'zfar' as the near and
87 //far planes, and so on. Descriptive variable names are a good thing!
88 static Matrix4 Orthographic(float znear, float zfar,float right, float left, float top, float bottom);
89
90 //Builds a view matrix suitable for sending straight to the vertex shader.
91 //Puts the camera at 'from', with 'lookingAt' centered on the screen, with
92 //'up' as the...up axis (pointing towards the top of the screen)
93 static Matrix4 BuildViewMatrix(const Vector3 &from, const Vector3 &lookingAt, const Vector3 up = Vector3(0,1,0));
94
95 Matrix4 GetTransposedRotation();
96
97 //Multiplies 'this' matrix by matrix 'a'. Performs the multiplication in 'OpenGL' order (ie, backwards)
98 inline Matrix4 operator*(const Matrix4 &a) const{
99 Matrix4 out;
100 //Students! You should be able to think up a really easy way of speeding this up...
101 for(unsigned int r = 0; r < 4; ++r) {
102 for(unsigned int c = 0; c < 4; ++c) {
103 out.values[c + (r*4)] = 0.0f;
104 for(unsigned int i = 0; i < 4; ++i) {
105 out.values[c + (r*4)] += this->values[c+(i*4)] * a.values[(r*4)+i];
106 }
107 }
108 }
109 return out;
110 }
111
112 inline Vector3 operator*(const Vector3 &v) const {
113 Vector3 vec;
114
115 float temp;
116
117 vec.x = v.x*values[0] + v.y*values[4] + v.z*values[8] + values[12];
118 vec.y = v.x*values[1] + v.y*values[5] + v.z*values[9] + values[13];
119 vec.z = v.x*values[2] + v.y*values[6] + v.z*values[10] + values[14];
120
121 temp = v.x*values[3] + v.y*values[7] + v.z*values[11] + values[15];
122
123 vec.x = vec.x/temp;
124 vec.y = vec.y/temp;
125 vec.z = vec.z/temp;
126
127 return vec;
128 };
129
130 inline Vector4 operator*(const Vector4 &v) const {
131 return Vector4(
132 v.x*values[0] + v.y*values[4] + v.z*values[8] +v.w * values[12],
133 v.x*values[1] + v.y*values[5] + v.z*values[9] +v.w * values[13],
134 v.x*values[2] + v.y*values[6] + v.z*values[10] +v.w * values[14],
135 v.x*values[3] + v.y*values[7] + v.z*values[11] +v.w * values[15]
136 );
137 };
138
139 //Handy string output for the matrix. Can get a bit messy, but better than nothing!
140 inline friend std::ostream& operator<<(std::ostream& o, const Matrix4& m){
141 o << "Mat4(";
142 o << "\t" << m.values[0] << "," << m.values[1] << "," << m.values[2] << "," << m.values [3] << std::endl;
143 o << "\t\t" << m.values[4] << "," << m.values[5] << "," << m.values[6] << "," << m.values [7] << std::endl;
144 o << "\t\t" << m.values[8] << "," << m.values[9] << "," << m.values[10] << "," << m.values [11] << std::endl;
145 o << "\t\t" << m.values[12] << "," << m.values[13] << "," << m.values[14] << "," << m.values [15] << " )" <<std::endl;
146 return o;
147 }
148 };
149
150} // namespace nom