diff --git a/Diamond/Textures/Texture.cs b/Diamond/Textures/Texture.cs index f9f29aa..bdd69b4 100644 --- a/Diamond/Textures/Texture.cs +++ b/Diamond/Textures/Texture.cs @@ -68,5 +68,10 @@ namespace Diamond.Textures tex.Unbind(); return tex; } + + public static Texture FromFile(string path) + { + return FromBitmap(new Bitmap(path)); + } } } \ No newline at end of file diff --git a/hexworld/HexRender.cs b/hexworld/HexRender.cs index bc3b124..ec7068d 100644 --- a/hexworld/HexRender.cs +++ b/hexworld/HexRender.cs @@ -1,8 +1,13 @@ using System; using System.Drawing; +using System.IO; +using System.Linq; +using Diamond; +using Diamond.Buffers; using Diamond.Level; using Diamond.Shaders; using Diamond.Textures; +using Newtonsoft.Json.Linq; using OpenTK; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL4; @@ -15,17 +20,32 @@ namespace hexworld #region GLObjects - private Level _level; + private Program _texPgm; + + private Texture _doorTex; + private Texture _grassTex; + + private GLBuffer _meshBuffer; + private GLBuffer _tileBuffer; protected override void OnClosed(EventArgs e) { - base.OnClosed(e); + _texPgm?.Dispose(); - _level?.Dispose(); + _doorTex?.Dispose(); + _grassTex?.Dispose(); + + _meshBuffer?.Dispose(); + _tileBuffer?.Dispose(); } #endregion + private SubArray _floorTiles; + private SubArray _doorTiles; + + private Mesh _cubeMesh; + private Matrix4 _view; private Matrix4 _proj; @@ -47,7 +67,48 @@ namespace hexworld { base.OnLoad(e); - _level = Level.LoadLevel(@"res\level.json"); + _texPgm = Program.FromFiles("res/obj.vs.glsl", "res/obj.fs.glsl"); + + _doorTex = Texture.FromFile("res/door.png"); + _grassTex = Texture.FromFile("res/grass.png"); + + _meshBuffer = new GLBuffer(BufferTarget.ArrayBuffer); + _tileBuffer = new GLBuffer(BufferTarget.ArrayBuffer, BufferUsageHint.DynamicDraw); + + var dir = "res"; + + var json = JObject.Parse(File.ReadAllText("res/level.json")); + + var allTiles = json["tiles"] + .GroupBy(ti => ti["tex"]) + .Select(g => g + .Select(ti => ti["pos"]) + .Select(pos => pos.ToObject()) + .Select(pos => new TileData(pos)) + .ToArray()) + .Select(arr => new SubArray(arr)) + .ToArray(); + + _doorTiles = allTiles[0]; + _floorTiles = allTiles[1]; + + _tileBuffer.Data(SubArray.Join(_doorTiles, _floorTiles)); + + var cubeMesh = json["models"] + .Select(path => (string) path) + .Select(path => Path.Combine(dir, path)) + .Select(path => Mesh.FromObj(path, false)) + .SelectMany(meshes => meshes) + .First(mesh => mesh.Name == "Cube"); + + _cubeMesh = cubeMesh; + + _meshBuffer.Data(cubeMesh.Vertices.ToArray()); + + // meshes, tiles, etc + + // can still use JObject queries to keep it short, + // but don't use Level. hat structure is broken } protected override void OnUpdateFrame(FrameEventArgs e) @@ -75,13 +136,18 @@ namespace hexworld GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); - var pgm = _level.Programs["textured"]; + _texPgm.Use(); - pgm.Use(); - GL.UniformMatrix4(pgm.GetUniform("view"), false, ref _view); - GL.UniformMatrix4(pgm.GetUniform("proj"), false, ref _proj); - - _level.Draw(); + _texPgm.SetAttribPointers(_meshBuffer); + _texPgm.SetAttribPointers(_tileBuffer); + + _grassTex.Bind(0); + + GL.Uniform1(_texPgm.GetUniform("tex"), 0); + GL.UniformMatrix4(_texPgm.GetUniform("view"), false, ref _view); + GL.UniformMatrix4(_texPgm.GetUniform("proj"), false, ref _proj); + + _cubeMesh.DrawInstanced(_floorTiles); SwapBuffers(); }