-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
360 lines (265 loc) · 9.36 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
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
// System
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <SDL2/SDL.h>
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
// Logging
#define LOG_INFO(TEXT) std::cout << "INFO: " << TEXT << std::endl;
#define LOG_VEC3(TEXT, vec3) std::cout << TEXT << " = " << vec3.x_ << ", " << vec3.y_ << ", " << vec3.z_ << std::endl;
#else
// Logging
#include "easylogging++.h"
INITIALIZE_EASYLOGGINGPP
#define LOG_INFO(TEXT) LOG(INFO) << TEXT;
#define LOG_VEC3(TEXT, vec3) LOG(INFO) << TEXT << " = " << vec3.x_ << ", " << vec3.y_ << ", " << vec3.z_;
#endif
// Raytracer
#include "Vec3.hpp"
#include "Ray.hpp"
#include "TextureImage.hpp"
#include "Camera.hpp"
//Screen dimension constants
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 480;
// Starts up SDL and creates window
bool init();
// Shuts down SDL
void close();
// The window we'll be rendering to
SDL_Window* window_ = NULL;
// The window renderer
SDL_Renderer* renderer_ = NULL;
// SDL Event handler
SDL_Event e;
// Screen texture
TextureImage screenTexture_;
// Timer
float timer = 0;
//Main loop flag
bool quit = false;
// Main camera
Camera camera = Camera(SCREEN_WIDTH, SCREEN_HEIGHT);
int pixelsDoneX = 0;
int pixelsDoneY = 0;
bool finishedRender = false;
bool init() {
//Initialization flag
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
success = false;
} else {
//Set texture filtering to linear
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) ) {
printf( "Warning: Linear texture filtering not enabled!" );
}
//Create window
window_ = SDL_CreateWindow( "Raytracer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( window_ == NULL ) {
printf( "Window could not be created! %s\n", SDL_GetError() );
success = false;
} else {
//Create renderer for window
renderer_ = SDL_CreateRenderer( window_, -1, SDL_RENDERER_ACCELERATED );
if( renderer_ == NULL ) {
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
} else {
//Initialize renderer color
SDL_SetRenderDrawColor( renderer_, 0xFF, 0xFF, 0xFF, 0xFF );
}
}
}
return success;
}
void close() {
//Free loaded images
screenTexture_.free();
//Destroy window
SDL_DestroyRenderer( renderer_ );
SDL_DestroyWindow( window_ );
window_ = NULL;
renderer_ = NULL;
//Quit SDL subsystems
SDL_Quit();
}
Vec3 randInUnitSphere() {
Vec3 p = Vec3(99,99,99);
//LOG_INFO(drand48());
do {
p = 1.0 * Vec3(drand48(), drand48(), drand48()) - Vec3(1,1,1);
} while(p.SquaredLength() >= 1.0 );
return p;
}
struct HitData {
float t;
Vec3 p;
Vec3 normal;
};
bool hitSphere(Vec3 center, float t_min, float t_max, float radius, const Ray ray, HitData &data) {
// Distance between center of sphere and ray origin
Vec3 dis = ray.Origin() - center;
float a = Dot(ray.Direction(), ray.Direction());
float b = Dot(dis, ray.Direction());
float c = Dot(dis, dis) - radius * radius;
float discriminant = b*b - a*c;
if(discriminant > 0) {
float temp = (-b - sqrt(b*b-a*c)) / (a);
if(temp < t_max && temp > t_min) {
// Set data
data.t = temp;
data.p = ray.PointAtParameter(data.t);
data.normal = Vec3(data.p - center).Normalized();
data.normal = 0.5*Vec3(data.normal.x_+1.0f, data.normal.y_+1.0f, data.normal.z_+1.0f);
return true;
}
temp = (-b + sqrt(discriminant)) / a;
if (temp < t_max && temp > t_min) {
data.t = temp;
data.p = ray.PointAtParameter(data.t);
data.normal = (data.p - center) / radius;
return true;
}
}
return false;
}
bool hit(const Ray &ray, float t_min, float t_max, HitData &data) {
// Main sphere
if(hitSphere(Vec3(0.4, 0.0, -1), t_min, t_max, 0.4, ray, data)) {
return true;
}
// Main sphere
if(hitSphere(Vec3(-0.4, 0.0, -1), t_min, t_max, 0.4, ray, data)) {
return true;
}
// Ground
if(hitSphere(Vec3(0.0, 100.4, -1), t_min, t_max, 100.0, ray, data)) {
return true;
}
return false;
}
Vec3 color(const Ray &ray, int depth) {
Vec3 out = Vec3(0,0,0);
HitData data;
if(hit(ray, 0.001, 9999999999.0, data)) {
Vec3 target = data.p + data.normal + randInUnitSphere();
out = 0.5 * color(Ray(data.p, target - data.p), depth+1);
//out = data.normal;
} else {
Vec3 direction = ray.Direction().Normalized();
float t = 0.5f * (direction.y_ + 1.0f);
out = (1.0-t) * Vec3(1.0, 1.0, 1.0) + t * Vec3(0.5, 0.7, 1.0);
}
return out;
}
void loop(void) {
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 ) {
//User requests quit
if( e.type == SDL_QUIT ) {
quit = true;
} else if( e.type == SDL_KEYDOWN ) {
// Any other key down
}
}
if(finishedRender) {
return;
}
if( !screenTexture_.lockTexture() ) {
printf( "Unable to lock Foo' texture!\n" );
} else {
// Edit pixels
Uint32 format = SDL_GetWindowPixelFormat( window_ );
SDL_PixelFormat* mappingFormat = SDL_AllocFormat( format );
// Get pixel data
Uint32* pixels = (Uint32*)screenTexture_.getPixels();
int pixelCount = ( screenTexture_.getPitch() / 4 ) * screenTexture_.getHeight();
if(pixels == nullptr) {
printf( "No pixels returned. \n" );
}
timer += 1;
int pixelsPerFrame = 500;
int pixelsRenderedThisFrame = 0;
bool finishedFrame = false;
// Fill screen
for( int y = pixelsDoneY; y < SCREEN_HEIGHT; ++y ) {
if(finishedFrame) break;
for( int x = pixelsDoneX; x < SCREEN_WIDTH; ++x ) {
if(finishedFrame) break;
int samplesPerRay = 50;
Vec3 col(0,0,0);
// Samples
for( int s = 0; s < samplesPerRay; ++s ) {
float u = (float)(x + drand48()) / float(SCREEN_WIDTH);
float v = (float)(y + drand48()) / float(SCREEN_HEIGHT);
Ray ray = Ray(camera.origin, camera.lowerLeft + (u*camera.horizontal) + (v*camera.vertical));
col += color(ray, 0);
}
col /= samplesPerRay;
// Bring into correct gamma
col = Vec3(sqrt(col.x_), sqrt(col.y_), sqrt(col.z_));
int ir = int(255.99*col.x_);
int ig = int(255.99*col.y_);
int ib = int(255.99*col.z_);
int i = x + SCREEN_WIDTH * y;
Uint32 pixel = SDL_MapRGBA( mappingFormat, ir, ig, ib, 255);
pixels[i] = pixel;
pixelsRenderedThisFrame += 1;
pixelsDoneX = x;
pixelsDoneY = y;
if(pixelsRenderedThisFrame >= pixelsPerFrame) {
finishedFrame = true;
}
}
if(finishedFrame == false) {
pixelsDoneX = 0;
}
}
// See if finished
if(pixelsDoneY >= SCREEN_HEIGHT && pixelsDoneX >= SCREEN_WIDTH) {
//finishedRender = true;
}
//Unlock texture
screenTexture_.unlockTexture();
//Free format
SDL_FreeFormat( mappingFormat );
}
//Clear screen
SDL_SetRenderDrawColor( renderer_, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( renderer_ );
//Modulate and render texture
screenTexture_.render(renderer_, 0, 0 );
//Update screen
SDL_RenderPresent( renderer_ );
}
int main( int argc, char* args[] ) {
#ifndef __EMSCRIPTEN__
// Configure logging
el::Configurations conf("./logging.conf");
el::Loggers::reconfigureAllLoggers(conf);
#endif
LOG_INFO("Raytracer application start");
//Start up SDL and create window
if( !init() ) {
LOG_INFO("Failed to initialize!");
} else {
// Create blank surface to render onto
if( !screenTexture_.createBlank(renderer_, window_, SCREEN_WIDTH, SCREEN_HEIGHT) ) {
LOG_INFO("Failed to create texture for screen");
} else {
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop, 0, 1);
#else
while( !quit ) {
loop();
}
#endif
}
}
//Free resources and close SDL
close();
return 0;
}