diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-01 10:51:15 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-01 10:51:15 +0100 |
| commit | 8bdc4754647c9c6691130fa91d51fee93c5fc88f (patch) | |
| tree | 2cfd7f72a21541c488ea48629eef47a6774fc2c4 /src/tests/test_3d.cc | |
| parent | 7905abd9f7ad35231289e729b42e3ad57a943ff5 (diff) | |
feat: Implement 3D system and procedural texture manager
- Extended mini_math.h with mat4 multiplication and affine transforms.
- Implemented TextureManager for runtime procedural texture generation and GPU upload.
- Added 3D system components: Camera, Object, Scene, and Renderer3D.
- Created test_3d_render mini-demo for interactive 3D verification.
- Fixed WebGPU validation errors regarding depthSlice and unimplemented WaitAny.
Diffstat (limited to 'src/tests/test_3d.cc')
| -rw-r--r-- | src/tests/test_3d.cc | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/tests/test_3d.cc b/src/tests/test_3d.cc new file mode 100644 index 0000000..33e6a04 --- /dev/null +++ b/src/tests/test_3d.cc @@ -0,0 +1,70 @@ +// 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)); + + 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); +} + +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)); // 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); + 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)); +} + +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()); +} + +int main() { + test_camera(); + test_object_transform(); + test_scene(); + std::cout << "--- 3D SYSTEM TESTS PASSED ---" << std::endl; + return 0; +} |
