-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3d_figures_drawing_engine.cpp
More file actions
367 lines (289 loc) · 14.7 KB
/
Copy path3d_figures_drawing_engine.cpp
File metadata and controls
367 lines (289 loc) · 14.7 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//
// Created by Kobe De Broeck on 3-4-2022.
//
#include "3d_figures_drawing_engine.h"
#include "l_parser.h"
#include <stack>
#include <fstream>
#include <vector>
#include <algorithm>
#include <list>
#include <cassert>
#include <memory>
#include "Lights.h"
#include "3d_lsystems.h"
#include "3d_fractals.h"
#include "3d_basic_figures.h"
#include "clipping.h"
#include "line_drawing_utils.h"
#include "z_buffering.h"
#include "perspective.h"
#include "Figure.h"
using namespace std;
namespace KDBRenderUtils {
struct WireFrameImageInfo {
unsigned imageSize{};
vector<Line2D> lines{};
NormColor backgroundColor{};
};
Figure readFigure(const ini::Section& figConf, bool triangulate)
{
Figure fig {};
string type = figConf["type"];
if (type == "LineDrawing") {
int nrPoints = figConf["nrPoints"];
int nrLines = figConf["nrLines"];
vector<Vector3D> vertices;
vector<Face> faces;
for (int j = 0; j < nrPoints; j++) {
auto point = figConf["point" + to_string(j)].as_double_tuple_or_die();
Vector3D v = toPoint3D(point);
vertices.push_back(v);
}
for (int j = 0; j < nrLines; j++) {
auto line = figConf["line" + to_string(j)].as_int_tuple_or_die();
if (line.size() != 2) cerr << "ERROR: line vector not of size 2\n";
faces.push_back({{line[0], line[1]}});
}
fig = {vertices, faces, {}};
}
if(type == "Cube") {
fig = createCube({});
}
if(type == "FractalCube") {
int nr_iterations = figConf["nrIterations"].as_int_or_die();
double fractal_scale = figConf["fractalScale"].as_double_or_die();
generateFractal(createCube({}), fig, nr_iterations, fractal_scale);
}
if(type == "Tetrahedron") {
fig = createTetrahedron({});
}
if(type == "FractalTetrahedron") {
int nr_iterations = figConf["nrIterations"].as_int_or_die();
double fractal_scale = figConf["fractalScale"].as_double_or_die();
generateFractal(createTetrahedron({}), fig, nr_iterations, fractal_scale);
}
if(type == "Octahedron") {
fig = createOctahedron({});
}
if(type == "FractalOctahedron") {
int nr_iterations = figConf["nrIterations"].as_int_or_die();
double fractal_scale = figConf["fractalScale"].as_double_or_die();
generateFractal(createOctahedron({}), fig, nr_iterations, fractal_scale);
}
if(type == "Icosahedron") {
fig = createIcosahedron({});
}
if(type == "FractalIcosahedron") {
int nr_iterations = figConf["nrIterations"].as_int_or_die();
double fractal_scale = figConf["fractalScale"].as_double_or_die();
generateFractal(createIcosahedron({}), fig, nr_iterations, fractal_scale);
}
if(type == "Dodecahedron") {
fig = createDodecahedron({});
}
if(type == "FractalDodecahedron") {
int nr_iterations = figConf["nrIterations"].as_int_or_die();
double fractal_scale = figConf["fractalScale"].as_double_or_die();
generateFractal(createDodecahedron({}), fig, nr_iterations, fractal_scale);
}
if(type == "Sphere") {
int n = figConf["n"].as_int_or_default(0);
fig = createSphere(1,n,{});
}
if(type == "Cone") {
int n = figConf["n"].as_int_or_default(3);
double height = figConf["height"].as_double_or_default(1);
fig = createCone(n, height, {});
}
if(type == "Cylinder") {
int n = figConf["n"].as_int_or_default(3);
double height = figConf["height"].as_double_or_default(1);
fig = createCylinder(n, height, {});
}
if(type == "Torus") {
double r = figConf["r"].as_double_or_default(1);
double R = figConf["R"].as_double_or_default(1);
int n = figConf["n"].as_int_or_default(3);
int m = figConf["m"].as_int_or_default(3);
fig = createTorus(r, R, n, m, {});
}
if(type == "MengerSponge") {
int nr_iterations = figConf["nrIterations"].as_int_or_die();
//fig = createMengerSponge(nr_iterations, {});
fig = {};
}
if(type == "3DLSystem") {
string inputFile = figConf["inputfile"].as_string_or_die();
fig = generate3DLSystemFigure(inputFile);
}
if(triangulate)
fig.triangulate();
double scale = figConf["scale"];
applyTransformation(fig, scaling(scale));
double rotateX = figConf["rotateX"];
applyTransformation(fig, rotationX(rotateX));
double rotateY = figConf["rotateY"];
applyTransformation(fig, rotationY(rotateY));
double rotateZ = figConf["rotateZ"];
applyTransformation(fig, rotationZ(rotateZ));
const auto& figCenter = figConf["center"].as_double_tuple_or_default({0,0,0});
applyTransformation(fig, translation(toPoint3D(figCenter)));
NormColor figureColor{};
if(figConf["color"].exists()) {
figureColor = toNormColor(figConf["color"]);
}
fig.color = figureColor;
fig.ambientReflection = figConf["color"].exists()? figureColor : toNormColor(figConf["ambientReflection"].as_double_tuple_or_die());
fig.diffuseReflection = toNormColor(figConf["diffuseReflection"].as_double_tuple_or_default({0,0,0}));
fig.specularReflection = toNormColor(figConf["specularReflection"].as_double_tuple_or_default({0,0,0}));
fig.reflectionCoefficient = figConf["reflectionCoefficient"].as_double_or_default(0);
return fig;
}
WireFrameImageInfo getWireFrameImageInfo(const ini::Configuration& configuration)
{
unsigned size = configuration["General"]["size"].as_int_or_default(0);
auto backgroundColorVec = configuration["General"]["backgroundcolor"].as_double_tuple_or_default({0,0,0});
unsigned nrFigures = configuration["General"]["nrFigures"].as_int_or_die();
auto eye = configuration["General"]["eye"].as_double_tuple_or_default({0,0,0});
vector<Figure> figures;
for(int i = 0; i < nrFigures; i++) {
string figName = "Figure" + to_string(i);
const auto &figConf = configuration[figName];
string type = configuration["General"]["type"]; // TODO: in een wireframe functie zal er mss toch nooit triangulatie gebeuren?
figures.push_back(readFigure(figConf, type == "ZBuffering"));
}
PolarCoord polarEye = toPolar(Vector3D::point(eye[0],eye[1],eye[2]));
auto lines = toPerspectiveProjectedLines(figures, polarEye, true);
return {size, lines, toNormColor(backgroundColorVec)};
}
img::EasyImage drawWireFrame(const ini::Configuration& configuration)
{
if(configuration["General"]["type"].as_string_or_default("") != "Wireframe") {
cerr << "drawWireFrame didn't get a Wireframe config!\n";
return {};
}
WireFrameImageInfo info = getWireFrameImageInfo(configuration);
return draw2DLines(info.lines, info.imageSize, info.backgroundColor);
}
img::EasyImage drawZBufferedWireFrame(const ini::Configuration& configuration)
{
if(configuration["General"]["type"].as_string_or_default("") != "ZBufferedWireframe") {
cerr << "drawWireFrame didn't get a ZBufferedWireframe config!\n";
return {};
}
WireFrameImageInfo info = getWireFrameImageInfo(configuration);
ZBuffer zBuffer(info.imageSize, info.imageSize);
return draw2DLines(zBuffer, info.lines, info.imageSize, info.backgroundColor);
}
img::EasyImage drawZBufferedTrianglesImage(const ini::Configuration& configuration)
{
if(configuration["General"]["type"].as_string_or_default("") != "ZBuffering" && configuration["General"]["type"].as_string_or_default("") != "LightedZBuffering" ) {
cerr << "drawZBufferedTrianglesImage didn't get a ZBuffering config!\n";
return {};
}
unsigned size = configuration["General"]["size"].as_int_or_default(0);
auto backgroundColorVec = configuration["General"]["backgroundcolor"].as_double_tuple_or_default({0,0,0});
unsigned nrFigures = configuration["General"]["nrFigures"].as_int_or_die();
auto eye = configuration["General"]["eye"].as_double_tuple_or_default({0,0,0});
PolarCoord polarEye = toPolar(Vector3D::point(eye[0],eye[1],eye[2]));
auto viewDirection = configuration["General"]["viewDirection"].as_double_tuple_or_default({-eye[0], -eye[1], -eye[2]});
auto polarMinusViewDir = toPolar(-Vector3D::point(viewDirection[0],viewDirection[1],viewDirection[2]));
PolarCoord polarForEyeCoordTransform = {polarMinusViewDir.theta, polarMinusViewDir.phi, polarEye.r};
// Lights
vector<AmbientLight> ambientLights;
vector<InfLight> infLights;
vector<unique_ptr<PointLight>> pointLights;
int nrLights = configuration["General"]["nrLights"].as_int_or_default(0);
if( nrLights == 0 ) {
ambientLights.push_back(AmbientLight{NormColor{1,1,1}});
}
for(int i = 0; i < nrLights; i++) {
const auto& lightConfig = configuration["Light"+to_string(i)];
NormColor ambientIntensity = toNormColor(lightConfig["ambientLight"].as_double_tuple_or_default({0,0,0}));
NormColor diffuseIntensity = toNormColor(lightConfig["diffuseLight"].as_double_tuple_or_default({0,0,0}));
NormColor specularIntensity = toNormColor(lightConfig["specularLight"].as_double_tuple_or_default({0,0,0}));
bool isInfinity = lightConfig["infinity"].as_bool_or_default(false);
if(isInfinity) {
Vector3D direction = toVector3D(lightConfig["direction"].as_double_tuple_or_die());
//Vector3D direction = toPoint3D(lightConfig["direction"].as_double_tuple_or_die()); // WRONG AF
direction = Vector3D::normalise(direction * eyePointTransformation(polarForEyeCoordTransform));
infLights.push_back(InfLight{ambientIntensity, diffuseIntensity, specularIntensity, direction});
continue;
}
if(lightConfig["location"].exists()) {
Vector3D position = toPoint3D(lightConfig["location"].as_double_tuple_or_die());
position = position * eyePointTransformation(polarForEyeCoordTransform);
if(lightConfig["spotAngle"].exists()) {
double spotAngle = lightConfig["spotAngle"].as_double_or_die();
pointLights.push_back(std::make_unique<SpotLight>(ambientIntensity, diffuseIntensity, specularIntensity, position, rad(spotAngle)));
continue;
}
pointLights.push_back(std::make_unique<PointLight>(ambientIntensity, diffuseIntensity, specularIntensity, position));
continue;
}
// Else it's an AmbientLight
ambientLights.push_back(AmbientLight{ambientIntensity});
}
// Figures
vector<Figure> figures;
for(int i = 0; i < nrFigures; i++) {
string figName = "Figure" + to_string(i);
const auto &figConf = configuration[figName];
figures.push_back(readFigure(figConf, true));
}
for(Figure& fig : figures) {
applyTransformation(fig, eyePointTransformation(polarForEyeCoordTransform));
}
if(configuration["General"]["clipping"].as_bool_or_default(false)) {
double hfov = configuration["General"]["hfov"].as_double_or_die();
double aspectRatio = configuration["General"]["aspectRatio"].as_double_or_die();
double dNear = configuration["General"]["dNear"].as_double_or_die(); // a distance is always positive
double dFar = configuration["General"]["dFar"].as_double_or_die();
for(Figure& fig : figures) {
std::list<Face> tris = {fig.faces.begin(), fig.faces.end()};
// Clipping against near plane
clipAgainstPlane(fig, tris, PlaneContext{ dNear, -dNear, true, PlaneContext::nearOrFar});
// clipping against far plane
clipAgainstPlane(fig, tris, PlaneContext{dNear, -dFar, false, PlaneContext::nearOrFar});
double left = -tan(rad(hfov)/2) * dNear;
// clipping against left plane
clipAgainstPlane(fig, tris, PlaneContext{dNear, left , true, PlaneContext::leftOrRight});
double right = -left;
// clipping against right plane
clipAgainstPlane(fig, tris, PlaneContext{ dNear, right , false, PlaneContext::leftOrRight});
double top = right/aspectRatio;
double bottom = -top;
// clipping against bottom plane
clipAgainstPlane(fig, tris, PlaneContext{dNear, bottom, true, PlaneContext::bottomOrTop});
// clipping against top plane
clipAgainstPlane(fig, tris, PlaneContext{ dNear, top, false, PlaneContext::bottomOrTop});
fig.faces = {tris.begin(), tris.end()};
}
}
unsigned imageX{};
unsigned imageY{};
double d{};
double dx{}, dy{};
vector<Line2D> perspectiveProjectedLines = toPerspectiveProjectedLines(figures, polarForEyeCoordTransform, false);
calcPointTransformationsFor2DLines(perspectiveProjectedLines, size, d, dx, dy, imageX, imageY);
img::EasyImage image{imageX, imageY, toEasyImgColor(toNormColor(backgroundColorVec))};
ZBuffer zBuffer{imageX,imageY};
for(Figure& fig : figures) {
//applyTransformation(fig, eyePointTransformation(polarForEyeCoordTransform));
for(const Face& face : fig.faces) {
if(face.point_indexes.size() != 3) {
cerr << "Tried to draw a triangle of a face that is not a triangle\n";
continue;
}
const Vector3D& A = fig.vertices[face.point_indexes[0]];
const Vector3D& B = fig.vertices[face.point_indexes[1]];
const Vector3D& C = fig.vertices[face.point_indexes[2]];
drawZBufTriangle(zBuffer, image, A, B, C, d, dx, dy,
fig.ambientReflection, fig.diffuseReflection, fig.specularReflection, 0,
ambientLights, infLights, pointLights);
}
}
return image;
}
}