forked from jbikker/voxpopuli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.cpp
591 lines (502 loc) · 19.8 KB
/
renderer.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
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#include "precomp.h"
#define WHITE = float4(1.0f);
#define BLACK = float4(0.0f);
// -----------------------------------------------------------
// Initialize the renderer
// -----------------------------------------------------------
void Renderer::Init()
{
frames = 0;
// create fp32 rgb pixel buffer to render to
accumulator = (float4*)MALLOC64(SCRWIDTH * SCRHEIGHT * 16);
memset(accumulator, 0, SCRWIDTH * SCRHEIGHT * 16);
// try to load a camera
FILE* f = fopen("camera.bin", "rb");
if (f)
{
fread(&camera, 1, sizeof(Camera), f);
fclose(f);
}
voxel_objects = new VoxelVolume[VOXELVOLUMES];
//voxel_objects = (VoxelVolume*)MALLOC64( N * N * N * (sizeof(VoxelVolume)) );
//memset(voxel_objects, 0, N * N * N * sizeof(VoxelVolume));
float3 pos = RandomFloat();
voxel_objects[0].min = 0.0f;
voxel_objects[0].max = 1.0f;
voxel_objects[0].model.rotation = float3(0.0f, 0.0f, 0.0f);
voxel_objects[0].model.mat = voxel_objects[0].model.matrix();
voxel_objects[0].model.inv = voxel_objects[0].model.mat.Inverted();
voxel_objects[0].populate_grid();
/*for (int i = 1; i < VOXELVOLUMES; i++)
{
voxel_objects[i].model.translation = pos * float3(i * 20.0f, 0.0f, 0.0f);
voxel_objects[i].model.rotation = float3(0.0f, 0.0f, 0.0f);
voxel_objects[i].model.mat = voxel_objects[0].model.matrix();
voxel_objects[i].model.inv = voxel_objects[0].model.mat.Inverted();
voxel_objects[i].min = 0.0f;
voxel_objects[i].max = 1.0f;
voxel_objects[i].populate_grid();
}*/
/*for (int z = 0; z < N / 3; z++)
{
for (int y = 0; y < 1; y++)
{
for (int x = 0; x < N / 3; x++)
{
int index = (z * (N / 3) * 1) + (y * (N / 3)) + x;
voxel_objects[index].min = 0.0f;
voxel_objects[index].max = 1.0f;
voxel_objects[index].model.translation = float3(x, y, z);
voxel_objects[index].model.mat = voxel_objects[index].model.matrix();
voxel_objects[index].model.inv = voxel_objects[index].model.mat.Inverted();
voxel_objects[index].populate_grid();
}
}
}*/
bvh.construct_bvh(voxel_objects);
//skydome = Skydome();
}
bool activate_lightsaber = false;
float roughness = 0.3f;
float3 point_a = 0.0f;
float3 point_b = 0.0f;
float rad = 0.005f;
float capsule_intersect(Ray& ray, float radius)
{
// Capsule Intersection (Source: https://iqules/intersectors/ilezles.org/artic)
float3 ba = point_b - point_a; // BA vector
float3 oa = ray.O - point_a;
float baba = dot(ba, ba);
float bard = dot(ba, normalize(ray.D));
float baoa = dot(ba, oa);
float rdoa = dot(normalize(ray.D), oa);
float oaoa = dot(oa, oa);
float a = baba - bard * bard;
float b = baba * rdoa - baoa * bard;
float c = baba * oaoa - baoa * baoa - radius * radius * baba;
float h = b * b - a * c;
if (h >= 0.0f)
{
float t = (-b - sqrt(h)) / a;
float t2 = (-b + sqrt(h)) / a;
float y = baoa + t * bard;
// Body
if (y > 0.0f && y < baba)
{
return (t + t2) * 0.5f;
}
// Caps
float3 oc = (y <= 0.0f) ? oa : ray.O - point_b;
b = dot(normalize(ray.D), oc);
c = dot(oc, oc) - radius * radius;
h = b * b - c;
if (h > 0.0f)
{
return ((-b - sqrt(h)) + (-b + sqrt(h))) * 0.5f;
}
}
return -1.0f;
}
float3 light_color = float3(1.0f, 0.0f, 0.0f);
// -----------------------------------------------------------
// Evaluate light transport
// -----------------------------------------------------------
float3 Renderer::Trace(Ray& ray)
{
float outer_rad = rad * 5.0f;
float3 lightsaber_cont = 0.0f;
float intensity = 0.0f;
bool lightsaber_hit = false;
float lightsaber_t = 1e34f;
if (activate_lightsaber)
{
float core_t = capsule_intersect(ray, rad);
float outer_t = capsule_intersect(ray, outer_rad);
float3 min, max;
if (point_a.y > point_b.y)
{
min = point_b;
max = point_a;
}
else
{
min = point_a;
max = point_b;
}
if (core_t >= 0.0f)
{
//return float3(1.0f, 1.0f, 1.0f);
lightsaber_t = core_t * 0.5f;
lightsaber_cont = 1.0f;
intensity = 1.0f;
}
else if (outer_t >= 0.0f)
{
lightsaber_hit = true;
lightsaber_t = outer_t * 0.5f;
float3 p = ray.O + outer_t * normalize(ray.D);
float y_diff = max.y - min.y;
float y_change = p.y - min.y;
float final_t = y_change / y_diff;
float3 center = lerp(min, max, final_t);
float fact = std::max(1.0f - (length(p - center) / outer_rad), 0.0f);
lightsaber_cont = light_color * fact;
intensity = lightsaber_cont.x;
if (lightsaber_cont.y > intensity)
intensity = lightsaber_cont.y;
if (lightsaber_cont.z > intensity)
intensity = lightsaber_cont.z;
}
}
ray.t = 0.0f;
scene.FindNearest(ray, GRIDLAYERS);
if (grid_view)
return ray.steps / 64.0f;
if (ray.voxel == 0)
return skydome.render(ray) * (1.0f - intensity) + lightsaber_cont;
float3 I = ray.O + (ray.t - 0.00001f) * ray.D;
const float3 L = normalize(float3(1, 4, 0.5f));
float3 Normal = ray.GetNormal();
float3 albedo = /*ray.GetAlbedo(scene.voxel_data)*/ float3(1.0f);
float3 final_color = /*albedo;*/ 0.0f;
/*if (ray.voxel == 220)
{
point_a = ray.O + ray.t * ray.D;
point_a += float3(0.02f, 0.1f, 0.0f);
}
point_b = point_a + float3(0.0f, 0.9f, 0.0f);*/
//// Convert u, v to texture coordinates
//VoxelData::Texture tex = scene.voxel_data[ray.voxel].texture;
//float2 uv = ray.GetUV();
//int texelX = uv.x * (tex.width);
//int texelY = uv.y * (tex.height);
//// Calculate index into texture data
//int index = (texelY * tex.width + texelX) * tex.channels;
//// Extract color channels from texture data
//uint8_t r = tex.data[index];
//uint8_t g = tex.data[index + 1];
//uint8_t b = tex.data[index + 2];
//albedo = float3(r, g, b) / 255.0f;
for (size_t i = 0; i < lights.size(); i++)
{
switch (lights[i].type)
{
case LightType::POINT:
{
float3 s_ray_dir = normalize(lights[i].pos - I);
float angle = dot(Normal, s_ray_dir);
if (angle <= 0)
continue;
float dist = length(lights[i].pos - I);
float falloff = max(1 / (dist * dist) - 0.25f, 0.0f);
if (falloff <= 0.0f)
continue;
//Ray shadow_ray = Ray(I, s_ray_dir);
Ray shadow_ray = Ray(lights[i].pos, -s_ray_dir, dist);
if (scene.IsOccluded(shadow_ray, GRIDLAYERS))
continue;
final_color += albedo * lights[i].color * falloff * angle;
}
break;
case LightType::DIRECTIONAL:
{
float angle = dot(Normal, normalize(-lights[i].dir));
if (angle <= 0)
continue;
Ray shadow_ray = Ray(I * lights[i].dir * 1000.0f, -lights[i].dir);
if (scene.IsOccluded(shadow_ray, GRIDLAYERS))
continue;
final_color += albedo * lights[i].color * angle;
}
break;
case LightType::SPOT:
{
// Source: https://math.hws.edu/graphicsbook/c7/s2.html#webgl3d.2.6
float spot_factor = 1.0f;
float3 spot_dir = normalize(lights[i].dir);
float3 s_ray_dir = normalize(lights[i].pos - I);
float a = dot(Normal, s_ray_dir);
if (a <= 0)
continue;
if (lights[i].cutoff_angle <= 0.0f)
continue;
float angle = dot(spot_dir, s_ray_dir);
if (angle >= lights[i].cutoff_angle)
spot_factor = powf(angle, lights[i].spot_exponent);
else
spot_factor = 0.0f;
Ray shadow_ray = Ray(lights[i].pos, -s_ray_dir, length(lights[i].pos - I));
if (scene.IsOccluded(shadow_ray, GRIDLAYERS))
continue;
final_color += albedo * lights[i].color * spot_factor * angle * a;
//float3 spot_dir = lights[i].dir;
//float3 s_ray_dir = lights[i].pos - I;
//float a = dot(Normal, normalize(s_ray_dir));
//if (a <= 0)
// continue;
//float angle = dot(normalize(-spot_dir), normalize(s_ray_dir));
//if (angle <= lights[i].inner_angle) // Outside
// continue;
//float spot_value = (angle - lights[i].inner_angle) / (lights[i].outer_angle - lights[i].inner_angle);
//Ray shadow_ray = Ray(I, s_ray_dir);
//if (scene.IsOccluded(shadow_ray))
// continue;
//final_color += albedo * lights[i].color * spot_value * a;
}
break;
case LightType::AREA:
{
// Monte Carlo Integration
float randomised_f = RandomFloat();
float x = lights[i].radius * cosf(randomised_f) * sinf(randomised_f);
float y = lights[i].radius * sinf(randomised_f) * sinf(randomised_f);
float z = lights[i].radius * cosf(randomised_f);
float3 new_pos = lights[i].pos + float3(x, y, z);
float3 s_ray_dir = normalize(new_pos - I);
float angle = dot(Normal, s_ray_dir);
float dist = length(new_pos - I);
float falloff = max(1 / (dist * dist) - 0.25f, 0.0f);
if (angle <= 0)
continue;
Ray shadow_ray = Ray(new_pos, -s_ray_dir, dist);
if (scene.IsOccluded(shadow_ray, GRIDLAYERS))
continue;
const float pdf = PI * 4 * 2 * lights[i].radius;
final_color += albedo * lights[i].color * falloff * angle / pdf;
}
break;
case LightType::LINE:
{
// Monte Carlo Integration
float randomised_f = RandomFloat();
float3 new_pos = lerp(point_a, point_b, randomised_f);
float3 s_ray_dir = normalize(new_pos - I);
float angle = dot(Normal, s_ray_dir);
float dist = length(new_pos - I);
float falloff = max(1 / (dist * dist) - 0.25f, 0.0f);
if (angle <= 0)
continue;
Ray shadow_ray = Ray(new_pos, -s_ray_dir, dist);
if (scene.IsOccluded(shadow_ray, GRIDLAYERS))
continue;
final_color += albedo * lights[i].color * falloff * angle;
}
break;
default:
break;
}
}
// Reflections (Source: https://jacco.ompf2.com/2022/05/27/how-to-build-a-bvh-part-8-whitted-style/)
float3 dir = normalize(ray.D);
float3 sec_D = dir - 2 * Normal * dot(Normal, dir);
float3 sec_O = I + Normal * 0.001f;
uint random_val = RandomUInt();
sec_D += diffusereflection(Normal, random_val) * roughness;
Ray secondary = Ray(sec_O, normalize(sec_D));
secondary.depth = ray.depth + 1;
if (secondary.depth >= 20)
return float3(0.0f);
return Trace(secondary);
// Ray shadow_ray = Ray(I, normalize(sun_pos - I));
/* visualize normal */ // return (N + 1) * 0.5f;
/* visualize distance */ // return float3( 1 / (1 + ray.t) );
/* visualize albedo */
/*if (ray.t < lightsaber_t)
{
return final_color;
}
return final_color * (1.0f - intensity) + lightsaber_cont;*/
}
// -----------------------------------------------------------
// Main application tick function - Executed once per frame
// -----------------------------------------------------------
void Renderer::Tick(float deltaTime)
{
time += deltaTime;
if (scene.has_changed)
{
frames = 1;
}
//float4 color = 0.0f;
// pixel loop
Timer t;
// lines are executed as OpenMP parallel tasks (disabled in DEBUG)
#pragma omp parallel for schedule(dynamic)
for (int y = 0; y < SCRHEIGHT; y++)
{
// trace a primary ray for each pixel on the line
for (int x = 0; x < SCRWIDTH; x++)
{
// // Generate Random Offsets Within Each Pixel
// float x_offset = Rand(1.0f) - 0.5f;
// float y_offset = Rand(1.0f) - 0.5f;
// // Calculate Sample Position Within The Pixel
// float sample_x = (float)x + 0.5f + x_offset;
// float sample_y = (float)y + 0.5f + y_offset;
// float4 p = float4(Trace(camera.GetPrimaryRay(sample_x, sample_y)), 0);
//
// if (scene.has_changed)
// accumulator[x + y * SCRWIDTH] = p;
// else
// accumulator[x + y * SCRWIDTH] += p;
//
// screen->pixels[x + y * SCRWIDTH] = RGBF32_to_RGB8(&(accumulator[x + y * SCRWIDTH] / float(frames)));
Ray r = camera.GetPrimaryRay(static_cast<float>(x), static_cast<float>(y));
bvh.intersect_bvh(voxel_objects, r, 0);
if (/*r.t < 1e34f*/ r.voxel != 0)
{
//color = float4(1.0f, 1.0f, 1.0f, 0.0f);
screen->pixels[x + y * SCRWIDTH] = RGBF32_to_RGB8(&float4(1.0f, 1.0f, 1.0f, 0.0f));
}
else
{
//color = float4(/*r.steps / 16.0f, */ 0.0f);
screen->pixels[x + y * SCRWIDTH] = RGBF32_to_RGB8(&float4(/*r.steps / 16.0f, */ 0.0f));
}
if (grid_view)
{
//color = float4(r.steps / 32.0f, 0.0f);
screen->pixels[x + y * SCRWIDTH] = RGBF32_to_RGB8(&float4(r.steps / 32.0f, 0.0f));
}
}
}
if (scene.has_changed)
scene.has_changed = false;
// performance report - running average - ms, MRays/s
static float avg = 10, alpha = 1;
avg = (1 - alpha) * avg + alpha * t.elapsed() * 1000;
if (alpha > 0.05f)
alpha *= 0.5f;
float fps = 1000.0f / avg, rps = (SCRWIDTH * SCRHEIGHT) / avg;
printf("%5.2fms (%.1ffps) - %.1fMrays/s\n", avg, fps, rps / 1000);
// handle user input
if (camera.HandleInput(deltaTime))
scene.has_changed = false;
frames++;
}
// -----------------------------------------------------------
// Update user interface (imgui)
// -----------------------------------------------------------
void Renderer::UI()
{
ImGui::SliderFloat("Roughness", &roughness, 0.0f, 1.0f);
ImGui::SliderFloat3("Point A", &point_a.x, 0.0f, 1.0f);
ImGui::SliderFloat3("Point B", &point_b.x, 0.0f, 1.0f);
ImGui::SliderFloat("Radius", &rad, 0.0f, 0.25f);
ImGui::Checkbox("Activate Lightsaber", &activate_lightsaber);
ImGui::Checkbox("Grid View", &grid_view);
if (ImGui::Button("Add Point Light"))
{
lights.push_back(Light(LightType::POINT));
scene.has_changed = true;
}
if (ImGui::Button("Add Directional Light"))
{
lights.push_back(Light(LightType::DIRECTIONAL));
scene.has_changed = true;
}
if (ImGui::Button("Add Spotlight"))
{
lights.push_back(Light(LightType::SPOT));
scene.has_changed = true;
}
if (ImGui::Button("Add Area Light"))
{
lights.push_back(Light(LightType::AREA));
scene.has_changed = true;
}
if (ImGui::Button("Add Line Light"))
{
lights.push_back(Light(LightType::LINE));
scene.has_changed = true;
}
for (size_t i = 0; i < lights.size(); i++)
{
switch (lights[i].type)
{
case LightType::POINT:
{
std::string name = "Point Light " + std::to_string(i);
if (ImGui::CollapsingHeader(name.c_str()))
{
scene.has_changed = true;
ImGui::SliderFloat3("Pos", &lights[i].pos.x, 0.0f, 1.0f);
ImGui::ColorEdit3("Color", &lights[i].color.x, ImGuiColorEditFlags_Float);
if (ImGui::SmallButton("Remove"))
lights.erase(lights.begin() + i);
}
}
break;
case LightType::DIRECTIONAL:
{
std::string name = "Directional Light " + std::to_string(i);
if (ImGui::CollapsingHeader(name.c_str()))
{
scene.has_changed = true;
ImGui::SliderFloat3("Direction", &lights[i].dir.x, -1.0f, 1.0f);
ImGui::ColorEdit3("Color", &lights[i].color.x, ImGuiColorEditFlags_Float);
if (ImGui::SmallButton("Remove"))
lights.erase(lights.begin() + i);
}
}
break;
case LightType::SPOT:
{
std::string name = "Spotlight " + std::to_string(i);
if (ImGui::CollapsingHeader(name.c_str()))
{
scene.has_changed = true;
ImGui::SliderFloat3("Direction", &lights[i].dir.x, -1.0f, 1.0f);
ImGui::SliderFloat3("Pos", &lights[i].pos.x, 0.0f, 1.0f);
/*ImGui::SliderFloat("Inner Angle", &lights[i].inner_angle, 0.0f, 1.0f);
ImGui::SliderFloat("Outer Angle", &lights[i].outer_angle, 1.0f, 2.0f);*/
ImGui::SliderFloat("Cutoff Angle", &lights[i].cutoff_angle, 0.0f, 1.0f);
ImGui::SliderFloat("Spot Exponent", &lights[i].spot_exponent, 0.0f, 100.0f);
ImGui::ColorEdit3("Color", &lights[i].color.x, ImGuiColorEditFlags_Float);
if (ImGui::SmallButton("Remove"))
lights.erase(lights.begin() + i);
}
}
break;
case LightType::AREA:
{
std::string name = "Area Light " + std::to_string(i);
if (ImGui::CollapsingHeader(name.c_str()))
{
scene.has_changed = true;
ImGui::SliderFloat3("Pos", &lights[i].pos.x, 0.0f, 1.0f);
ImGui::SliderFloat("Radius", &lights[i].radius, 0.0f, 1.0f);
ImGui::ColorEdit3("Color", &lights[i].color.x, ImGuiColorEditFlags_Float);
if (ImGui::SmallButton("Remove"))
lights.erase(lights.begin() + i);
}
}
break;
case LightType::LINE:
{
std::string name = "Line Light " + std::to_string(i);
if (ImGui::CollapsingHeader(name.c_str()))
{
scene.has_changed = true;
lights[i].color = light_color;
ImGui::ColorEdit3("Color", &light_color.x, ImGuiColorEditFlags_Float);
if (ImGui::SmallButton("Remove"))
lights.erase(lights.begin() + i);
}
}
break;
default:
break;
}
}
}
// -----------------------------------------------------------
// User wants to close down
// -----------------------------------------------------------
void Renderer::Shutdown()
{
// save current camera
FILE* f = fopen("camera.bin", "wb");
fwrite(&camera, 1, sizeof(Camera), f);
fclose(f);
}