diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-09 20:27:04 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-09 20:27:04 +0100 |
| commit | eff8d43479e7704df65fae2a80eefa787213f502 (patch) | |
| tree | 76f2fb8fe8d3db2c15179449df2cf12f7f54e0bf /src/tests/3d/test_3d.cc | |
| parent | 12378b1b7e9091ba59895b4360b2fa959180a56a (diff) | |
refactor: Reorganize tests into subsystem subdirectories
Restructured test suite for better organization and targeted testing:
**Structure:**
- src/tests/audio/ - 15 audio system tests
- src/tests/gpu/ - 12 GPU/shader tests
- src/tests/3d/ - 6 3D rendering tests
- src/tests/assets/ - 2 asset system tests
- src/tests/util/ - 3 utility tests
- src/tests/common/ - 3 shared test helpers
- src/tests/scripts/ - 2 bash test scripts (moved conceptually, not physically)
**CMake changes:**
- Updated add_demo_test macro to accept LABEL parameter
- Applied CTest labels to all 36 tests for subsystem filtering
- Updated all test file paths in CMakeLists.txt
- Fixed common helper paths (webgpu_test_fixture, etc.)
- Added custom targets for subsystem testing:
- run_audio_tests, run_gpu_tests, run_3d_tests
- run_assets_tests, run_util_tests, run_all_tests
**Include path updates:**
- Fixed relative includes in GPU tests to reference ../common/
**Documentation:**
- Updated doc/HOWTO.md with subsystem test commands
- Updated doc/CONTRIBUTING.md with new test organization
- Updated scripts/check_all.sh to reflect new structure
**Verification:**
- All 36 tests passing (100%)
- ctest -L <subsystem> filters work correctly
- make run_<subsystem>_tests targets functional
- scripts/check_all.sh passes
Backward compatible: make test and ctest continue to work unchanged.
handoff(Gemini): Test reorganization complete. 36/36 tests passing.
Diffstat (limited to 'src/tests/3d/test_3d.cc')
| -rw-r--r-- | src/tests/3d/test_3d.cc | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/src/tests/3d/test_3d.cc b/src/tests/3d/test_3d.cc new file mode 100644 index 0000000..e0fb2e0 --- /dev/null +++ b/src/tests/3d/test_3d.cc @@ -0,0 +1,126 @@ +// This file is part of the 64k demo project. +// It tests the 3D system components (Camera, Object, Scene). + +#include "3d/camera.h" +#include "3d/object.h" +#include "3d/scene.h" +#include <cassert> +#include <cmath> +#include <iostream> + +bool near(float a, float b, float e = 0.001f) { + return std::abs(a - b) < e; +} + +void test_camera() { + std::cout << "Testing Camera..." << std::endl; + Camera cam; + cam.position = vec3(0, 0, 10); + cam.target = vec3(0, 0, 0); + + mat4 view = cam.get_view_matrix(); + // Camera at (0,0,10) looking at (0,0,0). World (0,0,0) -> View (0,0,-10) + assert(near(view.m[14], -10.0f)); + + // Test Camera::set_look_at + cam.set_look_at({5, 0, 0}, {0, 0, 0}, + {0, 1, 0}); // Look at origin from (5,0,0) + mat4 view_shifted = cam.get_view_matrix(); + // The camera's forward vector (0,0,-1) should now point towards (-1,0,0) in + // world space. The translation part of the view matrix should be based on + // -dot(s, eye), -dot(u, eye), dot(f, eye) s = (0,0,-1), u = (0,1,0), f = + // (-1,0,0) m[12] = -dot({0,0,-1}, {5,0,0}) = 0 m[13] = -dot({0,1,0}, {5,0,0}) + // = 0 m[14] = dot({-1,0,0}, {5,0,0}) = -5 + assert(near(view_shifted.m[12], 0.0f)); + assert(near(view_shifted.m[13], 0.0f)); + assert(near(view_shifted.m[14], -5.0f)); + + // Test Camera::get_projection_matrix with varied parameters + // Change FOV and aspect ratio + mat4 proj = cam.get_projection_matrix(); + cam.fov_y_rad = 1.0472f; // 60 degrees + cam.aspect_ratio = 0.5f; // Narrower aspect ratio + mat4 proj_varied = cam.get_projection_matrix(); + // m[0] should increase due to narrower aspect ratio (1/tan(30deg)/0.5) + assert(proj_varied.m[0] > proj.m[0]); + // m[5] should increase due to larger FOV (1/tan(30deg)) + assert(proj_varied.m[5] < proj.m[5]); +} + +void test_object_transform() { + std::cout << "Testing Object Transform..." << std::endl; + Object3D obj; + obj.position = vec3(10, 0, 0); + + // Model matrix should translate by (10,0,0) + mat4 m = obj.get_model_matrix(); + assert(near(m.m[12], 10.0f)); + + // Test composed transformations (translate then rotate) + obj.position = vec3(5, 0, 0); + obj.rotation = quat::from_axis({0, 1, 0}, 1.570796f); // 90 deg Y rotation + m = obj.get_model_matrix(); + + // Transform point (1,0,0). Rotation around Y maps (1,0,0) to (0,0,-1). + // Translation moves it by (5,0,0). Final world pos: (5,0,-1). + vec4 p_comp(1, 0, 0, 1); + vec4 res_comp = m * p_comp; + assert(near(res_comp.x, 5.0f)); + assert(near(res_comp.z, -1.0f)); + + // Test Object3D::inv_model calculation + // Model matrix for translation (5,0,0) is just translation + obj.position = vec3(5, 0, 0); + obj.rotation = quat(); // Identity rotation + mat4 model_t = obj.get_model_matrix(); + mat4 inv_model_t = model_t.inverse(); + // Applying inv_model to a translated point should undo the translation. + // Point (5,0,0) should go to (0,0,0) + vec4 translated_point(5, 0, 0, 1); + vec4 original_space_t = + inv_model_t * + vec4(translated_point.x, translated_point.y, translated_point.z, 1.0); + assert(near(original_space_t.x, 0.0f) && near(original_space_t.y, 0.0f) && + near(original_space_t.z, 0.0f)); + + // Model matrix with rotation (90 deg Y) and translation (5,0,0) + obj.position = vec3(5, 0, 0); + obj.rotation = quat::from_axis({0, 1, 0}, 1.570796f); + mat4 model_trs = obj.get_model_matrix(); + mat4 inv_model_trs = model_trs.inverse(); + // Transform point (1,0,0) (local right) via TRS: Rotates to (0,0,-1), + // Translates to (5,0,-1) + vec4 p_trs(1, 0, 0, 1); + vec4 transformed_p = model_trs * p_trs; + assert(near(transformed_p.x, 5.0f) && near(transformed_p.z, -1.0f)); + // Apply inverse to transformed point to get back original point + vec4 original_space_trs = inv_model_trs * transformed_p; + assert(near(original_space_trs.x, 1.0f) && near(original_space_trs.y, 0.0f) && + near(original_space_trs.z, 0.0f)); +} + +void test_scene() { + std::cout << "Testing Scene..." << std::endl; + Scene scene; + scene.add_object(Object3D()); + assert(scene.objects.size() == 1); + scene.clear(); + assert(scene.objects.empty()); + + // Add multiple objects and check count + scene.add_object(Object3D()); + scene.add_object(Object3D()); + assert(scene.objects.size() == 2); + + // Test clearing the scene + scene.clear(); + assert(scene.objects.empty()); +} + +int main() { + test_camera(); + test_object_transform(); + test_scene(); + std::cout << "--- 3D SYSTEM TESTS PASSED ---" << std::endl; + return 0; +} |
