Files
SimpleRayTracing/header/Material/Material.h
Misaki 3c3168af7a 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.
2025-04-29 13:29:29 +09:00

64 lines
2.4 KiB
C

#ifndef MATERIAL_H
#define MATERIAL_H
#include "Algorithm/Sobol.h"
#include "Common.h"
#include "Rendering/Texture.h"
#define PROPERTY_SIZE 64
#define INVALID_MATERIAL_ID 255
typedef struct
{
vec3s normal;
vec3s position;
vec3s wi;
vec3s wo;
vec2s uv;
const texture_collection_t* textures;
} shading_context_t;
typedef void (*compute_surface_data_f)(const shading_context_t* context, const void* properties, void* surface_data_out);
typedef vec3s (*sample_bsdf_f)(const shading_context_t* context, const void* properties, const compute_surface_data_f compute_surface_data, uint32_t index, uint32_t bounce, float* pdf_out);
typedef float (*sample_bsdf_pdf_f)(const shading_context_t* context, const void* properties, const compute_surface_data_f compute_surface_data);
typedef vec3s (*evaluate_bsdf_f)(const shading_context_t* context, const void* properties, const compute_surface_data_f compute_surface_data);
typedef struct
{
char properties[PROPERTY_SIZE];
// TODO: alpha, displacement, etc.
vec3s emission; // We have to have emission outside of data because data is only for bsdf, and emission is not part of bsdf. Maybe another shading properties struct is needed if the number of properties increases.
compute_surface_data_f compute_surface_data;
sample_bsdf_f sample_bsdf;
sample_bsdf_pdf_f sample_bsdf_pdf;
evaluate_bsdf_f evaluate_bsdf;
} material_t;
typedef struct
{
uint8_t id;
} material_entity_t;
// TODO: Handle material remove, we can use something like sparse set.
// NOTE: 255 is invalid
typedef struct
{
uint8_t count;
uint8_t size;
material_t* buffer;
} material_collection_t;
bool material_collection_init(uint8_t size, material_collection_t* materials);
void material_collection_resize(material_collection_t* materials, size_t size);
void material_collection_free(material_collection_t* materials);
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);
vec3s sample_material_bsdf(const material_t* material, const shading_context_t* context, uint32_t sample_index, uint32_t bounce, float* pdf_out);
float sample_material_bsdf_pdf(const material_t* material, const shading_context_t* context);
vec3s evaluate_material_bsdf(const material_t* material, const shading_context_t* context);
#endif // MATERIAL_H