diff --git a/Diamond/Diamond.csproj b/Diamond/Diamond.csproj index cfa8083..04290a5 100644 --- a/Diamond/Diamond.csproj +++ b/Diamond/Diamond.csproj @@ -53,6 +53,8 @@ + + diff --git a/Diamond/Render/Camera.cs b/Diamond/Render/Camera.cs new file mode 100644 index 0000000..d41dae3 --- /dev/null +++ b/Diamond/Render/Camera.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using OpenTK; + +namespace Diamond.Render +{ + public class Camera + { + private Vector3 _position = Vector3.Zero; + private Vector3 _target = -Vector3.One; + private Vector3 _up = Vector3.UnitZ; + + public Matrix4 View; + public Matrix4 Projection; + + public Vector3 Position + { + set + { + _position = value; + UpdateView(); + } + get => _position; + } + + public Vector3 Target + { + set + { + _target = value; + UpdateView(); + } + get => _target; + } + + public Vector3 Up + { + set + { + _up = value; + UpdateView(); + } + get => _up; + } + + private void UpdateView() + { + View = Matrix4.LookAt(_position, _target, _up); + } + } +} \ No newline at end of file diff --git a/Diamond/Render/RenderGroup.cs b/Diamond/Render/RenderGroup.cs new file mode 100644 index 0000000..121783f --- /dev/null +++ b/Diamond/Render/RenderGroup.cs @@ -0,0 +1,46 @@ +using System; +using Diamond.Buffers; +using Diamond.Shaders; +using Diamond.Textures; +using Diamond.Util; +using OpenTK.Graphics.OpenGL4; + +namespace Diamond.Render +{ + public class RenderGroup + { + public SubArray Tiles; + public Mesh Mesh; + + public Buffer TileBuffer; + public Buffer MeshBuffer; + + public Program Program; + public Texture Texture; + + public Camera Camera; + + public void Draw() + { + Program.Use(); + + TileBuffer.PointTo(Program); + MeshBuffer.PointTo(Program); + + Texture.Bind(0); + + var texLoc = Program.UniformLocation("tex"); + var viewLoc = Program.UniformLocation("view"); + var projLoc = Program.UniformLocation("proj"); + + if (texLoc.HasValue) + GL.Uniform1(texLoc.Value, 0); + if (viewLoc.HasValue) + GL.UniformMatrix4(viewLoc.Value, false, ref Camera.View); + if (projLoc.HasValue) + GL.UniformMatrix4(projLoc.Value, false, ref Camera.Projection); + + Mesh.DrawInstanced(Tiles); + } + } +} \ No newline at end of file diff --git a/hexworld/HexRender.cs b/hexworld/HexRender.cs index 889c509..061be22 100644 --- a/hexworld/HexRender.cs +++ b/hexworld/HexRender.cs @@ -2,6 +2,7 @@ using System.IO; using System.Linq; using Diamond.Buffers; +using Diamond.Render; using Diamond.Shaders; using Diamond.Textures; using Diamond.Util; @@ -27,6 +28,8 @@ namespace hexworld private Buffer _meshBuffer; private Buffer _tileBuffer; + private RenderGroup _renderGroup; + protected override void OnClosed(EventArgs e) { _texPgm?.Dispose(); @@ -45,8 +48,7 @@ namespace hexworld private Mesh _cubeMesh; - private Matrix4 _view; - private Matrix4 _proj; + private Camera _camera; private double _time; @@ -100,6 +102,19 @@ namespace hexworld BufferUsageHint.DynamicDraw, "tile"); _meshBuffer = Buffer.FromData(cubeMesh.Vertices.ToArray(), BufferTarget.ArrayBuffer, BufferUsageHint.StaticDraw, "mesh"); + + _camera = new Camera(); + + _renderGroup = new RenderGroup() + { + Camera = _camera, + Mesh = _cubeMesh, + MeshBuffer = _meshBuffer, + Program = _texPgm, + Texture = _grassTex, + TileBuffer = _tileBuffer, + Tiles = _floorTiles + }; } protected override void OnUpdateFrame(FrameEventArgs e) @@ -108,9 +123,9 @@ namespace hexworld _time += e.Time; - _view = Matrix4.CreateRotationZ((float) _time / 3) * - Matrix4.LookAt(10 * Vector3.One, Vector3.Zero, Vector3.UnitZ); - _proj = Matrix4.CreateOrthographic(Width / 100f, Height / 100f, -100, 100); + _camera.View = Matrix4.CreateRotationZ((float) _time / 3) * + Matrix4.LookAt(10 * Vector3.One, Vector3.Zero, Vector3.UnitZ); + _camera.Projection = Matrix4.CreateOrthographic(Width / 100f, Height / 100f, -100, 100); } protected override void OnRenderFrame(FrameEventArgs e) @@ -127,29 +142,7 @@ namespace hexworld GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); - - if (_texPgm != null) - { - _texPgm.Use(); - - _meshBuffer.PointTo(_texPgm); - _tileBuffer.PointTo(_texPgm); - - _grassTex.Bind(0); - - var texLoc = _texPgm.UniformLocation("tex"); - var viewLoc = _texPgm.UniformLocation("view"); - var projLoc = _texPgm.UniformLocation("proj"); - - if (texLoc.HasValue) - GL.Uniform1(texLoc.Value, 0); - if (viewLoc.HasValue) - GL.UniformMatrix4(viewLoc.Value, false, ref _view); - if (projLoc.HasValue) - GL.UniformMatrix4(projLoc.Value, false, ref _proj); - - _cubeMesh.DrawInstanced(_floorTiles); - } + _renderGroup.Draw(); SwapBuffers(); }