-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
553 lines (451 loc) · 16.3 KB
/
main.cpp
File metadata and controls
553 lines (451 loc) · 16.3 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#define _USE_MATH_DEFINES
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#define NOMINMAX
#include <limits>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cassert>
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <random>
#include <cstdint>
#include <windows.h>
#include <vector>
#include <string>
#include <tuple>
#include <utility>
// Ðåéòðåñèíã
#include "geometry.h"
#include "stb_image_write.h"
#include "stb_image.h"
// GUI
#include <SFML/Graphics.hpp>
#include "../PNG/GUI/GUI.h"
#define PI 3.14159265358979323846
int envmap_width, envmap_height;
std::vector<Vec3f> envmap;
struct Date {
unsigned int day;
unsigned int month;
unsigned int year;
};
// Òî÷å÷íûå èñòî÷íèêîâ îñâåùåíèÿ
struct Light {
Light(const Vec3f &p, const float &i) : position(p), intensity(i) {}
Vec3f position;
float intensity;
};
// Îäíîöâåòíûå ñôåðû
struct Material {
float refractive_index;
Vec2f albedo;
Vec3f diffuse_color;
float specular_exponent;
Material() : refractive_index(1), albedo(1, 0), diffuse_color(), specular_exponent() {}
Material(const float &r, const Vec2f &a, const Vec3f &color, const float &spec) : refractive_index(r), albedo(a), diffuse_color(color), specular_exponent(spec) {}
};
// Äâóöâåòíûå ñôåðû
struct MaterialHard {
float refractive_index;
Vec2f albedo;
std::pair<Vec3f, Vec3f> diffuse_color;
float specular_exponent;
MaterialHard() : refractive_index(1), albedo(1, 0), diffuse_color(), specular_exponent() {}
MaterialHard(const float &r, const Vec2f &a, const std::pair<Vec3f, Vec3f> &color, const float &spec) : refractive_index(r), albedo(a), specular_exponent(spec) {
diffuse_color.first = color.first;
diffuse_color.second = color.second;
}
};
struct Sphere {
Vec3f center;
float radius;
Material material;
Sphere(const Vec3f &c, const float &r, const Material& m) : center(c), radius(r), material(m) {}
// ðàáîòà ñ îäíîé ôèãóðîé
bool ray_intersect(const Vec3f &orig, const Vec3f &dir, float &t0) const {
Vec3f L = center - orig;
float tca = L * dir;
float d2 = L * L - tca * tca;
if (d2 > radius*radius)
return false;
float thc = sqrtf(radius*radius - d2);
t0 = tca - thc;
float t1 = tca + thc;
if (t0 < 0)
t0 = t1;
if (t0 < 0)
return false;
return true;
}
};
struct SphereHard {
Vec3f center;
float radius;
MaterialHard materialHard;
SphereHard(const Vec3f &c, const float &r, const MaterialHard& m) : center(c), radius(r) {
materialHard.diffuse_color.first = m.diffuse_color.first;
materialHard.diffuse_color.second = m.diffuse_color.second;
}
// ðàáîòà ñ îäíîé ôèãóðîé
bool ray_intersect(const Vec3f &orig, const Vec3f &dir, float &t0) const {
Vec3f L = center - orig;
float tca = L * dir;
float d2 = L * L - tca * tca;
if (d2 > radius*radius)
return false;
float thc = sqrtf(radius*radius - d2);
t0 = tca - thc;
float t1 = tca + thc;
if (t0 < 0)
t0 = t1;
if (t0 < 0)
return false;
return true;
}
};
Vec3f reflect(const Vec3f &I, const Vec3f &N) {
return I - N * 2.f*(I*N);
}
Vec3f refract(const Vec3f &I, const Vec3f &N, const float &refractive_index) { // Snell's law
float cosi = -std::max(-1.f, std::min(1.f, I*N));
float etai = 1, etat = refractive_index;
Vec3f n = N;
// åñëè ëó÷ íàõîäèòñÿ âíóòðè îáúåêòà, ìåíÿåì ìåñòàìè èíäåêñû è èíâåðòèðóåì íîðìàëü, ÷òîáû ïîëó÷èòü ïðàâèëüíûé ðåçóëüòàò
if (cosi < 0) {
cosi = -cosi;
std::swap(etai, etat); n = -N;
}
float eta = etai / etat;
float k = 1 - eta * eta*(1 - cosi * cosi);
return k < 0 ? Vec3f(0, 0, 0) : I * eta + n * (eta * cosi - sqrtf(k));
};
// ðàáîòà ñ n ôèãóðàìè
bool scene_intersect(const Vec3f &orig, const Vec3f &dir, const std::vector<Sphere> &spheres, Vec3f &hit, Vec3f &N, Material &material) {
float spheres_dist = 3.40282e+38;
// äëÿ êàæäîé Îäíîöâåòíîé ñôåðû
for (size_t i = 0; i < spheres.size(); i++) {
float dist_i;
// åñëè ìû â îáëàñòè è ïîïàëè â îáëàñòü i ñôåðû
if (spheres[i].ray_intersect(orig, dir, dist_i) && dist_i < spheres_dist) {
// L
spheres_dist = dist_i;
// tca
hit = orig + dir * dist_i;
// dir
N = (hit - spheres[i].center).normalize();
// óçíàåì öâåò
material = spheres[i].material;
}
}
// â äîñòóïíîé îáëàñòè
return spheres_dist < 1000;
}
bool scene_intersectHard(const Vec3f &orig, const Vec3f &dir, const std::vector<SphereHard> &spheresHard, Vec3f &hit, Vec3f &N, MaterialHard &materialHard) {
float spheres_dist_d = 3.40282e+38;
// äëÿ êàæäîé Äâóöâåòíîé ñôåðû
for (size_t i = 0; i < spheresHard.size(); i++) {
float dist_i;
// åñëè ìû â îáëàñòè è ïîïàëè â îáëàñòü i ñôåðû
if (spheresHard[i].ray_intersect(orig, dir, dist_i) && dist_i < spheres_dist_d) {
// L
spheres_dist_d = dist_i;
// tca
hit = orig + dir * dist_i;
// dir
N = (hit - spheresHard[i].center).normalize();
// óçíàåì öâåò (!) Ðàíäîì
materialHard = spheresHard[i].materialHard;
}
}
return spheres_dist_d < 1000;
}
// èñõîäÿùèé èç orig â íàïðàâëåíèè dir
Vec3f cast_ray(const Vec3f &orig, const Vec3f &dir, const std::vector<Sphere> &spheres, const std::vector<Light> &lights, size_t depth, const Vec3f &col = Vec3f(0.2, 0.7, 0.8)) {
Vec3f point, N;
Material material;
// Óçíàåì color
if (depth > 4 || !scene_intersect(orig, dir, spheres, point, N, material)) {
return col;
}
// Óçíàåì light
float diffuse_light_intensity = 0, specular_light_intensity = 0;
for (size_t i = 0; i < lights.size(); i++) {
Vec3f light_dir = (lights[i].position - point).normalize();
// Äîáàâëåíèå òåíåé
float light_distance = (lights[i].position - point).norm();
Vec3f shadow_orig = light_dir * N < 0 ? point - N * 1e-3 : point + N * 1e-3;
Vec3f shadow_pt, shadow_N;
Material tmpmaterial;
if (scene_intersect(shadow_orig, light_dir, spheres, shadow_pt, shadow_N, tmpmaterial) && (shadow_pt - shadow_orig).norm() < light_distance)
continue;
diffuse_light_intensity += lights[i].intensity * std::max(0.f, light_dir * N);
specular_light_intensity += powf(std::max(0.f, -reflect(-light_dir, N)*dir), material.specular_exponent)*lights[i].intensity;
}
return material.diffuse_color * diffuse_light_intensity * material.albedo[0] + Vec3f(1., 1., 1.)*specular_light_intensity * material.albedo[1];
}
// èñõîäÿùèé èç orig â íàïðàâëåíèè dir
Vec3f cast_ray(const Vec3f &orig, const Vec3f &dir, const std::vector<SphereHard> &spheresHard, const std::vector<Light> &lights, size_t depth, const Vec3f &col = Vec3f(0.2, 0.7, 0.8), int pos = 0) {
Vec3f point, N;
MaterialHard material;
// Óçíàåì color
if (depth > 4 || !scene_intersectHard(orig, dir, spheresHard, point, N, material)) {
return col;
}
// Óçíàåì light
float diffuse_light_intensity = 0, specular_light_intensity = 0;
for (size_t i = 0; i < lights.size(); i++) {
Vec3f light_dir = (lights[i].position - point).normalize();
// Äîáàâëåíèå òåíåé
float light_distance = (lights[i].position - point).norm();
// ïðîâåðêà, íàõîäèòñÿ ëè òî÷êà â òåíè èñòî÷íèêîâ ñâåòà [i]
Vec3f shadow_orig = light_dir * N < 0 ? point - N * 1e-3 : point + N * 1e-3;
Vec3f shadow_pt, shadow_N;
MaterialHard tmpmaterialHard;
if (scene_intersectHard(shadow_orig, light_dir, spheresHard, shadow_pt, shadow_N, tmpmaterialHard) && (shadow_pt - shadow_orig).norm() < light_distance)
continue;
diffuse_light_intensity += lights[i].intensity * std::max(0.f, light_dir * N);
specular_light_intensity += powf(std::max(0.f, -reflect(-light_dir, N)*dir), material.specular_exponent)*lights[i].intensity;
}
Vec3f p = material.diffuse_color.first;
std::mt19937 gen;
std::uniform_int_distribution<> unif(0, pos / 2);
if (unif(gen) < pos / 4) {
return material.diffuse_color.second * diffuse_light_intensity * material.albedo[0] + Vec3f(1., 1., 1.)*specular_light_intensity * material.albedo[1];
}
return material.diffuse_color.first * diffuse_light_intensity * material.albedo[0] + Vec3f(1., 1., 1.)*specular_light_intensity * material.albedo[1];
}
void render(const std::vector<Sphere> &spheres, const std::vector<Light> &lights, const std::vector<SphereHard> &spheresHard = {}) {
const int width = 1024;
const int height = 768;
const double fov = M_PI / 3.;
std::vector<Vec3f> buf(width*height);
// Èòåðàöèè äåëÿòñÿ íà áëîêè íà chunk (ðàçìåðû áëîêà) èòåðàöèé è ñòàòè÷åñêè ðàçäåëÿþòñÿ ìåæäó ïîòîêàìè
// Åñëè chunk - íåîïðåäåëåí, òî èòåðàöèè äåëÿòñÿ ìåæäó ïîòîêàìè ðàâíîìåðíî è íåïðåðûâíî
// Èòåðàöèè äåëÿòñÿ íà chunk è âûïîëíåíèå ïðîãðàììû èäåò ïî áëîêàì. Ìîæíî ìåíÿòü ðàçìåð áëîêà
#pragma omp parallel for
for (size_t j = 0; j < height; j++) { // actual rendering loop
for (size_t i = 0; i < width; i++) {
double dir_x = (i + 0.5) - width / 2.;
// ýòî îäíîâðåìåííî ïåðåâîðà÷èâàåò èçîáðàæåíèå
double dir_y = -(j + 0.5) + height / 2.;
double dir_z = -height / (2.*tan(fov / 2.));
buf[i + j * width] = cast_ray(Vec3f(0, 0, 0), Vec3f(dir_x, dir_y, dir_z).normalize(), spheresHard, lights, 0, envmap[i + j * envmap_width], i + j * envmap_width);
// Åñëè íå çàêðàøèâàëè
if (buf[i + j * width].x == envmap[i + j * width].x && buf[i + j * width].y == envmap[i + j * width].y && buf[i + j * width].z == envmap[i + j * width].z) {
buf[i + j * width] = cast_ray(Vec3f(0, 0, 0), Vec3f(dir_x, dir_y, dir_z).normalize(), spheres, lights, 0, envmap[i + j * envmap_width]);
}
}
}
std::vector<unsigned char> pixmap(width*height * 3);
for (size_t i = 0; i < height*width; ++i) {
Vec3f &c = buf[i];
float max = std::max(c[0], std::max(c[1], c[2]));
if (max > 1) c = c * (1. / max);
for (size_t j = 0; j < 3; j++) {
pixmap[i * 3 + j] = (unsigned char)(255 * std::max(0.f, std::min(1.f, buf[i][j])));
}
}
std::cout << "done." << std::endl;
// stbi_write_jpg(s, width, height, 3, pixels, 100);
stbi_write_jpg("../result.jpg", width, height, 3, pixmap.data(), 100);
}
void CreateBackround() {
int nrChannel;
unsigned char *pixmap = stbi_load("../envmap.jpg", &envmap_width, &envmap_height, &nrChannel, 0);
if (!pixmap || 3 != nrChannel) {
std::cerr << "Error: can not load the environment map" << std::endl;
system("error");
exit(1);
}
envmap = std::vector<Vec3f>(envmap_width*envmap_height);
for (int j = envmap_height - 1; j >= 0; j--) {
for (int i = 0; i < envmap_width; i++) {
envmap[i + j * envmap_width] = Vec3f(pixmap[(i + j * envmap_width) * 3 + 0], pixmap[(i + j * envmap_width) * 3 + 1], pixmap[(i + j * envmap_width) * 3 + 2])*(1 / 255.);
}
}
stbi_image_free(pixmap);
}
float ConvertUnit(Date& date) {
// 11 Èþíÿ 1128 - âñå â ðÿä
// 11.06.1128 îòïðàâíàÿ òî÷êà
int days_point = 11 + 6 * 30 + 1128 * 365;
int month = 0;
switch (date.month) {
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
month = 31;
case 2:
month = 28;
case 4:case 6:case 9:case 11:
month = 30;
default:
break;
}
int days_date = date.day + date.month * month + date.year * 365;
return (days_date - days_point) / 10;
}
int getMonth(Date& date, std::string month) {
switch (month[0]) {
case 'J':
if (month[1] == 'a') {
return 1;
}
else if (month[2] == 'n') {
return 6;
}
else {
return 7;
}
case 'F':
return 2;
case 'M':
if (month[2] == 'r') {
return 3;
}
else {
return 5;
}
case 'A':
if (month[1] == 'p') {
return 4;
}
else {
return 8;
}
case 'S':
return 9;
case 'O':
return 10;
case 'N':
return 11;
case 'D':
return 12;
default:
std::string s = "unknown month: " + month;
// throw std::runtime_error(s);
}
}
Date ParseDate() {
// Êëàäåì èç ôàéëà output.txt äàííûå â ãîä, ìåñÿö, ÷èñëî
const std::string path = "../input.txt";
std::ifstream input(path);
Date date;
std::string smonth;
if (input) {
input >> date.day;
input.ignore(1);
input >> smonth;
date.month = getMonth(date, smonth.substr(0, 3));
input.ignore(1);
input >> date.year;
// (?) input.ignore(1);
}
else {
std::string s = "not found path: " + path;
throw std::runtime_error(s);
}
std::cout << "date_main = " << date.day << " " << date.month << " " << date.year << std::endl;
return date;
}
int main() {
while (true) {
float y = 0;
// Âûçûâàåì Ãðàôè÷åñêèé èíòåðôåéñ
GUI gui;
// gui.main1();
gui.Create();
const std::string path = "../input.txt";
std::ifstream input(path);
std::string line;
while (getline(input, line)) {
std::cout << "line in main: " << line << std::endl;
}
// Îáðàáàòûâàåì êîððåêòíóþ çàïèñü ïîëüçîâàòåëÿ
Date date = ParseDate();
// Çàãðóæàåì ôîí
CreateBackround();
// x0, y0, z0
std::vector<double> sun_param = { 0, 0, -65 };
// rad, omega
std::vector<double> mer_param = { 0.29, 4.15 };
std::vector<double> ven_param = { 0.605, 1.54 };
std::vector<double> earth_param = { 0.64, 1. };
std::vector<double> mars_param = { 0.34, 0.53 };
std::vector<double> jupiter_param = { 3.56, 0.86 };
std::vector<double> saturn_param = { 3., 0.034 };
std::vector<double> uran_param = { 1.225, 0.012 };
std::vector<double> neptune_param = { 1.255, 0.006 };
std::vector<double> pluto_param = { 0.14, 0.004 };
// Õàðàêòåðèñòèêè ïëàíåò
std::vector<std::vector<double>> origin_param = { mer_param, ven_param, earth_param, mars_param, jupiter_param, saturn_param, uran_param, neptune_param, pluto_param };
Material sun(2.0, Vec2f(0.9, 0.4), Vec3f(0.8, 0.5, 0.), 5.);
Material mercury(1.0, Vec2f(0.9, 0.1), Vec3f(109, 110, 133) * (1 / 255.), 20);
Material venus(1.0, Vec2f(0.9, 0.1), Vec3f(250, 234, 193) * (1 / 255.), 20);
MaterialHard earth(1.0, Vec2f(0.0, 10.0), std::make_pair(Vec3f(0.3, 0.8, 0.13), Vec3f(0.13, 0.24, 0.8)), 20);
MaterialHard mars(1.5, Vec2f(1.0, 10.0), std::make_pair(Vec3f(1., 0.27, 0.), Vec3f(0.502, 0., 0.)), 60);
MaterialHard jupiter(1.5, Vec2f(1.0, 10.0), std::make_pair(Vec3f(0.54, 0.27, 0.07), Vec3f(0.95, 0.64, 0.37)), 60);
MaterialHard saturn(1.5, Vec2f(1.0, 10.0), std::make_pair(Vec3f(1, 0.87, 0.67), Vec3f(0.96, 0.87, 0.7)), 60);
MaterialHard uran(1.5, Vec2f(1.0, 10.0), std::make_pair(Vec3f(0, 0.74, 1.), Vec3f(0.11, 0.56, 1.)), 30);
Material neptune(1.5, Vec2f(1.0, 10.0), Vec3f(0., 0., 1.), 80);
Material pluto(1.5, Vec2f(1.0, 10.0), Vec3f(1., 0.89, 0.88), 30);
std::vector<Material> materials = { mercury, venus, neptune, pluto };
std::vector<MaterialHard> materialsHard = { earth, mars, jupiter, saturn, uran };
std::vector<Light> lights;
std::vector<Sphere> spheres;
std::vector<SphereHard> spheresHard;
double unit = ConvertUnit(date);
std::cout << "unit = " << unit << std::endl;
std::vector<double> rad_orb = { 7.29, 9.185, 11.43, 13.41, 18.31, 26.77, 33.095, 36.575, 38.97 };
// âåðõíèé
lights.push_back(Light(Vec3f(sun_param[0], y + 10, sun_param[2] + 13), 1.5));
// ëåâûé
lights.push_back(Light(Vec3f(sun_param[0] - 10, y, sun_param[2]), 1.5));
// ïðàâûé
lights.push_back(Light(Vec3f(sun_param[0] + 10, y, sun_param[2]), 1.5));
// çàäíèé
lights.push_back(Light(Vec3f(sun_param[0], y, sun_param[2] - 10 - .1), 1.5));
// íèæíèé
lights.push_back(Light(Vec3f(sun_param[0], y - 10, sun_param[2] + 13), 1.5));
// Ñîëíöå
spheres.push_back(Sphere(Vec3f(sun_param[0], sun_param[1], sun_param[2]), 5, sun));
// Ìåðêóðèé, Âåíåðà
for (int i = 0; i < 2; i++) {
double x1 = sun_param[0] + rad_orb[i] * cos(origin_param[i][1] * unit / 57.3);
double z1 = sun_param[2] + rad_orb[i] * sin(origin_param[i][1] * unit / 57.3);
spheres.push_back(Sphere(Vec3f(x1, 0., z1), origin_param[i][0], materials[i]));
std::cout << "num planet: " << i << std::endl;
}
// Çåìëÿ, Ìàðñ, Þïèòåð, Ñàòóðí, Óðàí
for (int i = 2; i < 7; i++) {
double x1 = sun_param[0] + rad_orb[i] * cos(origin_param[i][1] * unit / 57.3);
double z1 = sun_param[2] + rad_orb[i] * sin(origin_param[i][1] * unit / 57.3);
spheresHard.push_back(SphereHard(Vec3f(x1, 0., z1), origin_param[i][0], materialsHard[i - 2]));
std::cout << "num planet: " << i << std::endl;
}
// Íåïòóí, Ïëóòîí
for (int i = 7; i < 9; i++) {
double x1 = sun_param[0] + rad_orb[i] * cos(origin_param[i][1] * unit / 57.3);
double z1 = sun_param[2] + rad_orb[i] * sin(origin_param[i][1] * unit / 57.3);
spheres.push_back(Sphere(Vec3f(x1, 0., z1), origin_param[i][0], materials[i - 5]));
std::cout << "num planet: " << i << std::endl;
}
// Âûâîä
render(spheres, lights, spheresHard);
for (int i = 0; i < 2; i++) {
spheres.pop_back();
}
for (int i = 2; i < 7; i++) {
spheresHard.pop_back();
}
for (int i = 7; i < 9; i++) {
spheres.pop_back();
}
gui.openIs("../result.jpg");
}
system("pause");
return 0;
}
// C:\Users\Asus\Desktop\CodeC\C++\Visual Studio\PNG\x64\Release