forked from Lupus590AtUni/Uni_NetworksAssignment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNA_Matrix.cpp
More file actions
140 lines (125 loc) · 2.81 KB
/
NA_Matrix.cpp
File metadata and controls
140 lines (125 loc) · 2.81 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
135
136
137
138
139
140
#include "NA_Matrix.h"
#include "NA_Vector.h"
#include <iostream>
using std::cout;
NA_Matrix::NA_Matrix(types type, float x, float y, float z) //this may look ugly but using it should be very pleasant to use
{
//start with identity matrix other matrices build off this
for(int i = 0; i<4;i++)
{
for(int j = 0;j<4;j++)
{
if(i==j)
{
matrix[i][j] = 1.0f;
}
else
{
matrix[i][j] = 0.0f;
}
}
}
//defining things in switches needs to be a thing
float cosD;
float sinD;
switch(type)
{
case identity:
//already made
break;
case translate:
matrix[0][3] = x;
matrix[1][3] = y;
matrix[2][3] = z;
break;
case rotateX:
cosD = cosf(x);
sinD = sinf(x);
matrix[1][1] = cosD;
matrix[2][2] = cosD;
matrix[1][2] = -sinD;
matrix[2][1] = sinD;
break;
case rotateY:
cosD = cosf(x);
sinD = sinf(x);
matrix[0][0] = cosD;
matrix[2][2] = cosD;
matrix[0][2] = -sinD;
matrix[2][0] = sinD;
break;
case rotateZ:
cosD = cosf(x);
sinD = sinf(x);
matrix[0][0] = cosD;
matrix[1][1] = cosD;
matrix[1][0] = -sinD;
matrix[0][1] = sinD;
break;
case scale:
matrix[3][3] = 1/x;
break;
case shearX:
matrix[0][0] = x;
break;
case shearY:
matrix[1][1] = x;
break;
case shearZ:
matrix[2][2] = x;
break;
default:
cout<<"NA_Matrix::NA_Matrix - Default used in switch statement\n";
}
}
NA_Matrix::~NA_Matrix(void)
{
}
NA_Matrix NA_Matrix::transpose()
{
NA_Matrix temp;
for(int i = 0; i<4; i++)
{
for(int j = 0; j<4; j++)
{
temp.matrix[i][j] = matrix[j][i];
}
}
return temp;
}
NA_Matrix NA_Matrix::matrixXmatrix(NA_Matrix m)
{
NA_Matrix temp;
for(int i = 0; i<4;i++)
{
for(int j = 0; j<4;j++)
{
float t;
t = m.matrix[j][i]*matrix[i][j]+m.matrix[j][i]*matrix[i][j]+m.matrix[j][i]+matrix[i][j]+m.matrix[j][i]+matrix[i][j];
temp.matrix[i][j] = t;
}
}
temp.correctW();
return temp;
}
NA_Vector NA_Matrix::matrixXvector(NA_Vector v)
{
float tX,tY,tZ,tW;//what a lot of temporary variables
tX = v.x*matrix[0][0]+v.y*matrix[0][1]+v.z*matrix[0][2]+v.w*matrix[0][3];
tY = v.x*matrix[1][0]+v.y*matrix[1][1]+v.z*matrix[1][2]+v.w*matrix[1][3];
tZ = v.x*matrix[2][0]+v.y*matrix[2][1]+v.z*matrix[2][2]+v.w*matrix[2][3];
tW = v.x*matrix[3][0]+v.y*matrix[3][1]+v.z*matrix[3][2]+v.w*matrix[3][3];
NA_Vector temp(tX,tY,tZ,tW);
return temp;
}
void NA_Matrix::correctW()
{
for(int i = 0; i<4;i++)
{
for(int j = 0; j<4;j++)
{
matrix[i][j] = matrix[i][j]/matrix[3][3];
}
}
matrix[3][3] = 1.0f;
}