-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
167 lines (132 loc) · 4.54 KB
/
main.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
#include <iostream>
#include <cmath>
#include <memory>
#include <vector>
#include <fstream>
#include <chrono>
#include <SDL2/SDL.h>
#include "common.h"
#include "Camera.h"
#include "Vec3.h"
#include "Color.h"
#include "Ray.h"
#include "Body.h"
#include "Sphere.h"
#include "Light.h"
#include "Ground.h"
#include "World.h"
#include "Plane.h"
Color ray_color(const Ray& r, const World& world, Color last_col, unsigned int depth) {
float t = -1.0; // position along ray of closest hit
std::shared_ptr<Body> hitobj; // object that was hit
for (const std::shared_ptr<Body>& b : world.bodies) {
float dist = b->hit(r);
if (dist > 0.0 && (t < 0.0 || t > dist)) {
hitobj = b;
t = dist;
}
}
if (t > 0.0) { // If the ray hits something
const Vec3 hitpos = r.at(t);
Color final_col = hitobj->color_at(hitpos) * last_col;
const Vec3 hitnorm = hitobj->normal_at(hitpos);
// For each light, combine color
// also check if light is blocked by another object
const float numLightf = static_cast<float>(world.lights.size());
for (const Light& l : world.lights) {
// Get direction to light from hit pos
const Vec3 dir = l.get_pos() - hitpos;
const Vec3 dir_unit = unit_vector(dir);
#ifdef SHADOWS
Ray shadowray = Ray(hitpos, dir_unit);
for (const std::shared_ptr<Body>& b : world.bodies) {
final_col *= !(b->get_id() != hitobj->get_id() && b->hit(shadowray) > 0.0);
//if (b->get_id() != hitobj->get_id() && b->hit(shadowray) > 0.0) {
// return Color(0.0, 0.0, 0.0);
//}
}
#endif // SHADOWS
// dot product with normal = lighting
float d = dot(unit_vector(dir), hitnorm);
d = d < 0.0 ? 0.0 : d;
final_col[0] *= l.get_color()[0] * d / numLightf;
final_col[1] *= l.get_color()[1] * d / numLightf;
final_col[2] *= l.get_color()[2] * d / numLightf;
}
#ifdef REFLECTIONS
// Reflect and call again
if (depth < MAX_REFLECTIONS && hitobj->get_reflectivity() > 0.001) {
const Ray reflected = r.reflect(hitnorm, hitpos, hitobj->get_reflectivity());
final_col = ray_color(reflected, world, final_col, depth+1);
}
#endif // REFLECTIONS
return final_col * r.get_brightness();
}
t = 0.5 * (r.direction().y() + 1.0);
return last_col * ((1.0 - t) * Color(1.0, 1.0, 1.0) + t * Color(0.4, 0.7, 1.0));
}
namespace {
inline Uint32 to_pixel(SDL_Surface* surface, const Color& color) {
return SDL_MapRGBA(surface->format,
static_cast<uint8_t>(color[0] * 255),
static_cast<uint8_t>(color[1] * 255),
static_cast<uint8_t>(color[2] * 255),
255);
}
} // namespace
int main() {
// Following https://raytracing.github.io/books/RayTracingInOneWeekend.html
SDL_Window* window = nullptr;
SDL_Surface* screen_surface = nullptr;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Raytr", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
screen_surface = SDL_GetWindowSurface(window);
// Scene
World world = World();
Camera camera;
camera.calculate_ray_directions();
bool quit = false;
float a = 0.0;
// Time
typedef std::chrono::high_resolution_clock Clock;
Clock::time_point time_now = Clock::now();
Clock::time_point time_last;
//Event handler
SDL_Event event;
#pragma omp parallel
#pragma omp master
{
while(!quit) {
time_last = time_now;
time_now = Clock::now();
std::chrono::duration<float> dt_duration = time_now - time_last;
float dt = dt_duration.count();
std::cout << "FPS: " << 1.0/dt << std::endl;
// Keyboard
while(SDL_PollEvent(&event) != 0) {
quit = quit | (event.type == SDL_QUIT);
camera.handle_event(event);
}
// Update
a += 10.0 * dt;
world.bodies[0]->get_pos().y() = (std::sin(a) / 4.0) + 0.25;
camera.update(dt);
// Draw
// Lock screen image
SDL_LockSurface(screen_surface);
Uint32* buffer = static_cast<Uint32*>(screen_surface->pixels);
#pragma omp taskloop grainsize(DEF_SCREEN_WIDTH)
for (size_t idx = 0; idx < SCREEN_WIDTH * SCREEN_HEIGHT; ++idx) {
const Color ray_color_out = ray_color(camera.make_ray(idx), world, Color(1.0, 1.0, 1.0), 0);
buffer[idx] = to_pixel(screen_surface, ray_color_out);
}
#pragma omp taskwait
SDL_UnlockSurface(screen_surface);
SDL_UpdateWindowSurface(window);
}
} // omp
// Close SDL
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}