From e2b05b70ffa558a230b411aa86f92b9ddebb57f3 Mon Sep 17 00:00:00 2001 From: miku-666 <74728189+NessieHax@users.noreply.github.com> Date: Sat, 23 Mar 2024 09:32:14 +0100 Subject: [PATCH] SceneViewport - Use DrawContext to store data for drawing bounding boxes --- PCK-Studio/Rendering/BoundingBox.cs | 35 +++++++++++++++++++ PCK-Studio/Rendering/CubeGroupMesh.cs | 9 +++-- PCK-Studio/Rendering/CubeMesh.cs | 47 ------------------------- PCK-Studio/Rendering/DrawContext.cs | 8 ++++- PCK-Studio/Rendering/SceneViewport.cs | 50 ++++++--------------------- 5 files changed, 56 insertions(+), 93 deletions(-) diff --git a/PCK-Studio/Rendering/BoundingBox.cs b/PCK-Studio/Rendering/BoundingBox.cs index eb1b1590..617676b7 100644 --- a/PCK-Studio/Rendering/BoundingBox.cs +++ b/PCK-Studio/Rendering/BoundingBox.cs @@ -17,5 +17,40 @@ public BoundingBox(Vector3 start, Vector3 end) Start = start; End = end; } + + public ColorVertex[] GetVertices() + { + Vector3 s = Start; + Vector3 e = End; + return [ + new ColorVertex(new Vector3(s.X, e.Y, e.Z)), + new ColorVertex(new Vector3(e.X, e.Y, e.Z)), + new ColorVertex(new Vector3(e.X, s.Y, e.Z)), + new ColorVertex(new Vector3(s.X, s.Y, e.Z)), + new ColorVertex(new Vector3(s.X, e.Y, s.Z)), + new ColorVertex(new Vector3(e.X, e.Y, s.Z)), + new ColorVertex(new Vector3(e.X, s.Y, s.Z)), + new ColorVertex(new Vector3(s.X, s.Y, s.Z)), + ]; + } + + public static int[] GetIndecies() + { + return [0, 1, + 1, 2, + 2, 3, + 3, 0, + + 4, 5, + 5, 6, + 6, 7, + 7, 4, + + 0, 4, + 1, 5, + 2, 6, + 3, 7 + ]; + } } } diff --git a/PCK-Studio/Rendering/CubeGroupMesh.cs b/PCK-Studio/Rendering/CubeGroupMesh.cs index 0a142e24..8bfc38cf 100644 --- a/PCK-Studio/Rendering/CubeGroupMesh.cs +++ b/PCK-Studio/Rendering/CubeGroupMesh.cs @@ -145,8 +145,7 @@ private Vector3 Transform } } - - internal Vector3 GetCenter(int index) + internal Vector3 GetCenter(int index) { if (!cubes.IndexInRange(index)) throw new IndexOutOfRangeException(); @@ -160,9 +159,9 @@ internal OutlineDefinition GetOutline(int index) throw new IndexOutOfRangeException(); CubeMesh cube = cubes[index]; - OutlineDefinition outline = cube.GetOutline(); - outline.verticies = outline.verticies.Select(pos => pos + Transform).ToArray(); - return outline; + var boundingBox = cube.GetBoundingBox(); + Vector3[] verticies = boundingBox.GetVertices().Select(pos => pos.Position + Transform).ToArray(); + return new OutlineDefinition() { verticies = verticies, indicies = BoundingBox.GetIndecies()}; } internal Vector3 GetFaceCenter(int index, Cube.Face face) diff --git a/PCK-Studio/Rendering/CubeMesh.cs b/PCK-Studio/Rendering/CubeMesh.cs index 77c05996..53f684fc 100644 --- a/PCK-Studio/Rendering/CubeMesh.cs +++ b/PCK-Studio/Rendering/CubeMesh.cs @@ -108,53 +108,6 @@ internal class CubeMesh : Cube } } - internal OutlineDefinition GetOutline() - { - List verts = new List(); - - Vector3 bottomRightBack = vertices[0].Position; - Vector3 bottomLeftBack = vertices[1].Position; - Vector3 topLeftBack = vertices[2].Position; - Vector3 topRightBack = vertices[3].Position; - - Vector3 bottomRightFront = vertices[4].Position; - Vector3 bottomLeftFront = vertices[5].Position; - Vector3 topLeftFront = vertices[6].Position; - Vector3 topRightFront = vertices[7].Position; - - OutlineDefinition outline = new OutlineDefinition(); - outline.verticies = [ - bottomRightBack, - bottomLeftBack, - topLeftBack, - topRightBack, - - bottomRightFront, - bottomLeftFront, - topLeftFront, - topRightFront, - ]; - - outline.indicies = [ - 0, 1, - 1, 2, - 2, 3, - 3, 0, - - 4, 5, - 5, 6, - 6, 7, - 7, 4, - - 0, 4, - 1, 5, - 2, 6, - 3, 7, - ]; - - return outline; - } - private static int[] indicesData = [ // Face 1 (Back) 0, 1, 2, diff --git a/PCK-Studio/Rendering/DrawContext.cs b/PCK-Studio/Rendering/DrawContext.cs index ff5a1531..2ffb5096 100644 --- a/PCK-Studio/Rendering/DrawContext.cs +++ b/PCK-Studio/Rendering/DrawContext.cs @@ -7,7 +7,7 @@ namespace PckStudio.Rendering { - internal class DrawContext + internal class DrawContext : IDisposable { internal readonly VertexArray VertexArray; internal readonly IndexBuffer IndexBuffer; @@ -19,5 +19,11 @@ public DrawContext(VertexArray vertexArray, IndexBuffer indexBuffer, PrimitiveTy IndexBuffer = indexBuffer; PrimitiveType = primitiveType; } + + public void Dispose() + { + VertexArray.Dispose(); + IndexBuffer.Dispose(); + } } } diff --git a/PCK-Studio/Rendering/SceneViewport.cs b/PCK-Studio/Rendering/SceneViewport.cs index 7d8fa185..937eaddc 100644 --- a/PCK-Studio/Rendering/SceneViewport.cs +++ b/PCK-Studio/Rendering/SceneViewport.cs @@ -66,7 +66,7 @@ public TimestepEventArgs(double seconds) protected virtual void OnUpdate(object sender, TimestepEventArgs e) { - Debug.WriteLine(e.Delta); + } private int refreshRate = 120; @@ -75,8 +75,7 @@ protected virtual void OnUpdate(object sender, TimestepEventArgs e) private bool isInitialized; private ShaderProgram colorShader; - private VertexArray VAO; - private IndexBuffer IBO; + private DrawContext boundingBoxDrawContext; protected void Init() { @@ -87,26 +86,13 @@ protected void Init() } MakeCurrent(); colorShader = ShaderProgram.Create(Resources.plainColorVertexShader, Resources.plainColorFragmentShader); - VAO = new VertexArray(); - IBO = IndexBuffer.Create( - 0, 1, - 1, 2, - 2, 3, - 3, 0, - - 4, 5, - 5, 6, - 6, 7, - 7, 4, - - 0, 4, - 1, 5, - 2, 6, - 3, 7); + var vao = new VertexArray(); VertexBufferLayout layout = new VertexBufferLayout(); layout.Add(ShaderDataType.Float3); layout.Add(ShaderDataType.Float4); - VAO.AddNewBuffer(layout); + vao.AddNewBuffer(layout); + var ibo = IndexBuffer.Create(BoundingBox.GetIndecies()); + boundingBoxDrawContext = new DrawContext(vao, ibo, PrimitiveType.Lines); isInitialized = true; } @@ -136,9 +122,9 @@ protected override void Dispose(bool disposing) timer.Dispose(); } MakeCurrent(); - VAO.Dispose(); - IBO.Dispose(); + boundingBoxDrawContext.Dispose(); colorShader.Dispose(); + isInitialized = false; base.Dispose(disposing); } @@ -157,25 +143,9 @@ protected void DrawBoundingBox(Matrix4 transform, BoundingBox boundingBox, Color GL.DepthFunc(DepthFunction.Always); Renderer.SetLineWidth(2f); + boundingBoxDrawContext.VertexArray.GetBuffer(0).SetData(boundingBox.GetVertices()); + Renderer.Draw(colorShader, boundingBoxDrawContext); - Vector3 s = boundingBox.Start; - Vector3 e = boundingBox.End; - - ColorVertex[] vertices = [ - new ColorVertex(new Vector3(s.X, e.Y, e.Z)), - new ColorVertex(new Vector3(e.X, e.Y, e.Z)), - new ColorVertex(new Vector3(e.X, s.Y, e.Z)), - new ColorVertex(new Vector3(s.X, s.Y, e.Z)), - new ColorVertex(new Vector3(s.X, e.Y, s.Z)), - new ColorVertex(new Vector3(e.X, e.Y, s.Z)), - new ColorVertex(new Vector3(e.X, s.Y, s.Z)), - new ColorVertex(new Vector3(s.X, s.Y, s.Z)), - ]; - - VAO.Bind(); - VAO.GetBuffer(0).SetData(vertices); - IBO.Bind(); - GL.DrawElements(PrimitiveType.Lines, IBO.GetCount(), DrawElementsType.UnsignedInt, 0); GL.DepthFunc(DepthFunction.Less); Renderer.SetLineWidth(1f); }