Update project structure and improve performance
Added new files for BVH, AABB, and Debug functionalities. Added new utility functions in Common.h. Added gamma correction function in PostProcessing.h. Changed the return type of path_trace to vec4s for alpha blending. Changed BSDF function signatures to include sample index and bounce. Changed the BSDF.h to replace inline functions with declarations. Changed the Light and SkyLight evaluation functions to include throughput and sample index. Changed the sphere creation function in GeometryUtilities.h for better quality. Changed the scene structure to include a BVH tree for improved ray intersection. Changed the scene initialization parameters for better performance. Created new Debug functions for ray intersection counting. Created new functions for triangle collection management in Triangle.c. Improved pixel updating logic in Window.c. Improved ray intersection performance with new BVH implementation. Removed unused includes from Common.h. Removed old library linking methods in CMakeLists.txt.
This commit is contained in:
75
header/Geometry/AABB.h
Normal file
75
header/Geometry/AABB.h
Normal file
@@ -0,0 +1,75 @@
|
||||
#ifndef AABB_H
|
||||
#define AABB_H
|
||||
|
||||
#include "cglm/struct/vec3.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
vec3s min;
|
||||
vec3s max;
|
||||
} aabb_t;
|
||||
|
||||
inline aabb_t invalid_aabb()
|
||||
{
|
||||
return (aabb_t){
|
||||
.min = {FLT_MAX, FLT_MAX, FLT_MAX},
|
||||
.max = {-FLT_MAX, -FLT_MAX, -FLT_MAX},
|
||||
};
|
||||
}
|
||||
|
||||
inline void aabb_growth(aabb_t* aabb, vec3s point)
|
||||
{
|
||||
aabb->min = glms_vec3_minv(aabb->min, point);
|
||||
aabb->max = glms_vec3_maxv(aabb->max, point);
|
||||
}
|
||||
|
||||
inline void aabb_growth_min_max(aabb_t* aabb, vec3s min, vec3s max)
|
||||
{
|
||||
aabb->min = glms_vec3_minv(aabb->min, min);
|
||||
aabb->max = glms_vec3_maxv(aabb->max, max);
|
||||
}
|
||||
|
||||
inline bool aabb_eq(aabb_t a, aabb_t b)
|
||||
{
|
||||
return glms_vec3_eqv(a.min, b.min) && glms_vec3_eqv(a.max, b.max);
|
||||
}
|
||||
|
||||
inline bool aabb_is_valid(aabb_t aabb)
|
||||
{
|
||||
return aabb.max.x >= aabb.min.x && aabb.max.y >= aabb.min.y && aabb.max.z >= aabb.min.z;
|
||||
}
|
||||
|
||||
inline aabb_t aabb_union(aabb_t a, aabb_t b)
|
||||
{
|
||||
if (!aabb_is_valid(a) && !aabb_is_valid(b))
|
||||
{
|
||||
return invalid_aabb();
|
||||
}
|
||||
else if (!aabb_is_valid(a))
|
||||
{
|
||||
return b;
|
||||
}
|
||||
else if (!aabb_is_valid(b))
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
aabb_t result;
|
||||
result.min = glms_vec3_minv(a.min, b.min);
|
||||
result.max = glms_vec3_maxv(a.max, b.max);
|
||||
return result;
|
||||
}
|
||||
|
||||
inline float aabb_surface_area(aabb_t aabb)
|
||||
{
|
||||
vec3s extent = glms_vec3_sub(aabb.max, aabb.min);
|
||||
return 2.0f * (extent.x * extent.y + extent.x * extent.z + extent.y * extent.z);
|
||||
}
|
||||
|
||||
inline float aabb_volume(aabb_t aabb)
|
||||
{
|
||||
vec3s extent = glms_vec3_sub(aabb.max, aabb.min);
|
||||
return extent.x * extent.y * extent.z;
|
||||
}
|
||||
|
||||
#endif // AABB_H
|
||||
@@ -25,8 +25,8 @@ inline void quad_create(vec3s center, vec3s forward, vec3s up, float size, uint8
|
||||
|
||||
inline void sphere_create(vec3s center, float radius, uint8_t material_id, triangle_collection_t* collection)
|
||||
{
|
||||
int segments = 8;
|
||||
int rings = 8;
|
||||
int segments = 16;
|
||||
int rings = 16;
|
||||
|
||||
for (int i = 0; i < rings; i++)
|
||||
{
|
||||
@@ -59,13 +59,15 @@ inline void sphere_create(vec3s center, float radius, uint8_t material_id, trian
|
||||
center.z + radius * sinf(theta1) * sinf(phi2)
|
||||
};
|
||||
|
||||
vec3s n1 = glms_vec3_normalize(glms_vec3_sub(p1, center));
|
||||
vec3s n2 = glms_vec3_normalize(glms_vec3_sub(p2, center));
|
||||
vec3s n3 = glms_vec3_normalize(glms_vec3_sub(p3, center));
|
||||
vec3s n4 = glms_vec3_normalize(glms_vec3_sub(p4, center));
|
||||
// vec3s n1 = glms_vec3_normalize(glms_vec3_sub(p1, center));
|
||||
// vec3s n2 = glms_vec3_normalize(glms_vec3_sub(p2, center));
|
||||
// vec3s n3 = glms_vec3_normalize(glms_vec3_sub(p3, center));
|
||||
// vec3s n4 = glms_vec3_normalize(glms_vec3_sub(p4, center));
|
||||
|
||||
triangle_create_with_normals(p3, p2, p1, n3, n2, n1, material_id, collection);
|
||||
triangle_create_with_normals(p3, p1, p4, n3, n1, n4, material_id, collection);
|
||||
// triangle_create_with_normals(p3, p2, p1, n3, n2, n1, material_id, collection);
|
||||
// triangle_create_with_normals(p3, p1, p4, n3, n1, n4, material_id, collection);
|
||||
triangle_create(p3, p2, p1, material_id, collection);
|
||||
triangle_create(p3, p1, p4, material_id, collection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,14 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// TODO: Currently transformation does not work because we store every triangle in to the same buffer and when bulding the bvh, we only consider that triangle buffer.
|
||||
// One solution for this is we can have two levels of bvh, one for scene and one for each mesh. The mesh level bvh will apply the transformation to the triangles.
|
||||
// The scene level bvh onlt tells the ray which mesh to check and the mesh level bvh will tell the ray which triangle to check.
|
||||
// For instancing, we may need another mesh_model_t struct to store the actual bvh and triangle data(like id and size), and each mesh_entity_t will have a transformation matrix and the id to that mesh_model_t.
|
||||
// This way we can share the same triangle and bvh for multiple instances of the same mesh, and we can also apply different transformations to each instance.
|
||||
typedef struct
|
||||
{
|
||||
mat4s local_to_world;
|
||||
uint64_t id;
|
||||
uint64_t size;
|
||||
} mesh_entity_t;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define TRIANGLE_H
|
||||
|
||||
#include "Common.h"
|
||||
#include "Geometry/AABB.h"
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct
|
||||
@@ -26,55 +27,26 @@ typedef struct
|
||||
triangle_t* buffer;
|
||||
}triangle_collection_t;
|
||||
|
||||
bool triangle_collection_init(size_t size, triangle_collection_t* triangles);
|
||||
void triangle_collection_resize(triangle_collection_t* collection, size_t size);
|
||||
void triangle_collection_free(triangle_collection_t* collection);
|
||||
|
||||
void triangle_create_with_normals(vec3s point1, vec3s point2, vec3s point3,
|
||||
vec3s normal1, vec3s normal2, vec3s normal3,
|
||||
uint8_t material_id, triangle_collection_t* collection);
|
||||
void triangle_create(vec3s point1, vec3s point2, vec3s point3, uint8_t material_id, triangle_collection_t* collection);
|
||||
|
||||
inline bool triangle_collection_init(size_t size, triangle_collection_t* triangles)
|
||||
inline vec3s triangle_centroid(const triangle_t triangle)
|
||||
{
|
||||
if (size > UINT64_MAX)
|
||||
{
|
||||
size = UINT64_MAX;
|
||||
}
|
||||
|
||||
triangle_collection_t temp = {0};
|
||||
temp.buffer = (triangle_t*)malloc(size * sizeof(triangle_t));
|
||||
if (temp.buffer == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
temp.size = (uint64_t)size;
|
||||
temp.count = 0;
|
||||
|
||||
*triangles = temp;
|
||||
return true;
|
||||
return glms_vec3_scale(glms_vec3_add(glms_vec3_add(triangle.point_1, triangle.point_2), triangle.point_3), 1.0f / 3.0f);
|
||||
}
|
||||
|
||||
inline void triangle_collection_resize(triangle_collection_t* collection, size_t size)
|
||||
inline aabb_t compute_bounds_triangle(triangle_t triangle)
|
||||
{
|
||||
if (size > UINT64_MAX)
|
||||
{
|
||||
size = UINT64_MAX;
|
||||
}
|
||||
|
||||
triangle_t* temp = (triangle_t*)realloc(collection->buffer, size * sizeof(triangle_t));
|
||||
if (temp != NULL)
|
||||
{
|
||||
collection->buffer = temp;
|
||||
collection->size = (uint64_t)size;
|
||||
}
|
||||
aabb_t bounds;
|
||||
bounds.min = glms_vec3_minv(triangle.point_1, glms_vec3_minv(triangle.point_2, triangle.point_3));
|
||||
bounds.max = glms_vec3_maxv(triangle.point_1, glms_vec3_maxv(triangle.point_2, triangle.point_3));
|
||||
return bounds;
|
||||
}
|
||||
|
||||
inline void triangle_collection_free(triangle_collection_t* collection)
|
||||
{
|
||||
if (collection->buffer != NULL)
|
||||
{
|
||||
free(collection->buffer);
|
||||
collection->buffer = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif // TRIANGLE_H
|
||||
|
||||
Reference in New Issue
Block a user