Update rendering, material handling, and shading logic

Changed README.md to update rendering settings and build instructions.
Changed BSDF.h to add functions for normal unpacking and tangent transformation.
Changed RayIntersection.h to include tangent vector in hit_result_t.
Changed Common.h to include vec2.h for 2D vector handling.
Changed String.h to add string_copy function and improve is_absolute_path.
Changed GeometryUtilities.h to enhance quad creation with tangent calculations.
Changed Mesh.h to include tangents in the vertex structure.
Changed Triangle.h to add tangents in the vertex structure for better normal mapping.
Changed Light.h to include tangents in the light shading context.
Changed SkyLight.h to introduce a new structure for sky lights.
Changed Material.h to include tangents in the shading context.
Changed SimpleLit.h to add normal and tangent textures for detailed shading.
Changed Texture.h to introduce a new structure for texture assets.
Changed BSDF.c to add functions for unpacking normals and transforming tangents.
Changed PathTracing.c to include tangents in the shading context.
Changed RayIntersection.c to calculate normals and tangents in ray-triangle intersections.
Changed Mesh.c to improve material texture loading and handle tangents.
Changed Material.c to enhance material collection initialization and resizing.
Changed SimpleLit.c to incorporate normal mapping with normal textures.
Changed Texture.c to improve management of texture assets and resources.
This commit is contained in:
2025-04-29 17:58:10 +09:00
parent 3c3168af7a
commit fb1ff5cac6
21 changed files with 257 additions and 145 deletions

View File

@@ -1,4 +1,6 @@
#include "ALgorithm/BSDF.h"
#include "cglm/util.h"
#include "cglm/struct/mat3.h"
float power_heuristic(float pdf_a, float pdf_b)
{
@@ -24,6 +26,33 @@ vec3s fresnel_schlick_vec3(vec3s f0, float cos_theta)
return glms_vec3_adds(glms_vec3_scale(f0, (1.0f - x5)), x5);
}
vec3s normal_unpack(vec3s normal)
{
vec3s unpacked_normal = glms_vec3_scale(normal, 2.0f);
unpacked_normal = glms_vec3_sub(unpacked_normal, glms_vec3_one());
float dot_xy = glm_clamp_zo(unpacked_normal.x * unpacked_normal.x + unpacked_normal.y * unpacked_normal.y);
unpacked_normal.z = fmaxf(FLT_MIN, sqrtf(1.0f - dot_xy));
return unpacked_normal;
}
vec3s normal_ts_to_ws(vec3s normal, vec3s tangent)
{
vec3s t = glms_vec3_normalize(tangent);
vec3s b = glms_vec3_cross(normal, t);
// Matrix in cglm is column-major, not row-major
mat3s tbn =
{
t.x, b.x, normal.x,
t.y, b.y, normal.y,
t.z, b.z, normal.z
};
return glms_vec3_normalize(glms_mat3_mulv(tbn, normal));
}
// BSDF sampling functions
float pdf_cosine_weighted_hemisphere(vec3s normal, vec3s wi)

View File

@@ -39,10 +39,11 @@ vec4s path_trace(const scene_t* scene, ray_t ray, uint32_t sample_index, uint16_
accumulated_color = glms_vec4_add(accumulated_color, glms_vec4(glms_vec3_mul(throughput, emission), 0.0f));
light_shading_context_t light_context = {
.normal = closest_hit.normal,
.hit_point = closest_hit.point,
.wo = active_ray.direction,
.normal = closest_hit.normal,
.tangent = closest_hit.tangent,
.uv = closest_hit.uv,
.wo = active_ray.direction,
.bounce_depth = depth,
@@ -64,10 +65,11 @@ vec4s path_trace(const scene_t* scene, ray_t ray, uint32_t sample_index, uint16_
// Bounce and prepare for the next iteration
shading_context_t shading_context = {
.normal = closest_hit.normal,
.position = closest_hit.point,
.wo = glms_vec3_negate(active_ray.direction),
.normal = closest_hit.normal,
.tangent = closest_hit.tangent,
.uv = closest_hit.uv,
.wo = glms_vec3_negate(active_ray.direction),
.textures = &scene->textures,
};

View File

@@ -14,7 +14,6 @@ ray_t ray_create(vec3s origin, vec3s direction)
((direction.x < 0.0f) ? 1 : 0) |
((direction.y < 0.0f) ? 2 : 0) |
((direction.z < 0.0f) ? 4 : 0)
,
};
}
@@ -168,12 +167,12 @@ hit_result_t ray_intersect_triangle(const ray_t* ray, const triangle_t* triangle
vec3s v1 = triangle->vertices[1].position;
vec3s v2 = triangle->vertices[2].position;
vec3s E1 = glms_vec3_sub(v1, v0);
vec3s E2 = glms_vec3_sub(v2, v0);
vec3s e1 = glms_vec3_sub(v1, v0);
vec3s e2 = glms_vec3_sub(v2, v0);
// Begin MöllerTrumbore
vec3s P = glms_vec3_cross(direction, E2);
float det = glms_vec3_dot(E1, P);
vec3s P = glms_vec3_cross(direction, e2);
float det = glms_vec3_dot(e1, P);
if (fabsf(det) < FLT_EPSILON)
{
@@ -191,7 +190,7 @@ hit_result_t ray_intersect_triangle(const ray_t* ray, const triangle_t* triangle
}
// Calculate barycentric v
vec3s Q = glms_vec3_cross(T, E1);
vec3s Q = glms_vec3_cross(T, e1);
float v = glms_vec3_dot(direction, Q) * invDet;
if (v < 0.0f || u + v > 1.0f)
{
@@ -199,22 +198,34 @@ hit_result_t ray_intersect_triangle(const ray_t* ray, const triangle_t* triangle
}
// Distance along the ray
float t = glms_vec3_dot(E2, Q) * invDet;
float t = glms_vec3_dot(e2, Q) * invDet;
if (t < RAY_EPSILON)
{
return result;
}
float w = 1.0f - u - v;
result.hit = true;
result.distance = t;
result.point = glms_vec3_add(origin, glms_vec3_scale(direction, t));
// Face normal (unchanged by windings)
vec3s n = triangle->face_normal;
result.normal = glms_vec3_dot(n, direction) < 0.0f ? n : glms_vec3_negate(n);
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));
if (glms_vec3_dot(normal, direction) > 0.0f)
{
normal = glms_vec3_negate(normal);
}
result.normal = glms_vec3_normalize(normal);
vec3s tangent = glms_vec3_scale(triangle->vertices[0].tangent, w);
tangent = glms_vec3_add(tangent, glms_vec3_scale(triangle->vertices[1].tangent, u));
tangent = glms_vec3_add(tangent, glms_vec3_scale(triangle->vertices[2].tangent, v));
result.tangent = glms_vec3_normalize(tangent);
// Interpolate UVs
float w = 1.0f - u - v;
result.uv.x = w * triangle->vertices[0].uv.x
+ u * triangle->vertices[1].uv.x
+ v * triangle->vertices[2].uv.x;