diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-04 10:12:06 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-04 10:12:06 +0100 |
| commit | c0682388bbe879b7db7b1dc62fa7ce664bacacc2 (patch) | |
| tree | 6fcc1a5cb3ac3a0317de8acc4ba41c8335a3d069 /src/tests/test_3d.cc | |
| parent | 898666e30a7102875d78d843619d072e5985b05d (diff) | |
feat(tests): Add comprehensive tests for math and 3d libraries
Diffstat (limited to 'src/tests/test_3d.cc')
| -rw-r--r-- | src/tests/test_3d.cc | 80 |
1 files changed, 65 insertions, 15 deletions
diff --git a/src/tests/test_3d.cc b/src/tests/test_3d.cc index 88b8db9..90869bf 100644 --- a/src/tests/test_3d.cc +++ b/src/tests/test_3d.cc @@ -22,11 +22,29 @@ void test_camera() { // 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(); - // Check aspect ratio influence (m[0] = 1/(tan(fov/2)*asp)) - // fov ~0.785 (45deg), tan(22.5) ~0.414. asp=1.777. - // m[0] should be around 1.35 - assert(proj.m[0] > 1.0f); + 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() { @@ -36,21 +54,44 @@ void test_object_transform() { // Model matrix should translate by (10,0,0) mat4 m = obj.get_model_matrix(); - assert(near(m.m[12], 10.0f)); // Col 3, Row 0 is x translation in Col-Major? - // Wait, my mat4 struct: - // r.m[12] = t.x; // Index 12 is translation X assert(near(m.m[12], 10.0f)); - // Rotate 90 deg Y - obj.rotation = quat::from_axis(vec3(0, 1, 0), 1.570796f); + // 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) -> Rot(0,0,-1) -> Trans(10,0,-1) - vec4 p(1, 0, 0, 1); - vec4 res = m * p; - assert(near(res.x, 10.0f)); // Rotated vector is (0,0,-1). + (10,0,0) - // translation -> (10,0,-1) - assert(near(res.z, -1.0f)); + // 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() { @@ -60,6 +101,15 @@ void test_scene() { 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() { |
