nomlib
Loading...
Searching...
No Matches
Matrix4.h
1/******************************************************************************
2Class:Matrix4
3Implements:
4Author:Rich Davison
5Description:VERY simple 4 by 4 matrix class. Students are encouraged to modify
6this as necessary! Overloading the [] operator to allow acces to the values
7array in a neater way might be a good start, as the floats that make the matrix
8up are currently public.
9
10-_-_-_-_-_-_-_,------,
11_-_-_-_-_-_-_-| /\_/\ NYANYANYAN
12-_-_-_-_-_-_-~|__( ^ .^) /
13_-_-_-_-_-_-_-"" ""
14
15*/
16#pragma once
17
18#include "export.hpp"
19#include "nomlib/math/math_helpers.hpp"
20//#include "common.h"
21#include <iostream>
22#include "Vector3.h"
23#include "Vector4.h"
24
25class Vector3;
26
27namespace nom {
28
29class NOM_EXPORT Matrix4 {
30public:
31 Matrix4(void);
32 Matrix4(float elements[16]);
33 ~Matrix4(void);
34
35 float values[16];
36
37 //Set all matrix values to zero
38 void ToZero();
39 //Sets matrix to identity matrix (1.0 down the diagonal)
40 void ToIdentity();
41
42 //Gets the OpenGL position vector (floats 12,13, and 14)
43 Vector3 GetPositionVector() const;
44 //Sets the OpenGL position vector (floats 12,13, and 14)
45 void SetPositionVector(const Vector3 in);
46
47 //Gets the scale vector (floats 1,5, and 10)
48 Vector3 GetScalingVector() const;
49 //Sets the scale vector (floats 1,5, and 10)
50 void SetScalingVector(const Vector3 &in);
51
52 //Creates a rotation matrix that rotates by 'degrees' around the 'axis'
53 //Analogous to glRotatef
54 static Matrix4 Rotation(float degrees, const Vector3 &axis);
55
56 //Creates a scaling matrix (puts the 'scale' vector down the diagonal)
57 //Analogous to glScalef
58 static Matrix4 Scale(const Vector3 &scale);
59
60 //Creates a translation matrix (identity, with 'translation' vector at
61 //floats 12, 13, and 14. Analogous to glTranslatef
62 static Matrix4 Translation(const Vector3 &translation);
63
64 //Creates a perspective matrix, with 'znear' and 'zfar' as the near and
65 //far planes, using 'aspect' and 'fov' as the aspect ratio and vertical
66 //field of vision, respectively.
67 static Matrix4 Perspective(float znear, float zfar, float aspect, float fov);
68
69 //Creates an orthographic matrix with 'znear' and 'zfar' as the near and
70 //far planes, and so on. Descriptive variable names are a good thing!
71 static Matrix4 Orthographic(float znear, float zfar,float right, float left, float top, float bottom);
72
73 //Builds a view matrix suitable for sending straight to the vertex shader.
74 //Puts the camera at 'from', with 'lookingAt' centered on the screen, with
75 //'up' as the...up axis (pointing towards the top of the screen)
76 static Matrix4 BuildViewMatrix(const Vector3 &from, const Vector3 &lookingAt, const Vector3 up = Vector3(0,1,0));
77
78 Matrix4 GetTransposedRotation();
79
80 //Multiplies 'this' matrix by matrix 'a'. Performs the multiplication in 'OpenGL' order (ie, backwards)
81 inline Matrix4 operator*(const Matrix4 &a) const{
82 Matrix4 out;
83 //Students! You should be able to think up a really easy way of speeding this up...
84 for(unsigned int r = 0; r < 4; ++r) {
85 for(unsigned int c = 0; c < 4; ++c) {
86 out.values[c + (r*4)] = 0.0f;
87 for(unsigned int i = 0; i < 4; ++i) {
88 out.values[c + (r*4)] += this->values[c+(i*4)] * a.values[(r*4)+i];
89 }
90 }
91 }
92 return out;
93 }
94
95 inline Vector3 operator*(const Vector3 &v) const {
96 Vector3 vec;
97
98 float temp;
99
100 vec.x = v.x*values[0] + v.y*values[4] + v.z*values[8] + values[12];
101 vec.y = v.x*values[1] + v.y*values[5] + v.z*values[9] + values[13];
102 vec.z = v.x*values[2] + v.y*values[6] + v.z*values[10] + values[14];
103
104 temp = v.x*values[3] + v.y*values[7] + v.z*values[11] + values[15];
105
106 vec.x = vec.x/temp;
107 vec.y = vec.y/temp;
108 vec.z = vec.z/temp;
109
110 return vec;
111 };
112
113 inline Vector4 operator*(const Vector4 &v) const {
114 return Vector4(
115 v.x*values[0] + v.y*values[4] + v.z*values[8] +v.w * values[12],
116 v.x*values[1] + v.y*values[5] + v.z*values[9] +v.w * values[13],
117 v.x*values[2] + v.y*values[6] + v.z*values[10] +v.w * values[14],
118 v.x*values[3] + v.y*values[7] + v.z*values[11] +v.w * values[15]
119 );
120 };
121
122 //Handy string output for the matrix. Can get a bit messy, but better than nothing!
123 inline friend std::ostream& operator<<(std::ostream& o, const Matrix4& m){
124 o << "Mat4(";
125 o << "\t" << m.values[0] << "," << m.values[1] << "," << m.values[2] << "," << m.values [3] << std::endl;
126 o << "\t\t" << m.values[4] << "," << m.values[5] << "," << m.values[6] << "," << m.values [7] << std::endl;
127 o << "\t\t" << m.values[8] << "," << m.values[9] << "," << m.values[10] << "," << m.values [11] << std::endl;
128 o << "\t\t" << m.values[12] << "," << m.values[13] << "," << m.values[14] << "," << m.values [15] << " )" <<std::endl;
129 return o;
130 }
131};
132
133} // namespace nom
134