121 BVHNode * node = &nodes[index];
124 if (node->
left != -1)
137 float tx1 = (aabb.
min_x - origin.
x) * inv_dir.
x;
138 float tx2 = (aabb.
max_x - origin.
x) * inv_dir.
x;
139 float ty1 = (aabb.
min_y - origin.
y) * inv_dir.
y;
140 float ty2 = (aabb.
max_y - origin.
y) * inv_dir.
y;
141 float tz1 = (aabb.
min_z - origin.
z) * inv_dir.
z;
142 float tz2 = (aabb.
max_z - origin.
z) * inv_dir.
z;
143 float txmin = fminf(tx1, tx2);
144 float txmax = fmaxf(tx1, tx2);
145 float tymin = fminf(ty1, ty2);
146 float tymax = fmaxf(ty1, ty2);
147 float tzmin = fminf(tz1, tz2);
148 float tzmax = fmaxf(tz1, tz2);
150 float t_enter = fmaxf(txmin, fmaxf(tymin, tzmin));
152 float t_exit = fminf(txmax, fminf(tymax, tzmax));
154 return (t_exit >= fmaxf(0.0f, t_enter) && t_enter < hit->t);
160 float tx1 = (aabb.
min_x - origin.
x) * inv_dir.
x;
161 float tx2 = (aabb.
max_x - origin.
x) * inv_dir.
x;
162 float ty1 = (aabb.
min_y - origin.
y) * inv_dir.
y;
163 float ty2 = (aabb.
max_y - origin.
y) * inv_dir.
y;
164 float tz1 = (aabb.
min_z - origin.
z) * inv_dir.
z;
165 float tz2 = (aabb.
max_z - origin.
z) * inv_dir.
z;
166 float txmin = fminf(tx1, tx2);
167 float txmax = fmaxf(tx1, tx2);
168 float tymin = fminf(ty1, ty2);
169 float tymax = fmaxf(ty1, ty2);
170 float tzmin = fminf(tz1, tz2);
171 float tzmax = fmaxf(tz1, tz2);
173 float t_enter = fmaxf(txmin, fmaxf(tymin, tzmin));
175 float t_exit = fminf(txmax, fminf(tymax, tzmax));
177 if (t_enter > t_exit)
return false;
178 if (t_exit < 0)
return false;
185 float tx1 = (aabb.
min_x - origin.
x) * inv_dir.
x;
186 float tx2 = (aabb.
max_x - origin.
x) * inv_dir.
x;
187 float ty1 = (aabb.
min_y - origin.
y) * inv_dir.
y;
188 float ty2 = (aabb.
max_y - origin.
y) * inv_dir.
y;
189 float tz1 = (aabb.
min_z - origin.
z) * inv_dir.
z;
190 float tz2 = (aabb.
max_z - origin.
z) * inv_dir.
z;
191 float txmin = fminf(tx1, tx2);
192 float txmax = fmaxf(tx1, tx2);
193 float tymin = fminf(ty1, ty2);
194 float tymax = fmaxf(ty1, ty2);
195 float tzmin = fminf(tz1, tz2);
196 float tzmax = fmaxf(tz1, tz2);
198 float t_enter = fmaxf(txmin, fmaxf(tymin, tzmin));
200 float t_exit = fminf(txmax, fminf(tymax, tzmax));
202 if (t_enter > t_exit)
return false;
203 if (t_exit < 0)
return false;
244 float tmin = -INFINITY;
245 float tmax = INFINITY;
250 float invd = 1.0f / local_dir.
x;
251 float t1 = (-obb.
extents.
x - local_orig.
x) * invd;
252 float t2 = ( obb.
extents.
x - local_orig.
x) * invd;
253 if (t1 > t2) {
float tmp = t1; t1 = t2; t2 = tmp; }
254 if (t1 > tmin) tmin = t1;
255 if (t2 < tmax) tmax = t2;
256 if (tmin > tmax)
return false;
262 float invd = 1.0f / local_dir.
y;
263 float t1 = (-obb.
extents.
y - local_orig.
y) * invd;
264 float t2 = ( obb.
extents.
y - local_orig.
y) * invd;
265 if (t1 > t2) {
float tmp = t1; t1 = t2; t2 = tmp; }
266 if (t1 > tmin) tmin = t1;
267 if (t2 < tmax) tmax = t2;
268 if (tmin > tmax)
return false;
274 float invd = 1.0f / local_dir.
z;
275 float t1 = (-obb.
extents.
z - local_orig.
z) * invd;
276 float t2 = ( obb.
extents.
z - local_orig.
z) * invd;
277 if (t1 > t2) {
float tmp = t1; t1 = t2; t2 = tmp; }
278 if (t1 > tmin) tmin = t1;
279 if (t2 < tmax) tmax = t2;
280 if (tmin > tmax)
return false;
283 if (tmax < 0.0f)
return false;
312 if (node->
left == -1 && node->
right == -1)
319 for (
int j = 0; j < node->
count; j++)
335 &tri_t, &hit_pos, &b)) {
344 col = tri_t <
hit->
t;
346 if (col && tri_t < hit->t) {
387 if (node->
left == -1 && node->
right == -1)
392 for (
int i = 0; i < node->
count; i++)
395 if (mn->
mesh == -1 || mn->
free)
continue;
403 vec3 inv_md = (
vec3){1.0f/md.
x, 1.0f/md.
y, 1.0f/md.
z};
413 for (
int j = 0; j < 24; j++)
455 vec3 inv_dir = {(dir.
x == 0) ? 100000.0f : 1.0f/dir.
x, (dir.
y == 0) ? 100000.0f : 1.0f/dir.
y, (dir.
z == 0) ? 100000.0f : 1.0f/dir.
z};
477 for (
int k = 0; k < node->
count; k++)
534 if (node->
left == -1 && node->
right == -1)
546 if (node->
right >= 0)
567 BVHNode * parent = &nodes[pidx];
595 longest = second_longest;
599 longest = third_longest;
615 longest = second_longest;
619 longest = third_longest;
635 longest = second_longest;
639 longest = third_longest;
645 BVHNode left_child = {0}, right_child = {0};
647 right_child.parent = pidx;
663 AABB tr = aabbs[indices[i]];
683 AABB tr = aabbs[indices[i]];
702 AABB tr = aabbs[indices[i]];
719 left_child.
count = left_count;
720 right_child.first_index = i;
721 right_child.count = parent->
count - left_count;
732 AABB * left_aabb = &left_child.
aabb;
736 AABB tr = aabbs[indices[k]];
750 right_child.aabb.min_x = INFINITY;
751 right_child.aabb.min_y = INFINITY;
752 right_child.aabb.min_z = INFINITY;
753 right_child.aabb.max_x = -INFINITY;
754 right_child.aabb.max_y = -INFINITY;
755 right_child.aabb.max_z = -INFINITY;
757 AABB * right_aabb = &right_child.aabb;
759 for (
int k = right_child.first_index; k < right_child.first_index + right_child.count; k++)
761 AABB tr = aabbs[indices[k]];
777 const int split_margin = 0;
778 bool can_split = (right_child.count > split_margin || left_count > split_margin);
779 bool can_leaf = !(right_child.count > split_margin || left_count > split_margin) || axes_tried == 2;
780 bool did_split = (left_count == 0 || right_child.count == 0);
781 if (!can_split || axes_tried == 2)
783 parent = &nodes[pidx];
785 size_t found_indices = 0;
814 parent = &nodes[pidx];
817 left_child.
left = -1;
818 left_child.
right = -1;
823 parent = &nodes[pidx];
827 parent = &nodes[pidx];
829 right_child.
left = -1;
830 right_child.right = -1;
832 *right = right_child;
835 parent = &nodes[pidx];
839 parent = &nodes[pidx];
855 aabb->
min_x = INFINITY;
856 aabb->
min_y = INFINITY;
857 aabb->
min_z = INFINITY;
858 aabb->
max_x = -INFINITY;
859 aabb->
max_y = -INFINITY;
860 aabb->
max_z = -INFINITY;
871 if (mn->
free)
continue;
881 float ex_x = fabsf(aabb->
max_x - aabb->
min_x) / 2;
882 float ex_y = fabsf(aabb->
max_y - aabb->
min_y) / 2;
883 float ex_z = fabsf(aabb->
max_z - aabb->
min_z) / 2;
884 float mid_x = ex_x + aabb->
min_x;
885 float mid_y = ex_y + aabb->
min_y;
886 float mid_z = ex_z + aabb->
min_z;
898 int bvh_indices_count = 0;
901 if (mns[i].mesh == -1 || mns[i].free)
919 root->
count = bvh_indices_count;
940 BVHNode * parent = &nodes[pidx];
967 longest = second_longest;
971 longest = third_longest;
987 longest = second_longest;
991 longest = third_longest;
1005 if (axes_tried == 1)
1007 longest = second_longest;
1011 longest = third_longest;
1015 BVHNode left_child = {0}, right_child = {0};
1016 left_child.
parent = pidx;
1017 right_child.parent = pidx;
1043 MeshFace * m = &faces[bvh_indices[i]];
1044 AABB * aabb = &aabbs[bvh_indices[i]];
1045 if (aabb->
center.
x < split_pos)
1064 MeshFace * m = &faces[bvh_indices[i]];
1065 AABB * aabb = &aabbs[bvh_indices[i]];
1066 if (aabb->
center.
y < split_pos)
1084 MeshFace * m = &faces[bvh_indices[i]];
1085 AABB * aabb = &aabbs[bvh_indices[i]];
1086 if (aabb->
center.
z < split_pos)
1103 left_child.
count = left_count;
1104 right_child.first_index = i;
1105 right_child.count = parent->
count - left_count;
1114 AABB * left_aabb = &left_child.
aabb;
1119 AABB * aabb = &aabbs[bvh_indices[k]];
1132 right_child.aabb.min_x = INFINITY;
1133 right_child.aabb.min_y = INFINITY;
1134 right_child.aabb.min_z = INFINITY;
1135 right_child.aabb.max_x = -INFINITY;
1136 right_child.aabb.max_y = -INFINITY;
1137 right_child.aabb.max_z = -INFINITY;
1139 AABB * right_aabb = &right_child.aabb;
1141 for (
int k = right_child.first_index; k < right_child.first_index + right_child.count; k++)
1144 AABB * aabb = &aabbs[bvh_indices[k]];
1157 const int split_margin = 3;
1158 bool can_split = (right_child.count > split_margin || left_count > split_margin);
1159 bool can_leaf = !(right_child.count > split_margin || left_count > split_margin) || axes_tried == 2;
1160 bool did_split = (left_count == 0 || right_child.count == 0);
1161 if (!can_split || axes_tried == 2)
1163 parent = &nodes[pidx];
1166 size_t found_indices = 0;
1196 parent = &nodes[pidx];
1199 left_child.
left = -1;
1200 left_child.
right = -1;
1205 parent = &nodes[pidx];
1209 parent = &nodes[pidx];
1211 right_child.
left = -1;
1212 right_child.right = -1;
1214 *right = right_child;
1217 parent = &nodes[pidx];
1221 parent = &nodes[pidx];
1252 size_t * indices = malloc(
sizeof(
size_t) * mesh->
tris.
count);
1253 for (
int i = 0; i < mesh->
tris.
count; i++)
1283 BVHNode * parent = &nodes[pidx];
1293 AXIS second_longest;
1309 if (axes_tried == 1)
1311 longest = second_longest;
1315 longest = third_longest;
1329 if (axes_tried == 1)
1331 longest = second_longest;
1335 longest = third_longest;
1349 if (axes_tried == 1)
1351 longest = second_longest;
1355 longest = third_longest;
1363 BVHNode left_child = {0}, right_child = {0};
1364 left_child.
parent = pidx;
1365 right_child.parent = pidx;
1389 AABB tr = aabbs[indices[i]];
1409 AABB tr = aabbs[indices[i]];
1428 AABB tr = aabbs[indices[i]];
1445 left_child.
count = left_count;
1446 right_child.first_index = i;
1447 right_child.count = parent->
count - left_count;
1458 AABB * left_aabb = &left_child.
aabb;
1460 float left_flux = 0.0f;
1461 float left_em_flux = 0.0f;
1464 AABB tr = aabbs[indices[k]];
1465 Light * l = &lights[indices[k]];
1470 left_em_flux += l->
power;
1478 left_flux += l->
power;
1484 left_child.
flux = left_flux;
1487 right_child.aabb.min_x = INFINITY;
1488 right_child.aabb.min_y = INFINITY;
1489 right_child.aabb.min_z = INFINITY;
1490 right_child.aabb.max_x = -INFINITY;
1491 right_child.aabb.max_y = -INFINITY;
1492 right_child.aabb.max_z = -INFINITY;
1494 AABB * right_aabb = &right_child.aabb;
1496 float right_flux = 0.0f;
1497 float right_em_flux = 0.0f;
1498 for (
int k = right_child.first_index; k < right_child.first_index + right_child.count; k++)
1501 AABB tr = aabbs[indices[k]];
1502 Light * l = &lights[indices[k]];
1507 right_em_flux += l->
power;
1515 right_flux += l->
power;
1520 right_child.flux = right_flux;
1521 right_child.emissive_flux = right_em_flux;
1526 const int split_margin = 3;
1527 bool can_split = (right_child.count > split_margin || left_count > split_margin);
1528 bool can_leaf = !(right_child.count > split_margin || left_count > split_margin) || axes_tried == 2;
1529 bool did_split = (left_count == 0 || right_child.count == 0);
1530 if (!can_split || axes_tried == 2)
1532 parent = &nodes[pidx];
1534 size_t found_indices = 0;
1563 parent = &nodes[pidx];
1566 left_child.
left = -1;
1567 left_child.
right = -1;
1572 parent = &nodes[pidx];
1576 parent = &nodes[pidx];
1578 right_child.
left = -1;
1579 right_child.right = -1;
1581 *right = right_child;
1584 parent = &nodes[pidx];
1588 parent = &nodes[pidx];
1609 parent_aabb.
min_x = INFINITY;
1610 parent_aabb.
min_y = INFINITY;
1611 parent_aabb.
min_z = INFINITY;
1612 parent_aabb.
max_x = -INFINITY;
1613 parent_aabb.
max_y = -INFINITY;
1614 parent_aabb.
max_z = -INFINITY;
1616 float total_flux = 0.0f;
1617 float emissive_flux = 0.0f;
1619 uint32_t directional_lights = 0;
1624 float lum = (l->
rgb.
x * 0.2126f) + (l->
rgb.
y * 0.7152f) + (l->
rgb.
z * 0.0722f);
1628 new_light->
lum = lum;
1634 new_light->
power = power;
1640 new_light->
power = power;
1645 new_light->
power = 0.0f;
1646 directional_lights++;
1662 total_flux += power;
1675 matem[i] = em_len * str;
1682 if (mn->
mesh == -1 || mn->
free)
continue;
1708 float lum = (l->
rgb.
x * 0.2126f) + (l->
rgb.
y * 0.7152f) + (l->
rgb.
z * 0.0722f);
1725 total_flux += power;
1727 emissive_flux += power;
1758 aabbs[i] = new_lights[i].
area;
1769 root->
aabb = parent_aabb;
1771 root->
flux = total_flux;
void init_scene_bvh()
Initializes or updates the pre-existing scene bvh based on all mesh nodes with valid meshes.
RayHit raycast_scene(vec3 start, vec3 dir, float initial_t, bool check_normal)
Raycasts through the scene and finds closest intersection to meshes.
bool ray_intersects_aabb(AABB aabb, vec3 origin, vec3 dir, vec3 inv_dir)
Checks if a ray intersects an aabb.
void process_collision_node(Mesh *m, MeshNode *mn, BVHNode *node, vec3 proposed, CollisionPoints *p)
Processes Player collision against a static MeshNode's triangle BVH node.
bool subdivide_scene_bvh(Vector *bvh_nodes, size_t *indices, AABB *aabbs, int pidx, int depth, int axes_tried)
Subdivides the SceneData's MeshNodes into a BVH.
bool BVH_slab_test_aabb(RayHit *hit, AABB aabb, vec3 origin, vec3 inv_dir)
Slab test for BVH.
void ray_traverse_mesh_bvh(Mesh *m, MeshNode *mn, BVHNode *nodes, vec3 inv_pos, vec3 mod_dir, vec3 inv_dir, RayHit *hit, bool check_normal)
Traverses a Mesh's triangle BVH.
bool subdivide_light_bvh(Vector *bvh_nodes, size_t *indices, AABB *aabbs, Light *lights, int pidx, int depth, int axes_tried)
Subdivides the SceneData's Lights into a BVH.
void ray_traverse_scene_bvh(SceneData *sd, BVHNode *nodes, vec3 origin, vec3 dir, vec3 mod_dir, vec3 inv_dir, RayHit *hit, bool check_normal)
Traverses a SceneData's MeshNode BVH.
void init_mesh_tri_bvh(Mesh *mesh)
Initializes the bvh for a mesh's triangles.
void build_light_bvh()
Initializes a bvh for all current lights and emissive triangles in the scene, and sets the flux up fo...
void assign_next_nodes(BVHNode *nodes, int index, int next)
Assigns next nodes for stackless traversal top-down.
void swap_size_t(size_t *a, size_t *b)
Swaps a and b.
bool ray_intersects_obb_with_point(OBB obb, vec3 origin, vec3 dir, vec3 *intersection_point)
Checks if a ray intersects an obb and outputs an intersection point.
bool subdivide_mesh_tri_bvh(Mesh *mesh, int pidx, int depth, int axes_tried)
Subdivides the Mesh's triangles into a BVH.
bool ray_intersects_aabb_with_point(AABB aabb, vec3 origin, vec3 dir, vec3 inv_dir, vec3 *intersection_point)
Checks if a ray intersects an aabb and outputs an intersection point.
bool traverse_collision_bvh(Mesh *m, MeshNode *mn, BVHNode *nodes, BVHNode *node, vec3 proposed, vec3 inv_p, CollisionPoints *p)
Traverses the mesh bvh to find collisions based on point vs aabb within a certain epsilon for player ...
RayHit ray_intersects_triangle(vec3 origin, vec3 dir, vec3 a, vec3 b, vec3 c)
bool collision(vec3 proposed, vec3 pts[3], vec3 normal, CollisionPoints *p)
vec3 rot(vec3 M[3], vec3 a)
Refer to Ken Perlin's 2 joint IK implmentation.
AABBBounds transform_aabb_by_model_matrix(MeshNode mn, mat4 mat)
LightBVH light_bvh
The LightBVH, required for baking lighting.
Vector mesh_nodes
MeshNodes.
Vector lights
The Lights that were created (emissive triangles become AREA_LIGHTS).
SceneData scene_data
SceneData that is more-so scene specific.
Globals GLOBALS
Engine Globals.
Vector materials
Materials.
AABB aabb
AABB over the entire scene.
size_t * indices
The indices sorted when initialized.
Vector nodes
Each node of the BVH.
size_t indices_count
The amount of indices on init (some lights are omitted).
The data for the current scene.
vec3 normal
The collision normal from B into A.
Points that track collision data such as direction, penetration depth, and more to generate Manifolds...
float intensity
The intensity of the Light.
size_t id
The ID inside of Globals.mesh_nodes.
vec3 pos
Position of light.
LightType light_type
The type of Light.
float lum
The luminosity for importance sampling.
PhysicsComponent * pc
The physics controller initialized based on data from this MeshNode.
float surface_area
The surface area of the triangle.
float power
The power for importance sampling.
int tri
The MeshFace this corresponds to.
float outer_angle
Outer angle for SPOT_LIGHTs.
bool free
If the MeshNode can be replaced.
int mesh
The Mesh it references.
float radius
The radius of the Light.
int mesh
The MeshNode this corresponds to.
vec3 rgb
Color of the light.
bool static_mesh
If this mesh is static (DrawArrays) or dynamic (DrawElements), used to indicate Lightmap status.
float threshold
The threshold (how far a light reaches).
AABB area
The physical area of AREA_LIGHTs.
The CPU side structure for Lights.
The Materials parsed in from scene files that determine rendering pipeline setup, uploads,...
MeshNodes that describe the position of Meshes, GLTF nodes for GLTFAnimations, or hold references to ...
Important globals for the entire engine and initialization functions that are mainly called on engine...
bool static_mesh
This Mesh is static and must create a BVH or if not it must perform incremental_hull.
size_t * bvh_indices
Indices for the BVH, sorted as the BVH is generated.
AABB aabb
The AABB of the mesh.
Vector tri_aabbs
AABBs for each triangle inside of this Mesh.
Vector tris
The MeshFaces.
Vector vertices
The Points.
A Mesh with geometry data that is either initialized as static or dynamic and split into SubMeshes.
DebugVertex DEBUG_LINES[DEBUG_LINES_MAX]
float vec3_dot(vec3 a, vec3 b)
vec3 vec3_normalize(vec3 v)
vec3 vec3_scale(vec3 a, float b)
vec3 vec3_sub(vec3 a, vec3 b)
float vec4_length(vec4 v)
vec3 vec3_add(vec3 a, vec3 b)
bool m4_inverse(float out[16], const float m[16])
float vec3_length(vec3 v)
Mesh functions that modify and initialize Mesh data.
float flux
The total light flux of this node.
int next
The pointer to the next node (either right child or parent's right child) for recursion-less traversa...
int index
The index of the node inside of the array.
AABB aabb
The bounding box of all of this node's children.
float emissive_flux
The only-emissive flux.
Nodes for the acceleration structures of BVHs.
void aabb_to_vertices(DebugVertex *out, AABB *aabb, vec3 offset)
Transfers an AABB to DebufVertex for a wireframe (GL_LINES).
The main render pass, data GPU uploads, and SSBO management.
void init_vector(Vector *v, size_t i, size_t type_size)
void calculate_aabb_extents(AABB *aabb)
void compute_aabb_from_points(vec3 *points, int point_count, AABB *aabb)
float triangle_area(vec3 a, vec3 b, vec3 c)
void calculate_aabb_bounds(AABB *aabb)
void * vector_expand(Vector *v, size_t i)
vec3 mat3_mul_vec3(mat3 m, vec3 v)
bool point_in_aabb(vec3 p, AABB *aabb)
#define CAST_VEC(vec, type)
vec3 mat4_mul_direction(const mat4 m, vec3 v)
mat3 mat3_inverse_transpose(mat3 m)
vec3 mat4_mul_point(mat4 m, vec3 v)
vec3 vec4_to_vec3(vec4 v)
mat3 mat3_from_mat4(const mat4 *m)
mat4 m4_transpose(mat4 in)
vec3 triangle_centroid(vec3 A, vec3 B, vec3 C)