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

@@ -71,36 +71,23 @@ material_entity_t material_create(const void* properties, size_t properties_size
return entity;
}
vec3s sample_material_bsdf(const material_t* material, vec3s normal, vec3s wo, vec2s uv, uint32_t sample_index, uint32_t bounce, float* pdf_out)
vec3s sample_material_bsdf(const material_t* material, const shading_context_t* context, uint32_t sample_index, uint32_t bounce, float* pdf_out)
{
vec3s wi = glms_vec3_zero();
if (material->compute_surface_data != NULL && material->sample_bsdf != NULL)
{
shading_context_t context =
{
.normal = normal,
.wo = wo,
.uv = uv,
};
wi = material->sample_bsdf(&context, material->properties, material->compute_surface_data, sample_index, bounce, pdf_out);
wi = material->sample_bsdf(context, material->properties, material->compute_surface_data, sample_index, bounce, pdf_out);
}
return wi;
}
float sample_material_bsdf_pdf(const material_t* material, vec3s normal, vec3s wo, vec3s wi, vec2s uv)
float sample_material_bsdf_pdf(const material_t* material, const shading_context_t* context)
{
float pdf = 0.0f;
if (material->compute_surface_data != NULL && material->sample_bsdf_pdf != NULL)
{
shading_context_t context =
{
.normal = normal,
.wo = wo,
.wi = wi,
.uv = uv,
};
pdf = material->sample_bsdf_pdf(&context, material->properties, material->compute_surface_data);
pdf = material->sample_bsdf_pdf(context, material->properties, material->compute_surface_data);
}
return pdf;

View File

@@ -12,21 +12,27 @@ void simple_lit_data_default(const shading_context_t* context, const void* prope
simple_lit_data_t* data = (simple_lit_data_t*)data_out;
data->albedo = prop->albedo;
if (prop->albedo_texture != NULL && prop->albedo_texture->data != NULL)
const texture_t* albedo_texture = get_texture(context->textures, prop->albedo_texture);
if (albedo_texture != NULL && albedo_texture->data != NULL)
{
data->albedo = glms_vec3_mul(data->albedo, glms_vec3(texture_sample(prop->albedo_texture, context->uv.x, context->uv.y)));
data->albedo = glms_vec3_mul(data->albedo, glms_vec3(texture_sample(albedo_texture, context->uv.x, context->uv.y)));
}
data->roughness = prop->roughness;
if (prop->roughness_texture != NULL && prop->roughness_texture->data != NULL)
const texture_t* roughness_texture = get_texture(context->textures, prop->roughness_texture);
if (roughness_texture != NULL && roughness_texture->data != NULL)
{
data->roughness = data->roughness * texture_sample(prop->roughness_texture, context->uv.x, context->uv.y).x;
data->roughness = data->roughness * texture_sample(roughness_texture, context->uv.x, context->uv.y).x;
}
data->metallic = prop->metallic;
if (prop->metallic_texture != NULL && prop->metallic_texture->data != NULL)
const texture_t* metallic_texture = get_texture(context->textures, prop->metallic_texture);
if (metallic_texture != NULL && metallic_texture->data != NULL)
{
data->metallic = data->metallic * texture_sample(prop->metallic_texture, context->uv.x, context->uv.y).x;
data->metallic = data->metallic * texture_sample(metallic_texture, context->uv.x, context->uv.y).x;
}
}