Add HDR files and improve light handling
Added three binary files: `golden_gate_hills_1k.hdr`, `rogland_sunset_1k.hdr`, and `studio_small_03_1k.hdr`. Added a new inline function `weight_nee_light` in `BSDF.h` to compute the weighted contribution of light based on the next event estimation (NEE). Added a new function pointer type `sky_free_f` in `Light.h` for freeing sky light data. Added a new structure `hdr_sky_data_t` in `SkyLight.h` to hold HDR sky data, including texture and intensity. Changed the `RAY_EPSILON` definition in `Common.h` to a new value. Changed the `light_collection_free` function in `Light.h` to include freeing sky light data if it exists. Changed the `sky_create_hdr_sky` function in `SkyLight.h` to initialize HDR sky data and compute marginal and conditional distributions. Changed the `texture_load` function in `Texture.h` to accept a `stride` parameter for different texture formats. Changed the `evaluate_bsdf_directional` function in `LightEvaluation.c` to handle light intensity checks. Changed the `evaluate_bsdf_const_sky` function in `SkyLight.c` to use a pointer for sky data and added checks for intensity. Removed TODO comments related to handling triangle and material removal in `Triangle.h` and `Light.h`. Removed the old `weight_sky_light` function in `SkyLight.h` and replaced it with the new `weight_nee_light` function. Updated the `scene_setup` function in `main.c` to change camera position and light direction, and to load HDR textures. Increased the sample count in the rendering configuration in `main.c` for better quality rendering.
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
|
||||
#define GET_CHANNEL_DATA(pixel, channel, channel_count, default) (channel < channel_count ? pixel[channel] : default) / 255.0f
|
||||
#define GET_CHANNEL_DATA(pixel, channel, channel_count, default, max) (channel < channel_count ? pixel[channel] : default) / max
|
||||
|
||||
bool texture_collection_init(uint16_t size, texture_collection_t* textures)
|
||||
{
|
||||
@@ -64,7 +64,7 @@ void texture_collection_free(texture_collection_t* textures)
|
||||
textures->buffer = NULL;
|
||||
}
|
||||
|
||||
texture_entity_t texture_load(const char* filename, bool srgb, texture_collection_t* textures)
|
||||
texture_entity_t texture_load(const char* filename, bool srgb, stride_t stride, texture_collection_t* textures)
|
||||
{
|
||||
// TODO: This hurts performance, consider using a hash map or similar structure for faster lookups
|
||||
|
||||
@@ -77,26 +77,43 @@ texture_entity_t texture_load(const char* filename, bool srgb, texture_collectio
|
||||
// }
|
||||
|
||||
int width, height, channels;
|
||||
uint8_t* data = stbi_load(filename, &width, &height, &channels, 0);
|
||||
char* data = NULL;
|
||||
|
||||
switch (stride)
|
||||
{
|
||||
case UINT_8:
|
||||
uint8_t* img_data = stbi_load(filename, &width, &height, &channels, 0);
|
||||
if (srgb)
|
||||
{
|
||||
// Convert to linear space if the texture is in sRGB format
|
||||
for (int i = 0; i < width * height * channels; i++)
|
||||
{
|
||||
img_data[i] = (uint8_t)(powf(img_data[i] / 255.0f, 2.2f) * 255.0f);
|
||||
}
|
||||
}
|
||||
data = (char*)img_data;
|
||||
break;
|
||||
|
||||
case UINT_16:
|
||||
data = (char*)stbi_load_16(filename, &width, &height, &channels, 0);
|
||||
break;
|
||||
case FLOAT_32:
|
||||
data = (char*)stbi_loadf(filename, &width, &height, &channels, 0);
|
||||
break;
|
||||
}
|
||||
|
||||
if (data == NULL)
|
||||
{
|
||||
return invalid_texture_entity();
|
||||
}
|
||||
|
||||
if (srgb)
|
||||
{
|
||||
// Convert to linear space if the texture is in sRGB format
|
||||
for (int i = 0; i < width * height * channels; i++)
|
||||
{
|
||||
data[i] = (uint8_t)(powf(data[i] / 255.0f, 2.2f) * 255.0f);
|
||||
}
|
||||
}
|
||||
|
||||
texture_t texture = {0};
|
||||
texture.width = (uint32_t)width;
|
||||
texture.height = (uint32_t)height;
|
||||
texture.channel_count = (uint8_t)channels;
|
||||
texture.stride = stride;
|
||||
texture.data = data;
|
||||
|
||||
texture.wrap_mode = REPEAT;
|
||||
texture.filter_mode = LINEAR;
|
||||
|
||||
@@ -130,20 +147,43 @@ static inline void warp_uv(wrap_mode_t mode, float* u, float* v)
|
||||
|
||||
static vec4s get_pixel_color(const texture_t* texture, uint32_t x, uint32_t y)
|
||||
{
|
||||
uint32_t pixel_index = y * texture->width + x;
|
||||
if (pixel_index >= texture->width * texture->height)
|
||||
if (x >= texture->width || y >= texture->height)
|
||||
{
|
||||
return (vec4s){0.0f, 0.0f, 0.0f, 1.0f};
|
||||
}
|
||||
|
||||
uint8_t* pixel = &texture->data[pixel_index * texture->channel_count];
|
||||
return (vec4s)
|
||||
uint32_t pixel_index = y * texture->width + x;
|
||||
size_t pixel_offset = (size_t)pixel_index * texture->channel_count * texture->stride;
|
||||
|
||||
uint8_t* base8 = (uint8_t*)texture->data + pixel_offset;
|
||||
uint16_t* base16 = (uint16_t*)(texture->data) + ((y * texture->width + x) * texture->channel_count);
|
||||
float* base32 = (float*)(texture->data) + ((y * texture->width + x) * texture->channel_count);
|
||||
|
||||
vec4s out = {0, 0, 0, 1}; // default alpha = 1
|
||||
|
||||
for (int c = 0; c < texture->channel_count && c < 4; ++c)
|
||||
{
|
||||
GET_CHANNEL_DATA(pixel, 0, texture->channel_count, 0),
|
||||
GET_CHANNEL_DATA(pixel, 1, texture->channel_count, 0),
|
||||
GET_CHANNEL_DATA(pixel, 2, texture->channel_count, 0),
|
||||
GET_CHANNEL_DATA(pixel, 3, texture->channel_count, 1)
|
||||
};
|
||||
float value = 0.0f;
|
||||
switch (texture->stride)
|
||||
{
|
||||
case UINT_8:
|
||||
value = base8[c] / 255.0f;
|
||||
break;
|
||||
case UINT_16:
|
||||
value = base16[c] / 65535.0f;
|
||||
break;
|
||||
case FLOAT_32:
|
||||
value = base32[c];
|
||||
break;
|
||||
default:
|
||||
value = (c == 3) ? 1.0f : 0.0f;
|
||||
break;
|
||||
}
|
||||
|
||||
out.raw[c] = value;
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
static vec4s nearest_filter(const texture_t* texture, float u, float v)
|
||||
|
||||
Reference in New Issue
Block a user