-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix4.h
More file actions
134 lines (103 loc) · 3.33 KB
/
Matrix4.h
File metadata and controls
134 lines (103 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//////////////////////////////////////////////////////////////////////
//
// University of Leeds
// COMP 5812M Foundations of Modelling & Rendering
// User Interface for Coursework
//
// September, 2020
//
// ------------------------
// Matrix4.h
// ------------------------
//
// A minimal class for a homogeneous 4x4 matrix
//
///////////////////////////////////////////////////
// include guard
#ifndef MATRIX4_H
#define MATRIX4_H
#include <iostream>
#include "Cartesian3.h"
#include "Homogeneous4.h"
#include "Matrix3.h"
#ifndef M_PI
#define M_PI 3.141592
#endif
#define DEG2RAD(x) (M_PI*(float)(x)/180.0)
// forward declaration
class Matrix4;
// this allows us to get a matrix in the
// column-major form preferred by OpenGL
class columnMajorMatrix
{ // class columnMajorMatrix
public:
float coordinates[16];
}; // class columnMajorMatrix
// the class itself, stored in row-major form
class Matrix4
{ // Matrix4
public:
// the coordinates
float coordinates[4][4];
// constructor - default to the zero matrix
Matrix4();
// constructor from a Matrix3
Matrix4(Matrix3 &other);
// equality operator
bool operator ==(const Matrix4 &other) const;
// indexing - retrieves the beginning of a line
// array indexing will then retrieve an element
float * operator [](const int rowIndex);
// similar routine for const pointers
const float * operator [](const int rowIndex) const;
// scalar operations
// multiplication operator (no division operator)
Matrix4 operator *(float factor) const;
// vector operations on homogeneous coordinates
// multiplication is the only operator we use
Homogeneous4 operator *(const Homogeneous4 &vector) const;
// and on Cartesian coordinates
Cartesian3 operator *(const Cartesian3 &vector) const;
// matrix operations
// addition operator
Matrix4 operator +(const Matrix4 &other) const;
// subtraction operator
Matrix4 operator -(const Matrix4 &other) const;
// multiplication operator
Matrix4 operator *(const Matrix4 &other) const;
// matrix transpose
Matrix4 transpose() const;
// returns a column-major array of 16 values
// for use with OpenGL
columnMajorMatrix columnMajor() const;
// routine that returns a row vector as a Homogeneous4
Homogeneous4 row(int rowNum);
// and similar for a column
Homogeneous4 column(int colNum);
// methods that return particular matrices
static Matrix4 Zero();
// the identity matrix
static Matrix4 Identity();
static Matrix4 Translate(const Cartesian3 &vector);
// rotations around main axes
static Matrix4 RotateX(float degrees);
static Matrix4 RotateY(float degrees);
static Matrix4 RotateZ(float degrees);
static Matrix4 GetRotation(const Cartesian3& vector1, const Cartesian3& vector2);
// routines to retrieve the rotation and translation component
// NOTE: NO ERROR CHECKING: assumes only pure rotation plus translation
Matrix4 GetRotationMatrix();
Cartesian3 GetTranslationVector();
// routine to retrieve a Matrix3x3
Matrix3 GetMatrix3();
// routine to transpose a matrix
Matrix4 Transpose();
}; // Matrix4
// scalar operations
// additional scalar multiplication operator
Matrix4 operator *(float factor, const Matrix4 &matrix);
// stream input
std::istream & operator >> (std::istream &inStream, Matrix4 &value);
// stream output
std::ostream & operator << (std::ostream &outStream, const Matrix4 &value);
#endif