Refactor path tracing and material rendering system

Added a new enum `path_state` and a struct `path_output` in `Common.h` to manage path tracing states and outputs.
Added a new `simple_lit_render_loop` function in `SimpleLit.c` to handle rendering for simple lit materials, integrating light contributions and BSDF evaluations.

Changed the function signatures in `Light.h` to use `path_output` instead of `vec3s` for BSDF evaluations.
Changed the `evaluate_bsdf_directional` function in `LightEvaluation.h` to return `path_output` instead of `vec3s`.
Changed the `evaluate_bsdf_sky` function in `LightEvaluation.h` to return `path_output` and updated its implementation accordingly.
Changed the function signature of `evaluate_bsdf_const_sky` in `SkyLight.h` to return `path_output` and modified its implementation to match.

Removed several old BSDF-related functions in `Material.c` that are no longer needed due to the new structure.

Updated the `material_create` function in `Material.c` to accept the new render loop function pointer.
Updated the `path_trace` function in `PathTracing.c` to utilize the new `path_output` structure for managing light contributions and state transitions during path tracing.
Updated the `evaluate_bsdf_directional` function in `LightEvaluation.c` to return `path_output` and adjusted its internal logic to populate the output structure.
Updated the `evaluate_bsdf_const_sky` function in `SkyLight.c` to return `path_output` and modified its logic to handle light contributions correctly.
Updated the intensity of the sky light to `0.0f` during scene setup in `main.c`, effectively disabling it.
This commit is contained in:
2025-05-01 01:21:39 +09:00
parent 2c0d5a2364
commit 0061609267
13 changed files with 240 additions and 201 deletions

View File

@@ -2,8 +2,11 @@
#include "Algorithm/BSDF.h"
#include "Algorithm/RayIntersection.h"
vec3s evaluate_bsdf_directional(directional_light_t light, const light_shading_context_t* context, vec3s throughput, uint32_t sample_index)
path_output evaluate_bsdf_directional(directional_light_t light, const light_shading_context_t* context, vec3s throughput, uint32_t sample_index)
{
path_output output = {0.0f};
output.state = TERMINATE;
float angular_radius = glm_rad(light.angular_diameter / 2.0f);
uint16_t d1 = sobol_get_dimension(context->bounce_depth, PRNG_LIGHT_U);
@@ -14,7 +17,7 @@ vec3s evaluate_bsdf_directional(directional_light_t light, const light_shading_c
float n_dot_l = glms_vec3_dot(context->normal, wi);
if (n_dot_l <= 0.0f)
{
return glms_vec3_zero();
return output;
}
ray_t shadow_ray = ray_create(BIAS_RAY_ORIGION(context->hit_point, context->normal), wi);
@@ -25,20 +28,14 @@ vec3s evaluate_bsdf_directional(directional_light_t light, const light_shading_c
&closest, &shadow_hit);
if (shadow_hit.hit)
{
return glms_vec3_zero();
return output;
}
shading_context_t shading_context = {
.normal = context->normal,
.wi = wi,
.wo = glms_vec3_negate(context->wo),
.uv = context->uv,
.textures = context->textures,
};
vec3s bsdf = evaluate_material_bsdf(context->material, &shading_context);
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)); // we always assume pdf = 1.0f for directional light
return glms_vec3_mul(light_radiance, light_contribute);
vec3s light_contribute = glms_vec3_scale( throughput, fmaxf(0.0f, n_dot_l)); // we always assume pdf = 1.0f for directional light
output.direct_lighting = glms_vec3_mul(light_radiance, light_contribute);
output.wi = wi;
output.state = NORMAL;
return output;
}

View File

@@ -1,16 +1,18 @@
#include "Lighting/SkyLight.h"
#include "Algorithm/BSDF.h"
#include "Algorithm/RayIntersection.h"
#include "Material/Material.h"
vec3s evaluate_bsdf_const_sky(const void* data, const light_shading_context_t* context, vec3s throughput, uint32_t sample_index)
path_output 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;
vec3s sky_color = glms_vec3_scale(sky_data.color, sky_data.intensity);
path_output output = {0.0f};
output.state = TERMINATE;
if (context == NULL)
{
return glms_vec3_mul(sky_color, throughput);
output.direct_lighting = glms_vec3_mul(sky_color, throughput);
return output;
}
uint16_t d1 = sobol_get_dimension(context->bounce_depth, PRNG_LIGHT_U);
@@ -26,25 +28,15 @@ vec3s evaluate_bsdf_const_sky(const void* data, const light_shading_context_t* c
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();
return output;
}
shading_context_t shading_context = {
.normal = context->normal,
.wi = wi,
.wo = glms_vec3_negate(context->wo),
.uv = context->uv,
.textures = context->textures,
};
vec3s bsdf = evaluate_material_bsdf(context->material, &shading_context);
bsdf = glms_vec3_mul(bsdf, sky_color);
float cos_theta = fmaxf(glms_vec3_dot(wi, context->normal), 0.0f);
output.direct_lighting = glms_vec3_scale(throughput, cos_theta / pdf);
float pdf_bsdf = sample_material_bsdf_pdf(context->material, &shading_context);
float weight = power_heuristic(pdf, pdf_bsdf);
vec3s env_contrib = glms_vec3_scale(glms_vec3_mul(throughput, bsdf), cos_theta / pdf);
return glms_vec3_scale(env_contrib, weight);
output.wi = wi;
output.pdf = pdf;
output.state = NORMAL;
return output;
}