|
Untitled
|
Acceleration structure generation and how we traverse them. More...
#include "core/camera.h"#include "utilities/mathf.h"#include "mesh.h"#include "utilities/types.h"#include "utilities/utilities.h"#include <math.h>#include "render/render.h"#include "load/init.h"Go to the source code of this file.
Functions | |
| void | swap_size_t (size_t *a, size_t *b) |
| Swaps a and b. | |
| void | assign_next_nodes (BVHNode *nodes, int index, int next) |
| Assigns next nodes for stackless traversal top-down. | |
| 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. | |
| 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 | 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 | subdivide_mesh_tri_bvh (Mesh *mesh, int pidx, int depth, int axes_tried) |
| Subdivides the Mesh's triangles into a 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. | |
| bool | ray_intersects_aabb (AABB aabb, vec3 origin, vec3 dir, vec3 inv_dir) |
| Checks if a ray intersects an aabb. | |
| 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 | 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. | |
| RayHit | raycast_scene (vec3 start, vec3 dir, float initial_t, bool check_normal) |
| Raycasts through the scene and finds closest intersection to meshes. | |
| 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 collisions. | |
| void | init_scene_bvh () |
| Initializes or updates the pre-existing scene bvh based on all mesh nodes with valid meshes. | |
| 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 for importance sampling. | |
Variables | |
| const float | RAY_EPS = 1e-6f |
Acceleration structure generation and how we traverse them.
Definition in file acceleration.c.
| void swap_size_t | ( | size_t * | a, |
| size_t * | b ) |
Swaps a and b.
Definition at line 111 of file acceleration.c.
References swap_size_t().
Referenced by subdivide_light_bvh(), subdivide_mesh_tri_bvh(), subdivide_scene_bvh(), and swap_size_t().
| void assign_next_nodes | ( | BVHNode * | nodes, |
| int | index, | ||
| int | next ) |
Assigns next nodes for stackless traversal top-down.
| nodes | The BVHNodes to assign over. |
| index | The recursive current node index. |
| next | Either the parent's right or parent-parent's right etc. |
Definition at line 118 of file acceleration.c.
References assign_next_nodes(), BVHNode::left, BVHNode::next, and BVHNode::right.
Referenced by assign_next_nodes(), build_light_bvh(), init_mesh_tri_bvh(), and init_scene_bvh().
Slab test for BVH.
| hit | The RayHit that tests its t value. |
| aabb | The AABB to slab test. |
| origin | The origin of the ray. |
| inv_dir | \(\frac{1}{\mathit{ray}}\) |
Definition at line 134 of file acceleration.c.
References BVH_slab_test_aabb(), hit, AABB::max_x, AABB::max_y, AABB::max_z, AABB::min_x, AABB::min_y, AABB::min_z, vec3::x, vec3::y, and vec3::z.
Referenced by BVH_slab_test_aabb(), ray_traverse_mesh_bvh(), and ray_traverse_scene_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.
| m | The target Mesh. |
| mn | The target MeshNode. |
| nodes | The BVHNode tree. |
| inv_pos | \(M^{-1} \begin{bmatrix} o_x \\ o_y \\ o_z \\ 1 \end{bmatrix}\) |
| mod_dir | \(M^{-1} \begin{bmatrix} d_x \\ d_y \\ d_z \\ 0 \end{bmatrix}\) |
| inv_dir | \(\frac{1}{\mathit{mod\_dir}}\) |
| hit | The current RayHit data for the query. |
| check_normal | If it should cull backfaces. |
Definition at line 299 of file acceleration.c.
References BVHNode::aabb, RayHit::b0, RayHit::b1, RayHit::b2, Mesh::bvh_indices, BVH_slab_test_aabb(), CAST_VEC, BVHNode::count, BVHNode::first_index, hit, RayHit::hit, HIT_STATIC, MeshNode::id, MeshFace::indices, BVHNode::left, RayHit::mesh_node_idx, BVHNode::next, MeshFace::normal, Point::pos, RayHit::pos, ray_intersects_triangle(), ray_traverse_mesh_bvh(), BVHNode::right, RayHit::t, RayHit::tri, Mesh::tris, vec3_dot(), Mesh::vertices, vec3::x, vec3::y, and vec3::z.
Referenced by ray_traverse_mesh_bvh(), and ray_traverse_scene_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.
| sd | The SceneData to process. |
| nodes | The BVH. |
| origin | The ray origin. |
| dir | The ray dir. |
| mod_dir | The ray transformed into this scene - unused. |
| inv_dir | \(\frac{1}{\mathit{mod\_dir}}\) |
| hit | The RayHit data for the search. |
| check_normal | If it should cull backfaces. |
Definition at line 373 of file acceleration.c.
References BVHNode::aabb, Mesh::aabb, aabb_to_vertices(), SceneData::bvh_indices, BVH_slab_test_aabb(), CAST_VEC, DebugVertex::color, BVHNode::count, DEBUG_LINE_COUNT, DEBUG_LINES, BVHNode::first_index, MeshNode::free, MeshNode::global_matrix, GLOBALS, hit, RayHit::hit, HIT_DYNAMIC, BVHNode::left, m4_inverse(), mat4_mul_direction(), mat4_mul_point(), MeshNode::mesh, RayHit::mesh_node_idx, Globals::mesh_nodes, Globals::meshes, BVHNode::next, Mesh::nodes, RayHit::pos, ray_intersects_aabb_with_point(), ray_traverse_mesh_bvh(), ray_traverse_scene_bvh(), BVHNode::right, Mesh::static_mesh, RayHit::t, transform_aabb_by_model_matrix(), vec3_length(), vec3_sub(), vec3::x, vec3::y, and vec3::z.
Referenced by ray_traverse_scene_bvh(), and raycast_scene().
| 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.
| m | The static Mesh. |
| mn | The static MeshNode. |
| node | The BVH node to process. |
| proposed | The position to check. |
| p | The collision data to modify. |
Definition at line 470 of file acceleration.c.
References A, B, CollisionPoints::bias_impulse, Mesh::bvh_indices, C, CAST_VEC, collision(), CollisionPoints::collision, BVHNode::count, CollisionPoints::d, BVHNode::first_index, MeshNode::global_matrix, MeshNode::id, MeshFace::indices, m4_inverse(), m4_transpose(), mat4_mul_direction(), mat4_mul_point(), CollisionPoints::mnB, CollisionPoints::normal, MeshFace::normal, CollisionPoints::normal_impulse, MeshNode::pc, CollisionPoints::pcB, Point::pos, process_collision_node(), CollisionPoints::tangent_impulse, Mesh::tris, vec3_normalize(), and Mesh::vertices.
Referenced by process_collision_node(), and traverse_collision_bvh().
| 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.
| bvh_nodes | The created BVHNodes. |
| indices | The array of indices that are sorted as the BVH is built. |
| aabbs | The AABBs of each MeshNode. |
| pidx | The parent idx. |
| depth | The current depth. |
| axes_tried | How many axes have been unsuccessfully tried for a given node before a split is decided as impossible at 3. |
Definition at line 562 of file acceleration.c.
References BVHNode::aabb, calculate_aabb_extents(), CAST_VEC, AABB::center, BVHNode::count, Vector::count, AABB::extents, BVHNode::first_index, BVHNode::index, BVHNode::left, AABB::max_x, AABB::max_y, AABB::max_z, AABB::min_x, AABB::min_y, AABB::min_z, BVHNode::next, BVHNode::parent, BVHNode::right, subdivide_scene_bvh(), swap_size_t(), vector_expand(), X, vec3::x, Y, vec3::y, Z, and vec3::z.
Referenced by init_scene_bvh(), subdivide_light_bvh(), and subdivide_scene_bvh().
| bool subdivide_mesh_tri_bvh | ( | Mesh * | mesh, |
| int | pidx, | ||
| int | depth, | ||
| int | axes_tried ) |
Subdivides the Mesh's triangles into a BVH.
| mesth | The Mesh. |
| pidx | The parent idx. |
| depth | The current depth. |
| axes_tried | How many axes have been unsuccessfully tried for a given node before a split is decided as impossible at 3. |
Definition at line 936 of file acceleration.c.
References BVHNode::aabb, Mesh::bvh_indices, calculate_aabb_extents(), CAST_VEC, AABB::center, BVHNode::count, Vector::count, AABB::extents, BVHNode::first_index, BVHNode::index, BVHNode::left, AABB::max_x, AABB::max_y, AABB::max_z, AABB::min_x, AABB::min_y, AABB::min_z, BVHNode::next, Mesh::nodes, BVHNode::parent, BVHNode::right, subdivide_mesh_tri_bvh(), swap_size_t(), Mesh::tri_aabbs, Mesh::tris, vector_expand(), Mesh::vertices, X, vec3::x, Y, vec3::y, Z, and vec3::z.
Referenced by init_mesh_tri_bvh(), and subdivide_mesh_tri_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.
| bvh_nodes | The created BVHNodes. |
| indices | The array of indices that are sorted as the BVH is built. |
| aabbs | The AABBs of each MeshNode. |
| lights | The array of Lights. |
| pidx | The parent idx. |
| depth | The current depth. |
| axes_tried | How many axes have been unsuccessfully tried for a given node before a split is decided as impossible at 3. |
Definition at line 1278 of file acceleration.c.
References BVHNode::aabb, AREA_LIGHT, calculate_aabb_extents(), CAST_VEC, AABB::center, BVHNode::count, Vector::count, DIRECTIONAL_LIGHT, BVHNode::emissive_flux, AABB::extents, BVHNode::first_index, BVHNode::flux, BVHNode::index, BVHNode::left, Light::light_type, AABB::max_x, AABB::max_y, AABB::max_z, AABB::min_x, AABB::min_y, AABB::min_z, BVHNode::next, BVHNode::parent, Light::power, BVHNode::right, subdivide_light_bvh(), subdivide_scene_bvh(), swap_size_t(), vector_expand(), X, vec3::x, Y, vec3::y, Z, and vec3::z.
Referenced by build_light_bvh(), and subdivide_light_bvh().
Checks if a ray intersects an aabb.
| aabb | The aabb to check. |
| origin | The origin of the ray. |
| dir | The dir of the ray. |
| inv_dir | 1.0f/dir of the ray. |
Definition at line 157 of file acceleration.c.
References AABB::max_x, AABB::max_y, AABB::max_z, AABB::min_x, AABB::min_y, AABB::min_z, ray_intersects_aabb(), vec3::x, vec3::y, and vec3::z.
Referenced by ray_intersects_aabb().
| 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.
| aabb | The aabb to check. |
| origin | The origin of the ray. |
| dir | The dir of the ray. |
| inv_dir | 1.0f/dir of the ray. |
| intersection_point | A pointer to where we will place the intersection point. |
Definition at line 182 of file acceleration.c.
References AABB::max_x, AABB::max_y, AABB::max_z, AABB::min_x, AABB::min_y, AABB::min_z, ray_intersects_aabb_with_point(), vec3_add(), vec3_scale(), vec3::x, vec3::y, and vec3::z.
Referenced by process_sdl_event(), ray_intersects_aabb_with_point(), and ray_traverse_scene_bvh().
Checks if a ray intersects an obb and outputs an intersection point.
| obb | The obb to check. |
| origin | The origin of the ray. |
| dir | The dir of the ray. |
| intersection_point | A pointer to where we will place the intersection point. |
Definition at line 220 of file acceleration.c.
References mat3::a0, mat3::a1, mat3::a2, mat3::b0, mat3::b1, mat3::b2, mat3::c0, mat3::c1, mat3::c2, OBB::center, OBB::extents, RAY_EPS, ray_intersects_obb_with_point(), OBB::rot, vec3_add(), vec3_dot(), vec3_normalize(), vec3_scale(), vec3_sub(), vec3::x, vec3::y, and vec3::z.
Referenced by check_lighting_pv(), process_sdl_event(), and ray_intersects_obb_with_point().
Raycasts through the scene and finds closest intersection to meshes.
| start | The ray's starting position. |
| dir | The dir of the ray. |
| initial_t | The starting T to begin evaluating, usually set at INFINITY. |
| check_normal | To check backfaces or not. |
Definition at line 448 of file acceleration.c.
References SceneData::bvh_nodes, CAST_VEC, Vector::count, MeshNode::global_matrix, GLOBALS, hit, RayHit::hit, mat4_mul_point(), RayHit::mesh_node_idx, Globals::mesh_nodes, RayHit::pos, ray_traverse_scene_bvh(), raycast_scene(), Globals::scene_data, RayHit::t, vec3::x, vec3::y, and vec3::z.
Referenced by find_closest_visible_aportal(), interpolate_nearest_aprobes(), pathfind_audio_occlusion(), process_sdl_event(), raycast_scene(), recalculate_audio_portal_paths(), resolve_collisions(), update_audio_occlusion(), update_body_from_legs(), and update_leg().
| 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 collisions.
| m | The mesh we are traversing. |
| mn | The mesh node we are traversing. |
| nodes | The bvh nodes we are traversing. |
| node | The current node. |
| proposed | The position we are checking. |
| inv_pos | The position transformed into mesh node local space using inv matrix. |
| p | The array of collision points to append to. |
Definition at line 514 of file acceleration.c.
References BVHNode::aabb, BVHNode::left, AABB::max_x, AABB::max_y, AABB::max_z, AABB::min_x, AABB::min_y, AABB::min_z, point_in_aabb(), process_collision_node(), BVHNode::right, and traverse_collision_bvh().
Referenced by player_find_collision(), and traverse_collision_bvh().
| void init_scene_bvh | ( | ) |
Initializes or updates the pre-existing scene bvh based on all mesh nodes with valid meshes.
Definition at line 850 of file acceleration.c.
References BVHNode::aabb, Mesh::aabb, SceneData::aabb, assign_next_nodes(), Vector::buffer, SceneData::bvh_indices, SceneData::bvh_indices_count, SceneData::bvh_nodes, CAST_VEC, AABB::center, BVHNode::count, Vector::count, AABB::extents, BVHNode::first_index, MeshNode::free, MeshNode::global_matrix, GLOBALS, BVHNode::index, init_scene_bvh(), init_vector(), BVHNode::left, AABB::max_x, AABB::max_y, AABB::max_z, MeshNode::mesh, Globals::mesh_nodes, Globals::meshes, AABB::min_x, AABB::min_y, AABB::min_z, BVHNode::next, BVHNode::parent, BVHNode::right, Globals::scene_data, subdivide_scene_bvh(), transform_aabb_by_model_matrix(), and vector_expand().
Referenced by init_scene_bvh(), main(), and render_main_menu().
| void init_mesh_tri_bvh | ( | Mesh * | mesh | ) |
Initializes the bvh for a mesh's triangles.
Only performed on static meshes.
Definition at line 1231 of file acceleration.c.
References BVHNode::aabb, Mesh::aabb, assign_next_nodes(), Mesh::bvh_indices, CAST_VEC, compute_aabb_from_points(), BVHNode::count, Vector::count, BVHNode::first_index, BVHNode::index, MeshFace::indices, init_mesh_tri_bvh(), init_vector(), BVHNode::left, BVHNode::next, Mesh::nodes, BVHNode::parent, Point::pos, BVHNode::right, subdivide_mesh_tri_bvh(), Mesh::tri_aabbs, Mesh::tris, vector_expand(), and Mesh::vertices.
Referenced by init_mesh(), and init_mesh_tri_bvh().
| void build_light_bvh | ( | ) |
Initializes a bvh for all current lights and emissive triangles in the scene, and sets the flux up for importance sampling.
Definition at line 1600 of file acceleration.c.
References Light::A, BVHNode::aabb, Light::area, AREA_LIGHT, assign_next_nodes(), Light::B, Vector::buffer, build_light_bvh(), Mesh::bvh_indices, SceneData::bvh_indices, SceneData::bvh_indices_count, Light::C, calculate_aabb_bounds(), calculate_aabb_extents(), CAST_VEC, AABB::center, BVHNode::count, Vector::count, DIRECTIONAL_LIGHT, BVHNode::emissive_flux, Material::emissive_strength, AABB::extents, BVHNode::first_index, BVHNode::flux, MeshNode::free, MeshNode::global_matrix, GLOBALS, BVHNode::index, LightBVH::indices, MeshFace::indices, LightBVH::indices_count, init_vector(), Light::intensity, Material::ke, BVHNode::left, SceneData::light_bvh, Light::light_type, Globals::lights, LightBVH::lights, Light::lum, M_PI, mat3_from_mat4(), mat3_inverse_transpose(), mat3_mul_vec3(), mat4_mul_point(), MeshFace::material_id, Globals::materials, AABB::max_x, AABB::max_y, AABB::max_z, Light::mesh, MeshNode::mesh, Globals::mesh_nodes, Globals::meshes, AABB::min_x, AABB::min_y, AABB::min_z, BVHNode::next, LightBVH::nodes, Light::normal, MeshFace::normal, Light::outer_angle, BVHNode::parent, POINT_LIGHT, Light::pos, Point::pos, Light::power, Light::radius, Light::rgb, BVHNode::right, rot(), Globals::scene_data, SPOT_LIGHT, MeshNode::static_mesh, subdivide_light_bvh(), Light::surface_area, Light::threshold, Light::tri, triangle_area(), triangle_centroid(), Mesh::tris, vec3_normalize(), vec3_scale(), vec4_length(), vec4_to_vec3(), vector_expand(), Mesh::vertices, vec3::x, vec3::y, and vec3::z.
Referenced by build_light_bvh(), and render_light_menu().
| const float RAY_EPS = 1e-6f |
Definition at line 218 of file acceleration.c.
Referenced by ray_intersects_obb_with_point().