Skip to content

Commit

Permalink
SceneViewport - Use DrawContext to store data for drawing bounding boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
NessieHax committed Mar 23, 2024
1 parent f6e08f3 commit e2b05b7
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 93 deletions.
35 changes: 35 additions & 0 deletions PCK-Studio/Rendering/BoundingBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
}
}
}
9 changes: 4 additions & 5 deletions PCK-Studio/Rendering/CubeGroupMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ private Vector3 Transform
}
}


internal Vector3 GetCenter(int index)
internal Vector3 GetCenter(int index)
{
if (!cubes.IndexInRange(index))
throw new IndexOutOfRangeException();
Expand All @@ -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)
Expand Down
47 changes: 0 additions & 47 deletions PCK-Studio/Rendering/CubeMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,53 +108,6 @@ internal class CubeMesh : Cube
}
}

internal OutlineDefinition GetOutline()
{
List<Vector3> verts = new List<Vector3>();

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,
Expand Down
8 changes: 7 additions & 1 deletion PCK-Studio/Rendering/DrawContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace PckStudio.Rendering
{
internal class DrawContext
internal class DrawContext : IDisposable
{
internal readonly VertexArray VertexArray;
internal readonly IndexBuffer IndexBuffer;
Expand All @@ -19,5 +19,11 @@ public DrawContext(VertexArray vertexArray, IndexBuffer indexBuffer, PrimitiveTy
IndexBuffer = indexBuffer;
PrimitiveType = primitiveType;
}

public void Dispose()
{
VertexArray.Dispose();
IndexBuffer.Dispose();
}
}
}
50 changes: 10 additions & 40 deletions PCK-Studio/Rendering/SceneViewport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public TimestepEventArgs(double seconds)

protected virtual void OnUpdate(object sender, TimestepEventArgs e)
{
Debug.WriteLine(e.Delta);

}

private int refreshRate = 120;
Expand All @@ -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()
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
Expand Down

0 comments on commit e2b05b7

Please sign in to comment.