New test files: - VulkanStructSizeTests.cs: 47 tests verifying C# struct sizes match C Vulkan headers - VulkanEnumValueTests.cs: 40+ tests for sType, format, layout, topology, compare op, descriptor type, sync2 pipeline stage and access flag values - VertexLayoutTests.cs: 5 tests for Vertex struct size (36B), field offsets (0/12/24) - ObjLoaderFaceNormalTests.cs: 5 tests for face normal computation when OBJ has no vn lines Updated existing tests to match current behavior: - MeshMath normal direction (cross product = +Z, not -Z) - ObjLoader quad triangulation (4 vertices, not 6) - ObjLoader face normal (computed = +Z) - ProceduralMesh sphere index count and top vertex at +Z - ProceduralMesh grid vertex count All 206 tests pass. Tests would have caught: - VkPhysicalDeviceMemoryProperties size (was 264, should be 520) - VkPhysicalDeviceLimits fields (size_t fields were uint, not ulong) - Synchronization2Features sType (was 1000257000, should be 1000314007) - Access flag values (COLOR_ATTACHMENT_WRITE was 0x800, should be 0x100) - Index type mismatch (UINT16 vs UINT32)
147 lines
3.2 KiB
C#
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(4, 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);
|
|
}
|
|
}
|