transfered from codeberg

This commit is contained in:
2026-03-26 14:46:39 -05:00
parent 630f28bb7e
commit 5ed2173793
136 changed files with 14932 additions and 0 deletions

View File

@@ -0,0 +1,112 @@
#include "test_frustum.h"
#include "frustum.h"
#include "_internal/private_frustum.h"
#include "unity.h"
#include "raylib.h"
#include "raymath.h"
/* Verify that an identity matrix results in planes that face inward correctly */
void test_extract_frustum_planes_from_identity_matrix(void)
{
frustum_plane_struct planes[number_of_planes];
Matrix identity_matrix = MatrixIdentity();
extract_frustum_planes(planes, identity_matrix);
/* With an identity matrix, the 'Left' plane normal should point along +X
* because the plane equation is Ax + By + Cz + D = 0 and it faces inward.
*/
TEST_ASSERT_FLOAT_WITHIN(0.0001f, 1.0f, planes[left].normal.x);
TEST_ASSERT_FLOAT_WITHIN(0.0001f, 0.0f, planes[left].normal.y);
TEST_ASSERT_FLOAT_WITHIN(0.0001f, 0.0f, planes[left].normal.z);
}
/* Verify that a box inside the view area is correctly identified as visible */
void test_bounding_box_inside_frustum_is_not_culled(void)
{
frustum_plane_struct planes[number_of_planes];
/* Create a standard perspective projection matrix
* (FOV: 90, Aspect: 1.0, Near: 0.1, Far: 100.0)
*/
Matrix projection = MatrixPerspective(90.0f * DEG2RAD, 1.0f, 0.1f, 100.0f);
Matrix view = MatrixLookAt((Vector3){ 0, 0, 0 },
(Vector3){ 0, 0, -1 },
(Vector3){ 0, 1, 0 });
Matrix view_projection = MatrixMultiply(view, projection);
extract_frustum_planes(planes, view_projection);
/* Define a box directly in front of the camera (negative Z axis) */
BoundingBox box_inside;
box_inside.min = (Vector3){ -1.0f, -1.0f, -10.0f };
box_inside.max = (Vector3){ 1.0f, 1.0f, -5.0f };
TEST_ASSERT_TRUE_MESSAGE(is_axis_aligned_bounding_box_in_frustum(planes,
box_inside),
"Box directly in front of camera should be visible");
}
/* Verify that a box behind the camera is correctly culled */
void test_bounding_box_behind_camera_is_culled(void)
{
frustum_plane_struct planes[number_of_planes];
Matrix projection = MatrixPerspective(90.0f * DEG2RAD, 1.0f, 0.1f, 100.0f);
Matrix view = MatrixLookAt((Vector3){ 0, 0, 0 },
(Vector3){ 0, 0, -1 },
(Vector3){ 0, 1, 0 });
Matrix view_projection = MatrixMultiply(view, projection);
extract_frustum_planes(planes, view_projection);
/* Define a box behind the camera (positive Z axis) */
BoundingBox box_behind;
box_behind.min = (Vector3){ -1.0f, -1.0f, 5.0f };
box_behind.max = (Vector3){ 1.0f, 1.0f, 10.0f };
TEST_ASSERT_FALSE_MESSAGE(is_axis_aligned_bounding_box_in_frustum(planes,
box_behind),
"Box behind the camera should be culled");
}
/* Verify that a box far beyond the far clipping plane is culled */
void test_bounding_box_beyond_far_plane_is_culled(void)
{
frustum_plane_struct planes[number_of_planes];
/* Far plane is set to 100.0 */
Matrix projection = MatrixPerspective(90.0f * DEG2RAD, 1.0f, 0.1f, 100.0f);
Matrix view = MatrixLookAt((Vector3){ 0, 0, 0 },
(Vector3){ 0, 0, -1 },
(Vector3){ 0, 1, 0 });
Matrix view_projection = MatrixMultiply(view, projection);
extract_frustum_planes(planes, view_projection);
/* Define a box at Z = -150 (outside the 100 unit limit) */
BoundingBox box_too_far;
box_too_far.min = (Vector3){ -1.0f, -1.0f, -160.0f };
box_too_far.max = (Vector3){ 1.0f, 1.0f, -150.0f };
TEST_ASSERT_FALSE_MESSAGE(is_axis_aligned_bounding_box_in_frustum(planes,
box_too_far),
"Box beyond the far clipping plane should be culled");
}
void run_frustum_tests(void)
{
RUN_TEST(test_extract_frustum_planes_from_identity_matrix);
RUN_TEST(test_bounding_box_inside_frustum_is_not_culled);
RUN_TEST(test_bounding_box_behind_camera_is_culled);
RUN_TEST(test_bounding_box_beyond_far_plane_is_culled);
}

View File

@@ -0,0 +1,15 @@
#ifndef test_frustum_h
#define test_frustum_h
void test_extract_frustum_planes_from_identity_matrix(void);
void test_bounding_box_inside_frustum_is_not_culled(void);
void test_bounding_box_behind_camera_is_culled(void);
void test_bounding_box_beyond_far_plane_is_culled(void);
void run_frustum_tests(void);
#endif