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.
This commit is contained in:
@@ -12,11 +12,14 @@ static void get_surface_data(const shading_context_t* context, const void* prope
|
||||
{
|
||||
const simple_lit_properties_t* prop = (simple_lit_properties_t*)properties;
|
||||
|
||||
float camera_distance = glms_vec3_distance(context->camera_position, context->position);
|
||||
vec3s view = (context->camera_direction);
|
||||
|
||||
data_out->albedo = prop->albedo;
|
||||
const texture_t* albedo_texture = get_texture(context->textures, prop->albedo_texture);
|
||||
if (albedo_texture != NULL && albedo_texture->data != NULL)
|
||||
{
|
||||
data_out->albedo = glms_vec3_mul(data_out->albedo, glms_vec3(texture_sample(albedo_texture, context->uv.x, context->uv.y)));
|
||||
data_out->albedo = glms_vec3_mul(data_out->albedo, glms_vec3(texture_sample(albedo_texture, context->uv, view, context->normal, camera_distance)));
|
||||
}
|
||||
|
||||
// NOTE: Currently normal map will produce lots of fireflies, need to be fixed.
|
||||
@@ -25,7 +28,7 @@ static void get_surface_data(const shading_context_t* context, const void* prope
|
||||
const texture_t* normal_texture = get_texture(context->textures, prop->normal_texture);
|
||||
if (normal_texture != NULL && normal_texture->data != NULL)
|
||||
{
|
||||
vec3s normal_sample = glms_vec3(texture_sample(normal_texture, context->uv.x, context->uv.y));
|
||||
vec3s normal_sample = glms_vec3(texture_sample(normal_texture, context->uv, view, context->normal, camera_distance));
|
||||
normal_sample = normal_unpack(normal_sample);
|
||||
data_out->normal = normal_ts_to_ws(normal_sample, context->normal, context->tangent);
|
||||
}
|
||||
@@ -34,14 +37,14 @@ static void get_surface_data(const shading_context_t* context, const void* prope
|
||||
const texture_t* roughness_texture = get_texture(context->textures, prop->roughness_texture);
|
||||
if (roughness_texture != NULL && roughness_texture->data != NULL)
|
||||
{
|
||||
data_out->roughness = data_out->roughness * texture_sample(roughness_texture, context->uv.x, context->uv.y).x;
|
||||
data_out->roughness = data_out->roughness * texture_sample(roughness_texture, context->uv, view, context->normal, camera_distance).x;
|
||||
}
|
||||
|
||||
data_out->metallic = prop->metallic;
|
||||
const texture_t* metallic_texture = get_texture(context->textures, prop->metallic_texture);
|
||||
if (metallic_texture != NULL && metallic_texture->data != NULL)
|
||||
{
|
||||
data_out->metallic = data_out->metallic * texture_sample(metallic_texture, context->uv.x, context->uv.y).x;
|
||||
data_out->metallic = data_out->metallic * texture_sample(metallic_texture, context->uv, view, context->normal, camera_distance).x;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,15 +91,16 @@ static vec3s sample_bsdf_simple_lit(const shading_context_t* context, const surf
|
||||
// We can use a inversion sampling where cos(theta) = powf(random_float(), 1.0f / (specular_exponent + 1.0f)) and phi = 2 * PI * random_float()
|
||||
float specular_exponent = roughness_to_blinn_phong_specular_exponent(surface_data->roughness);
|
||||
|
||||
float theta = acosf(powf(sobol_sample(sample_index, d1), 1.0f / (specular_exponent + 1.0f)));
|
||||
// float theta = acosf(powf(sobol_sample(sample_index, d1), 1.0f / (specular_exponent + 1.0f)));
|
||||
float cos_theta = powf(sobol_sample(sample_index, d1), 1.0f / (specular_exponent + 1.0f));
|
||||
float sin_theta = sin_from_cos(cos_theta);
|
||||
float phi = 2.0f * PI * sobol_sample(sample_index, d2);
|
||||
// float theta = acosf(powf(random_float(), 1.0f / (specular_exponent + 1.0f)));
|
||||
// float phi = 2.0f * (float)M_PI * random_float();
|
||||
|
||||
vec3s h_ts = (vec3s)
|
||||
{
|
||||
sinf(theta) * cosf(phi),
|
||||
sinf(theta) * sinf(phi),
|
||||
cosf(theta)
|
||||
sin_theta * cosf(phi),
|
||||
sin_theta * sinf(phi),
|
||||
cos_theta
|
||||
};
|
||||
|
||||
vec3s tangent_u; // World-space tangent (U)
|
||||
@@ -259,7 +263,6 @@ path_output simple_lit_render_loop(const shading_context_t* properties, const sh
|
||||
vec3s bsdf = evaluate_bsdf_simple_lit(context, &surface_data, output.wi);
|
||||
float cos_theta = fmaxf(0.0f, glms_vec3_dot(output.wi, surface_data.normal));
|
||||
|
||||
output.normal = surface_data.normal;
|
||||
output.bsdf = glms_vec3_scale(bsdf, cos_theta / output.pdf);
|
||||
output.state = PS_NORMAL;
|
||||
return output;
|
||||
|
||||
Reference in New Issue
Block a user