From eaee81322af375efb1e1928963eccab20c418c62 Mon Sep 17 00:00:00 2001 From: RDW Date: Sun, 17 Dec 2023 21:36:48 +0100 Subject: [PATCH] Tests: Add a snapshot test for the GND geometry generation I don't think anything else is feasible here, there's too much data. --- Tests/FileFormats/RagnarokGND.spec.lua | 104 +++ Tests/Fixtures/Snapshots/gnd-geometry.json | 810 +++++++++++++++++++++ 2 files changed, 914 insertions(+) create mode 100644 Tests/Fixtures/Snapshots/gnd-geometry.json diff --git a/Tests/FileFormats/RagnarokGND.spec.lua b/Tests/FileFormats/RagnarokGND.spec.lua index 80ddafd4..4928256f 100644 --- a/Tests/FileFormats/RagnarokGND.spec.lua +++ b/Tests/FileFormats/RagnarokGND.spec.lua @@ -1,3 +1,5 @@ +local ffi = require("ffi") + local RagnarokGND = require("Core.FileFormats.RagnarokGND") local GND_WITHOUT_WATER_PLANE = C_FileSystem.ReadFile(path.join("Tests", "Fixtures", "no-water-plane.gnd")) @@ -127,4 +129,106 @@ describe("RagnarokGND", function() assertEquals(gnd.waterPlanes[0].texture_cycling_interval, 3) end) end) + + describe("GridPositionToCubeID", function() + local gnd = RagnarokGND() + gnd.gridSizeU = 3 + gnd.gridSizeV = 3 + it("should return the corresponding C-style cube index if a valid grid position was given", function() + assertEquals(gnd:GridPositionToCubeID(1, 1), 0) -- C arrays start at zero + assertEquals(gnd:GridPositionToCubeID(2, 1), 1) + assertEquals(gnd:GridPositionToCubeID(3, 1), 2) + assertEquals(gnd:GridPositionToCubeID(1, 2), 3) + assertEquals(gnd:GridPositionToCubeID(2, 2), 4) + assertEquals(gnd:GridPositionToCubeID(3, 2), 5) + assertEquals(gnd:GridPositionToCubeID(1, 3), 6) + assertEquals(gnd:GridPositionToCubeID(2, 3), 7) + assertEquals(gnd:GridPositionToCubeID(3, 3), 8) + end) + + it("should fail if the given grid position is out of bounds", function() + -- The purpose of this is to make sure OOB access raises an error, rather than potentially SEGFAULTing + assertFailure(function() + return gnd:GridPositionToCubeID(4, 1) + end, "Grid position (4, 1) is out of bounds") + + assertFailure(function() + return gnd:GridPositionToCubeID(1, 4) + end, "Grid position (1, 4) is out of bounds") + + assertFailure(function() + return gnd:GridPositionToCubeID(1, -1) + end, "Grid position (1, -1) is out of bounds") + + assertFailure(function() + return gnd:GridPositionToCubeID(-1, 1) + end, "Grid position (-1, 1) is out of bounds") + + assertFailure(function() + return gnd:GridPositionToCubeID(1, 0) + end, "Grid position (1, 0) is out of bounds") + + assertFailure(function() + return gnd:GridPositionToCubeID(0, 1) + end, "Grid position (0, 1) is out of bounds") + + assertFailure(function() + return gnd:GridPositionToCubeID(0, 0) + end, "Grid position (0, 0) is out of bounds") + end) + end) + + describe("GenerateGroundMeshSections", function() + it("should generate one ground mesh section per diffuse texture", function() + local gnd = RagnarokGND() + gnd.gridSizeU = 3 + gnd.gridSizeV = 3 + gnd.cubeGrid = ffi.new("gnd_groundmesh_cube_t[?]", gnd.gridSizeU * gnd.gridSizeV) + for gridV = 1, gnd.gridSizeV, 1 do + for gridU = 1, gnd.gridSizeU, 1 do + local cubeID = (gridU - 1) + (gridV - 1) * gnd.gridSizeU + local cube = gnd.cubeGrid[cubeID] + cube.southwest_corner_altitude = -4 * cubeID + 1 + cube.southeast_corner_altitude = -4 * cubeID + 2 + cube.northwest_corner_altitude = -4 * cubeID + 3 + cube.northeast_corner_altitude = -4 * cubeID + 4 + end + end + gnd.texturedSurfaces = ffi.new("gnd_textured_surface_t[?]", gnd.gridSizeU * gnd.gridSizeV) + gnd.texturedSurfaces[0].bottom_left_color.red = 255 + gnd.texturedSurfaces[0].bottom_left_color.green = 255 + gnd.texturedSurfaces[0].bottom_left_color.blue = 255 + gnd.texturedSurfaces[0].bottom_left_color.alpha = 255 + gnd.texturedSurfaces[0].uvs.bottom_left_u = 0 + gnd.texturedSurfaces[0].uvs.bottom_left_v = 0 + gnd.texturedSurfaces[0].uvs.bottom_right_u = 0.25 * 4 + gnd.texturedSurfaces[0].uvs.bottom_right_v = 0 + gnd.texturedSurfaces[0].uvs.top_left_u = 0 + gnd.texturedSurfaces[0].uvs.top_left_v = 0.25 * 4 + gnd.texturedSurfaces[0].uvs.top_right_u = 0.25 * 4 + gnd.texturedSurfaces[0].uvs.top_right_v = 0.25 * 4 + gnd.texturedSurfaceCount = gnd.gridSizeU * gnd.gridSizeV + gnd.diffuseTextureCount = 1 + gnd.diffuseTexturePaths = { + "whatever.bmp", + } + gnd.groundMeshSections = { + { + vertexPositions = {}, + vertexColors = {}, + triangleConnections = {}, + diffuseTextureCoords = {}, + }, + } + local sections = gnd:GenerateGroundMeshSections() + assertEquals(#sections, 1) -- Index starts at zero + assertEquals(table.count(sections), 1) + local json = require("json") + local jsonDump = json.prettier(sections) + -- Leaving this here because the snapshot likely needs to be recreated once lightmaps/normals are needed + -- C_FileSystem.WriteFile(path.join("Tests", "Fixtures", "Snapshots", "gnd-geometry.json"), jsonDump) + local snapshot = C_FileSystem.ReadFile(path.join("Tests", "Fixtures", "Snapshots", "gnd-geometry.json")) + assertEquals(snapshot, jsonDump) + end) + end) end) diff --git a/Tests/Fixtures/Snapshots/gnd-geometry.json b/Tests/Fixtures/Snapshots/gnd-geometry.json new file mode 100644 index 00000000..eb91fc03 --- /dev/null +++ b/Tests/Fixtures/Snapshots/gnd-geometry.json @@ -0,0 +1,810 @@ +[ + { + "diffuseTextureCoords": [ + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 1, + 0, + 0, + 1, + 1, + 1 + ], + "triangleConnections": [ + 0, + 1, + 2, + 1, + 3, + 2, + 4, + 5, + 6, + 5, + 7, + 6, + 8, + 9, + 10, + 9, + 11, + 10, + 12, + 13, + 14, + 13, + 15, + 14, + 16, + 17, + 18, + 17, + 19, + 18, + 20, + 21, + 22, + 21, + 23, + 22, + 24, + 25, + 26, + 25, + 27, + 26, + 28, + 29, + 30, + 29, + 31, + 30, + 32, + 33, + 34, + 33, + 35, + 34, + 36, + 37, + 38, + 37, + 39, + 38, + 40, + 41, + 42, + 41, + 43, + 42, + 44, + 45, + 46, + 45, + 47, + 46, + 48, + 49, + 50, + 49, + 51, + 50, + 52, + 53, + 54, + 53, + 55, + 54, + 56, + 57, + 58, + 57, + 59, + 58, + 60, + 61, + 62, + 61, + 63, + 62, + 64, + 65, + 66, + 65, + 67, + 66, + 68, + 69, + 70, + 69, + 71, + 70, + 72, + 73, + 74, + 73, + 75, + 74, + 76, + 77, + 78, + 77, + 79, + 78, + 80, + 81, + 82, + 81, + 83, + 82 + ], + "vertexColors": [ + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 1, + 1, + 1, + 0, + 0, + 0, + 1, + 1, + 1, + 1, + 1, + 1, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0 + ], + "vertexPositions": [ + 0, + -0.2, + 0, + 2, + -0.4, + 0, + 0, + -0.6000000000000001, + 2, + 2, + -0.8, + 2, + 0, + -0.6000000000000001, + 2, + 2, + -0.8, + 2, + 0, + 2.2, + 2, + 2, + 2, + 2, + 2, + -0.8, + 2, + 2, + -0.4, + 0, + 2, + 0.2, + 2, + 2, + 0.6000000000000001, + 0, + 2, + 0.6000000000000001, + 0, + 4, + 0.4, + 0, + 2, + 0.2, + 2, + 4, + 0, + 2, + 2, + 0.2, + 2, + 4, + 0, + 2, + 2, + 3, + 2, + 4, + 2.8000000000000004, + 2, + 4, + 0, + 2, + 4, + 0.4, + 0, + 4, + 1, + 2, + 4, + 1.4000000000000002, + 0, + 4, + 1.4000000000000002, + 0, + 6, + 1.2000000000000002, + 0, + 4, + 1, + 2, + 6, + 0.8, + 2, + 4, + 1, + 2, + 6, + 0.8, + 2, + 4, + 3.8000000000000004, + 2, + 6, + 3.6, + 2, + 0, + 2.2, + 2, + 2, + 2, + 2, + 0, + 1.8, + 4, + 2, + 1.6, + 4, + 0, + 1.8, + 4, + 2, + 1.6, + 4, + 0, + 4.6000000000000009, + 4, + 2, + 4.4, + 4, + 2, + 1.6, + 4, + 2, + 2, + 2, + 2, + 2.6, + 4, + 2, + 3, + 2, + 2, + 3, + 2, + 4, + 2.8000000000000004, + 2, + 2, + 2.6, + 4, + 4, + 2.4000000000000005, + 4, + 2, + 2.6, + 4, + 4, + 2.4000000000000005, + 4, + 2, + 5.4, + 4, + 4, + 5.2, + 4, + 4, + 2.4000000000000005, + 4, + 4, + 2.8000000000000004, + 2, + 4, + 3.4000000000000005, + 4, + 4, + 3.8000000000000004, + 2, + 4, + 3.8000000000000004, + 2, + 6, + 3.6, + 2, + 4, + 3.4000000000000005, + 4, + 6, + 3.2, + 4, + 4, + 3.4000000000000005, + 4, + 6, + 3.2, + 4, + 4, + 6.2, + 4, + 6, + 6, + 4, + 0, + 4.6000000000000009, + 4, + 2, + 4.4, + 4, + 0, + 4.2, + 6, + 2, + 4, + 6, + 2, + 4, + 6, + 2, + 4.4, + 4, + 2, + 5, + 6, + 2, + 5.4, + 4, + 2, + 5.4, + 4, + 4, + 5.2, + 4, + 2, + 5, + 6, + 4, + 4.800000000000001, + 6, + 4, + 4.800000000000001, + 6, + 4, + 5.2, + 4, + 4, + 5.800000000000001, + 6, + 4, + 6.2, + 4, + 4, + 6.2, + 4, + 6, + 6, + 4, + 4, + 5.800000000000001, + 6, + 6, + 5.6000000000000009, + 6 + ] + } +] \ No newline at end of file