Add texture handling and refactor material functions

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.
This commit is contained in:
2025-04-29 13:29:29 +09:00
parent 3de6b83d32
commit 3c3168af7a
13 changed files with 83 additions and 60 deletions

View File

@@ -12,6 +12,11 @@ float roughness_to_blinn_phong_specular_exponent(float roughness)
return glm_clamp(2.0f * 1.0f / (fmaxf(roughness * roughness, FLT_EPSILON)) - 2.0f, FLT_EPSILON, 1.0f / FLT_EPSILON);
}
float blinn_phong_specular_exponent_to_roughness(float specular_exponent)
{
return sqrtf(2.0f / (specular_exponent + 2.0f));
}
vec3s fresnel_schlick_vec3(vec3s f0, float cos_theta)
{
float x = 1.0f - cos_theta;

View File

@@ -7,11 +7,6 @@
// 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)
{
const triangle_collection_t* triangles = &scene->triangles;
const bvh_tree_t* bvh_tree = &scene->bvh_tree;
const material_collection_t* materials = &scene->materials;
const light_collection_t* lights = &scene->lights;
vec4s accumulated_color = (vec4s){0.0f, 0.0f, 0.0f, 1.0f};
vec3s throughput = glms_vec3_one();
@@ -39,7 +34,7 @@ vec4s path_trace(const scene_t* scene, ray_t ray, uint32_t sample_index, uint16_
}
// Add the emission of the hit material to the accumulated color
material_t* hit_material = &materials->buffer[triangles->buffer[closest_hit.triangle_id].material_id];
const material_t* hit_material = &scene->materials.buffer[scene->triangles.buffer[closest_hit.triangle_id].material_id];
vec3s emission = hit_material->emission;
accumulated_color = glms_vec4_add(accumulated_color, glms_vec4(glms_vec3_mul(throughput, emission), 0.0f));
@@ -51,15 +46,16 @@ vec4s path_trace(const scene_t* scene, ray_t ray, uint32_t sample_index, uint16_
.bounce_depth = depth,
.bvh_tree = bvh_tree,
.material = hit_material
.bvh_tree = &scene->bvh_tree,
.material = hit_material,
.textures = &scene->textures,
};
// Running the light loop.
// TODO: Implementing other light types.
for (uint32_t i = 0; i < lights->directional_light_count; i++)
for (uint32_t i = 0; i < scene->lights.directional_light_count; i++)
{
vec3s l = evaluate_bsdf_directional(lights->directional_lights[i], &light_context, throughput, sample_index);
vec3s l = evaluate_bsdf_directional(scene->lights.directional_lights[i], &light_context, throughput, sample_index);
accumulated_color = glms_vec4_add(accumulated_color, glms_vec4(l, 0.0f));
}
@@ -67,26 +63,28 @@ vec4s path_trace(const scene_t* scene, ray_t ray, uint32_t sample_index, uint16_
accumulated_color = glms_vec4_add(accumulated_color, glms_vec4(sky_light, 0.0f));
// Bounce and prepare for the next iteration
vec3s wo = glms_vec3_negate(active_ray.direction); // We need to negate the direction of the incoming ray
vec3s wi = sample_material_bsdf(hit_material, closest_hit.normal, wo, closest_hit.uv, sample_index, depth, &pdf_bsdf);
shading_context_t shading_context = {
.normal = closest_hit.normal,
.position = closest_hit.point,
.wo = glms_vec3_negate(active_ray.direction),
.uv = closest_hit.uv,
.textures = &scene->textures,
};
vec3s wi = sample_material_bsdf(hit_material, &shading_context, sample_index, depth, &pdf_bsdf);
shading_context.wi = wi;
if (pdf_bsdf <= 0.0f)
{
break;
}
shading_context_t shading_context = {
.normal = closest_hit.normal,
.position = closest_hit.point,
.wi = wi,
.wo = wo,
.uv = closest_hit.uv,
};
vec3s bsdf = evaluate_material_bsdf(hit_material, &shading_context);
float cos_theta = fmaxf(0.0f, glms_vec3_dot(wi, closest_hit.normal));
throughput = glms_vec3_mul(throughput, glms_vec3_scale(bsdf, cos_theta / pdf_bsdf));
// We do Russian roulette to decide whether to continue tracing or terminate the path
// We do Russian roulette to decide whether to continue tracing or terminate the path
if (depth > 1)
{
float q = fminf(glms_vec3_max(throughput), 0.95f);