Files
Cortex_Engine/tests/Engine.Tests/ObjLoaderTests.cs
T
emil28092005 fb6e26a268 feat: modular HAL, Raylib backend, PBR shading, textures, 60 unit tests
- Replace hardcoded SDL3 windowing with IWindow/IInputState/Key abstractions
- Each render backend owns its window (Raylib GLFW, SDL3 for Vulkan)
- Raylib backend: DrawModelEx, custom GLSL shader with Fresnel, ACES
  tonemapping, gamma correction, hemisphere ambient
- Fix backface culling, mesh memory (NativeMemory.Alloc), texture loading
- Camera controllers use backend-agnostic Key enum (inverted yaw/strafe)
- Demo scene: 8 cubes, 7 spheres, torus knot OBJ with checker texture
- Extract ProceduralMesh + MeshMath from Program.cs to Engine.Graphics
- Vulkan backend deferred (compiles, untested, IWindow-compatible)
- 60 unit tests: ObjLoader, camera controllers, AiCommandProcessor,
  RenderBackendFactory, Timing, ProceduralMesh, MeshMath, Transform
- AGENTS.md for opencode integration
2026-06-17 13:49:12 +03:00

147 lines
3.2 KiB
C#

using System.IO;
using System.Numerics;
using Engine.Core;
using Engine.Core.Components;
using Engine.Graphics.Loaders;
namespace Engine.Tests;
public class ObjLoaderTests
{
private static readonly string TempDir = Path.Combine(Path.GetTempPath(), "CortexEngineTests");
private static string WriteTempObj(string content)
{
Directory.CreateDirectory(TempDir);
var path = Path.Combine(TempDir, $"test_{Guid.NewGuid():N}.obj");
File.WriteAllText(path, content);
return path;
}
[Fact]
public void Loads_Single_Triangle()
{
var path = WriteTempObj("""
v 0 0 0
v 1 0 0
v 0 1 0
f 1 2 3
""");
var mesh = ObjLoader.Load(path, new Vector3(1, 1, 1));
Assert.Equal(3, mesh.Vertices.Length);
Assert.Equal(3, mesh.Indices.Length);
}
[Fact]
public void Triangulates_Quad_As_Fan()
{
var path = WriteTempObj("""
v 0 0 0
v 1 0 0
v 1 1 0
v 0 1 0
f 1 2 3 4
""");
var mesh = ObjLoader.Load(path);
Assert.Equal(6, mesh.Vertices.Length);
Assert.Equal(6, mesh.Indices.Length);
}
[Fact]
public void Parses_Face_With_Texcoord_Format()
{
var path = WriteTempObj("""
v 0 0 0
v 1 0 0
v 0 1 0
vt 0 0
vt 1 0
vt 0 1
f 1/1 2/2 3/3
""");
var mesh = ObjLoader.Load(path);
Assert.Equal(3, mesh.Vertices.Length);
}
[Fact]
public void Parses_Face_With_Normal_Format()
{
var path = WriteTempObj("""
v 0 0 0
v 1 0 0
v 0 1 0
vn 0 0 1
f 1//1 2//1 3//1
""");
var mesh = ObjLoader.Load(path);
Assert.Equal(3, mesh.Vertices.Length);
}
[Fact]
public void Computes_Face_Normal_For_Triangle()
{
var path = WriteTempObj("""
v 0 0 0
v 1 0 0
v 0 1 0
f 1 2 3
""");
var mesh = ObjLoader.Load(path);
var normal = mesh.Vertices[0].Normal;
Assert.Equal(0f, normal.X, 0.001f);
Assert.Equal(0f, normal.Y, 0.001f);
Assert.Equal(1f, normal.Z, 0.001f);
}
[Fact]
public void Skips_Comments_And_Blank_Lines()
{
var path = WriteTempObj("""
# This is a comment
v 0 0 0
# Another comment
v 1 0 0
v 0 1 0
f 1 2 3
""");
var mesh = ObjLoader.Load(path);
Assert.Equal(3, mesh.Vertices.Length);
}
[Fact]
public void Throws_On_Empty_File()
{
var path = WriteTempObj("# just a comment\n");
Assert.Throws<InvalidOperationException>(() => ObjLoader.Load(path));
}
[Fact]
public void Default_Color_When_Not_Specified()
{
var path = WriteTempObj("""
v 0 0 0
v 1 0 0
v 0 1 0
f 1 2 3
""");
var mesh = ObjLoader.Load(path);
Assert.Equal(0.7f, mesh.Vertices[0].Color.X, 0.001f);
}
}