SkinRenderer - Reduced guidelines buffer size

This commit is contained in:
miku-666
2024-02-23 18:22:32 +01:00
parent ebf9138b20
commit a4acbf4b33
6 changed files with 78 additions and 64 deletions

View File

@@ -159,6 +159,7 @@
<Compile Include="Rendering\IVertexLayout.cs" />
<Compile Include="Rendering\DrawContext.cs" />
<Compile Include="Rendering\LineVertex.cs" />
<Compile Include="Rendering\OutlineDefinition.cs" />
<Compile Include="Rendering\Renderer.cs" />
<Compile Include="Rendering\GenericMesh.cs" />
<Compile Include="Rendering\Shader\ShaderLibrary.cs" />

View File

@@ -115,13 +115,15 @@ namespace PckStudio.Rendering
return cubes[index].Center + Transform;
}
internal Vector3[] GetCubical(int index)
internal OutlineDefinition GetOutline(int index)
{
if (!cubes.IndexInRange(index))
throw new IndexOutOfRangeException();
CubeData cube = cubes[index];
return cube.GetOutline().Select(pos => pos + Transform).ToArray();
OutlineDefinition outline = cube.GetOutline();
outline.verticies = outline.verticies.Select(pos => pos + Transform).ToArray();
return outline;
}
internal Vector3 GetFaceCenter(int index, CubeData.CubeFace face)

View File

@@ -146,7 +146,7 @@ namespace PckStudio.Rendering
}
}
internal Vector3[] GetOutline()
internal OutlineDefinition GetOutline()
{
List<Vector3> verts = new List<Vector3>();
@@ -160,43 +160,37 @@ namespace PckStudio.Rendering
Vector3 topLeftFront = vertices[6].Position;
Vector3 topRightFront = vertices[7].Position;
OutlineDefinition outline = new OutlineDefinition();
outline.verticies = [
bottomRightBack,
bottomLeftBack,
topLeftBack,
topRightBack,
return [
topLeftBack,
topLeftFront,
topLeftBack,
topRightBack,
topRightBack,
topRightFront,
topLeftFront,
topRightFront,
bottomLeftBack,
bottomLeftFront,
bottomLeftBack,
bottomRightBack,
bottomRightBack,
bottomRightFront,
bottomLeftFront,
bottomRightFront,
topLeftFront,
bottomLeftFront,
topRightFront,
bottomRightFront,
topLeftBack,
bottomLeftBack,
topRightBack,
bottomRightBack,
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 void UpdateVertices()

View File

@@ -11,21 +11,21 @@ namespace PckStudio.Rendering
internal class IndexBuffer : IDisposable
{
private int _id;
private List<int> _indecies;
private List<int> _indicies;
public IndexBuffer(params int[] indecies)
{
_indecies = new List<int>(indecies);
_indicies = new List<int>(indecies);
}
/// <summary>
/// Creates and attaches created index buffer
/// </summary>
/// <param name="indecies"></param>
/// <param name="indicies"></param>
/// <returns></returns>
public static IndexBuffer Create(params int[] indecies)
public static IndexBuffer Create(params int[] indicies)
{
var ib = new IndexBuffer(indecies);
var ib = new IndexBuffer(indicies);
ib.Attach();
return ib;
}
@@ -34,11 +34,11 @@ namespace PckStudio.Rendering
{
_id = GL.GenBuffer();
Bind();
GL.BufferData(BufferTarget.ElementArrayBuffer, _indecies.Count * sizeof(int), _indecies.ToArray(), BufferUsageHint.StaticDraw);
GL.BufferData(BufferTarget.ElementArrayBuffer, _indicies.Count * sizeof(int), _indicies.ToArray(), BufferUsageHint.StaticDraw);
Unbind();
}
public int GetCount() => _indecies.Count;
public int GetCount() => _indicies.Count;
public void Bind()
{

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
namespace PckStudio.Rendering
{
internal struct OutlineDefinition
{
internal int[] indicies;
internal Vector3[] verticies;
}
}

View File

@@ -503,13 +503,22 @@ namespace PckStudio.Rendering
// Cubical draw context
{
VertexArray lineVAO = new VertexArray();
List<LineVertex> vertices = new List<LineVertex>(24 * 6);
vertices.AddRange(head.GetCubical(0).Select(pos => new LineVertex(pos, lineColor)));
vertices.AddRange(body.GetCubical(0).Select(pos => new LineVertex(pos, lineColor)));
vertices.AddRange(rightArm.GetCubical(0).Select(pos => new LineVertex(pos, lineColor)));
vertices.AddRange(leftArm.GetCubical(0).Select(pos => new LineVertex(pos, lineColor)));
vertices.AddRange(rightLeg.GetCubical(0).Select(pos => new LineVertex(pos, lineColor)));
vertices.AddRange(leftLeg.GetCubical(0).Select(pos => new LineVertex(pos, lineColor)));
void AddOutline(OutlineDefinition outline, ref List<LineVertex> vertices, ref List<int> indices)
{
int offset = vertices.Count;
vertices.AddRange(outline.verticies.Select(pos => new LineVertex(pos, lineColor)));
indices.AddRange(outline.indicies.Select(i => i + offset));
}
List<LineVertex> vertices = new List<LineVertex>(8 * 6);
List<int> indices = new List<int>(24 * 6);
AddOutline(head.GetOutline(0), ref vertices, ref indices);
AddOutline(body.GetOutline(0), ref vertices, ref indices);
AddOutline(rightArm.GetOutline(0), ref vertices, ref indices);
AddOutline(leftArm.GetOutline(0), ref vertices, ref indices);
AddOutline(rightLeg.GetOutline(0), ref vertices, ref indices);
AddOutline(leftLeg.GetOutline(0), ref vertices, ref indices);
VertexBuffer buffer = new VertexBuffer();
buffer.SetData(vertices.ToArray());
VertexBufferLayout layout = new VertexBufferLayout();
@@ -518,7 +527,7 @@ namespace PckStudio.Rendering
lineVAO.AddBuffer(buffer, layout);
lineVAO.Bind();
_cubicalDrawContext = new DrawContext(lineVAO, buffer.GenIndexBuffer(), PrimitiveType.Lines);
_cubicalDrawContext = new DrawContext(lineVAO, IndexBuffer.Create(indices.ToArray()), PrimitiveType.Lines);
}
GLErrorCheck();
@@ -530,31 +539,23 @@ namespace PckStudio.Rendering
Vector3 bodyCenterBottom = body.GetFaceCenter(0, CubeData.CubeFace.Bottom);
LineVertex[] data = [
new LineVertex(head.GetFaceCenter(0, CubeData.CubeFace.Top), lineColor),
new LineVertex(bodyCenterTop, lineColor),
new LineVertex(bodyCenterBottom, lineColor),
new LineVertex(rightArm.GetFaceCenter(0, CubeData.CubeFace.Bottom), lineColor),
new LineVertex(rightArm.GetFaceCenter(0, CubeData.CubeFace.Top), lineColor),
new LineVertex(rightArm.GetFaceCenter(0, CubeData.CubeFace.Top), lineColor),
new LineVertex(bodyCenterTop, lineColor),
new LineVertex(leftArm.GetFaceCenter(0, CubeData.CubeFace.Top), lineColor),
new LineVertex(leftArm.GetFaceCenter(0, CubeData.CubeFace.Bottom), lineColor),
new LineVertex(leftArm.GetFaceCenter(0, CubeData.CubeFace.Top), lineColor),
new LineVertex(leftArm.GetFaceCenter(0, CubeData.CubeFace.Top), lineColor),
new LineVertex(bodyCenterTop, lineColor),
new LineVertex(rightLeg.GetFaceCenter(0, CubeData.CubeFace.Bottom), lineColor),
new LineVertex(rightLeg.GetFaceCenter(0, CubeData.CubeFace.Top), lineColor),
new LineVertex(rightLeg.GetFaceCenter(0, CubeData.CubeFace.Top), lineColor),
new LineVertex(bodyCenterBottom, lineColor),
new LineVertex(bodyCenterBottom, lineColor),
new LineVertex(bodyCenterTop, lineColor),
new LineVertex(leftLeg.GetFaceCenter(0, CubeData.CubeFace.Top), lineColor),
new LineVertex(leftLeg.GetFaceCenter(0, CubeData.CubeFace.Bottom), lineColor),
new LineVertex(leftLeg.GetFaceCenter(0, CubeData.CubeFace.Top), lineColor),
new LineVertex(leftLeg.GetFaceCenter(0, CubeData.CubeFace.Top), lineColor),
new LineVertex(bodyCenterBottom, lineColor),
new LineVertex(bodyCenterBottom, lineColor),
new LineVertex(bodyCenterTop, lineColor),
];
VertexBuffer buffer = new VertexBuffer();
buffer.SetData(data);
@@ -730,6 +731,7 @@ namespace PckStudio.Rendering
int slimValue = slim ? 3 : 4;
rightArm.ReplaceCube(0, new(-3, -2, -2), new(slimValue, 12, 4), new(40, 16));
rightArmOverlay.ReplaceCube(0, new(-3, -2, -2), new(slimValue, 12, 4), new(40, 32), scale: OverlayScale);
leftArm.ReplaceCube(0, new(-1, -2, -2), new(slimValue, 12, 4), new(32, 48));
leftArmOverlay.ReplaceCube(0, new(-1, -2, -2), new(slimValue, 12, 4), new(48, 48), scale: OverlayScale);