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:
2025-05-06 17:46:35 +09:00
parent 3be0bbc7f3
commit e1693764f7
12 changed files with 366 additions and 104 deletions

View File

@@ -32,6 +32,9 @@ vec4s path_trace(const scene_t* scene, ray_t ray, uint32_t sample_index, uint16_
const material_t* hit_material = &scene->materials.buffer[scene->triangles.buffer[closest_hit.triangle_id].material_id];
shading_context_t shading_context =
{
.camera_position = scene->camera.position,
.camera_direction = glms_vec3_normalize(glms_vec3_sub(closest_hit.point, scene->camera.position)),
.position = closest_hit.point,
.normal = closest_hit.normal,
.tangent = closest_hit.tangent,
@@ -52,7 +55,6 @@ vec4s path_trace(const scene_t* scene, ray_t ray, uint32_t sample_index, uint16_
accumulated_color = glms_vec4_add(accumulated_color, glms_vec4(material_output.direct_lighting, 0.0f));
pdf_bsdf = material_output.pdf;
float cos_theta = fmaxf(0.0f, glms_vec3_dot(material_output.wi, closest_hit.normal));
throughput = glms_vec3_mul(throughput, material_output.bsdf);
// We do Russian roulette to decide whether to continue tracing or terminate the path
@@ -76,7 +78,7 @@ vec4s path_trace(const scene_t* scene, ray_t ray, uint32_t sample_index, uint16_
// active_ray = ray_create(BIAS_RAY_ORIGION(closest_hit.point, glms_vec3_negate(closest_hit.normal)), active_ray.direction);
// continue;
default:
active_ray = ray_create(offset_ray_origin(closest_hit.point, material_output.normal, shading_context.wo), material_output.wi);
active_ray = ray_create(offset_ray_origin(closest_hit.point, closest_hit.normal, shading_context.wo), material_output.wi);
depth++;
break;
}
@@ -99,6 +101,9 @@ void render_aov(const scene_t* scene, ray_t ray, uint32_t sample_index, uint16_t
const material_t* hit_material = &scene->materials.buffer[scene->triangles.buffer[closest_hit.triangle_id].material_id];
shading_context_t shading_context =
{
.camera_position = scene->camera.position,
.camera_direction = glms_vec3_normalize(glms_vec3_sub(closest_hit.point, scene->camera.position)),
.position = closest_hit.point,
.normal = closest_hit.normal,
.tangent = closest_hit.tangent,

View File

@@ -16,7 +16,7 @@ ray_t ray_create(vec3s origin, vec3s direction)
((direction.z < 0.0f) ? 4 : 0)
};
ray.esp = glms_vec3_max(glms_vec3_abs(ray.origin)) * gamma(10);
ray.esp = glms_vec3_max(glms_vec3_abs(ray.origin)) * gamma(15);
return ray;
}
@@ -40,14 +40,16 @@ static inline float next_float_down(float value)
return nextafterf(value, -INFINITY);
}
vec3s offset_ray_origin(vec3s point, vec3s normal, vec3s wo)
vec3s offset_ray_origin(vec3s point, vec3s normal, vec3s w)
{
vec3s abs_normal = glms_vec3_abs(normal);
float c = glms_vec3_max(glms_vec3_abs(point)) * gamma(10);
float d = glms_vec3_dot(abs_normal, (vec3s){c, c, c});
float c = glms_vec3_max(glms_vec3_abs(point)) * gamma(15);
vec3s error = (vec3s){c, c, c};
// float g = gamma(10);
// vec3s error = {fabsf(point.x) * g, fabsf(point.y) * g, fabsf(point.z) * g};
float d = glms_vec3_dot(glms_vec3_abs(normal), error);
vec3s offset = glms_vec3_scale(normal, d);
if (glms_vec3_dot(glms_vec3_negate(wo), normal) < 0.0f)
if (glms_vec3_dot(glms_vec3_negate(w), normal) < 0.0f)
{
offset = glms_vec3_negate(offset);
}