Add HDR sky lighting support and improve rendering

Added support for HDR sky lighting, including sampling environment maps with a hierarchical CDF.
Added new utility functions for handling HDR sky data, including memory management and sampling functions.
Added functions to improve texture handling, including pixel data retrieval and texture coordinate management.
Changed the path tracing algorithm to enhance light evaluation from HDR skies and adjust light contribution calculations.
Changed BSDF sampling functions to utilize constants from Common.h for better readability.
Changed the main application logic to load HDR textures and configure the scene with improved lighting settings.
Refactored ray intersection logic to enhance accuracy and performance in triangle intersections.
Adjusted the sample count in the rendering configuration to optimize performance.
Updated the README.md to document new features and example renders.
This commit is contained in:
2025-05-04 01:33:00 +09:00
parent 9a1069db90
commit 4c62b3ecde
13 changed files with 381 additions and 320 deletions

View File

@@ -63,100 +63,8 @@ vec3s offset_ray_origin(vec3s point, vec3s normal, vec3s wo)
}
return position;
//return point;
}
#if 0
// TODO: We still have small amount of block dots in current implementation. It's because of floating point precision. May need to fall back to double or handle it in a different way.
hit_result_t ray_intersect_triangle(const ray_t* ray, const triangle_t* triangle)
{
hit_result_t result = {0};
vec3s edge1 = glms_vec3_sub(triangle->vertices[1].position, triangle->vertices[0].position);
vec3s edge2 = glms_vec3_sub(triangle->vertices[2].position, triangle->vertices[1].position);
vec3s edge3 = glms_vec3_sub(triangle->vertices[0].position, triangle->vertices[2].position);
vec3s normal = triangle->face_normal;
float n_dot_r = glms_vec3_dot(normal, ray->direction);
if (n_dot_r > 0.0f)
{
normal = glms_vec3_negate(normal);
}
// triangle is parallel to the ray
if (fabsf(n_dot_r) < FLT_EPSILON)
{
return result;
}
// Get distance from ray origin to triangle plane
float distance = (glms_vec3_dot(normal, triangle->vertices[0].position) - glms_vec3_dot(normal, ray->origin)) / glms_vec3_dot(normal, ray->direction);
float eps = gamma(3) * glms_vec3_max(glms_vec3_abs(ray->origin));
if (distance <= eps)
{
return result;
}
vec3s intersection_point = glms_vec3_add(ray->origin, glms_vec3_scale(ray->direction, distance));
// Check if the intersection point is inside the triangle using barycentric coordinates
vec3s vp = glms_vec3_sub(intersection_point, triangle->vertices[0].position);
vec3s vp2 = glms_vec3_sub(intersection_point, triangle->vertices[1].position);
vec3s vp3 = glms_vec3_sub(intersection_point, triangle->vertices[2].position);
vec3s c1 = glms_vec3_cross(edge1, vp);
vec3s c2 = glms_vec3_cross(edge2, vp2);
vec3s c3 = glms_vec3_cross(edge3, vp3);
float n_dot_c1 = glms_vec3_dot(normal, c1);
float n_dot_c2 = glms_vec3_dot(normal, c2);
float n_dot_c3 = glms_vec3_dot(normal, c3);
bool r1 = n_dot_c1 > 0.0f && n_dot_c2 > 0.0f && n_dot_c3 > 0.0f;
bool r2 = n_dot_c1 < 0.0f && n_dot_c2 < 0.0f && n_dot_c3 < 0.0f;
if (!r1 && !r2)
{
return result;
}
// TODO: Normal interpolation
vec3s v0v1 = glms_vec3_sub(triangle->vertices[1].position, triangle->vertices[0].position);
vec3s v0v2 = glms_vec3_sub(triangle->vertices[2].position, triangle->vertices[0].position);
vec3s v0p = glms_vec3_sub(intersection_point, triangle->vertices[0].position);
float d00 = glms_vec3_dot(v0v1, v0v1); // dot(v0v1, v0v1)
float d01 = glms_vec3_dot(v0v1, v0v2); // dot(v0v1, v0v2)
float d11 = glms_vec3_dot(v0v2, v0v2); // dot(v0v2, v0v2)
float d20 = glms_vec3_dot(v0p, v0v1); // dot(v0p, v0v1)
float d21 = glms_vec3_dot(v0p, v0v2); // dot(v0p, v0v2)
float denom = d00 * d11 - d01 * d01;
//if (denom < FLT_EPSILON)
//{
// return result;
//}
float invDenom = 1.0f / denom;
float u = (d11 * d20 - d01 * d21) * invDenom; // This is b1 (weight for V1)
float v = (d00 * d21 - d01 * d20) * invDenom; // This is b2 (weight for V2)
float w = 1.0f - u - v;
//vec3s smooth_normal = glms_vec3_add(glms_vec3_scale(triangle->vertices[0].normal, u), glms_vec3_add(glms_vec3_scale(triangle->vertices[1].normal, v), glms_vec3_scale(triangle->vertices[2].normal, w)));
result.hit = true;
result.point = intersection_point;
result.normal = normal;
result.uv = (vec2s)
{
.x = w * triangle->vertices[0].uv.x + u * triangle->vertices[1].uv.x + v * triangle->vertices[2].uv.x,
.y = w * triangle->vertices[0].uv.y + u * triangle->vertices[1].uv.y + v * triangle->vertices[2].uv.y,
};
result.distance = distance;
return result;
}
#else
hit_result_t ray_intersect_triangle(const ray_t* ray, const triangle_t* triangle)
{
hit_result_t result = {0};
@@ -210,6 +118,7 @@ hit_result_t ray_intersect_triangle(const ray_t* ray, const triangle_t* triangle
result.distance = t;
result.point = glms_vec3_add(origin, glms_vec3_scale(direction, t));
// Should we output u, v, w instead of normal, tangent, and uv?
vec3s normal = glms_vec3_scale(triangle->vertices[0].normal, w);
normal = glms_vec3_add(normal, glms_vec3_scale(triangle->vertices[1].normal, u));
normal = glms_vec3_add(normal, glms_vec3_scale(triangle->vertices[2].normal, v));
@@ -230,7 +139,6 @@ hit_result_t ray_intersect_triangle(const ray_t* ray, const triangle_t* triangle
return result;
}
#endif
bool ray_intersect_aabb(const ray_t* ray, aabb_t aabb, float* enter_out, float* exit_out)
{