-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRaytracer.cpp
416 lines (335 loc) · 12.7 KB
/
Raytracer.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
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
// Main file, defines entry point
#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include "Vector3.h"
#include "Color.h"
#include "Objects.h"
#include "Ray.h"
#include <vector>
#include <list>
#include <fstream>
#include <stdint.h>
#include <thread>
#include <mutex>
#include <chrono> // Benchmarking
#include <iostream> // Debug messages
#include <string>
#define M_PI 3.14159265358979323846
const int image_width = 1920;
const int image_height = 1080;
// Set this to true to enable animated rendering, eg. updating of the RenderWindow while it is rendering.
// Note: Very expensive, slows rendering by some 50%;
const bool pretty = false;
// If set to true, will render the result to result.png in the executable's directory in addition to displaying
const bool render_to_file = true;
class Light
{
public:
Vector3f direction;
Color color;
float intensity;
Light(Vector3f direction, Color color, float intensity) : direction(direction), color(color), intensity(intensity) {}
};
class SphericalLight
{
public:
Vector3f position;
Color color;
float intensity;
SphericalLight(Vector3f position, Color color, float intensity) : position(position), color(color), intensity(intensity) {}
};
// Defines the Scene, "World" to be rendered
class Scene {
public:
// Height and width of the image resulting from rendering this scene
int height;
int width;
// Field of View parameter
float fov;
// Collection of objects to be rendered
std::vector<Object*> objects;
std::vector<Light*> lights;
std::vector<SphericalLight*> spherical_lights;
float shadow_bias;
};
// TODO: Program camera rotation
class Camera {
public:
Vector3f position;
Vector3f direction;
Camera() : position(Vector3f(0,0,0)), direction(Vector3f(0, 0, -1.0f)) { }
};
Camera cam;
// Degree to radian conversion helper
float rad(float degrees)
{
return (float)((degrees*M_PI) / 180);
}
// Accepts pixel coordinates of the resulting image and returns a directional ray for our "camera"
Ray create_ray(int x, int y, Scene scene)
{
// Correct distortion for different aspect ratios
float aspect_ratio = (float)scene.width / (float)scene.height;
// Adjust for different FOVs
float fov_correction = tan(rad(scene.fov) / 2.0f);
// Translation of image X/Y pixel to World position, our "camera sensor" rectangle in the Scene.
float sensor_x = (((x + 0.5f) / scene.width) * 2.0f - 1.0f) * aspect_ratio * fov_correction;
float sensor_y = (1.0f - ((y + 0.5f) / scene.height) * 2.0f) * fov_correction;
Ray ray;
// Initialize Origin to cam.position, our point of ray casting
ray.origin = cam.position;
// Set the direction vector to a normalized version of our calculated position.
ray.direction = Vector3f(sensor_x, sensor_y, -1.0f).normalize();
// debug
//std::cout << "Ray created at " << ray.origin << " with direction " << ray.direction << "\n";
return ray;
}
sf::Image image;
sf::Texture texture;
sf::Sprite sprite;
std::mutex render_mutex;
class HitResult
{
public:
Vector3f point;
Vector3f surface_normal;
Object* object;
HitResult() : object(nullptr) {}
HitResult(Vector3f point, Vector3f surface_normal, Object* object) : point(point), surface_normal(surface_normal), object(object) {}
};
HitResult trace(Ray &ray, const Scene &scene)
{
float closest_distance = 0;
Object *closest = nullptr;
HitResult hit;
for (unsigned int k = 0; k < scene.objects.size(); k++)
{
Object& obj = *scene.objects[k];
// If we hit something, color it with the hit object's color.
float distance = obj.intersects(ray);
if (distance == -1) continue;
if (distance < closest_distance || closest == nullptr)
{
closest_distance = distance;
closest = &obj;
}
}
if (closest != nullptr)
{
hit.object = closest;
hit.point = ray.origin + (ray.direction * closest_distance);
hit.surface_normal = hit.object->surface_normal(hit.point);
}
return hit;
}
Color get_pixel_color(Ray &ray, const Scene &scene)
{
Color pixel_color(16, 16, 16);
HitResult hit = trace(ray, scene);
if (hit.object != nullptr)
{
pixel_color = Color(0, 0, 0);
for (int i = 0; i < scene.lights.size(); i++)
{
Light& light = *scene.lights[i];
Vector3f direction_to_light = light.direction * -1;
// Check if it is in light
Ray shadow_ray;
shadow_ray.origin = hit.point + (hit.surface_normal * scene.shadow_bias);
shadow_ray.direction = direction_to_light;
HitResult shadow_trace = trace(shadow_ray, scene);
bool in_light = shadow_trace.object == nullptr;
float light_intensity = (in_light ? light.intensity : 0.0f);
float light_power = std::max(hit.surface_normal.dot(direction_to_light) * light_intensity, 0.0f);
Color light_color = light.color * light_power * hit.object->reflectivity;
pixel_color = pixel_color + hit.object->color * light_color;
}
for (int i = 0; i < scene.spherical_lights.size(); i++)
{
SphericalLight& light = *scene.spherical_lights[i];
// Get normalized vector defining the direction from our point to the light
Vector3f direction_to_light = (light.position - hit.point).normalize();
// Check if it is in light
Ray shadow_ray;
shadow_ray.origin = hit.point + (hit.surface_normal * scene.shadow_bias);
shadow_ray.direction = direction_to_light;
HitResult shadow_trace = trace(shadow_ray, scene);
bool in_light = shadow_trace.object == nullptr; // In Light detection is fucked.
//if (!in_light)
//std::cout << direction_to_light;
float light_intensity = (in_light ? light.intensity/(4*M_PI*std::powf((light.position - hit.point).length(),2)) : 0.0f);
float light_power = std::abs(hit.surface_normal.dot(direction_to_light)) * light_intensity;
Color light_color = light.color * light_power * hit.object->reflectivity;
pixel_color = pixel_color + hit.object->color * light_color;
}
}
return pixel_color;
}
void render_part(int line_from, int line_to, const Scene &scene, sf::RenderWindow* window)
{
for (int i = line_from; i < line_to; i++)
{
for (int j = 0; j < scene.width; j++)
{
Ray ray = create_ray(j, i, scene);
image.setPixel(j, i, get_pixel_color(ray, scene));
}
// Note: This makes the rendering process "animated" and lets you see the threads at work.
// It is also stupidly expensive and increases Render time by some 50%.
if (pretty)
{
render_mutex.lock();
window->setActive(true);
texture.loadFromImage(image);
sprite.setTexture(texture, true);
window->clear();
window->draw(sprite);
window->display();
window->setActive(false);
render_mutex.unlock();
}
// Also, the whole locking and setActive() business is necessary because OpenGL doesn't allow a Context to be active in multiple threads at once.
}
}
// The main render function.
// Note: This is multithreaded, it launches several render threads sharing the workload to take advantage of multicore systems.
// Also: Threads != Cores, the Operating System has to worry about Thread->Core allocation, this is just to enable the OS to do so.
sf::Image render(Scene scene, sf::RenderWindow &window, int frameNo)
{
// Initialize Bitmap
image.create(scene.width, scene.height, sf::Color::Black);
// Setup renderthreads
// Autodetect hardware logical core count
const int thread_count = std::thread::hardware_concurrency();
//std::cout << thread_count;
std::list<std::thread*> threads;
int step = (int)floor(scene.height / thread_count);
// Launch threads
for (int i = 0; i < thread_count-1; i++)
{
std::thread* renderthread = new std::thread(render_part, i*step, i*step + step, scene, &window);
threads.push_back(renderthread);
}
// Last thread does extra work
std::thread* renderthread = new std::thread(render_part, (thread_count - 1)*step, scene.height, scene, &window);
threads.push_back(renderthread);
// Sync threads
for (int i = 0; i < thread_count; i++)
{
threads.back()->join();
threads.pop_back();
}
if (render_to_file)
image.saveToFile((std::string)"result" + std::to_string(frameNo) + (std::string)".png");
window.setActive(true);
texture.loadFromImage(image);
sprite.setTexture(texture, true);
window.clear();
window.draw(sprite);
window.display();
return image;
}
int main()
{
// Test scene setup
Scene test_scene;
test_scene.fov = 90;
test_scene.width = image_width;
test_scene.height = image_height;
test_scene.shadow_bias = 0.00001f;
// A sphere to be rendered in the world
Sphere test_sphere1(Vector3f(0, 0, -5.0f), 1.0f, Color::Green);
test_scene.objects.push_back(&test_sphere1);
Sphere test_sphere2(Vector3f(0, 3, -5.0f), 0.8f, Color::Blue);
test_scene.objects.push_back(&test_sphere2);
Sphere test_sphere3(Vector3f(1.0f, 0.5f, -2.0f), 0.7f, Color::Red);
test_scene.objects.push_back(&test_sphere3);
Sphere test_sphere4(Vector3f(-0.4f, -0.4f, -1.0f), 0.5f, Color::White);
test_scene.objects.push_back(&test_sphere4);
Sphere test_sphere5(Vector3f(-2, -0, -8.0f), 2.0f, Color::Yellow);
test_scene.objects.push_back(&test_sphere5);
Sphere test_sphere6(Vector3f(-2, 2, -3.5f), 1.0f, Color::Cyan);
test_scene.objects.push_back(&test_sphere6);
// A test Plane in the world
Plane* test_plane = new Plane(Vector3f(0, 0, -10.0f), Vector3f(0, 0, -1), Color(135, 206, 255));
test_plane->debugID = "BackPlane";
test_scene.objects.push_back(test_plane);
Plane* test_plane2 = new Plane(Vector3f(0, -2.0f, -10.0f), Vector3f(0, -1, 0), Color(64, 64, 64));
test_plane2->debugID = "FloorPlane";
//test_scene.objects.push_back(test_plane2);
Light light1(Vector3f(1, -1, -1).normalize(), Color(255, 165, 0), 1.0f);
test_scene.lights.push_back(&light1);
Light light2(Vector3f(-1, -1, -1).normalize(), Color::Blue, 0.8f);
test_scene.lights.push_back(&light2);
Light light3(Vector3f(-1, -1, -2).normalize(), Color::Magenta, 0.3f);
test_scene.lights.push_back(&light3);
SphericalLight sLight1(Vector3f(0, 0, -2.0f), Color::White, 300.0f);
test_scene.spherical_lights.push_back(&sLight1);
// SFML Window creation
sf::RenderWindow window(sf::VideoMode(image_width, image_height), "Raytracer v0.1");
window.setActive(false);
// Benchmarking of rendering call
std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
render(test_scene, window, 0);
std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
std::cout << "Rendered in " << std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count() << "ms" << "\n";
int frameNo = 1;
// Main SFML loop
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
{
window.close();
EXIT_SUCCESS;
}
if (event.type == sf::Event::MouseButtonPressed)
{
if (event.mouseButton.button == sf::Mouse::Left)
{
std::cout << "\nThe left button was pressed" << std::endl;
std::cout << "--------------------------------------------" << std::endl;
std::cout << "Clicked window x: " << event.mouseButton.x << std::endl;
std::cout << "Clicked window y: " << event.mouseButton.y << std::endl;
const int &x = event.mouseButton.x;
const int &y = event.mouseButton.y;
Ray ray = create_ray(x, y, test_scene);
std::cout << "Ray direction: " << ray.direction << std::endl;
std::cout << "Ray origin: " << ray.origin << std::endl;
HitResult hit = trace(ray, test_scene);
std::cout << "Hit object ID: " << hit.object->debugID << std::endl;
std::cout << "Hit point: " << hit.point << std::endl;
std::cout << "Hit surface normal: " << hit.surface_normal << std::endl;
//hit.object->color = Color::Red;
// Check for collision on the way to our sphereLight
SphericalLight& light = *test_scene.spherical_lights[0];
// Get normalized vector defining the direction from our point to the light
Vector3f direction_to_light = (light.position - hit.point).normalize();
Ray shadow_ray;
shadow_ray.origin = hit.point + (hit.surface_normal * test_scene.shadow_bias);
shadow_ray.direction = direction_to_light;
HitResult shadow_trace = trace(shadow_ray, test_scene);
bool in_light = shadow_trace.object == nullptr; // In Light detection is fucked.
if(!in_light)
shadow_trace.object->color = Color(255, 105, 180);
std::cout << "Point is in light: " << (in_light ? "TRUE" : "FALSE") << std::endl;
}
}
}
window.clear();
sLight1.position = sLight1.position + Vector3f(0, 0.08f, 0.0f);
cam.position = cam.position + Vector3f(0, 0, 0.01f);
test_sphere1.center = test_sphere1.center + Vector3f(0, 0.1, 0.0f);
test_sphere3.center = test_sphere3.center - Vector3f(-0.1, 0, 0.0f);
render(test_scene, window, frameNo);
window.draw(sprite);
window.display();
++frameNo;
}
return 0;
}