SceneViewport - Use DrawContext to store data for drawing bounding boxes

This commit is contained in:
miku-666
2024-03-23 09:32:14 +01:00
parent f6e08f3336
commit e2b05b70ff
5 changed files with 56 additions and 93 deletions

View File

@@ -17,5 +17,40 @@ namespace PckStudio.Rendering
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
];
}
}
}

View File

@@ -145,8 +145,7 @@ namespace PckStudio.Rendering
}
}
internal Vector3 GetCenter(int index)
internal Vector3 GetCenter(int index)
{
if (!cubes.IndexInRange(index))
throw new IndexOutOfRangeException();
@@ -160,9 +159,9 @@ namespace PckStudio.Rendering
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)

View File

@@ -108,53 +108,6 @@ namespace PckStudio.Rendering
}
}
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,

View File

@@ -7,7 +7,7 @@ using OpenTK.Graphics.OpenGL;
namespace PckStudio.Rendering
{
internal class DrawContext
internal class DrawContext : IDisposable
{
internal readonly VertexArray VertexArray;
internal readonly IndexBuffer IndexBuffer;
@@ -19,5 +19,11 @@ namespace PckStudio.Rendering
IndexBuffer = indexBuffer;
PrimitiveType = primitiveType;
}
public void Dispose()
{
VertexArray.Dispose();
IndexBuffer.Dispose();
}
}
}

View File

@@ -66,7 +66,7 @@ namespace PckStudio.Rendering
protected virtual void OnUpdate(object sender, TimestepEventArgs e)
{
Debug.WriteLine(e.Delta);
}
private int refreshRate = 120;
@@ -75,8 +75,7 @@ namespace PckStudio.Rendering
private bool isInitialized;
private ShaderProgram colorShader;
private VertexArray VAO;
private IndexBuffer IBO;
private DrawContext boundingBoxDrawContext;
protected void Init()
{
@@ -87,26 +86,13 @@ namespace PckStudio.Rendering
}
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 @@ namespace PckStudio.Rendering
timer.Dispose();
}
MakeCurrent();
VAO.Dispose();
IBO.Dispose();
boundingBoxDrawContext.Dispose();
colorShader.Dispose();
isInitialized = false;
base.Dispose(disposing);
}
@@ -157,25 +143,9 @@ namespace PckStudio.Rendering
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);
}