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

@@ -1,4 +1,5 @@
#include "Geometry/Mesh.h"
#include "ALgorithm/BSDF.h"
#include "Common/String.h"
#include "Material/SimpleLit.h"
#include "assimp/cimport.h"
@@ -26,10 +27,11 @@ mesh_entity_t mesh_load(const char* filename, scene_t* scene)
struct aiColor4D base_color;
aiGetMaterialColor(src, AI_MATKEY_COLOR_DIFFUSE, &base_color);
float smoothness = 0.5f;
aiGetMaterialFloat(src, AI_MATKEY_SHININESS, &smoothness);
float roughness = 0.5f;
aiGetMaterialFloat(src, AI_MATKEY_SHININESS, &roughness);
roughness = blinn_phong_specular_exponent_to_roughness(roughness);
texture_t* albedo_texture = NULL;
texture_entity_t albedo_entity = invalid_texture_entity();
if (aiGetMaterialTextureCount(src, aiTextureType_DIFFUSE) > 0)
{
struct aiString path;
@@ -47,7 +49,7 @@ mesh_entity_t mesh_load(const char* filename, scene_t* scene)
texture_entity_t entity = texture_load(path.data, true, &scene->textures);
if (entity.id != INVALID_TEXTURE_ID)
{
albedo_texture = &scene->textures.buffer[entity.id];
albedo_entity = entity;
}
}
}
@@ -55,13 +57,14 @@ mesh_entity_t mesh_load(const char* filename, scene_t* scene)
simple_lit_properties_t prop =
{
.albedo = {base_color.r, base_color.g, base_color.b},
.roughness = 1.0f - smoothness,
.roughness = roughness,
.metallic = 0.0f,
.albedo_texture = albedo_texture,
.albedo_texture = albedo_entity,
};
material_create_simple_lit_default(&prop, &scene->materials);
entity.material_count++;
}
for (uint32_t i = 0; i < mesh_scene->mNumMeshes; i++)