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:
@@ -1,25 +1,32 @@
|
||||
#include "Lighting/LightEvaluation.h"
|
||||
#include "Algorithm/BSDF.h"
|
||||
#include "Algorithm/RayIntersection.h"
|
||||
#include "Algorithm/Sobol.h"
|
||||
|
||||
vec3s evaluate_bsdf_directional( directional_light_t light, const light_shading_context_t* context, sobol_state_t* sobol_state)
|
||||
vec3s evaluate_bsdf_directional(directional_light_t light, const light_shading_context_t* context, vec3s throughput, uint32_t sample_index)
|
||||
{
|
||||
float angular_radius = glm_rad(light.angular_diameter / 2.0f);
|
||||
vec3s wi = random_cosine_direction_angular(light.direction, angular_radius, sobol_state);
|
||||
|
||||
ray_t shadow_ray = {
|
||||
.origin = glms_vec3_add(context->hit_point, glms_vec3_scale(context->normal, 0.001f)),
|
||||
.direction = wi,
|
||||
};
|
||||
uint16_t d1 = sobol_get_dimension(context->bounce_depth, PRNG_LIGHT_U);
|
||||
uint16_t d2 = sobol_get_dimension(context->bounce_depth, PRNG_LIGHT_V);
|
||||
|
||||
hit_result_t shadow_hit = ray_intersect_any(context->triangles, shadow_ray);
|
||||
if (shadow_hit.hit)
|
||||
vec3s wi = random_uniform_cdf_direction_angular(light.direction, sample_index, angular_radius, d1, d2);
|
||||
|
||||
float n_dot_l = glms_vec3_dot(context->normal, wi);
|
||||
if (n_dot_l <= 0.0f)
|
||||
{
|
||||
return glms_vec3_zero();
|
||||
}
|
||||
|
||||
float n_dot_l = glms_vec3_dot(context->normal, light.direction);
|
||||
if (n_dot_l <= 0.0f)
|
||||
ray_t shadow_ray = {
|
||||
.origin = glms_vec3_add(context->hit_point, glms_vec3_scale(context->normal, 1.192092896e-04F)),
|
||||
.direction = wi,
|
||||
};
|
||||
|
||||
float closest = FLT_MAX;
|
||||
hit_result_t shadow_hit = {1};
|
||||
ray_intersect_bvh(shadow_ray, context->bvh_tree->nodes, context->bvh_tree->primitive_indices, context->bvh_tree->triangles, 0,
|
||||
&closest, &shadow_hit);
|
||||
if (shadow_hit.hit)
|
||||
{
|
||||
return glms_vec3_zero();
|
||||
}
|
||||
@@ -31,10 +38,7 @@ vec3s evaluate_bsdf_directional( directional_light_t light, const light_shading_
|
||||
};
|
||||
vec3s bsdf = evaluate_material_bsdf(context->material, &shading_context);
|
||||
|
||||
vec3s light_irradiance = glms_vec3_scale(light.color, light.intensity);
|
||||
float cos_theta = fmaxf(n_dot_l, 0.0f);
|
||||
vec3s irradiance_contrib = glms_vec3_scale(light_irradiance, cos_theta);
|
||||
|
||||
vec3s light_contrib = glms_vec3_scale(glms_vec3_mul(context->throughput, bsdf), cos_theta); // glms_vec3_scale(glms_vec3_mul(throughput, bsdf), cos_theta / 1.0f); pdf for directional light is 1.0f, we can ignore it.
|
||||
return glms_vec3_mul(irradiance_contrib, light_contrib);
|
||||
vec3s light_radiance = glms_vec3_scale(light.color, light.intensity);
|
||||
vec3s light_contribute = glms_vec3_scale(glms_vec3_mul(throughput, bsdf), fmaxf(0.0f, n_dot_l));
|
||||
return glms_vec3_mul(light_radiance, light_contribute);
|
||||
}
|
||||
|
||||
@@ -3,27 +3,34 @@
|
||||
#include "Algorithm/RayIntersection.h"
|
||||
#include "Material/Material.h"
|
||||
|
||||
vec3s evaluate_bsdf_const_sky(const void* data, const light_shading_context_t* context, sobol_state_t* sobol_state)
|
||||
vec3s evaluate_bsdf_const_sky(const void* data, const light_shading_context_t* context, vec3s throughput, uint32_t sample_index)
|
||||
{
|
||||
constant_sky_data_t sky_data = *(const constant_sky_data_t*)data;
|
||||
constant_sky_data_t sky_data = *(const constant_sky_data_t*)data;
|
||||
vec3s sky_color = glms_vec3_scale(sky_data.color, sky_data.intensity);
|
||||
|
||||
if (context == NULL)
|
||||
{
|
||||
return sky_data.color; // No context, return the sky color directly.
|
||||
return glms_vec3_mul(sky_color, throughput);
|
||||
// return sky_data.color;
|
||||
}
|
||||
|
||||
vec3s wi = random_cosine_direction(context->normal, sobol_state);
|
||||
float pdf = pdf_cosine_weighted_hemisphere(context->normal, wi);
|
||||
uint16_t d1 = sobol_get_dimension(context->bounce_depth, PRNG_LIGHT_U);
|
||||
uint16_t d2 = sobol_get_dimension(context->bounce_depth, PRNG_LIGHT_V);
|
||||
|
||||
vec3s wi = random_uniform_cdf_direction(context->normal, sample_index, d1, d2);
|
||||
float pdf = 1.0f / (4.0f * (float)M_PI);
|
||||
|
||||
ray_t shadow_ray = {
|
||||
.origin = glms_vec3_add(context->hit_point, glms_vec3_scale(context->normal, 0.001f)),
|
||||
.origin = glms_vec3_add(context->hit_point, glms_vec3_scale(context->normal, 1.192092896e-04F)),
|
||||
.direction = wi,
|
||||
};
|
||||
|
||||
hit_result_t shadow_hit = ray_intersect_any(context->triangles, shadow_ray);
|
||||
float closest = FLT_MAX;
|
||||
hit_result_t shadow_hit = {1};
|
||||
ray_intersect_bvh(shadow_ray, context->bvh_tree->nodes, context->bvh_tree->primitive_indices, context->bvh_tree->triangles, 0, &closest, &shadow_hit);
|
||||
if (shadow_hit.hit)
|
||||
{
|
||||
return glms_vec3_zero(); // Skip if the ray hits something
|
||||
return glms_vec3_zero();
|
||||
}
|
||||
|
||||
shading_context_t shading_context = {
|
||||
@@ -33,12 +40,12 @@ vec3s evaluate_bsdf_const_sky(const void* data, const light_shading_context_t* c
|
||||
};
|
||||
|
||||
vec3s bsdf = evaluate_material_bsdf(context->material, &shading_context);
|
||||
bsdf = glms_vec3_mul(bsdf, sky_data.color);
|
||||
bsdf = glms_vec3_mul(bsdf, sky_color);
|
||||
float cos_theta = fmaxf(glms_vec3_dot(wi, context->normal), 0.0f);
|
||||
|
||||
float pdf_bsdf = sample_material_bsdf_pdf(context->material, shading_context.normal, shading_context.wo, shading_context.wi);
|
||||
float weight = power_heuristic(pdf, pdf_bsdf);
|
||||
|
||||
vec3s env_contrib = glms_vec3_scale(glms_vec3_mul(context->throughput, bsdf), cos_theta / pdf);
|
||||
vec3s env_contrib = glms_vec3_scale(glms_vec3_mul(throughput, bsdf), cos_theta / pdf);
|
||||
return glms_vec3_scale(env_contrib, weight);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user