-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.hpp
177 lines (146 loc) · 6.77 KB
/
render.hpp
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
//
// Created by Andrew Yang on 4/24/21.
//
#ifndef RAYTRACING_RENDER_HPP
#define RAYTRACING_RENDER_HPP
#include <SFML/Graphics.hpp>
#include "denoise.hpp"
#include "timer.hpp"
#include "parallel/pixels.hpp"
#include "parallel/task.hpp"
#include "parallel/params.hpp"
void render_window(point3& lookfrom, point3& lookat, float vfov, float aperture, hittable_list& world, color& background) {
// Render window
sf::RenderWindow window(sf::VideoMode(params::WIDTH, (params::WIDTH/params::ASPECT_RATIO)),
"Ray Tracing", sf::Style::Titlebar | sf::Style::Close);
window.setSize(sf::Vector2u(700, 700/params::ASPECT_RATIO));
sf::Texture tex;
sf::Sprite sprite;
if(!tex.create(params::WIDTH, params::HEIGHT)) {
std::cerr << "Couldn't create texture!" << std::endl;
return;
}
tex.setSmooth(false);
sprite.setTexture(tex);
pixels pix = pixels(params::WIDTH, params::HEIGHT);
std::vector<float> data(params::WIDTH * params::HEIGHT * 5);
int wind_w = 700;
int wind_h = 700/params::ASPECT_RATIO;
float popw = wind_w / 2.0f;
float poph = wind_h / 2.5f;
sf::RectangleShape popup(sf::Vector2f(popw, poph));
popup.move((float)wind_w/2 - popw/2, (float)wind_h/2 - poph/2);
popup.setOutlineThickness(7);
popup.setOutlineColor(sf::Color(120, 120, 120));
sf::Font title;
title.loadFromFile("/Users/andrewyang/CLionProjects/untitled/arial.ttf");
sf::Text textt("Title", title);
textt.setFillColor(sf::Color::Black);
textt.setCharacterSize(wind_w / 43.333f);
sf::FloatRect textTitle = textt.getLocalBounds();
textt.setOrigin(textTitle.left + textTitle.width/2.0f,
textTitle.top + textTitle.height/2.0f);
textt.setPosition(sf::Vector2f((float)wind_w/2.7f,(float)wind_h/2.7f));
sf::RectangleShape buttonl(sf::Vector2f(wind_w/9, wind_h/11.5f));
buttonl.setFillColor(sf::Color::Blue);
buttonl.move((float)wind_w/2 - wind_w/4.4, (float)wind_h/1.75f);
sf::Font fontl;
fontl.loadFromFile("/Users/andrewyang/CLionProjects/untitled/arial.ttf");
sf::Text textl("No", fontl);
textl.setFillColor(sf::Color::White);
textl.setCharacterSize(wind_w / 43.333f);
sf::FloatRect textRectl = textl.getLocalBounds();
textl.setOrigin(textRectl.left + textRectl.width/2.0f,
textRectl.top + textRectl.height/2.0f);
textl.setPosition(sf::Vector2f((float)wind_w/3.05f,(float)wind_h/1.62f));
sf::RectangleShape buttonr(sf::Vector2f(wind_w/9, wind_h/11.5f));
buttonr.setFillColor(sf::Color::Blue);
buttonr.move((float)wind_w/2 + (float)wind_w/9, (float)wind_h/1.75f);
sf::Font fontr;
fontr.loadFromFile("/Users/andrewyang/CLionProjects/untitled/arial.ttf");
sf::Text textr("Yes", fontr);
textr.setFillColor(sf::Color::White);
textr.setCharacterSize(wind_w / 43.333f);
sf::FloatRect textRectr = textl.getLocalBounds();
textr.setOrigin(textRectr.left + textRectr.width/2.0f,
textRectr.top + textRectr.height/2.0f);
textr.setPosition(sf::Vector2f((float)wind_w/1.52f,(float)wind_h/1.62f));
bool clicked = false;
bool hide = false;
// Camera
vec3 vup(0, 1, 0);
auto dist_to_focus = 10.0f;
camera cam(lookfrom, lookat, vup, vfov, params::ASPECT_RATIO, aperture, dist_to_focus, .0f, 1.0f);
const unsigned int n_threads = std::thread::hardware_concurrency();
std::cout << "Detected " << n_threads << " concurrent threads." << std::endl;
std::vector<std::thread> threads(n_threads);
Timer timer;
for(auto& t : threads) t = std::thread(Task{&world, &cam, background, &data[0]});
bool finished_rendering = false;
while(window.isOpen()) {
sf::Event event{};
while(window.pollEvent(event)) {
if(event.type == sf::Event::Closed) window.close();
if(finished_rendering && !clicked) {
if(event.type == sf::Event::MouseButtonPressed) {
sf::Vector2f mousePos = window.mapPixelToCoords(sf::Mouse::getPosition(window));
if (buttonl.getGlobalBounds().contains(mousePos)) {
buttonl.setFillColor(sf::Color(20,20,150));
}
if (buttonr.getGlobalBounds().contains(mousePos)) {
buttonr.setFillColor(sf::Color(20,20,150));
}
}
else if(event.type == sf::Event::MouseButtonReleased) {
sf::Vector2f mousePos = window.mapPixelToCoords(sf::Mouse::getPosition(window));
if(buttonl.getGlobalBounds().contains(mousePos)) {
window.create(sf::VideoMode(params::WIDTH, (params::WIDTH/params::ASPECT_RATIO)),
"Ray Tracing", sf::Style::Titlebar | sf::Style::Close);
window.setSize(sf::Vector2u(700, 700/params::ASPECT_RATIO));
clicked = true;
hide = false;
}
if(buttonr.getGlobalBounds().contains(mousePos)) {
window.create(sf::VideoMode(params::WIDTH, (params::WIDTH/params::ASPECT_RATIO)),
"Ray Tracing", sf::Style::Titlebar | sf::Style::Close);
window.setSize(sf::Vector2u(700, 700/params::ASPECT_RATIO));
data_denoise(data);
tex.update(&pix.get_pixels(data)[0]);
clicked = true;
hide = false;
}
buttonl.setFillColor(sf::Color::Blue);
buttonr.setFillColor(sf::Color::Blue);
}
}
}
if(!finished_rendering)
tex.update(&pix.get_pixels(data)[0]);
window.clear();
if(!hide)
window.draw(sprite);
if(!clicked && finished_rendering) {
window.draw(popup);
window.draw(buttonl);
window.draw(textl);
window.draw(buttonr);
window.draw(textr);
window.draw(textt);
}
window.display();
if(!finished_rendering && done_count == n_threads) {
textt.setString(" Finished rendering in:\n" + timer.to_string() + " Apply OpenImageDenoise?");
window.create(sf::VideoMode(wind_w, wind_h),
"Ray Tracing", sf::Style::Titlebar | sf::Style::Close);
hide = true;
finished_rendering = true;
}
sf::sleep(sf::milliseconds(200));
}
std::cout << "Waiting for all threads to join" << std::endl;
for(auto& t : threads) t.join();
tex.copyToImage().saveToFile("output/out.png");
std::cout << "Saved image to out.png" << std::endl;
return;
}
#endif //RAYTRACING_RENDER_HPP