Files
SimpleRayTracing/source/Lighting/LightEvaluation.c
Misaki e1693764f7 Add mipmap support and refactor texture handling
Note: Currently version still have lots of fireflies after applying normal map. And those fireflies are mostly coming from the nee sky. Need to double check the sky cdf and ray intersection.

Changed the `hit_result_t` structure to rename a parameter in `RayIntersection.h`.
Removed the `normal` field from the `path_output` structure in `Common.h`.
Added new fields `screen_size`, `camera_position`, and `camera_direction` in `Material.h`.
Changed the `mipmap_t` structure in `Texture.h` to include a `max_mip` field and modify the `data` field.
Changed the `texture_load` function in `Texture.c` to include a `mipmap` parameter and improve texture data handling.
Changed the `path_trace` function in `PathTracing.c` to update the `shading_context_t` structure and ray creation.
Changed the `evaluate_bsdf_directional` function in `LightEvaluation.c` to modify angular radius calculation.
Changed the `sky_create_hdr_sky` and `evaluate_bsdf_hdr_sky` functions in `SkyLight.c` to enhance texture sampling.
Changed the `get_surface_data` function in `SimpleLit.c` to incorporate camera distance and view direction in calculations.
Changed the `texture_get_pixel` function in `Texture.c` to improve pixel data retrieval.
Changed the `warp_uv` function to use a `vec2s` structure for UV coordinates.
Changed the `texture_sample` function to include additional parameters for improved sampling accuracy.
Changed the `scene_setup` function in `main.c` to adjust sun light intensity and HDRI texture loading.
2025-05-06 17:46:42 +09:00

47 lines
1.6 KiB
C

#include "Lighting/LightEvaluation.h"
#include "Algorithm/BSDF.h"
#include "Algorithm/RayIntersection.h"
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 = PS_TERMINATE;
output.pdf = 1.0f;
if (light.intensity <= 0.0f)
{
return output;
}
float angular_radius = glm_rad(light.angular_diameter * 0.5f);
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_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 output;
}
ray_t shadow_ray = ray_create(offset_ray_origin(context->position, context->normal, context->wo), wi);
float closest = FLT_MAX;
hit_result_t shadow_hit = {0};
ray_intersect_bvh_any(&shadow_ray, context->bvh_tree->nodes, context->bvh_tree->primitive_indices, context->bvh_tree->triangles, 0, &shadow_hit);
if (shadow_hit.hit)
{
return output;
}
vec3s light_radiance = glms_vec3_scale(light.color, light.intensity);
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 = PS_NORMAL;
return output;
}