Repository navigation
Expand file tree
/
Copy pathG3DMath.cpp
More file actions
195 lines (167 loc) · 4.95 KB
/
Copy pathG3DMath.cpp
File metadata and controls
195 lines (167 loc) · 4.95 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* G3DMath.cpp
*
* Math routines for handling 3D homogeneous coordinates
*/
#include <math.h>
#include <stdint.h>
#include "G3DMath.h"
/************************************************************************/
/* */
/* Matrix Routines */
/* */
/************************************************************************/
/* G3DMatrix::G3DMatrix
*
* Construct. This initializes with setIdentity
*/
G3DMatrix::G3DMatrix()
{
setIdentity();
}
/************************************************************************/
/* */
/* Matrix Creation */
/* */
/************************************************************************/
/* G3DMatrix::setIdentity
*
* Set to identity matrix
*/
void G3DMatrix::setIdentity()
{
for (uint8_t i = 0; i < 4; ++i) {
for (uint8_t j = 0; j < 4; ++j) {
a[i][j] = (i == j) ? 1.0f : 0.0f;
}
}
}
/* G3DMatrix::setTranslate
*
* Create a translation matrix
*/
void G3DMatrix::setTranslate(float x, float y, float z)
{
setIdentity();
a[0][3] = x;
a[1][3] = y;
a[2][3] = z;
}
/* G3DMatrix::setScale
*
* Scale matrix
*/
void G3DMatrix::setScale(float x, float y, float z)
{
setIdentity();
a[0][0] = x;
a[1][1] = y;
a[2][2] = z;
}
/* G3DMatrix::setScale
*
* Scale matrix
*/
void G3DMatrix::setScale(float s)
{
setIdentity();
a[0][0] = s;
a[1][1] = s;
a[2][2] = s;
}
/* G3DMatrix::setRotate
*
* Rotational matrix. Axis is 0 (x), 1 (y) or 2 (z)
*/
void G3DMatrix::setRotate(uint8_t axis, float angle)
{
setIdentity();
float c = cos(angle);
float s = sin(angle);
switch (axis) {
case AXIS_X:
a[1][1] = c;
a[2][2] = c;
a[1][2] = -s;
a[2][1] = s;
break;
case AXIS_Y:
a[0][0] = c;
a[2][2] = c;
a[0][2] = s;
a[2][0] = -s;
break;
case AXIS_Z:
a[0][0] = c;
a[1][1] = c;
a[0][1] = s;
a[1][0] = -s;
break;
default:
return;
}
}
/* G3DMatrix::setPerspective
*
* Set the perspective matrix. See https://github.com/w3woody/clipping
*/
void G3DMatrix::setPerspective(float fov, float near)
{
setIdentity();
a[0][0] = fov;
a[1][1] = fov;
a[2][2] = 0.0f;
a[3][3] = 0.0f;
a[2][3] = -1.0f;
a[3][2] = -near;
}
/************************************************************************/
/* */
/* Matrix Math */
/* */
/************************************************************************/
/* G3DMatrix::multiply
*
* Multiply the provided matrix into this one. This is done via pre-
* multiplication, and allows us to chain transformations. The way this
* is done implies you must start with the last (perspective) matrix first,
* pushing earlier transformations later. This allows you (if you wish)
* to build a push stack of matrices, though in the limited confines of
* an embedded processor that stack cannot be very large...
*/
void G3DMatrix::multiply(const G3DMatrix &m)
{
float tmp[4];
float n;
for (uint8_t i = 0; i < 4; ++i) {
/*
* Comlumn by column multiply and replace.
*/
for (uint8_t j = 0; j < 4; ++j) {
n = 0.0f;
for (uint8_t k = 0; k < 4; ++k) {
n += m.a[k][j] * a[i][k];
}
tmp[j] = n;
}
for (uint8_t j = 0; j < 4; ++j) {
a[i][j] = tmp[j];
}
}
}
/************************************************************************/
/* */
/* Vector multiplication */
/* */
/************************************************************************/
/* G3DVector:multiply
*
* Set this vector to the multiplcation of the provided vector and
* matrix
*/
void G3DVector::multiply(const G3DMatrix &m, const G3DVector &v)
{
x = m.a[0][0] * v.x + m.a[0][1] * v.y + m.a[0][2] * v.z + m.a[0][3] * v.w;
y = m.a[1][0] * v.x + m.a[1][1] * v.y + m.a[1][2] * v.z + m.a[1][3] * v.w;
z = m.a[2][0] * v.x + m.a[2][1] * v.y + m.a[2][2] * v.z + m.a[2][3] * v.w;
w = m.a[3][0] * v.x + m.a[3][1] * v.y + m.a[3][2] * v.z + m.a[3][3] * v.w;
}