diff --git a/Raylib-cs/types/Mesh.cs b/Raylib-cs/types/Mesh.cs index 116542e..6f6980d 100644 --- a/Raylib-cs/types/Mesh.cs +++ b/Raylib-cs/types/Mesh.cs @@ -9,6 +9,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 +33,37 @@ 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; #endregion @@ -63,22 +72,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 +96,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/MeshExtensions.cs b/Raylib-cs/types/MeshExtensions.cs new file mode 100644 index 0000000..964ce6e --- /dev/null +++ b/Raylib-cs/types/MeshExtensions.cs @@ -0,0 +1,90 @@ +using System; + +namespace Raylib_cs; + +internal static unsafe class MeshExtensions +{ + /// Allocate mesh vertices + public static void AllocVertices(ref this Mesh mesh) + { + mesh.Vertices = Raylib.New(3 * mesh.VertexCount); + } + + /// Allocate mesh texcoords + public static void AllocTexcoords(ref this Mesh mesh) + { + mesh.TexCoords = Raylib.New(2 * mesh.VertexCount); + } + + /// Allocate mesh texcoords2 + public static void AllocTexcoords2(ref this Mesh mesh) + { + mesh.TexCoords2 = Raylib.New(2 * mesh.VertexCount); + } + + /// Allocate mesh normals + public static void AllocNormals(ref this Mesh mesh) + { + mesh.Normals = Raylib.New(3 * mesh.VertexCount); + } + + /// Allocate mesh tangents + public static void AllocTangents(ref this Mesh mesh) + { + mesh.Tangents = Raylib.New(4 * mesh.VertexCount); + } + + /// Allocate mesh colors + public static void AllocColors(ref this Mesh mesh) + { + mesh.Colors = Raylib.New(4 * mesh.VertexCount); + } + + /// Allocate mesh indices + public static void AllocIndices(ref this Mesh mesh) + { + mesh.Indices = Raylib.New(3 * mesh.TriangleCount); + } + + /// Access mesh vertices + public static Span VerticesAs(this Mesh mesh) where T : unmanaged + { + return new(mesh.Vertices, 3 * mesh.VertexCount * sizeof(float) / sizeof(T)); + } + + /// Access mesh texcoords + public static Span TexcoordsAs(this Mesh mesh) where T : unmanaged + { + return new(mesh.TexCoords, 2 * mesh.VertexCount * sizeof(float) / sizeof(T)); + } + + /// Access mesh texcoords2 + public static Span Texcoords2As(this Mesh mesh) where T : unmanaged + { + return new(mesh.TexCoords2, 2 * mesh.VertexCount * sizeof(float) / sizeof(T)); + } + + /// Access mesh normals + public static Span NormalsAs(this Mesh mesh) where T : unmanaged + { + return new(mesh.Normals, 3 * mesh.VertexCount * sizeof(float) / sizeof(T)); + } + + /// Access mesh tangents + public static Span TangentsAs(this Mesh mesh) where T : unmanaged + { + return new(mesh.Tangents, 4 * mesh.VertexCount * sizeof(float) / sizeof(T)); + } + + /// Access mesh colors + public static Span ColorsAs(this Mesh mesh) where T : unmanaged + { + return new(mesh.Colors, 4 * mesh.VertexCount * sizeof(byte) / sizeof(T)); + } + + /// Access mesh indices + public static Span IndicesAs(this Mesh mesh) where T : unmanaged + { + return new(mesh.Indices, 3 * mesh.TriangleCount * sizeof(ushort) / sizeof(T)); + } +} 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) {