Fixed cdf and added Standard Lit

This commit is contained in:
2025-12-29 22:01:44 +09:00
parent e1693764f7
commit adee5acd10
24 changed files with 830 additions and 570 deletions

View File

@@ -1,6 +1,7 @@
#include "Algorithm/PathTracing.h"
#include "Algorithm/RayIntersection.h"
#include "Lighting/LightEvaluation.h"
#include "Algorithm/BSDF.h"
// TODO: Split the diffuse and specular into different Monte Carlo, so we can decide the sample count for each one
vec4s path_trace(const scene_t* scene, ray_t ray, uint32_t sample_index, uint16_t max_depth)
@@ -9,7 +10,9 @@ vec4s path_trace(const scene_t* scene, ray_t ray, uint32_t sample_index, uint16_
vec3s throughput = glms_vec3_one();
ray_t active_ray = ray;
float pdf_bsdf = 1.0f;
// PDF of the direction that generated the current ray segment (used for MIS on env hits).
float last_bsdf_pdf = 0.0f;
uint16_t depth = 0;
while (depth < max_depth)
@@ -25,11 +28,26 @@ vec4s path_trace(const scene_t* scene, ray_t ray, uint32_t sample_index, uint16_
.textures = &scene->textures,
};
path_output sky_output = evaluate_bsdf_sky(&scene->lights, &light_context, throughput, sample_index);
accumulated_color = glms_vec4_add(accumulated_color, glms_vec4(sky_output.direct_lighting, 0.0f));
// MIS for BSDF-sampled environment hit (for depth==0 camera ray, use weight 1).
float w = 1.0f;
if (depth > 0)
{
float pdf_env = sky_output.pdf;
float pdf_bsdf = last_bsdf_pdf;
if (pdf_env > 0.0f && pdf_bsdf > 0.0f)
{
w = power_heuristic(pdf_bsdf, pdf_env);
}
}
vec3s env_contrib = glms_vec3_scale(sky_output.direct_lighting, w);
accumulated_color = glms_vec4_add(accumulated_color, glms_vec4(env_contrib, 0.0f));
break;
}
const material_t* hit_material = &scene->materials.buffer[scene->triangles.buffer[closest_hit.triangle_id].material_id];
uint8_t material_id = scene->triangles.buffer[closest_hit.triangle_id].material_id;
const material_t* hit_material = &scene->materials.buffer[material_id];
shading_context_t shading_context =
{
.camera_position = scene->camera.position,
@@ -52,10 +70,25 @@ vec4s path_trace(const scene_t* scene, ray_t ray, uint32_t sample_index, uint16_
};
path_output material_output = render_material(hit_material, &shading_context);
if (glms_vec3_isinf(material_output.direct_lighting) || glms_vec3_isnan(material_output.direct_lighting))
{
goto end_path_trace;
}
accumulated_color = glms_vec4_add(accumulated_color, glms_vec4(material_output.direct_lighting, 0.0f));
pdf_bsdf = material_output.pdf;
if (material_output.pdf < FLT_EPSILON)
{
goto end_path_trace;
}
last_bsdf_pdf = material_output.pdf;
throughput = glms_vec3_mul(throughput, material_output.bsdf);
if (glms_vec3_isinf(throughput) || glms_vec3_isnan(throughput))
{
goto end_path_trace;
}
// We do Russian roulette to decide whether to continue tracing or terminate the path
if (depth > 1)