2
0
mirror of https://github.com/raylib-cs/raylib-cs synced 2025-04-03 11:09:40 -04:00

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)
This commit is contained in:
Nickolas McDonald 2023-09-09 13:01:59 -04:00 committed by GitHub
parent 730e0136b5
commit c642be6148
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 141 additions and 13 deletions

View File

@ -1,3 +1,4 @@
using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace Raylib_cs; namespace Raylib_cs;
@ -9,6 +10,15 @@ namespace Raylib_cs;
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public unsafe partial struct Mesh public unsafe partial struct Mesh
{ {
/// <summary>
/// Creates a mesh ready for default vertex data allocation
/// </summary>
public Mesh(int vertexCount, int triangleCount)
{
VertexCount = vertexCount;
TriangleCount = triangleCount;
}
/// <summary> /// <summary>
/// Number of vertices stored in arrays /// Number of vertices stored in arrays
/// </summary> /// </summary>
@ -24,37 +34,149 @@ public unsafe partial struct Mesh
/// <summary> /// <summary>
/// Vertex position (XYZ - 3 components per vertex) (shader-location = 0) /// Vertex position (XYZ - 3 components per vertex) (shader-location = 0)
/// </summary> /// </summary>
public float* Vertices; public float* Vertices = default;
/// <summary> /// <summary>
/// Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1) /// Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1)
/// </summary> /// </summary>
public float* TexCoords; public float* TexCoords = default;
/// <summary> /// <summary>
/// Vertex second texture coordinates (useful for lightmaps) (shader-location = 5) /// Vertex second texture coordinates (useful for lightmaps) (shader-location = 5)
/// </summary> /// </summary>
public float* TexCoords2; public float* TexCoords2 = default;
/// <summary> /// <summary>
/// Vertex normals (XYZ - 3 components per vertex) (shader-location = 2) /// Vertex normals (XYZ - 3 components per vertex) (shader-location = 2)
/// </summary> /// </summary>
public float* Normals; public float* Normals = default;
/// <summary> /// <summary>
/// Vertex tangents (XYZW - 4 components per vertex) (shader-location = 4) /// Vertex tangents (XYZW - 4 components per vertex) (shader-location = 4)
/// </summary> /// </summary>
public float* Tangents; public float* Tangents = default;
/// <summary> /// <summary>
/// Vertex colors (RGBA - 4 components per vertex) (shader-location = 3) /// Vertex colors (RGBA - 4 components per vertex) (shader-location = 3)
/// </summary> /// </summary>
public byte* Colors; public byte* Colors = default;
/// <summary> /// <summary>
/// Vertex indices (in case vertex data comes indexed) /// Vertex indices (in case vertex data comes indexed)
/// </summary> /// </summary>
public ushort* Indices; public ushort* Indices = default;
/// <summary>
/// Allocate <see cref="Vertices"/>
/// </summary>
public void AllocVertices()
{
Vertices = Raylib.New<float>(3 * VertexCount);
}
/// <summary>
/// Allocate <see cref="TexCoords"/>
/// </summary>
public void AllocTexCoords()
{
TexCoords = Raylib.New<float>(2 * VertexCount);
}
/// <summary>
/// Allocate <see cref="TexCoords2"/>
/// </summary>
public void AllocTexCoords2()
{
TexCoords2 = Raylib.New<float>(2 * VertexCount);
}
/// <summary>
/// Allocate <see cref="Normals"/>
/// </summary>
public void AllocNormals()
{
Normals = Raylib.New<float>(3 * VertexCount);
}
/// <summary>
/// Allocate <see cref="Tangents"/>
/// </summary>
public void AllocTangents()
{
Tangents = Raylib.New<float>(4 * VertexCount);
}
/// <summary>
/// Allocate <see cref="Colors"/>
/// </summary>
public void AllocColors()
{
Colors = Raylib.New<byte>(4 * VertexCount);
}
/// <summary>
/// Allocate <see cref="Indices"/>
/// </summary>
public void AllocIndices()
{
Indices = Raylib.New<ushort>(3 * TriangleCount);
}
/// <summary>
/// Access <see cref="Vertices"/>
/// </summary>
public readonly Span<T> VerticesAs<T>() where T : unmanaged
{
return new(Vertices, 3 * VertexCount * sizeof(float) / sizeof(T));
}
/// <summary>
/// Access <see cref="TexCoords"/>
/// </summary>
public readonly Span<T> TexCoordsAs<T>() where T : unmanaged
{
return new(TexCoords, 2 * VertexCount * sizeof(float) / sizeof(T));
}
/// <summary>
/// Access <see cref="TexCoords2"/>
/// </summary>
public readonly Span<T> TexCoords2As<T>() where T : unmanaged
{
return new(TexCoords2, 2 * VertexCount * sizeof(float) / sizeof(T));
}
/// <summary>
/// Access <see cref="Normals"/>
/// </summary>
public readonly Span<T> NormalsAs<T>() where T : unmanaged
{
return new(Normals, 3 * VertexCount * sizeof(float) / sizeof(T));
}
/// <summary>
/// Access <see cref="Tangents"/>
/// </summary>
public readonly Span<T> TangentsAs<T>() where T : unmanaged
{
return new(Tangents, 4 * VertexCount * sizeof(float) / sizeof(T));
}
/// <summary>
/// Access <see cref="Colors"/>
/// </summary>
public readonly Span<T> ColorsAs<T>() where T : unmanaged
{
return new(Colors, 4 * VertexCount * sizeof(byte) / sizeof(T));
}
/// <summary>
/// Access <see cref="Indices"/>
/// </summary>
public readonly Span<T> IndicesAs<T>() where T : unmanaged
{
return new(Indices, 3 * TriangleCount * sizeof(ushort) / sizeof(T));
}
#endregion #endregion
@ -63,22 +185,22 @@ public unsafe partial struct Mesh
/// <summary> /// <summary>
/// Animated vertex positions (after bones transformations) /// Animated vertex positions (after bones transformations)
/// </summary> /// </summary>
public float* AnimVertices; public float* AnimVertices = default;
/// <summary> /// <summary>
/// Animated normals (after bones transformations) /// Animated normals (after bones transformations)
/// </summary> /// </summary>
public float* AnimNormals; public float* AnimNormals = default;
/// <summary> /// <summary>
/// Vertex bone ids, up to 4 bones influence by vertex (skinning) /// Vertex bone ids, up to 4 bones influence by vertex (skinning)
/// </summary> /// </summary>
public byte* BoneIds; public byte* BoneIds = default;
/// <summary> /// <summary>
/// Vertex bone weight, up to 4 bones influence by vertex (skinning) /// Vertex bone weight, up to 4 bones influence by vertex (skinning)
/// </summary> /// </summary>
public float* BoneWeights; public float* BoneWeights = default;
#endregion #endregion
@ -87,12 +209,12 @@ public unsafe partial struct Mesh
/// <summary> /// <summary>
/// OpenGL Vertex Array Object id /// OpenGL Vertex Array Object id
/// </summary> /// </summary>
public uint VaoId; public uint VaoId = default;
/// <summary> /// <summary>
/// OpenGL Vertex Buffer Objects id (default vertex data, uint[]) /// OpenGL Vertex Buffer Objects id (default vertex data, uint[])
/// </summary> /// </summary>
public uint* VboId; public uint* VboId = default;
#endregion #endregion
} }

View File

@ -222,6 +222,12 @@ public static unsafe partial class Raylib
} }
} }
/// <summary>C++ style memory allocator</summary>
public static T* New<T>(int count) where T : unmanaged
{
return (T*)MemAlloc(count * sizeof(T));
}
/// <summary>Load file data as byte array (read)</summary> /// <summary>Load file data as byte array (read)</summary>
public static byte* LoadFileData(string fileName, ref uint bytesRead) public static byte* LoadFileData(string fileName, ref uint bytesRead)
{ {