-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
285 lines (222 loc) · 11.1 KB
/
main.cpp
File metadata and controls
285 lines (222 loc) · 11.1 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
#include <iostream>
#include <string>
#include <vector>
#include <QImage>
#include <QApplication>
#include <QLabel>
#include <chrono>
#include "Scene.h"
#include "Render.h"
#include "ConvertToQImage.h"
#include "LoadFromStl.h"
#include "LoadFromRt.h"
void draw(Scene &scene, const Screen &screen, int height, int width, const std::string name) {
#ifdef COUNT_STATS
KDNode::_resetStats();
#endif
std::chrono::steady_clock::time_point buildStart = std::chrono::steady_clock::now();
scene.buildScene();
std::chrono::steady_clock::time_point buildEnd = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
Render render(&scene, screen, height, width);
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
QImage image = ConvertToQImage(&render).getQImage();
image.save(QString::fromStdString(name) + ".png");
std::cerr << name << " build time: " <<
std::chrono::duration_cast<std::chrono::milliseconds>(buildEnd - buildStart).count() << "\n";
std::cerr << name << " time: " <<
std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "\n";
#ifdef COUNT_STATS
std::cerr << "build calls: " << KDNode::_getBuildCalls() << "\n";
std::cerr << "find calls: " << KDNode::_getFindCalls() <<
" (" << KDNode::_getFindCalls() * 1.0 / (height * width) << " per pix)" << "\n\n";
#endif
}
void drawTriangle1() {
std::string caseName = __FUNCTION__;
Scene scene;
scene.addObject(new Triangle(Point(0, 0, 0), Point(1, 0, 0), Point(0, 1, 0), Material(Color(1, 0, 0), 0, 0)));
scene.addLightSource(LightSource(Point(1, 1, 1), 1));
draw(scene, Screen(Point(-4.0 / 3, -1, 1), Vector(8.0 / 3, 0, 0), Vector(0, 2, 0)), 600, 800, caseName);
}
void drawTriangle2() {
std::string caseName = __FUNCTION__;
Scene scene;
LoadFromStl::load(std::string("../models/triangle.stl"), Material(Color(0.5, 0, 0), 0, 0), scene);
scene.addLightSource(LightSource(Point(2, 2, 2), 10));
draw(scene, Screen(Point(-4.0 / 3, -1, 5), Vector(8.0 / 3, 0, 0), Vector(0, 2, 0)), 600, 800, caseName);
}
void drawPolygon1() {
std::string caseName = __FUNCTION__;
Scene scene;
std::vector<Point> vertices = {
Point(0, 1, 0),
Point(-1, 0, 0),
Point(1, -2, 0),
Point(1, 0, 0)
};
scene.addObject(new Polygon(vertices, Material(Color(1, 0, 0), 0, 0)));
scene.addLightSource(LightSource(Point(1, 1, 1), 1.5));
draw(scene, Screen(Point(-4.0 / 3, -1.7, 3), Vector(8.0 / 3, 0, 0), Vector(0, 2, 0)), 600, 800, caseName);
}
void drawSphere1() {
std::string caseName = __FUNCTION__;
Scene scene;
scene.addObject(new Sphere(Point(0, 0, 0), 0.7, Material(Color(0, 1, 0), 0, 0)));
scene.addLightSource(LightSource(Point(1, 1, 1), 1));
draw(scene, Screen(Point(-4.0 / 3, -1, 1), Vector(8.0 / 3, 0, 0), Vector(0, 2, 0)), 600, 800, caseName);
}
void drawSphereAndTriangle1() {
std::string caseName = __FUNCTION__;
Scene scene;
scene.addObject(new Sphere(Point(0, 0, 0), 0.7, Material(Color(0, 1, 0), 0, 0)));
scene.addObject(new Triangle(Point(0, 0, 0.4), Point(1, 0, 0.4), Point(0, 1, 0.4), Material(Color(1, 0, 0), 0, 0)));
scene.addLightSource(LightSource(Point(-1, -1, 1.5), 1.3));
scene.addLightSource(LightSource(Point(0.0, 0.0, 5), 3));
draw(scene, Screen(Point(-4.0 / 3, -1, 1), Vector(8.0 / 3, 0, 0), Vector(0, 2, 0)), 600, 800, caseName);
}
void drawGnomeWithBottle1() {
std::string caseName = __FUNCTION__;
Scene scene;
LoadFromStl::load(std::string("../models/gnome.stl"), Material(Color(1, 0, 0), 0, 0), scene);
scene.addLightSource(LightSource(Point(0, 5, 10), 60));
scene.addLightSource(LightSource(Point(10, 5, 0), 60));
draw(scene, Screen(Point(-10.0 / 3, -0.5, 10), Vector(20.0 / 3, 0, 0), Vector(0, 5, 0)), 600, 800, caseName);
}
void drawGnomeWithBottle2() {
std::string caseName = __FUNCTION__;
Scene scene;
LoadFromStl::load(std::string("../models/gnome.stl"), Material(Color(1, 0, 0), 0, 0), scene);
scene.addLightSource(LightSource(Point(-10, 10, 0), 60));
scene.addLightSource(LightSource(Point(10, 5, 0), 60));
scene.addLightSource(LightSource(Point(-10, 10, 10), 60));
draw(scene, Screen(Point(5, 0, 10.0 / 3), Vector(0, 0, -20.0 / 3), Vector(0, 5, 0)), 600, 800, caseName);
}
void drawUtahTeapot1() {
std::string caseName = __FUNCTION__;
Scene scene;
LoadFromStl::load(std::string("../models/utah_teapot.stl"), Material(Color(1, 0, 0), 0, 0), scene);
scene.addLightSource(LightSource(Point(0, 100, 0), 3000));
scene.addLightSource(LightSource(Point(0, 0, 100), 3000));
draw(scene, Screen(Point(-200.0 / 3, -50, 50), Vector(400.0 / 3, 0, 0), Vector(0, 100, 0)), 600, 800, caseName);
}
void drawUtahTeapot2() {
std::string caseName = __FUNCTION__;
Scene scene;
LoadFromStl::load(std::string("../models/utah_teapot.stl"), Material(Color(1, 0, 0), 0, 0), scene);
scene.addLightSource(LightSource(Point(0, 100, 0), 3000));
scene.addLightSource(LightSource(Point(0, 0, 100), 3000));
draw(scene, Screen(Point(200.0 / 3, 50, -50), Vector(-400.0 / 3, 0, 0), Vector(0, 0, 100)), 600, 800, caseName);
}
void drawMirror1() {
std::string caseName = __FUNCTION__;
Scene scene;
scene.addObject(new Polygon({Point(-1, -1, 1), Point(0, -1, 0), Point(0, 1, 0), Point(-1, 1, 1)},
Material(Color(0, 0, 1), 0.5, 0)));
// scene.addObject(new Polygon({Point(0, -10, -10), Point(1, -10, 10), Point(1, 10, 10), Point(0, 10, -10)},
// Color(1, 0, 0), 0.5));
scene.addObject(new Polygon({Point(0, -10, 0), Point(1, -10, 10), Point(1, 10, 10), Point(0, 10, 0)},
Material(Color(1, 0, 0), 0.5, 0)));
scene.addLightSource(LightSource(Point(0, 0, 100), 100000));
draw(scene, Screen(Point(-4.0 / 3, -1, 3), Vector(8.0 / 3, 0, 0), Vector(0, 2, 0)), 600, 800, caseName);
}
void drawMirror2() {
std::string caseName = __FUNCTION__;
Scene scene;
scene.addObject(new Polygon({Point(-1, -1, 1), Point(0, -1, 0), Point(0, 1, 0), Point(-1, 1, 1)},
Material(Color(0, 1, 0), 0.5, 0)));
scene.addObject(new Sphere(Point(-1, 0, 0), 1, Material(Color(0, 0, 1), 0.95, 0)));
scene.addObject(new Polygon({Point(0, 0, 0), Point(1, 0, 10), Point(1, 10, 10), Point(0, 10, 0)},
Material(Color(1, 0, 0), 0.5, 0)));
scene.addLightSource(LightSource(Point(0, 0, 20), 700));
draw(scene, Screen(Point(-4.0 / 3, -1, 5), Vector(8.0 / 3, 0, 0), Vector(0, 2, 0)), 600, 800, caseName);
}
void drawMirror3() {
std::string caseName = __FUNCTION__;
Scene scene;
scene.addObject(new Sphere(Point(-2, 0, 0), 1.5, Material(Color(0, 0, 1), 0.8, 0)));
scene.addObject(new Sphere(Point(2, 0, 0), 1.5, Material(Color(0, 0, 1), 0.8, 0)));
scene.addLightSource(LightSource(Point(0, 0, 20), 500));
draw(scene, Screen(Point(-4.0 / 3, -1, 10), Vector(8.0 / 3, 0, 0), Vector(0, 2, 0)), 600, 800, caseName);
}
void drawGnomeWithBottleWithMirrors1() {
std::string caseName = __FUNCTION__;
Scene scene;
LoadFromStl::load(std::string("../models/gnome.stl"), Material(Color(1, 0, 0), 0, 0), scene);
scene.addObject(new Polygon({Point(-10, -2, -10), Point(-10, 7, -10), Point(-13, 7, 0), Point(-13, -2, 0)},
// Material(Color(1, 0, 0), 0.0, 0)));
Material(Color(1, 0, 0), 1, 0)));
scene.addObject(new Polygon({Point(-13, -2, 0), Point(-13, 7, 0), Point(-10, 7, 10), Point(-10, -2, 10)},
// Material(Color(0, 0, 1), 0.0, 0)));
Material(Color(0, 0, 1), 0.7, 0)));
scene.addLightSource(LightSource(Point(-10, 10, 0), 60));
scene.addLightSource(LightSource(Point(10, 5, 0), 60));
scene.addLightSource(LightSource(Point(-10, 10, 10), 60));
draw(scene, Screen(Point(5, 0, 10.0 / 3), Vector(0, 0, -20.0 / 3), Vector(0, 5, 0)), 600, 800, caseName);
}
void drawGlass1() {
std::string caseName = __FUNCTION__;
Scene scene;
scene.addObject(new Sphere(Point(0, 0, -3), 1, Material(Color(0, 0, 1), 0, 0)));
scene.addObject(new Polygon({Point(-5, 0, 0), Point(-5, -5, 0), Point(5, -5, 0), Point(5, 0, 0)},
Material(Color(0, 1, 0), 0, 0.5)));
scene.addObject(new Polygon({Point(-5, 0, -1), Point(-5, -5, -1), Point(5, -5, -1), Point(5, 0, -1)},
Material(Color(0, 1, 0), 0, 0.5)));
scene.addLightSource(LightSource(Point(0, 20, 20), 700));
draw(scene, Screen(Point(-4.0 / 3, -1, 5), Vector(8.0 / 3, 0, 0), Vector(0, 2, 0)), 600, 800, caseName);
}
void drawGlass2() {
std::string caseName = __FUNCTION__;
Scene scene;
scene.addObject(new Sphere(Point(0, 1, -3), 1, Material(Color(0, 0, 1), 0, 0)));
scene.addObject(new Sphere(Point(0, 0, -1), 1, Material(Color(0, 0, 1), 0, 0.5)));
scene.addLightSource(LightSource(Point(0, 20, 20), 700));
// scene.addLightSource(LightSource(Point(20, -20, 20), 700));
draw(scene, Screen(Point(-4.0 / 3, -1, 5), Vector(8.0 / 3, 0, 0), Vector(0, 2, 0)), 600, 800, caseName);
}
void drawInternalReflection1() {
std::string caseName = __FUNCTION__;
Scene scene;
scene.addObject(new Polygon({Point(-1, 0, 30), Point(-1, 0, -10), Point(-1, -5, -10), Point(-1, -5, 30)},
Material(Color(0, 1, 0), 0, 0.01)));
scene.addObject(new Polygon({Point(1, 0, 30), Point(1, -5, 30), Point(1, -5, -10), Point(1, 0, -10)},
Material(Color(0, 1, 0), 0, 0.01)));
scene.addObject(new Polygon({Point(-1, 5, 30), Point(-1, 5, -10), Point(-1, 0, -10), Point(-1, 0, 30)},
Material(Color(0, 1, 0), 0, 0.0)));
scene.addObject(new Polygon({Point(1, 5, 30), Point(1, 0, 30), Point(1, 0, -10), Point(1, 5, -10)},
Material(Color(0, 1, 0), 0, 0.0)));
scene.addObject(new Sphere(Point(0, 0, -15), 1, Material(Color(0, 0, 1), 0, 0)));
scene.addLightSource(LightSource(Point(0, 20, 20), 700));
draw(scene, Screen(Point(-4.0 / 3, -1, 5), Vector(8.0 / 3, 0, 0), Vector(0, 2, 0)), 600, 800, caseName);
}
void drawRtFile(const std::string &name, const std::string &caseName) {
Scene scene;
Screen screen;
LoadFromRt::load(std::string(name), scene, screen);
draw(scene, screen, 768, 1024, caseName);
}
int main(int argc, char *argv[]) {
drawTriangle1();
drawTriangle2();
drawPolygon1();
drawSphere1();
drawSphereAndTriangle1();
drawGnomeWithBottle1();
drawGnomeWithBottle2();
drawUtahTeapot1();
drawUtahTeapot2();
drawMirror1();
drawMirror2();
drawMirror3();
drawGnomeWithBottleWithMirrors1();
drawGlass1();
drawGlass2();
drawInternalReflection1();
drawRtFile("../models/coloringtest.rt", "rt_coloringtest");
// drawRtFile("../models/gnome_pure.rt", "rt_gnome_pure");
drawRtFile("../models/pool.rt", "rt_pool");
drawRtFile("../models/quad.rt", "rt_quad");
drawRtFile("../models/simple.rt", "rt_simple");
// drawRtFile("../models/spheres_pure.rt", "rt_spheres_pure");
return 0;
}