Changed README.md to update rendering settings and build instructions. Changed BSDF.h to add functions for normal unpacking and tangent transformation. Changed RayIntersection.h to include tangent vector in hit_result_t. Changed Common.h to include vec2.h for 2D vector handling. Changed String.h to add string_copy function and improve is_absolute_path. Changed GeometryUtilities.h to enhance quad creation with tangent calculations. Changed Mesh.h to include tangents in the vertex structure. Changed Triangle.h to add tangents in the vertex structure for better normal mapping. Changed Light.h to include tangents in the light shading context. Changed SkyLight.h to introduce a new structure for sky lights. Changed Material.h to include tangents in the shading context. Changed SimpleLit.h to add normal and tangent textures for detailed shading. Changed Texture.h to introduce a new structure for texture assets. Changed BSDF.c to add functions for unpacking normals and transforming tangents. Changed PathTracing.c to include tangents in the shading context. Changed RayIntersection.c to calculate normals and tangents in ray-triangle intersections. Changed Mesh.c to improve material texture loading and handle tangents. Changed Material.c to enhance material collection initialization and resizing. Changed SimpleLit.c to incorporate normal mapping with normal textures. Changed Texture.c to improve management of texture assets and resources.
109 lines
2.9 KiB
C
109 lines
2.9 KiB
C
#include "Material/Material.h"
|
|
#include <string.h>
|
|
|
|
bool material_collection_init(uint8_t size, material_collection_t* materials)
|
|
{
|
|
if (size > 254)
|
|
{
|
|
size = 254;
|
|
}
|
|
|
|
material_collection_t temp = {0};
|
|
temp.buffer = (material_t*)malloc(size * sizeof(material_t));
|
|
if (temp.buffer == NULL)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
temp.size = (uint8_t)size;
|
|
temp.count = 0;
|
|
*materials = temp;
|
|
|
|
return true;
|
|
}
|
|
|
|
void material_collection_resize(material_collection_t* materials, size_t size)
|
|
{
|
|
if (size == INVALID_MATERIAL_ID)
|
|
{
|
|
size = INVALID_MATERIAL_ID - 1;
|
|
}
|
|
|
|
if (size == materials->size)
|
|
{
|
|
return;
|
|
}
|
|
|
|
material_t* temp = (material_t*)realloc(materials->buffer, size * sizeof(material_t));
|
|
if (temp != NULL)
|
|
{
|
|
materials->buffer = temp;
|
|
materials->size = (uint8_t)size;
|
|
}
|
|
}
|
|
|
|
void material_collection_free(material_collection_t* materials)
|
|
{
|
|
if (materials->buffer != NULL)
|
|
{
|
|
free(materials->buffer);
|
|
materials->buffer = NULL;
|
|
}
|
|
}
|
|
|
|
material_entity_t material_create(const void* properties, size_t properties_size, compute_surface_data_f surface_data, sample_bsdf_f sample, sample_bsdf_pdf_f sample_pdf, evaluate_bsdf_f evaluate, material_collection_t* collection)
|
|
{
|
|
material_t material = {0};
|
|
|
|
if (collection->count >= collection->size)
|
|
{
|
|
material_collection_resize(collection, collection->size * 2);
|
|
}
|
|
|
|
memcpy(material.properties, properties, properties_size);
|
|
material.compute_surface_data = surface_data;
|
|
material.sample_bsdf = sample;
|
|
material.sample_bsdf_pdf = sample_pdf;
|
|
material.evaluate_bsdf = evaluate;
|
|
|
|
material_entity_t entity = {.id = collection->count};
|
|
|
|
collection->buffer[collection->count] = material;
|
|
collection->count++;
|
|
|
|
return entity;
|
|
}
|
|
|
|
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)
|
|
{
|
|
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, const shading_context_t* context)
|
|
{
|
|
float pdf = 0.0f;
|
|
if (material->compute_surface_data != NULL && material->sample_bsdf_pdf != NULL)
|
|
{
|
|
pdf = material->sample_bsdf_pdf(context, material->properties, material->compute_surface_data);
|
|
}
|
|
|
|
return pdf;
|
|
}
|
|
|
|
vec3s evaluate_material_bsdf(const material_t* material, const shading_context_t* context)
|
|
{
|
|
vec3s bsdf_color = glms_vec3_zero();
|
|
if (material->compute_surface_data != NULL && material->evaluate_bsdf != NULL)
|
|
{
|
|
bsdf_color = material->evaluate_bsdf(context, material->properties, material->compute_surface_data);
|
|
}
|
|
|
|
return bsdf_color;
|
|
}
|