Skip to content

Commit

Permalink
Added EBO support
Browse files Browse the repository at this point in the history
  • Loading branch information
TheVave committed Feb 24, 2024
1 parent e4c333c commit a42f1ee
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 18 deletions.
46 changes: 44 additions & 2 deletions SharpPhysics/2d/_2DSGLRenderer/Main/Internal2dRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ namespace SharpPhysics._2d._2DSGLRenderer.Main
/// </summary>
public class Internal2dRenderer
{
/// <summary>
/// vbo data buffer
/// </summary>
public float[] vboDataBuf;
/// <summary>
/// Current loading ebo
/// </summary>
public uint[] curEbo;

/// <summary>
/// The time since the program started
/// </summary>
Expand Down Expand Up @@ -490,8 +499,14 @@ public virtual void LD()
BNDVAO(i);
// creates vbo
INITVBO(i);
// gets vbo data buffer
GTVBOBUF(i);
// gets ebo
GTEBO(i);
// sets vbo data
STVBO(i);
// sets ebo data
STEBO(i);
// compiles shaders and shader progs
CMPLPROGC(ObjectsToRender[i].VrtxShader, ObjectsToRender[i].FragShader, i);
// sets the texture supporting attributes
Expand All @@ -507,7 +522,8 @@ public virtual void LD()
// sets the texture info to the shader
STTXTRUNI(i);
}

// cleans up a little
BUFCLNUP();
// loads user-defined info
OL.Invoke();
//initializes fps counter
Expand Down Expand Up @@ -551,6 +567,32 @@ public virtual void LD()
//OL.Invoke();
}

public virtual void GTVBOBUF(int objid)
{
vboDataBuf = GVFPS(ObjectsToRender[objid].objToSim.ObjectMesh);
}

public virtual void BUFCLNUP()
{
Array.Clear(vboDataBuf);
Array.Clear(curEbo);
}

public unsafe virtual void STEBO(int objid)
{
ObjectsToRender[objid].eboPtr = gl.GenBuffer();
gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, ObjectsToRender[objid].eboPtr);
fixed (void* i = &curEbo)
{
gl.BufferData(BufferTargetARB.ElementArrayBuffer, (nuint)(curEbo.Length * sizeof(uint)), i, BufferUsageARB.StaticDraw);
}
}

public virtual void GTEBO(int objid)
{
curEbo = RenderingUtils.GetEbo(ref vboDataBuf);
}

/// <summary>
/// Initializes the fps counter
/// </summary>
Expand Down Expand Up @@ -706,7 +748,7 @@ public unsafe virtual void STSTDATTRIB()
/// </summary>
public virtual unsafe void STVBO(int objid)
{
float[] data = MSHTXCRDS(GVFPS(ObjectsToRender[objid].objToSim.ObjectMesh), ObjectsToRender[objid].objToSim.ObjectMesh);
float[] data = MSHTXCRDS(vboDataBuf, ObjectsToRender[objid].objToSim.ObjectMesh);
fixed (float* buf = data)
gl.BufferData(BufferTargetARB.ArrayBuffer, (nuint)(sizeof(float) * data.Length), buf, BufferUsageARB.StaticDraw);
}
Expand Down
2 changes: 1 addition & 1 deletion SharpPhysics/SharpPhysics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<AnalysisLevel>6.0-all</AnalysisLevel>
<GenerateDocumentationFile>False</GenerateDocumentationFile>
<AssemblyName>SharpPhysics</AssemblyName>
<NoWarn>CA1707;CA1051;CA1062;CA1805;CS8618;CA2211;</NoWarn>
<NoWarn>CA1707;CA1051;CA1062;CA1805;CS8618;CA2211;CS8500;</NoWarn>
<Description>A 2d renderer &amp; physics simulator made entirely with C#.
WARNING! THIS PROGRAM IS STILL UNDER DEVELOPMENT!</Description>
<PackageIcon>logo.png</PackageIcon>
Expand Down
15 changes: 15 additions & 0 deletions SharpPhysics/Utilities/MISC/ArrayUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,20 @@ public static T[] ConcatArray<T>(T[] baseArr, T[] Concater)
Concater.CopyTo(values, baseArr.Length);
return values;
}
/// <summary>
/// Exact same behavior as Array.IndexOf
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="val"></param>
/// <param name="array"></param>
/// <returns></returns>
public static int SpanIndexOf<T>(T val, T[] array)
{
for (int i = 0; i < array.Length; i++)
{
if (array[i].Equals(val)) return i;
}
return -1;
}
}
}
31 changes: 17 additions & 14 deletions SharpPhysics/Utilities/MISC/RenderingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,29 +150,32 @@ public static float[] MashMeshTextureFloats(float[] first, float[] last)

/// <summary>
/// Collects ebo data
/// WIP!
/// </summary>
/// <param name="vbo"></param>
/// <returns></returns>
public static uint[] GetEbo(ref float[] vertices)
{
Span<int> skipIdxs = [];
Span<uint> curEbo = [];
Span<int> curIdxs = [];
for (uint i = 0; i < vertices.Length; i++)
{
int[] rets = AllIndexesOf(vertices, vertices[i]);
if (rets.Length == 1) ArrayUtils.AddSpanObject(curEbo, i);
else if (rets.Length > 1)
{
Dictionary<string, uint> indexMap = [];
List<uint> indices = [];

}
else
for (int i = 0; i < vertices.Length; i += 3)
{
string vertexKey = $"{vertices[i]},{vertices[i + 1]},{vertices[i + 2]}";
if (!indexMap.ContainsKey(vertexKey))
{
ErrorHandler.ThrowError(1, true);
indexMap[vertexKey] = (uint)indexMap.Count;
}
indices.Add(indexMap[vertexKey]);
}

var distinctVertices = indexMap.Keys.SelectMany(v => v.Split(',').Select(float.Parse)).ToArray();
for (int i = 0; i < indices.Count; i++)
{
vertices[i * 3] = distinctVertices[(int)indices[i] * 3];
vertices[i * 3 + 1] = distinctVertices[(int)indices[i] * 3 + 1];
vertices[i * 3 + 2] = distinctVertices[(int)indices[i] * 3 + 2];
}
return [];
return indices.ToArray();
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion SharpPhysics/Utilities/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public static class Utils
{
public static nint NULLPTR = 0;
public static IntPtr NULLPTR = 0;
}
}

0 comments on commit a42f1ee

Please sign in to comment.