Files
SimpleRayTracing/header/Material/Material.h
Misaki e1693764f7 Add mipmap support and refactor texture handling
Note: Currently version still have lots of fireflies after applying normal map. And those fireflies are mostly coming from the nee sky. Need to double check the sky cdf and ray intersection.

Changed the `hit_result_t` structure to rename a parameter in `RayIntersection.h`.
Removed the `normal` field from the `path_output` structure in `Common.h`.
Added new fields `screen_size`, `camera_position`, and `camera_direction` in `Material.h`.
Changed the `mipmap_t` structure in `Texture.h` to include a `max_mip` field and modify the `data` field.
Changed the `texture_load` function in `Texture.c` to include a `mipmap` parameter and improve texture data handling.
Changed the `path_trace` function in `PathTracing.c` to update the `shading_context_t` structure and ray creation.
Changed the `evaluate_bsdf_directional` function in `LightEvaluation.c` to modify angular radius calculation.
Changed the `sky_create_hdr_sky` and `evaluate_bsdf_hdr_sky` functions in `SkyLight.c` to enhance texture sampling.
Changed the `get_surface_data` function in `SimpleLit.c` to incorporate camera distance and view direction in calculations.
Changed the `texture_get_pixel` function in `Texture.c` to improve pixel data retrieval.
Changed the `warp_uv` function to use a `vec2s` structure for UV coordinates.
Changed the `texture_sample` function to include additional parameters for improved sampling accuracy.
Changed the `scene_setup` function in `main.c` to adjust sun light intensity and HDRI texture loading.
2025-05-06 17:46:42 +09:00

107 lines
2.8 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;
} 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_entity_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_entity_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_entity_t invalid_material_entity()
{
return (material_entity_t){.id = INVALID_MATERIAL_ID};
}
inline bool is_material_entity_valid(material_entity_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