From 6eb95ddd1fa818a1df221423e9f94a68805a722b Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Fri, 1 Nov 2024 10:48:45 +0800 Subject: [PATCH] feat: update BMapSharp - finish BMMesh bindings. - finish a half of BMMeshTrans bindings. - use getGenericValue and setGenericValue for most bindings. --- .../BMapSharp/BMapSharp/BMapWrapper.cs | 150 +++++++++++++++--- .../BMapSharp/BMapSharpTestbench/Program.cs | 76 +++++---- 2 files changed, 169 insertions(+), 57 deletions(-) diff --git a/BMapBindings/BMapSharp/BMapSharp/BMapWrapper.cs b/BMapBindings/BMapSharp/BMapSharp/BMapWrapper.cs index dfdaf8e..54a478d 100644 --- a/BMapBindings/BMapSharp/BMapSharp/BMapWrapper.cs +++ b/BMapBindings/BMapSharp/BMapSharp/BMapWrapper.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using BMapSharp.VirtoolsTypes; @@ -58,6 +59,8 @@ namespace BMapSharp.BMapWrapper { => StructAssigner(pstruct, count, iem); internal static void CKShortFaceIndicesAssigner(IntPtr pstruct, uint count, IEnumerable iem) => StructAssigner(pstruct, count, iem); + internal static void ShortAssigner(IntPtr pstruct, uint count, IEnumerable iem) + => StructAssigner(pstruct, count, iem); private static IEnumerable StructIterator(IntPtr pstruct, uint count) { var stride = Marshal.SizeOf(); @@ -74,6 +77,8 @@ namespace BMapSharp.BMapWrapper { => StructIterator(pstruct, count); internal static IEnumerable CKShortFaceIndicesIterator(IntPtr pstruct, uint count) => StructIterator(pstruct, count); + internal static IEnumerable ShortIterator(IntPtr pstruct, uint count) + => StructIterator(pstruct, count); #endregion @@ -199,10 +204,12 @@ namespace BMapSharp.BMapWrapper { protected delegate bool FctGenericValueGetter(IntPtr bmf, uint id, out T val); protected delegate bool FctGenericValueSetter(IntPtr bmf, uint id, T val); + [MethodImpl(MethodImplOptions.AggressiveInlining)] protected T getGenericValue(FctGenericValueGetter fct) { BMapException.ThrowIfFailed(fct(getPointer(), getCKID(), out T out_val)); return out_val; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] protected void setGenericValue(FctGenericValueSetter fct, T val) { BMapException.ThrowIfFailed(fct(getPointer(), getCKID(), val)); } @@ -298,39 +305,96 @@ namespace BMapSharp.BMapWrapper { public class BMMesh : BMObject { internal BMMesh(IntPtr raw_pointer, uint ckid) : base(raw_pointer, ckid) { } + + public VXMESH_LITMODE GetLitMode() => getGenericValue(BMap.BMMesh_GetLitMode); + public void SetLitMode(VXMESH_LITMODE mode) => setGenericValue(BMap.BMMesh_SetLitMode, mode); + + public uint GetVertexCount() => getGenericValue(BMap.BMMesh_GetVertexCount); + public void SetVertexCount(uint count) => setGenericValue(BMap.BMMesh_SetVertexCount, count); + public IEnumerable GetVertexPositions() { + BMapException.ThrowIfFailed(BMap.BMMesh_GetVertexPositions(getPointer(), getCKID(), out IntPtr out_mem)); + return Utils.VxVector3Iterator(out_mem, GetVertexCount()); + } + public void SetVertexPositions(IEnumerable iem) { + BMapException.ThrowIfFailed(BMap.BMMesh_GetVertexPositions(getPointer(), getCKID(), out IntPtr out_mem)); + Utils.VxVector3Assigner(out_mem, GetVertexCount(), iem); + } + public IEnumerable GetVertexNormals() { + BMapException.ThrowIfFailed(BMap.BMMesh_GetVertexNormals(getPointer(), getCKID(), out IntPtr out_mem)); + return Utils.VxVector3Iterator(out_mem, GetVertexCount()); + } + public void SetVertexNormals(IEnumerable iem) { + BMapException.ThrowIfFailed(BMap.BMMesh_GetVertexNormals(getPointer(), getCKID(), out IntPtr out_mem)); + Utils.VxVector3Assigner(out_mem, GetVertexCount(), iem); + } + public IEnumerable GetVertexUVs() { + BMapException.ThrowIfFailed(BMap.BMMesh_GetVertexUVs(getPointer(), getCKID(), out IntPtr out_mem)); + return Utils.VxVector2Iterator(out_mem, GetVertexCount()); + } + public void SetVertexUVs(IEnumerable iem) { + BMapException.ThrowIfFailed(BMap.BMMesh_GetVertexUVs(getPointer(), getCKID(), out IntPtr out_mem)); + Utils.VxVector2Assigner(out_mem, GetVertexCount(), iem); + } + + public uint GetFaceCount() => getGenericValue(BMap.BMMesh_GetFaceCount); + public void SetFaceCount(uint count) => setGenericValue(BMap.BMMesh_SetFaceCount, count); + public IEnumerable GetFaceIndices() { + BMapException.ThrowIfFailed(BMap.BMMesh_GetFaceIndices(getPointer(), getCKID(), out IntPtr out_mem)); + return Utils.CKShortFaceIndicesIterator(out_mem, GetFaceCount()); + } + public void SetFaceIndices(IEnumerable iem) { + BMapException.ThrowIfFailed(BMap.BMMesh_GetFaceIndices(getPointer(), getCKID(), out IntPtr out_mem)); + Utils.CKShortFaceIndicesAssigner(out_mem, GetFaceCount(), iem); + } + public IEnumerable GetFaceMaterialSlotIndexs() { + BMapException.ThrowIfFailed(BMap.BMMesh_GetFaceMaterialSlotIndexs(getPointer(), getCKID(), out IntPtr out_mem)); + return Utils.ShortIterator(out_mem, GetFaceCount()); + } + public void SetFaceMaterialSlotIndexs(IEnumerable iem) { + BMapException.ThrowIfFailed(BMap.BMMesh_GetFaceMaterialSlotIndexs(getPointer(), getCKID(), out IntPtr out_mem)); + Utils.ShortAssigner(out_mem, GetFaceCount(), iem); + } + + public uint GetMaterialSlotCount() => getGenericValue(BMap.BMMesh_GetMaterialSlotCount); + public void SetMaterialSlotCount(uint count) => setGenericValue(BMap.BMMesh_SetMaterialSlotCount, count); + public IEnumerable GetMaterialSlots() { + uint count = GetMaterialSlotCount(); + for (uint i = 0; i < count; ++i) { + BMapException.ThrowIfFailed(BMap.BMMesh_GetMaterialSlot(getPointer(), getCKID(), i, out uint out_mtlid)); + if (out_mtlid == Utils.INVALID_CKID) yield return null; + else yield return new BMMaterial(getPointer(), out_mtlid); + } + } + public void SetMaterialSlots(IEnumerable iem) { + uint count = GetMaterialSlotCount(); + var itor = iem.GetEnumerator(); + for (uint i = 0; i < count; ++i) { + if (!itor.MoveNext()) throw new BMapException("The length of given material array is too short when assigning material slots."); + uint mtlid = itor.Current is null ? Utils.INVALID_CKID : itor.Current.getCKID(); + BMapException.ThrowIfFailed(BMap.BMMesh_SetMaterialSlot(getPointer(), getCKID(), i, mtlid)); + } + } + } public class BM3dObject : BMObject { internal BM3dObject(IntPtr raw_pointer, uint ckid) : base(raw_pointer, ckid) { } - public VxMatrix GetWorldMatrix() { - BMapException.ThrowIfFailed(BMap.BM3dObject_GetWorldMatrix(getPointer(), getCKID(), out VxMatrix out_mat)); - return out_mat; - } - - public void SetWorldMatrix(VxMatrix mat) { - BMapException.ThrowIfFailed(BMap.BM3dObject_SetWorldMatrix(getPointer(), getCKID(), mat)); - } + public VxMatrix GetWorldMatrix() => getGenericValue(BMap.BM3dObject_GetWorldMatrix); + public void SetWorldMatrix(VxMatrix mat) => setGenericValue(BMap.BM3dObject_SetWorldMatrix, mat); public BMMesh GetCurrentMesh() { BMapException.ThrowIfFailed(BMap.BM3dObject_GetCurrentMesh(getPointer(), getCKID(), out uint out_meshid)); if (out_meshid == Utils.INVALID_CKID) return null; else return new BMMesh(getPointer(), out_meshid); } - public void SetCurrentMesh(BMMesh mesh) { uint meshid = (mesh is null) ? Utils.INVALID_CKID : mesh.getCKID(); BMapException.ThrowIfFailed(BMap.BM3dObject_SetCurrentMesh(getPointer(), getCKID(), meshid)); } - public bool GetVisibility() { - BMapException.ThrowIfFailed(BMap.BM3dObject_GetVisibility(getPointer(), getCKID(), out bool out_isVisible)); - return out_isVisible; - } - - public void SetVisibility(bool visb) { - BMapException.ThrowIfFailed(BMap.BM3dObject_SetVisibility(getPointer(), getCKID(), visb)); - } + public bool GetVisibility() => getGenericValue(BMap.BM3dObject_GetVisibility); + public void SetVisibility(bool visb) => setGenericValue(BMap.BM3dObject_SetVisibility, visb); } public class BMGroup : BMObject { @@ -340,11 +404,7 @@ namespace BMapSharp.BMapWrapper { BMapException.ThrowIfFailed(BMap.BMGroup_AddObject(getPointer(), getCKID(), member.getCKID())); } - public uint GetObjectCount() { - BMapException.ThrowIfFailed(BMap.BMGroup_GetObjectCount(getPointer(), getCKID(), out uint out_count)); - return out_count; - } - + public uint GetObjectCount() => getGenericValue(BMap.BMGroup_GetObjectCount); public IEnumerable GetObjects() { var size = GetObjectCount(); for (uint i = 0; i < size; ++i) { @@ -424,6 +484,16 @@ namespace BMapSharp.BMapWrapper { public BMFileWriter(string temp_folder, string texture_folder, string[] encodings) : base(allocateHandle(temp_folder, texture_folder, encodings)) { } + public void Save(string filename, CK_TEXTURE_SAVEOPTIONS texture_save_opt, bool use_compress, int compress_level) { + BMapException.ThrowIfFailed(BMap.BMFile_Save( + getPointer(), + filename, + texture_save_opt, + use_compress, + compress_level + )); + } + private delegate bool FctProtoCreateObject(IntPtr bmf, out uint id); private delegate T FctProtoCreateInstance(IntPtr bmf, uint id); private T createCKObject(FctProtoCreateObject fct_crt, FctProtoCreateInstance fct_inst) { @@ -448,6 +518,42 @@ namespace BMapSharp.BMapWrapper { } public BMMeshTrans() : base(allocateHandle()) { } + public void Parse(BMMesh objmesh) { + BMapException.ThrowIfFailed(BMap.BMMeshTrans_Parse( + getPointer(), + objmesh.getPointer(), + objmesh.getCKID() + )); + } + + public void PrepareVertex(uint count, IEnumerable iem) { + // Prepare count first + BMapException.ThrowIfFailed(BMap.BMMeshTrans_PrepareVertexCount(getPointer(), count)); + // Then put data + BMapException.ThrowIfFailed(BMap.BMMeshTrans_PrepareVertex(getPointer(), out IntPtr out_mem)); + Utils.VxVector3Assigner(out_mem, count, iem); + } + public void PrepareNormal(uint count, IEnumerable iem) { + // Prepare count first + BMapException.ThrowIfFailed(BMap.BMMeshTrans_PrepareNormalCount(getPointer(), count)); + // Then put data + BMapException.ThrowIfFailed(BMap.BMMeshTrans_PrepareNormal(getPointer(), out IntPtr out_mem)); + Utils.VxVector3Assigner(out_mem, count, iem); + } + public void PrepareUV(uint count, IEnumerable iem) { + // Prepare count first + BMapException.ThrowIfFailed(BMap.BMMeshTrans_PrepareUVCount(getPointer(), count)); + // Then put data + BMapException.ThrowIfFailed(BMap.BMMeshTrans_PrepareUV(getPointer(), out IntPtr out_mem)); + Utils.VxVector2Assigner(out_mem, count, iem); + } + + // public void PrepareMtlSlot(uint count, IEnumerable iem) { + // // Prepare count first + // BMapException.ThrowIfFailed(BMap.BMMeshTrans_PrepareMtlSlotCount(getPointer(), count)); + // // Then put data + // BMapException.ThrowIfFailed(BMap.BMMeshTrans_PrepareMtlSlot(getPointer(), out IntPtr out_mem)); + // } } diff --git a/BMapBindings/BMapSharp/BMapSharpTestbench/Program.cs b/BMapBindings/BMapSharp/BMapSharpTestbench/Program.cs index a55dcea..5e468de 100644 --- a/BMapBindings/BMapSharp/BMapSharpTestbench/Program.cs +++ b/BMapBindings/BMapSharp/BMapSharpTestbench/Program.cs @@ -42,44 +42,49 @@ namespace BMapSharpTestbench { // Console.WriteLine($"\tMatrix: {obj.GetWorldMatrix().ToManaged()}"); // } - // Console.WriteLine("===== Meshes ====="); - // foreach (var mesh in reader.GetMeshes()) { - // Console.WriteLine(mesh.GetName()); - // } + Console.WriteLine("===== Meshes ====="); + foreach (var mesh in reader.GetMeshes()) { + Console.WriteLine(mesh.GetName()); - Console.WriteLine("===== Materials ====="); - foreach (var mtl in reader.GetMaterials()) { - Console.WriteLine(mtl.GetName()); - - Console.WriteLine($"\tDiffuse: {mtl.GetDiffuse().ToManagedRGBA()}"); - Console.WriteLine($"\tAmbient: {mtl.GetAmbient().ToManagedRGBA()}"); - Console.WriteLine($"\tSpecular: {mtl.GetSpecular().ToManagedRGBA()}"); - Console.WriteLine($"\tEmissive: {mtl.GetEmissive().ToManagedRGBA()}"); - - Console.WriteLine($"\tSpecular Power: {mtl.GetSpecularPower()}"); - - Console.WriteLine($"\tTexture Border Color: {mtl.GetTextureBorderColor().ToManagedRGBA()}"); - - Console.WriteLine($"\tTexture Blend Mode: {mtl.GetTextureBlendMode()}"); - Console.WriteLine($"\tTexture Min Mode: {mtl.GetTextureMinMode()}"); - Console.WriteLine($"\tTexture Mag Mode: {mtl.GetTextureMagMode()}"); - Console.WriteLine($"\tSource Blend: {mtl.GetSourceBlend()}"); - Console.WriteLine($"\tDest Blend: {mtl.GetDestBlend()}"); - Console.WriteLine($"\tFill Mode: {mtl.GetFillMode()}"); - Console.WriteLine($"\tShade Mode: {mtl.GetShadeMode()}"); - - Console.WriteLine($"\tAlpha Test Enabled: {mtl.GetAlphaTestEnabled()}"); - Console.WriteLine($"\tAlpha Blend Enabled: {mtl.GetAlphaBlendEnabled()}"); - Console.WriteLine($"\tPerspective Correction Enabled: {mtl.GetPerspectiveCorrectionEnabled()}"); - Console.WriteLine($"\tZ Write Enabled: {mtl.GetZWriteEnabled()}"); - Console.WriteLine($"\tTwo Sided Enabled: {mtl.GetTwoSidedEnabled()}"); - - Console.WriteLine($"\tAlpha Ref: {mtl.GetAlphaRef()}"); - - Console.WriteLine($"\tAlpha Func: {mtl.GetAlphaFunc()}"); - Console.WriteLine($"\tZ Func: {mtl.GetZFunc()}"); + Console.WriteLine($"\tLit Mode: {mesh.GetLitMode()}"); + Console.WriteLine($"\tVertex Count: {mesh.GetVertexCount()}"); + Console.WriteLine($"\tFace Count: {mesh.GetFaceCount()}"); + Console.WriteLine($"\tMaterial Slot Count: {mesh.GetMaterialSlotCount()}"); } + // Console.WriteLine("===== Materials ====="); + // foreach (var mtl in reader.GetMaterials()) { + // Console.WriteLine(mtl.GetName()); + + // Console.WriteLine($"\tDiffuse: {mtl.GetDiffuse().ToManagedRGBA()}"); + // Console.WriteLine($"\tAmbient: {mtl.GetAmbient().ToManagedRGBA()}"); + // Console.WriteLine($"\tSpecular: {mtl.GetSpecular().ToManagedRGBA()}"); + // Console.WriteLine($"\tEmissive: {mtl.GetEmissive().ToManagedRGBA()}"); + + // Console.WriteLine($"\tSpecular Power: {mtl.GetSpecularPower()}"); + + // Console.WriteLine($"\tTexture Border Color: {mtl.GetTextureBorderColor().ToManagedRGBA()}"); + + // Console.WriteLine($"\tTexture Blend Mode: {mtl.GetTextureBlendMode()}"); + // Console.WriteLine($"\tTexture Min Mode: {mtl.GetTextureMinMode()}"); + // Console.WriteLine($"\tTexture Mag Mode: {mtl.GetTextureMagMode()}"); + // Console.WriteLine($"\tSource Blend: {mtl.GetSourceBlend()}"); + // Console.WriteLine($"\tDest Blend: {mtl.GetDestBlend()}"); + // Console.WriteLine($"\tFill Mode: {mtl.GetFillMode()}"); + // Console.WriteLine($"\tShade Mode: {mtl.GetShadeMode()}"); + + // Console.WriteLine($"\tAlpha Test Enabled: {mtl.GetAlphaTestEnabled()}"); + // Console.WriteLine($"\tAlpha Blend Enabled: {mtl.GetAlphaBlendEnabled()}"); + // Console.WriteLine($"\tPerspective Correction Enabled: {mtl.GetPerspectiveCorrectionEnabled()}"); + // Console.WriteLine($"\tZ Write Enabled: {mtl.GetZWriteEnabled()}"); + // Console.WriteLine($"\tTwo Sided Enabled: {mtl.GetTwoSidedEnabled()}"); + + // Console.WriteLine($"\tAlpha Ref: {mtl.GetAlphaRef()}"); + + // Console.WriteLine($"\tAlpha Func: {mtl.GetAlphaFunc()}"); + // Console.WriteLine($"\tZ Func: {mtl.GetZFunc()}"); + // } + // Console.WriteLine("===== Textures ====="); // foreach (var tex in reader.GetTextures()) { // Console.WriteLine(tex.GetName()); @@ -92,6 +97,7 @@ namespace BMapSharpTestbench { } Console.WriteLine("===== Done ====="); + Console.WriteLine("Press any key to quit..."); Console.ReadKey(true); }