81 lines
2.0 KiB
C
81 lines
2.0 KiB
C
#include "Material.h"
|
|
|
|
material_collection_t material_collection_create(size_t size)
|
|
{
|
|
if (size > 254)
|
|
{
|
|
size = 254; // Limit the count to 254 to fit in a uint8_t
|
|
}
|
|
|
|
material_collection_t collection = {0};
|
|
collection.buffer = (material_t*)malloc(size * sizeof(material_t));
|
|
collection.size = (uint8_t)size;
|
|
return collection;
|
|
}
|
|
|
|
void material_collection_resize(material_collection_t* materials, size_t size)
|
|
{
|
|
if (size > 254)
|
|
{
|
|
size = 254; // Limit the count to 254 to fit in a uint8_t
|
|
}
|
|
|
|
material_t* temp = 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_t material_create(const sample_bsdf_f sample, const evaluate_bsdf_f evaluate, void* data, material_collection_t* collection)
|
|
{
|
|
material_t material = {0};
|
|
|
|
if (collection->count >= collection->size)
|
|
{
|
|
material_collection_resize(collection, collection->size * 2);
|
|
}
|
|
|
|
material.sample_bsdf = sample;
|
|
material.evaluate_bsdf = evaluate;
|
|
material.data = data;
|
|
material.id = collection->count;
|
|
|
|
collection->buffer[collection->count] = material;
|
|
collection->count++;
|
|
|
|
return material;
|
|
}
|
|
|
|
vec3s sample_material_bsdf(material_t* material, const vec3s normal, const vec3s wo, float* pdf_out)
|
|
{
|
|
vec3s wi = glms_vec3_zero();
|
|
if (material->sample_bsdf != NULL)
|
|
{
|
|
wi = material->sample_bsdf(material->data, normal, wo, pdf_out);
|
|
}
|
|
|
|
return wi;
|
|
}
|
|
|
|
vec3s evaluate_material_bsdf(material_t* material, const shading_context_t* context)
|
|
{
|
|
vec3s bsdf_color = glms_vec3_zero();
|
|
if (material->evaluate_bsdf != NULL)
|
|
{
|
|
bsdf_color = material->evaluate_bsdf(context, material->data);
|
|
}
|
|
|
|
return bsdf_color;
|
|
}
|