Added a new function `blinn_phong_specular_exponent_to_roughness` in `BSDF.h` to convert a specular exponent to roughness. Added a `textures` member to the `shading_context_t` structure in `Material.h` for passing texture information during shading. Added a new member `textures` in the `light_shading_context_t` structure in `Light.h` to hold texture information. Added inline functions in `Texture.h` for handling texture entities, including `invalid_texture_entity`, `is_texture_entity_valid`, and `get_texture`. Changed the texture-related members in `simple_lit_properties_t` in `SimpleLit.h` from pointers to `texture_entity_t` to better manage texture entities. Changed the `sample_material_bsdf` and `sample_material_bsdf_pdf` functions in `Material.h` to accept a `shading_context_t` instead of individual parameters. Changed the `mesh_load` function in `Mesh.c` to use the new roughness calculation and texture entity handling. Changed the `path_trace` function in `PathTracing.c` to use the new structure and functions for handling materials and textures. Refactored the `sample_material_bsdf` and `sample_material_bsdf_pdf` functions in `Material.c` to utilize the new `shading_context_t` structure. Updated the `material_collection_init` function in `Material.h` to reflect changes in the material sampling functions. Updated the `evaluate_bsdf_directional` and `evaluate_bsdf_const_sky` functions to use the new shading context structure. Adjusted the `simple_lit_data_default` function in `SimpleLit.c` to work with the new texture handling approach. Added texture entity handling in `Texture.c` for managing invalid texture entities.
51 lines
1.8 KiB
C
51 lines
1.8 KiB
C
#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)
|
|
{
|
|
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 glms_vec3_mul(sky_color, throughput);
|
|
}
|
|
|
|
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 = ray_create(BIAS_RAY_ORIGION(context->hit_point, context->normal), 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();
|
|
}
|
|
|
|
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);
|
|
|
|
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);
|
|
}
|