From c642be6148aa4d20ee6b4ddffe47505262ba5455 Mon Sep 17 00:00:00 2001 From: Nickolas McDonald <43690021+n77y@users.noreply.github.com> Date: Sat, 9 Sep 2023 13:01:59 -0400 Subject: [PATCH] Improve utils for mesh generation and processing (#199) * Add memory allocation to Raylib.Utils for memory allocation (#179) * Add utils to Mesh for allocating and accessing mesh attributes (#179) --- Raylib-cs/types/Mesh.cs | 148 +++++++++++++++++++++++++++++--- Raylib-cs/types/Raylib.Utils.cs | 6 ++ 2 files changed, 141 insertions(+), 13 deletions(-) diff --git a/Raylib-cs/types/Mesh.cs b/Raylib-cs/types/Mesh.cs index 116542e..ccec9d3 100644 --- a/Raylib-cs/types/Mesh.cs +++ b/Raylib-cs/types/Mesh.cs @@ -1,3 +1,4 @@ +using System; using System.Runtime.InteropServices; namespace Raylib_cs; @@ -9,6 +10,15 @@ namespace Raylib_cs; [StructLayout(LayoutKind.Sequential)] public unsafe partial struct Mesh { + /// + /// Creates a mesh ready for default vertex data allocation + /// + public Mesh(int vertexCount, int triangleCount) + { + VertexCount = vertexCount; + TriangleCount = triangleCount; + } + /// /// Number of vertices stored in arrays /// @@ -24,37 +34,149 @@ public unsafe partial struct Mesh /// /// Vertex position (XYZ - 3 components per vertex) (shader-location = 0) /// - public float* Vertices; + public float* Vertices = default; /// /// Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1) /// - public float* TexCoords; + public float* TexCoords = default; /// /// Vertex second texture coordinates (useful for lightmaps) (shader-location = 5) /// - public float* TexCoords2; + public float* TexCoords2 = default; /// /// Vertex normals (XYZ - 3 components per vertex) (shader-location = 2) /// - public float* Normals; + public float* Normals = default; /// /// Vertex tangents (XYZW - 4 components per vertex) (shader-location = 4) /// - public float* Tangents; + public float* Tangents = default; /// /// Vertex colors (RGBA - 4 components per vertex) (shader-location = 3) /// - public byte* Colors; + public byte* Colors = default; /// /// Vertex indices (in case vertex data comes indexed) /// - public ushort* Indices; + public ushort* Indices = default; + + /// + /// Allocate + /// + public void AllocVertices() + { + Vertices = Raylib.New(3 * VertexCount); + } + + /// + /// Allocate + /// + public void AllocTexCoords() + { + TexCoords = Raylib.New(2 * VertexCount); + } + + /// + /// Allocate + /// + public void AllocTexCoords2() + { + TexCoords2 = Raylib.New(2 * VertexCount); + } + + /// + /// Allocate + /// + public void AllocNormals() + { + Normals = Raylib.New(3 * VertexCount); + } + + /// + /// Allocate + /// + public void AllocTangents() + { + Tangents = Raylib.New(4 * VertexCount); + } + + /// + /// Allocate + /// + public void AllocColors() + { + Colors = Raylib.New(4 * VertexCount); + } + + /// + /// Allocate + /// + public void AllocIndices() + { + Indices = Raylib.New(3 * TriangleCount); + } + + /// + /// Access + /// + public readonly Span VerticesAs() where T : unmanaged + { + return new(Vertices, 3 * VertexCount * sizeof(float) / sizeof(T)); + } + + /// + /// Access + /// + public readonly Span TexCoordsAs() where T : unmanaged + { + return new(TexCoords, 2 * VertexCount * sizeof(float) / sizeof(T)); + } + + /// + /// Access + /// + public readonly Span TexCoords2As() where T : unmanaged + { + return new(TexCoords2, 2 * VertexCount * sizeof(float) / sizeof(T)); + } + + /// + /// Access + /// + public readonly Span NormalsAs() where T : unmanaged + { + return new(Normals, 3 * VertexCount * sizeof(float) / sizeof(T)); + } + + /// + /// Access + /// + public readonly Span TangentsAs() where T : unmanaged + { + return new(Tangents, 4 * VertexCount * sizeof(float) / sizeof(T)); + } + + /// + /// Access + /// + public readonly Span ColorsAs() where T : unmanaged + { + return new(Colors, 4 * VertexCount * sizeof(byte) / sizeof(T)); + } + + /// + /// Access + /// + public readonly Span IndicesAs() where T : unmanaged + { + return new(Indices, 3 * TriangleCount * sizeof(ushort) / sizeof(T)); + } #endregion @@ -63,22 +185,22 @@ public unsafe partial struct Mesh /// /// Animated vertex positions (after bones transformations) /// - public float* AnimVertices; + public float* AnimVertices = default; /// /// Animated normals (after bones transformations) /// - public float* AnimNormals; + public float* AnimNormals = default; /// /// Vertex bone ids, up to 4 bones influence by vertex (skinning) /// - public byte* BoneIds; + public byte* BoneIds = default; /// /// Vertex bone weight, up to 4 bones influence by vertex (skinning) /// - public float* BoneWeights; + public float* BoneWeights = default; #endregion @@ -87,12 +209,12 @@ public unsafe partial struct Mesh /// /// OpenGL Vertex Array Object id /// - public uint VaoId; + public uint VaoId = default; /// /// OpenGL Vertex Buffer Objects id (default vertex data, uint[]) /// - public uint* VboId; + public uint* VboId = default; #endregion } diff --git a/Raylib-cs/types/Raylib.Utils.cs b/Raylib-cs/types/Raylib.Utils.cs index 96b4cdc..98b9526 100644 --- a/Raylib-cs/types/Raylib.Utils.cs +++ b/Raylib-cs/types/Raylib.Utils.cs @@ -222,6 +222,12 @@ public static unsafe partial class Raylib } } + /// C++ style memory allocator + public static T* New(int count) where T : unmanaged + { + return (T*)MemAlloc(count * sizeof(T)); + } + /// Load file data as byte array (read) public static byte* LoadFileData(string fileName, ref uint bytesRead) {