-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.cpp
249 lines (209 loc) · 6.29 KB
/
geometry.cpp
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "geometry.h"
#include <cmath>
#include <new>
#include <vector>
#include "RasterUtil.h"
#include "fwd.h"
#include "shaders.h"
#include "vecs.h"
#pragma region Edge
EDGE::EDGE(EDGE &_edge) {
v1 = new VECTOR_3();
v2 = new VECTOR_3();
*v1 = *(_edge.v1);
*v2 = *(_edge.v2);
}
EDGE::EDGE(VECTOR_3 *_v1, VECTOR_3 *_v2) {
v1 = _v1;
v2 = _v2;
}
EDGE::~EDGE() {
delete v1;
delete v2;
}
void EDGE::Render(RASTER *raster) {
VECTOR_2 t1 = RasterUtil::CoordToScreen(v1, raster);
VECTOR_2 t2 = RasterUtil::CoordToScreen(v2, raster);
RasterUtil::BresenhamAnyDir(raster, t1.x, t1.y, t2.x, t2.y, t1.col, t2.col,
8.0f, 8.0f);
}
VECTOR_3 *EDGE::GetVertex(unsigned int vert) { return vert == 1 ? v1 : v2; }
#pragma endregion
#pragma region Matrix
MATRIX::MATRIX(float *position) {
matrix = new float[16];
Pos(position);
}
MATRIX::~MATRIX() { delete matrix; }
float *MATRIX::MultBy(float *position) {
float *arr = new float[16];
for (unsigned int i = 0; i < 16; i++) {
*(arr + i) = 0;
}
for (unsigned int i = 0; i < 4; i++) {
for (unsigned int j = 0; j < 4; j++) {
for (unsigned int k = 0; k < 4; k++) {
*(arr + i + j * 4) += *(position + i + k * 4) * *(matrix + k + j * 4);
}
}
}
return &arr[0];
}
void MATRIX::Pos(float *position) {
for (unsigned int i = 0; i < 16; i++) {
*(matrix + i) = *(position + i);
}
}
float *MATRIX::GetMatrix() { return matrix; }
void MATRIX::RotateX(MATRIX *_matrix, float rads) {
float arr[4][4] = {{1, 0, 0, 0},
{0, cos(rads), -sin(rads), 0},
{0, sin(rads), cos(rads), 0},
{0, 0, 0, 1}};
float *temp = _matrix->MultBy(&arr[0][0]);
_matrix->Pos(temp);
}
void MATRIX::RotateY(MATRIX *_matrix, float rads) {
float arr[4][4] = {{cos(rads), 0, sin(rads), 0},
{0, 1, 0, 0},
{-sin(rads), 0, cos(rads), 0},
{0, 0, 0, 1}};
float *temp = _matrix->MultBy(&arr[0][0]);
_matrix->Pos(temp);
}
void MATRIX::RotateZ(MATRIX *_matrix, float rads) {
float arr[4][4] = {{cos(rads), -sin(rads), 0, 0},
{sin(rads), cos(rads), 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}};
float *temp = _matrix->MultBy(&arr[0][0]);
_matrix->Pos(temp);
}
void MATRIX::DRotate(unsigned int axis, float degrees) {
(*rotMatrices[axis])(this, degrees * 0.01744f);
}
void MATRIX::Translate(float xOff, float yOff, float zOff) {
float arr[4][4]{
{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {xOff, yOff, zOff, 1}};
Pos(MultBy(&arr[0][0]));
}
void MATRIX::Negate() {
for (unsigned int i = 0; i < 16; i++) {
*(matrix + i) = *(matrix + i) * -1;
}
}
void MATRIX::SetCoord(unsigned int coord, float val) {
*(matrix + 12 + coord) = val;
}
#pragma endregion
#pragma region Mesh
MESH::MESH(MESH &_mesh) {
float IdentityMatrix[4][4]{
{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
localMatrix = new MATRIX(&IdentityMatrix[0][0]);
worldMatrix = new MATRIX(&IdentityMatrix[0][0]);
for (unsigned int i = 0; i < _mesh.edges.size(); i++) {
edges.push_back(new EDGE(*(_mesh.edges[i])));
}
for (unsigned int i = 0; i < _mesh.tris.size(); i++) {
tris.push_back(new TRI(*(_mesh.tris[i])));
}
SetLocalMatrix(_mesh.GetLocalMatrix()->GetMatrix());
SetWorldMatrix(_mesh.GetWorldMatrix()->GetMatrix());
texture = _mesh.texture;
texWidth = _mesh.texWidth;
texHeight = _mesh.texHeight;
}
MESH::MESH(std::vector<EDGE *> &_edges, float *position) {
edges = _edges;
float IdentityMatrix[4][4]{
{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
localMatrix = new MATRIX(&IdentityMatrix[0][0]);
worldMatrix = new MATRIX(position);
}
MESH::~MESH() {
for (int i = 0; i < edges.size(); i++) {
delete edges[i];
}
delete localMatrix;
delete worldMatrix;
}
void MESH::RenderWireframe(RASTER *raster) {
for (unsigned int i = 0; i < edges.size(); i++) {
edges[i]->Render(raster);
}
}
void MESH::RenderFaces(RASTER *raster) {
for (unsigned int i = 0; i < tris.size(); i++) {
tris[i]->Render(raster, texture, texWidth, texHeight);
}
}
void MESH::MultLocalMatrix(MATRIX *_matrix) {
localMatrix->MultBy(_matrix->GetMatrix());
}
void MESH::SetLocalMatrix(float *position) { localMatrix->Pos(position); }
void MESH::SetWorldMatrix(float *position) { worldMatrix->Pos(position); }
void MESH::MultWorldMatrix(MATRIX *_matrix) {
worldMatrix->MultBy(_matrix->GetMatrix());
}
MATRIX *MESH::GetWorldMatrix() { return worldMatrix; }
MATRIX *MESH::GetLocalMatrix() { return localMatrix; }
std::vector<EDGE *> MESH::GetEdges() { return edges; }
std::vector<TRI *> MESH::GetTris() { return tris; }
void MESH::TrisFromQuad(VECTOR_3 *_v1, VECTOR_3 *_v2, VECTOR_3 *_v3,
VECTOR_3 *_v4, unsigned int col) {
// _v1 and _v4 represent non-adjacent vertices.
TRI *tri1 = new TRI(_v1, _v2, _v3, col);
TRI *tri2 = new TRI(_v2, _v3, _v4, col);
tris.push_back(tri1);
tris.push_back(tri2);
}
void MESH::SetTexture(const unsigned int *_texture, unsigned int _width,
unsigned int _height) {
texture = _texture;
texWidth = _width;
texHeight = _height;
}
#pragma endregion
#pragma region Triangle
TRI::TRI(VECTOR_3 *_v1, VECTOR_3 *_v2, VECTOR_3 *_v3, unsigned int _col) {
v1 = _v1;
v2 = _v2;
v3 = _v3;
col = _col;
}
TRI::TRI(TRI &_tri) {
v1 = new VECTOR_3();
v2 = new VECTOR_3();
v3 = new VECTOR_3();
*v1 = *(_tri.v1);
*v2 = *(_tri.v2);
*v3 = *(_tri.v3);
col = _tri.col;
}
TRI::~TRI() {}
void TRI::Render(RASTER *raster) {
VECTOR_2 a = RasterUtil::CoordToScreen(v1, raster);
VECTOR_2 b = RasterUtil::CoordToScreen(v2, raster);
VECTOR_2 c = RasterUtil::CoordToScreen(v3, raster);
RasterUtil::DrawTriangleCol(a, b, c, raster, col);
}
void TRI::Render(RASTER *raster, const unsigned int *texture,
unsigned int texWidth, unsigned int texHeight) {
VECTOR_2 a = RasterUtil::CoordToScreen(v1, raster);
VECTOR_2 b = RasterUtil::CoordToScreen(v2, raster);
VECTOR_2 c = RasterUtil::CoordToScreen(v3, raster);
RasterUtil::DrawTriangleTex(a, b, c, raster, texture, texWidth, texHeight);
}
VECTOR_3 *TRI::GetVertex(unsigned int vert) {
if (vert == 1) {
return v1;
}
if (vert == 2) {
return v2;
}
if (vert == 3) {
return v3;
}
}
#pragma endregion