diff --git a/Diamond/Diamond.csproj b/Diamond/Diamond.csproj index 37e24f4..e48f8b1 100644 --- a/Diamond/Diamond.csproj +++ b/Diamond/Diamond.csproj @@ -58,8 +58,8 @@ - + diff --git a/Diamond/Level/Level.cs b/Diamond/Level/Level.cs index e5ab0df..78e2f28 100644 --- a/Diamond/Level/Level.cs +++ b/Diamond/Level/Level.cs @@ -6,18 +6,14 @@ using System.Linq; using Diamond.Buffers; using Diamond.Shaders; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using OpenTK; using OpenTK.Graphics.OpenGL4; namespace Diamond.Level { public class Level { - [JsonProperty("models")] - private string[] MeshNames { get; set; } - - [JsonProperty("tiles")] - private TileInfo[] TileInfos { get; set; } - private TileData[] _allTiles; private ObjVertex[] _allVertices; @@ -38,55 +34,50 @@ namespace Diamond.Level public static Level LoadLevel(string file) { - var level = JsonConvert.DeserializeObject(File.ReadAllText(file)); + var levelData = JObject.Parse(File.ReadAllText(file)); var dir = Path.GetDirectoryName(file); + // this is horrendous, but not as bad as trying to directly deserialize it. + + var meshes = levelData["models"] + .Select(path => Mesh.FromObj(Path.Combine(dir, (string) path))) + .SelectMany(objects => objects) + .ToArray(); - // region assemble mesh map - var meshes = new Dictionary>(); + var meshDict = meshes + .ToDictionary(mesh => mesh.Name, mesh => mesh); - foreach (var meshPath in level.MeshNames) - { - var objects = Mesh.FromObj(Path.Combine(dir, meshPath)); - Debug.WriteLine(string.Join("\n", objects.Select(o => o.Name))); - foreach (var mesh in objects) + var allVertices = Mesh.Join(meshes); + + var tilegroups = levelData["tiles"] + .Select(tile => new { - meshes[mesh.Name] = mesh; - } - } + info = new + { + mesh = meshDict[(string) tile["mesh"]] + }, + pos = tile["pos"].ToObject() + }) + .GroupBy(tile => tile.info) + .Select(group => new TileGroup(new SubArray( + group.Select(data => new TileData(data.pos)) + .ToArray()), + group.Key.mesh)) + .ToArray(); - // region store all used meshes - level._meshes = meshes.Values.ToArray(); - // join meshes - level._allVertices = Mesh.Join(level._meshes); - Debug.WriteLine(level._allVertices.Length); - Debug.WriteLine(level._meshes[1].Vertices.Length); - Debug.WriteLine(level._meshes[1].Vertices.Offset); + var tileArrays = tilegroups + .Select(group => group.Tiles); - var groupDict = new Dictionary>(); + var allTiles = SubArray.Join(tileArrays); - foreach (var tileInfo in level.TileInfos) + var level = new Level { - var meshName = tileInfo.Mesh; - if (!groupDict.ContainsKey(meshName)) - groupDict[meshName] = new List(); - groupDict[meshName].Add(tileInfo.TileData); - } - - var groupList = new List(); - var tileSubArrayList = new List>(); - - foreach (var kvp in groupDict) - { - var sa = new SubArray(kvp.Value.ToArray()); - groupList.Add(new TileGroup(sa, meshes[kvp.Key])); - tileSubArrayList.Add(sa); - } - - level._tileGroups = groupList.ToArray(); - - level._allTiles = SubArray.Join(tileSubArrayList); + _allTiles = allTiles, + _allVertices = allVertices, + _meshes = meshes, + _tileGroups = tilegroups + }; level.InitializeBuffers(); diff --git a/Diamond/Level/TileData.cs b/Diamond/Level/TileData.cs index ae16c46..11408f4 100644 --- a/Diamond/Level/TileData.cs +++ b/Diamond/Level/TileData.cs @@ -7,7 +7,6 @@ namespace Diamond.Level [VertexData(Divisor = 1)] public struct TileData { - [JsonProperty("pos")] [VertexPointer("glbpos", 3)] public Vector3 Position; diff --git a/Diamond/Level/TileInfo.cs b/Diamond/Level/TileInfo.cs deleted file mode 100644 index 1cbb3a6..0000000 --- a/Diamond/Level/TileInfo.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Newtonsoft.Json; -using OpenTK; - -namespace Diamond.Level -{ - internal class TileInfo - { - [JsonProperty("mesh")] - public string Mesh { get; set; } - - [JsonProperty("pos")] - public Vector3 Position { get; set; } - - public TileData TileData => new TileData(Position); - - public override string ToString() - { - return $"Mesh: {Mesh}, Position: {Position}"; - } - } -} \ No newline at end of file diff --git a/Diamond/Mesh.cs b/Diamond/Mesh.cs index 1c55449..68348c4 100644 --- a/Diamond/Mesh.cs +++ b/Diamond/Mesh.cs @@ -59,34 +59,8 @@ namespace Diamond } } - [VertexData] - public struct ObjVertex - { - [VertexPointer("position", 3)] - public Vector3 Position; - - [VertexPointer("uv", 2)] - public Vector2 UV; - - [VertexPointer("normal", 3)] - public Vector3 Normal; - - public ObjVertex(Vector3 position, Vector2 uv, Vector3 normal) - { - Position = position; - UV = uv; - Normal = normal; - } - } - public static class Mesh { - public static Mesh FromJson(string file) where T : struct - { - var vertices = JsonConvert.DeserializeObject(File.ReadAllText(file)); - return new Mesh(vertices); - } - public static Mesh[] FromObj(string file) { var lines = File.ReadAllLines(file); diff --git a/Diamond/ObjVertex.cs b/Diamond/ObjVertex.cs new file mode 100644 index 0000000..3da5537 --- /dev/null +++ b/Diamond/ObjVertex.cs @@ -0,0 +1,25 @@ +using Diamond.Buffers; +using OpenTK; + +namespace Diamond +{ + [VertexData] + public struct ObjVertex + { + [VertexPointer("position", 3)] + public Vector3 Position; + + [VertexPointer("uv", 2)] + public Vector2 UV; + + [VertexPointer("normal", 3)] + public Vector3 Normal; + + public ObjVertex(Vector3 position, Vector2 uv, Vector3 normal) + { + Position = position; + UV = uv; + Normal = normal; + } + } +} \ No newline at end of file diff --git a/hexworld/HexRender.cs b/hexworld/HexRender.cs index e0c5095..156b0e9 100644 --- a/hexworld/HexRender.cs +++ b/hexworld/HexRender.cs @@ -47,7 +47,7 @@ namespace hexworld public HexRender(int width, int height) - : base(width, height, new GraphicsMode(32, 24, 0, 0)) + : base(width, height, new GraphicsMode(32, 24, 0, 8)) { Width = width; Height = Height;