Update project structure and improve performance
Added new files for BVH, AABB, and Debug functionalities. Added new utility functions in Common.h. Added gamma correction function in PostProcessing.h. Changed the return type of path_trace to vec4s for alpha blending. Changed BSDF function signatures to include sample index and bounce. Changed the BSDF.h to replace inline functions with declarations. Changed the Light and SkyLight evaluation functions to include throughput and sample index. Changed the sphere creation function in GeometryUtilities.h for better quality. Changed the scene structure to include a BVH tree for improved ray intersection. Changed the scene initialization parameters for better performance. Created new Debug functions for ray intersection counting. Created new functions for triangle collection management in Triangle.c. Improved pixel updating logic in Window.c. Improved ray intersection performance with new BVH implementation. Removed unused includes from Common.h. Removed old library linking methods in CMakeLists.txt.
This commit is contained in:
140
source/main.c
140
source/main.c
@@ -3,10 +3,13 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "Algorithm/Sobol.h"
|
||||
#include "Debug.h"
|
||||
#include "Geometry/GeometryUtilities.h"
|
||||
#include "Geometry/Mesh.h"
|
||||
#include "Lighting/SkyLight.h"
|
||||
#include "Material/SimpleLit.h"
|
||||
#include "Rendering/Scene.h"
|
||||
#include "Geometry/GeometryUtilities.h"
|
||||
#include "Rendering/PostProcessing.h"
|
||||
#include "Window.h"
|
||||
|
||||
static void save_img(render_target_t* source, uint32_t width, uint32_t height, const char* filename)
|
||||
@@ -30,8 +33,8 @@ static void save_img(render_target_t* source, uint32_t width, uint32_t height,
|
||||
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ PWSTR pCmdLine, _In_ int nCmdShow)
|
||||
{
|
||||
rendering_config_t config = {
|
||||
.width = 1280 / 2,
|
||||
.height = 720 / 2,
|
||||
.width = 1920 / 1,
|
||||
.height = 1080 / 1,
|
||||
.sample_count = 64,
|
||||
.max_depth = 4,
|
||||
.bucket_size = 64,
|
||||
@@ -49,47 +52,55 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
|
||||
sobol_init();
|
||||
|
||||
scene_t scene;
|
||||
if (!scene_init(64, 8, 4, &scene))
|
||||
if (!scene_init(67000, 8, 1, &scene))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
scene.camera.position = (vec3s){0.0f, 0.0f, 5.0f};
|
||||
scene.camera.position = (vec3s){-7.5f, 2.0f, 0.0f};
|
||||
scene.camera.forward = glms_vec3_normalize((vec3s){1.0f, 0.0f, 0.0f});
|
||||
|
||||
light_entity_t sun = light_create_directional_light(&scene.lights);
|
||||
directional_light_t* sun_light = &scene.lights.directional_lights[sun.id];
|
||||
sun_light->direction = glms_vec3_normalize((vec3s){0.75f, 0.75f, 1.0f});
|
||||
sun_light->direction = glms_vec3_normalize((vec3s){-0.5f, 1.0f, 0.15f});
|
||||
sun_light->color = (vec3s){1.0f, 0.93f, 0.87f};
|
||||
sun_light->intensity = 3.0f;
|
||||
sun_light->intensity = 2.0f;
|
||||
sun_light->angular_diameter = 0.53f;
|
||||
|
||||
scene.lights.sky_light = sky_create_constant_sky(&(constant_sky_data_t){
|
||||
.color = (vec3s){0.73f, 0.82f, 1.0f}
|
||||
scene.lights.sky_light = sky_create_constant_sky(&(constant_sky_data_t)
|
||||
{
|
||||
.color = (vec3s){0.73f, 0.82f, 1.0f},
|
||||
.intensity = 1.0f,
|
||||
});
|
||||
|
||||
simple_lit_data_t gray_lit_data = {
|
||||
simple_lit_data_t gray_lit_data =
|
||||
{
|
||||
.albedo = (vec3s){0.73f, 0.73f, 0.73f},
|
||||
.roughness = 0.5f,
|
||||
.metallic = 0.0f,
|
||||
};
|
||||
simple_lit_data_t blue_lit_data = {
|
||||
simple_lit_data_t blue_lit_data =
|
||||
{
|
||||
.albedo = (vec3s){0.0f, 0.0f, 1.0f},
|
||||
.roughness = 0.5f,
|
||||
.metallic = 0.0f,
|
||||
};
|
||||
simple_lit_data_t red_lit_data = {
|
||||
simple_lit_data_t red_lit_data =
|
||||
{
|
||||
.albedo = (vec3s){1.0f, 0.0f, 0.0f},
|
||||
.roughness = 0.5f,
|
||||
.metallic = 0.0f,
|
||||
};
|
||||
simple_lit_data_t green_lit_data = {
|
||||
simple_lit_data_t green_lit_data =
|
||||
{
|
||||
.albedo = (vec3s){0.0f, 1.0f, 0.0f},
|
||||
.roughness = 0.5f,
|
||||
.metallic = 0.0f,
|
||||
};
|
||||
simple_lit_data_t floor_lit_data = {
|
||||
simple_lit_data_t floor_lit_data =
|
||||
{
|
||||
.albedo = (vec3s){1.0f, 1.0f, 1.0f},
|
||||
.roughness = 0.75f,
|
||||
.roughness = 0.95f,
|
||||
.metallic = 0.0f,
|
||||
};
|
||||
material_entity_t gray_material = material_create_simple_lit(&gray_lit_data, &scene.materials);
|
||||
@@ -99,57 +110,67 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
|
||||
material_entity_t green_material = material_create_simple_lit(&green_lit_data, &scene.materials);
|
||||
material_entity_t floor_material = material_create_simple_lit(&floor_lit_data, &scene.materials);
|
||||
|
||||
// scene.materials.buffer[gray_light_material.id].emission = (vec3s){10.0f, 10.0f, 10.0f};
|
||||
//scene.materials.buffer[gray_light_material.id].emission = (vec3s){10.0f, 10.0f, 10.0f};
|
||||
|
||||
quad_create(
|
||||
(vec3s){0.0f, 1.95f, 0.0f},
|
||||
(vec3s){0.0f, -1.0f, 0.0f},
|
||||
(vec3s){0.0f, 0.0f, 1.0f},
|
||||
1.0f, gray_light_material.id, &scene.triangles
|
||||
);
|
||||
mesh_load("F:/c/SimpleRayTracer/assets/sponza.obj", floor_material.id, &scene.triangles, &scene.materials);
|
||||
|
||||
// sphere_create(
|
||||
// (vec3s){0.0f, 0.0f, 0.0f},
|
||||
// quad_create(
|
||||
// (vec3s){0.0f, 3.95f, 0.0f},
|
||||
// (vec3s){0.0f, -1.0f, 0.0f},
|
||||
// (vec3s){0.0f, 0.0f, 1.0f},
|
||||
// 1.0f, gray_light_material.id, &scene.triangles
|
||||
// );
|
||||
|
||||
quad_create(
|
||||
(vec3s){0.0f, -2.0f, 0.0f},
|
||||
(vec3s){0.0f, 1.0f, 0.0f},
|
||||
(vec3s){0.0f, 0.0f, 1.0f},
|
||||
4.0f, floor_material.id, &scene.triangles
|
||||
);
|
||||
quad_create(
|
||||
(vec3s){0.0f, 2.0f, 0.0f},
|
||||
(vec3s){0.0f, -1.0f, 0.0f},
|
||||
(vec3s){0.0f, 0.0f, 1.0f},
|
||||
4.0f, gray_material.id, &scene.triangles
|
||||
);
|
||||
quad_create(
|
||||
(vec3s){0.0f, 0.0f, -2.0f},
|
||||
(vec3s){0.0f, 0.0f, 1.0f},
|
||||
(vec3s){0.0f, 1.0f, 0.0f},
|
||||
4.0f, green_material.id, &scene.triangles
|
||||
);
|
||||
quad_create(
|
||||
(vec3s){-2.0f, 0.0f, 0.0f},
|
||||
(vec3s){1.0f, 0.0f, 0.0f},
|
||||
(vec3s){0.0f, 1.0f, 0.0f},
|
||||
4.0f, blue_material.id, &scene.triangles
|
||||
);
|
||||
quad_create(
|
||||
(vec3s){2.0f, 0.0f, 0.0f},
|
||||
(vec3s){-1.0f, 0.0f, 0.0f},
|
||||
(vec3s){0.0f, 1.0f, 0.0f},
|
||||
4.0f, red_material.id, &scene.triangles
|
||||
);
|
||||
// quad_create(
|
||||
// (vec3s){0.0f, 0.0f, 0.0f},
|
||||
// (vec3s){0.0f, 1.0f, 0.0f},
|
||||
// (vec3s){0.0f, 0.0f, 1.0f},
|
||||
// 4.0f, floor_material.id, &scene.triangles
|
||||
// );
|
||||
// quad_create(
|
||||
// (vec3s){0.0f, 4.0f, 0.0f},
|
||||
// (vec3s){0.0f, -1.0f, 0.0f},
|
||||
// (vec3s){0.0f, 0.0f, 1.0f},
|
||||
// 4.0f, gray_material.id, &scene.triangles
|
||||
// );
|
||||
// quad_create(
|
||||
// (vec3s){0.0f, 2.0f, -2.0f},
|
||||
// (vec3s){0.0f, 0.0f, 1.0f},
|
||||
// (vec3s){0.0f, 1.0f, 0.0f},
|
||||
// 4.0f, green_material.id, &scene.triangles
|
||||
// );
|
||||
// quad_create(
|
||||
// (vec3s){-2.0f, 2.0f, 0.0f},
|
||||
// (vec3s){1.0f, 0.0f, 0.0f},
|
||||
// (vec3s){0.0f, 1.0f, 0.0f},
|
||||
// 4.0f, blue_material.id, &scene.triangles
|
||||
// );
|
||||
// quad_create(
|
||||
// (vec3s){2.0f, 2.0f, 0.0f},
|
||||
// (vec3s){-1.0f, 0.0f, 0.0f},
|
||||
// (vec3s){0.0f, 1.0f, 0.0f},
|
||||
// 4.0f, red_material.id, &scene.triangles
|
||||
// );
|
||||
|
||||
#pragma endregion
|
||||
|
||||
render_target_t img;
|
||||
render_target_init(config.width, config.height, &img);
|
||||
// render_target_t img = scene_render(&scene, config);
|
||||
// save_img(&img, config.width, config.height, "output.png");
|
||||
|
||||
if (!scene_build_bvh(&scene))
|
||||
{
|
||||
fprintf(stderr, "Failed to build BVH\n");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
// BVH Debug
|
||||
for (uint32_t i = 0; i < scene.bvh_tree.node_count; i++)
|
||||
{
|
||||
bvh_node_t* node = &scene.bvh_tree.nodes[i];
|
||||
aabb_t bounds = node->bounds;
|
||||
vec3s min = bounds.min;
|
||||
vec3s max = bounds.max;
|
||||
printf("Node %u: Min: (%f, %f, %f), Max: (%f, %f, %f)\n", i, min.x, min.y, min.z, max.x, max.y, max.z);
|
||||
}
|
||||
|
||||
MSG msg;
|
||||
uint16_t tile_index = 0;
|
||||
@@ -169,14 +190,15 @@ int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
|
||||
DispatchMessage(&msg);
|
||||
}
|
||||
|
||||
if (scene_render_tile(&scene, &ctx, &img, config, tile_index, ¤t_tile))
|
||||
if (scene_render_tile(&scene, &ctx, config, tile_index, DEBUG_BVH, &img, ¤t_tile))
|
||||
{
|
||||
for (uint32_t y = current_tile.y; y < current_tile.y + current_tile.height; y++)
|
||||
{
|
||||
for (uint32_t x = current_tile.x; x < current_tile.x + current_tile.width; x++)
|
||||
{
|
||||
vec4s pixel = render_target_get_pixel(&img, x, y);
|
||||
// pixel = aces_tone_map(pixel);
|
||||
pixel = gamma_correct(pixel, 2.2f);
|
||||
pixel = aces_tone_map(pixel);
|
||||
window_update_pixels(pixel, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user