111 lines
2.9 KiB
C
111 lines
2.9 KiB
C
#ifndef MATERIAL_H
|
|
#define MATERIAL_H
|
|
|
|
#include "Algorithm/BVH.h"
|
|
#include "Algorithm/Sobol.h"
|
|
#include "Common.h"
|
|
#include "Lighting/Light.h"
|
|
#include "Rendering/AOV.h"
|
|
#include "Rendering/Texture.h"
|
|
|
|
#define PROPERTY_SIZE 64
|
|
#define INVALID_MATERIAL_ID 255
|
|
|
|
typedef struct
|
|
{
|
|
vec4s screen_size; // w, h, 1/w, 1/h
|
|
|
|
vec3s camera_position;
|
|
vec3s camera_direction;
|
|
|
|
vec3s position;
|
|
vec3s normal;
|
|
vec3s tangent;
|
|
vec3s wo;
|
|
vec3s throughput;
|
|
|
|
vec2s uv;
|
|
|
|
uint32_t sample_index;
|
|
uint32_t bounce_depth;
|
|
|
|
const bvh_tree_t* bvh_tree;
|
|
const triangle_collection_t* triangles;
|
|
const light_collection_t* lights;
|
|
const texture_collection_t* textures;
|
|
|
|
uint64_t triangle_id;
|
|
float cone_width;
|
|
float spread_angle;
|
|
} shading_context_t;
|
|
|
|
typedef path_output (*material_render_loop_f)(const void* properties, const shading_context_t* context);
|
|
typedef void (*material_render_aov_f)(const void* properties, const shading_context_t* context, aov_output_t* aov_output);
|
|
|
|
typedef struct
|
|
{
|
|
// 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.
|
|
|
|
size_t properties_size;
|
|
void* properties;
|
|
|
|
material_render_loop_f render_loop;
|
|
material_render_aov_f render_aov;
|
|
} material_t;
|
|
|
|
typedef struct
|
|
{
|
|
uint8_t id;
|
|
} material_handle_t;
|
|
|
|
// TODO: Handle material remove, we can use a lookup array.
|
|
// 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_handle_t material_create(const void* properties, size_t properties_size, material_render_loop_f render_loop, material_render_aov_f render_aov, material_collection_t* collection);
|
|
// void material_free(material_entity_t entity, material_collection_t* collection);
|
|
|
|
|
|
inline material_handle_t invalid_material_entity()
|
|
{
|
|
return (material_handle_t){.id = INVALID_MATERIAL_ID};
|
|
}
|
|
|
|
inline bool is_material_entity_valid(material_handle_t entity)
|
|
{
|
|
return entity.id != INVALID_MATERIAL_ID;
|
|
}
|
|
|
|
inline path_output render_material(const material_t* material, const shading_context_t* context)
|
|
{
|
|
if (material == NULL || material->render_loop == NULL)
|
|
{
|
|
return (path_output){0};
|
|
}
|
|
|
|
return material->render_loop(material->properties, context);
|
|
}
|
|
|
|
inline void render_material_aov(const material_t* material, const shading_context_t* context, aov_output_t* aov_output)
|
|
{
|
|
if (material == NULL || material->render_loop == NULL)
|
|
{
|
|
return;
|
|
}
|
|
|
|
material->render_aov(material->properties, context, aov_output);
|
|
}
|
|
|
|
#endif // MATERIAL_H
|