From 9f64d7f2cdd009bc094f0dcc3119f6660186e580 Mon Sep 17 00:00:00 2001 From: David Allemang Date: Tue, 7 Mar 2017 02:16:21 -0500 Subject: [PATCH] Suggested changes --- Diamond/Buffers/Buffer.cs | 8 +++---- Diamond/GLObject.cs | 4 ++-- Diamond/Render/Camera.cs | 22 +++++++++++++++++++ Diamond/Render/VertexBuffer.cs | 40 +++++++++++++++++++++++++++++----- hexworld/Driver.cs | 5 ----- hexworld/HexRender.cs | 37 +++++++++++++++---------------- 6 files changed, 79 insertions(+), 37 deletions(-) diff --git a/Diamond/Buffers/Buffer.cs b/Diamond/Buffers/Buffer.cs index 6e8af2e..3d74834 100644 --- a/Diamond/Buffers/Buffer.cs +++ b/Diamond/Buffers/Buffer.cs @@ -14,9 +14,8 @@ namespace Diamond.Buffers public class Buffer : GLObject where T : struct { internal readonly BufferWrap Wrapper; - private readonly VertexDataInfo _vdi; - protected readonly int Size; + private readonly VertexDataInfo _vdi; /// /// The target for this buffer; its type @@ -37,7 +36,6 @@ namespace Diamond.Buffers { Wrapper = wrapper; Name = name; - Size = Marshal.SizeOf(); _vdi = VertexDataInfo.GetInfo(); } @@ -45,7 +43,7 @@ namespace Diamond.Buffers /// Upload data to this buffer /// /// The data to upload - public void Data(T[] data) => Wrapper.Data(Size, data); + public void Data(T[] data) => Wrapper.Data(_vdi.Stride, data); /// /// Upload a range of data to this buffer @@ -53,7 +51,7 @@ namespace Diamond.Buffers /// The range offset /// The range length /// The data to upload, offset and length apply to both this and the target - public void Data(int offset, int count, T[] data) => Wrapper.SubData(Size, offset, count, data); + public void Data(int offset, int count, T[] data) => Wrapper.SubData(_vdi.Stride, offset, count, data); /// /// Upload a range of data to this buffer diff --git a/Diamond/GLObject.cs b/Diamond/GLObject.cs index 79ba886..f3511e2 100644 --- a/Diamond/GLObject.cs +++ b/Diamond/GLObject.cs @@ -11,12 +11,12 @@ namespace Diamond /// /// Logger for all GLObjects /// - protected static readonly Logger Logger = LogManager.GetLogger("GLObject"); + protected static readonly Logger Logger = LogManager.GetLogger(nameof(GLObject)); /// /// Name of this GLObject used for identification /// - public string Name { get; protected set; } = "GLObject"; + public string Name { get; protected set; } = nameof(GLObject); /// /// Delegate Dispose to underlying wrapper class diff --git a/Diamond/Render/Camera.cs b/Diamond/Render/Camera.cs index b33716b..10512c3 100644 --- a/Diamond/Render/Camera.cs +++ b/Diamond/Render/Camera.cs @@ -2,15 +2,28 @@ namespace Diamond.Render { + /// + /// Manages a projection and view matrix + /// public class Camera { private Vector3 _position = Vector3.Zero; private Vector3 _target = -Vector3.One; private Vector3 _up = Vector3.UnitZ; + /// + /// The view matrix + /// public Matrix4 View; + + /// + /// The projection matrix + /// public Matrix4 Projection; + /// + /// Sets and updates the position of the view matrix + /// public Vector3 Position { set @@ -21,6 +34,9 @@ namespace Diamond.Render get => _position; } + /// + /// Sets and updates the target of the view matrix + /// public Vector3 Target { set @@ -31,6 +47,9 @@ namespace Diamond.Render get => _target; } + /// + /// Sets and updates the up vector of the view matrix + /// public Vector3 Up { set @@ -41,6 +60,9 @@ namespace Diamond.Render get => _up; } + /// + /// Recalculate the view matrix + /// private void UpdateView() { View = Matrix4.LookAt(_position, _target, _up); diff --git a/Diamond/Render/VertexBuffer.cs b/Diamond/Render/VertexBuffer.cs index c32bea4..3314124 100644 --- a/Diamond/Render/VertexBuffer.cs +++ b/Diamond/Render/VertexBuffer.cs @@ -12,21 +12,38 @@ using Buffer = Diamond.Buffers.Buffer; namespace Diamond.Render { + /// + /// Manage a vertex buffer object + /// + /// Buffer data type public class VertexBuffer : IDisposable where T : struct { private Logger Logger = LogManager.GetLogger("VertexBuffer"); + /// + /// The name of this buffer object for identification + /// public string Name { get; set; } + + /// + /// The underlying buffer for this object + /// public Buffer Buffer; + + /// + /// A subset of the Buffer's array for this buffer + /// public SubArray Vertices; + + /// + /// Primitive type to render this object + /// public PrimitiveType Primitive; - private static readonly VertexDataInfo tVdi; - - static VertexBuffer() - { - tVdi = VertexDataInfo.GetInfo(); - } + /// + /// Vertex data info for this type of data + /// + private static readonly VertexDataInfo tVdi = VertexDataInfo.GetInfo(); internal VertexBuffer(Buffer buffer, SubArray vertices, PrimitiveType primitive, string name) { @@ -36,6 +53,9 @@ namespace Diamond.Render Name = name; } + /// + /// Render this buffer + /// public void Draw() { Buffer.PointTo(Program.Current); @@ -47,6 +67,11 @@ namespace Diamond.Render tVdi.DisableVertexPointers(); } + /// + /// Render this buffer using a second buffer as instance data + /// + /// + /// public void DrawInstanced(VertexBuffer instance) where TI : struct { var tiVdi = VertexBuffer.tVdi; @@ -76,6 +101,9 @@ namespace Diamond.Render } } + /// + /// Static operations for vertex buffers + /// public static class VertexBuffer { public static VertexBuffer[] FromArrays(T[][] arrays, PrimitiveType primitive = PrimitiveType.Triangles, diff --git a/hexworld/Driver.cs b/hexworld/Driver.cs index f298ecc..d65bf39 100644 --- a/hexworld/Driver.cs +++ b/hexworld/Driver.cs @@ -5,11 +5,6 @@ public static void Main(string[] args) { using (var gw = new HexRender(1280, 720)) gw.Run(); - -// var tile = JsonConvert.DeserializeObject("{\"mesh\":\"RightColumn\", \"pos\": {\"x\": 1, \"y\": 2, \"z\": 3}}"); -// -// Console.Out.WriteLine("tile = {0}", tile); -// Console.ReadKey(); } } } \ No newline at end of file diff --git a/hexworld/HexRender.cs b/hexworld/HexRender.cs index c1dc8ab..b38cfe5 100644 --- a/hexworld/HexRender.cs +++ b/hexworld/HexRender.cs @@ -46,7 +46,6 @@ namespace hexworld #endregion private List> _renderGroups; -// private RenderGroup _renderGroup; private Camera _camera; @@ -100,25 +99,25 @@ namespace hexworld _camera = new Camera(); - _renderGroups = new List>(); - - _renderGroups.Add(new RenderGroup() + _renderGroups = new List> { - Vertices = _meshVbos["Cube"], - Instance = _tileVbos[0], - Program = _texPgm, - Texture = _stoneTex, - Camera = _camera, - }); - - _renderGroups.Add(new RenderGroup() - { - Vertices = _meshVbos["Cube"], - Instance = _tileVbos[1], - Program = _texPgm, - Texture = _grassTex, - Camera = _camera, - }); + new RenderGroup() + { + Vertices = _meshVbos["Cube"], + Instance = _tileVbos[0], + Program = _texPgm, + Texture = _stoneTex, + Camera = _camera, + }, + new RenderGroup() + { + Vertices = _meshVbos["Cube"], + Instance = _tileVbos[1], + Program = _texPgm, + Texture = _grassTex, + Camera = _camera, + } + }; } protected override void OnUpdateFrame(FrameEventArgs e)