From 2bd0c980b551e56bc8c7b44351762c78c9311428 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Sun, 8 Oct 2023 20:56:29 +0800 Subject: [PATCH] add mesh interface functions --- BMap/BMExports.cpp | 99 +++++++++++++++++++++++++++++- BMap/BMExports.hpp | 68 ++++++++++++++++++++ LibCmo/CK2/ObjImpls/CK3dEntity.cpp | 4 +- LibCmo/CK2/ObjImpls/CKMesh.cpp | 19 ++++-- LibCmo/CK2/ObjImpls/CKMesh.hpp | 2 +- 5 files changed, 185 insertions(+), 7 deletions(-) diff --git a/BMap/BMExports.cpp b/BMap/BMExports.cpp index f479404..e27f6cf 100644 --- a/BMap/BMExports.cpp +++ b/BMap/BMExports.cpp @@ -433,6 +433,103 @@ bool BMTexture_SetVideoFormat(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(Lib #pragma region CKMesh +bool BMMesh_GetVertexCount(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::CKDWORD, out_count)) { + auto obj = CheckCKMesh(bmfile, objid); + if (obj == nullptr) return false; + + BMPARAM_OUT_ASSIGN(out_count, obj->GetVertexCount()); + return true; +} +bool BMMesh_SetVertexCount(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::CKDWORD, count)) { + auto obj = CheckCKMesh(bmfile, objid); + if (obj == nullptr) return false; + + obj->SetVertexCount(count); + return true; +} +bool BMMesh_GetVertexPositions(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VxVector3*, out_mem)) { + auto obj = CheckCKMesh(bmfile, objid); + if (obj == nullptr) return false; + + BMPARAM_OUT_ASSIGN(out_mem, obj->GetVertexPositions()); + return true; +} +bool BMMesh_GetVertexNormals(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VxVector3*, out_mem)) { + auto obj = CheckCKMesh(bmfile, objid); + if (obj == nullptr) return false; + + BMPARAM_OUT_ASSIGN(out_mem, obj->GetVertexNormals()); + return true; +} +bool BMMesh_GetVertexUVs(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VxVector2*, out_mem)) { + auto obj = CheckCKMesh(bmfile, objid); + if (obj == nullptr) return false; + + BMPARAM_OUT_ASSIGN(out_mem, obj->GetVertexUVs()); + return true; +} + +bool BMMesh_GetFaceCount(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::CKDWORD, out_count)) { + auto obj = CheckCKMesh(bmfile, objid); + if (obj == nullptr) return false; + + BMPARAM_OUT_ASSIGN(out_count, obj->GetFaceCount()); + return true; +} +bool BMMesh_SetFaceCount(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::CKDWORD, count)) { + auto obj = CheckCKMesh(bmfile, objid); + if (obj == nullptr) return false; + + obj->SetFaceCount(count); + return true; +} +bool BMMesh_GetFaceIndices(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::CKWORD*, out_mem)) { + auto obj = CheckCKMesh(bmfile, objid); + if (obj == nullptr) return false; + + BMPARAM_OUT_ASSIGN(out_mem, obj->GetFaceIndices()); + return true; +} +bool BMMesh_GetFaceMaterialSlotIndexs(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::CKWORD*, out_mem)) { + auto obj = CheckCKMesh(bmfile, objid); + if (obj == nullptr) return false; + + BMPARAM_OUT_ASSIGN(out_mem, obj->GetFaceMaterialSlotIndexs()); + return true; +} + +bool BMMesh_GetMaterialSlotCount(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::CKDWORD, out_count)) { + auto obj = CheckCKMesh(bmfile, objid); + if (obj == nullptr) return false; + + BMPARAM_OUT_ASSIGN(out_count, obj->GetMaterialSlotCount()); + return true; +} +bool BMMesh_SetMaterialSlotCount(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::CKDWORD, count)) { + auto obj = CheckCKMesh(bmfile, objid); + if (obj == nullptr) return false; + + obj->SetMaterialSlotCount(count); + return true; +} +bool BMMesh_GetMaterialSlot(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::CKDWORD, index), BMPARAM_OUT(LibCmo::CK2::CK_ID, out_mtlid)) { + auto obj = CheckCKMesh(bmfile, objid); + if (obj == nullptr) return false; + if (index >= obj->GetMaterialSlotCount()) return false; + + BMPARAM_OUT_ASSIGN(out_mtlid, SafeGetID(obj->GetMaterialSlots()[index])); + return true; +} +bool BMMesh_SetMaterialSlot(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::CKDWORD, index), BMPARAM_IN(LibCmo::CK2::CK_ID, mtlid)) { + auto obj = CheckCKMesh(bmfile, objid); + auto mtlobj = CheckCKMaterial(bmfile, mtlid); + if (obj == nullptr /*|| mtlobj == nullptr*/) return false; // allow nullptr assign + if (index >= obj->GetMaterialSlotCount()) return false; + + obj->GetMaterialSlots()[index] = mtlobj; + return true; +} + #pragma endregion #pragma region CK3dObject @@ -464,7 +561,7 @@ bool BM3dEntity_GetCurrentMesh(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(L bool BM3dEntity_SetCurrentMesh(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::CK2::CK_ID, meshid)) { auto obj = CheckCK3dObject(bmfile, objid); auto meshobj = CheckCKMesh(bmfile, meshid); - if (obj == nullptr || meshobj == nullptr) return false; + if (obj == nullptr /*|| meshobj == nullptr*/) return false; //allow nullptr assign obj->SetCurrentMesh(meshobj); return true; diff --git a/BMap/BMExports.hpp b/BMap/BMExports.hpp index 55bd632..8d10650 100644 --- a/BMap/BMExports.hpp +++ b/BMap/BMExports.hpp @@ -163,10 +163,78 @@ LIBCMO_EXPORT bool BMTexture_SetVideoFormat(BMPARAM_OBJECT_DECL(bmfile, objid), #pragma region CKMaterial +LIBCMO_EXPORT bool BMMaterial_GetDiffuse(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VxColor, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetDiffuse(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::VxMath::VxColor, col)); +LIBCMO_EXPORT bool BMMaterial_GetAmbient(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VxColor, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetAmbient(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::VxMath::VxColor, col)); +LIBCMO_EXPORT bool BMMaterial_GetSpecular(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VxColor, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetSpecular(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::VxMath::VxColor, col)); +LIBCMO_EXPORT bool BMMaterial_GetEmissive(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VxColor, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetEmissive(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::VxMath::VxColor, col)); +LIBCMO_EXPORT bool BMMaterial_GetSpecularPower(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::CKFLOAT, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetSpecularPower(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::CKFLOAT, val)); + +LIBCMO_EXPORT bool BMMaterial_GetTexture(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::CK2::CK_ID, out_tex)); +LIBCMO_EXPORT bool BMMaterial_SetTexture(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::CK2::CK_ID, tex)); +LIBCMO_EXPORT bool BMMaterial_GetTextureBorderColor(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::CKDWORD, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetTextureBorderColor(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::CKDWORD, val)); + +LIBCMO_EXPORT bool BMMaterial_GetTextureBlendMode(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VXTEXTURE_BLENDMODE, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetTextureBlendMode(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::VxMath::VXTEXTURE_BLENDMODE, val)); +LIBCMO_EXPORT bool BMMaterial_GetTextureMinMode(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VXTEXTURE_FILTERMODE, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetTextureMinMode(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::VxMath::VXTEXTURE_FILTERMODE, val)); +LIBCMO_EXPORT bool BMMaterial_GetTextureMagMode(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VXTEXTURE_FILTERMODE, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetTextureMagMode(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::VxMath::VXTEXTURE_FILTERMODE, val)); +LIBCMO_EXPORT bool BMMaterial_GetTextureAddressMode(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VXTEXTURE_ADDRESSMODE, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetTextureAddressMode(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::VxMath::VXTEXTURE_ADDRESSMODE, val)); + +LIBCMO_EXPORT bool BMMaterial_GetSourceBlend(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VXBLEND_MODE, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetSourceBlend(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::VxMath::VXBLEND_MODE, val)); +LIBCMO_EXPORT bool BMMaterial_GetDestBlend(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VXBLEND_MODE, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetDestBlend(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::VxMath::VXBLEND_MODE, val)); +LIBCMO_EXPORT bool BMMaterial_GetFillMode(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VXFILL_MODE, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetFillMode(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::VxMath::VXFILL_MODE, val)); +LIBCMO_EXPORT bool BMMaterial_GetShadeMode(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VXSHADE_MODE, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetShadeMode(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::VxMath::VXSHADE_MODE, val)); + +LIBCMO_EXPORT bool BMMaterial_GetAlphaTestEnabled(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(bool, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetAlphaTestEnabled(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(bool, enabled)); +LIBCMO_EXPORT bool BMMaterial_GetAlphaBlendEnabled(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(bool, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetAlphaBlendEnabled(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(bool, enabled)); +LIBCMO_EXPORT bool BMMaterial_GetPerspectiveCorrectionEnabled(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(bool, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetPerspectiveCorrectionEnabled(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(bool, enabled)); +LIBCMO_EXPORT bool BMMaterial_GetZWriteEnabled(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(bool, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetZWriteEnabled(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(bool, enabled)); +LIBCMO_EXPORT bool BMMaterial_GetTwoSidedEnabled(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(bool, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetTwoSidedEnabled(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(bool, enabled)); + +LIBCMO_EXPORT bool BMMaterial_GetAlphaRef(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::CKBYTE, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetAlphaRef(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::CKBYTE, val)); +LIBCMO_EXPORT bool BMMaterial_GetAlphaFunc(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VXCMPFUNC, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetAlphaFunc(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::VxMath::VXCMPFUNC, val)); +LIBCMO_EXPORT bool BMMaterial_GetZFunc(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VXCMPFUNC, out_val)); +LIBCMO_EXPORT bool BMMaterial_SetZFunc(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::VxMath::VXCMPFUNC, val)); + #pragma endregion #pragma region CKMesh +LIBCMO_EXPORT bool BMMesh_GetVertexCount(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::CKDWORD, out_count)); +LIBCMO_EXPORT bool BMMesh_SetVertexCount(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::CKDWORD, count)); +LIBCMO_EXPORT bool BMMesh_GetVertexPositions(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VxVector3*, out_mem)); +LIBCMO_EXPORT bool BMMesh_GetVertexNormals(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VxVector3*, out_mem)); +LIBCMO_EXPORT bool BMMesh_GetVertexUVs(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::VxMath::VxVector2*, out_mem)); + +LIBCMO_EXPORT bool BMMesh_GetFaceCount(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::CKDWORD, out_count)); +LIBCMO_EXPORT bool BMMesh_SetFaceCount(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::CKDWORD, count)); +LIBCMO_EXPORT bool BMMesh_GetFaceIndices(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::CKWORD*, out_mem)); +LIBCMO_EXPORT bool BMMesh_GetFaceMaterialSlotIndexs(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::CKWORD*, out_mem)); + +LIBCMO_EXPORT bool BMMesh_GetMaterialSlotCount(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_OUT(LibCmo::CKDWORD, out_count)); +LIBCMO_EXPORT bool BMMesh_SetMaterialSlotCount(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::CKDWORD, count)); +LIBCMO_EXPORT bool BMMesh_GetMaterialSlot(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::CKDWORD, index), BMPARAM_OUT(LibCmo::CK2::CK_ID, out_mtlid)); +LIBCMO_EXPORT bool BMMesh_SetMaterialSlot(BMPARAM_OBJECT_DECL(bmfile, objid), BMPARAM_IN(LibCmo::CKDWORD, index), BMPARAM_IN(LibCmo::CK2::CK_ID, mtlid)); + #pragma endregion #pragma region CK3dObject diff --git a/LibCmo/CK2/ObjImpls/CK3dEntity.cpp b/LibCmo/CK2/ObjImpls/CK3dEntity.cpp index dd16046..05ea1ab 100644 --- a/LibCmo/CK2/ObjImpls/CK3dEntity.cpp +++ b/LibCmo/CK2/ObjImpls/CK3dEntity.cpp @@ -310,7 +310,9 @@ namespace LibCmo::CK2::ObjImpls { void CK3dEntity::SetCurrentMesh(CKMesh* mesh) { m_CurrentMesh = mesh; - AddPotentialMesh(mesh); + if (mesh != nullptr) { + AddPotentialMesh(mesh); + } } #pragma endregion diff --git a/LibCmo/CK2/ObjImpls/CKMesh.cpp b/LibCmo/CK2/ObjImpls/CKMesh.cpp index f2a5ce8..6120081 100644 --- a/LibCmo/CK2/ObjImpls/CKMesh.cpp +++ b/LibCmo/CK2/ObjImpls/CKMesh.cpp @@ -12,7 +12,7 @@ namespace LibCmo::CK2::ObjImpls { m_VertexPosition(), m_VertexNormal(), m_VertexUV(), m_VertexColor(), m_VertexSpecularColor(), // init mtl slots - m_MtlSlotCount(0), + m_MaterialSlotCount(0), m_MaterialSlot(), // init face data m_FaceCount(0), @@ -577,22 +577,27 @@ namespace LibCmo::CK2::ObjImpls { } VxMath::VxVector3* CKMesh::GetVertexPositions() { + if (m_VertexCount == 0) return nullptr; return m_VertexPosition.data(); } VxMath::VxVector3* CKMesh::GetVertexNormals() { + if (m_VertexCount == 0) return nullptr; return m_VertexNormal.data(); } VxMath::VxVector2* CKMesh::GetVertexUVs() { + if (m_VertexCount == 0) return nullptr; return m_VertexUV.data(); } CKDWORD* CKMesh::GetVertexColors() { + if (m_VertexCount == 0) return nullptr; return m_VertexColor.data(); } CKDWORD* CKMesh::GetVertexSpecularColors() { + if (m_VertexCount == 0) return nullptr; return m_VertexSpecularColor.data(); } @@ -601,15 +606,16 @@ namespace LibCmo::CK2::ObjImpls { #pragma region Material Slot Section CKDWORD CKMesh::GetMaterialSlotCount() const { - return m_MtlSlotCount; + return m_MaterialSlotCount; } void CKMesh::SetMaterialSlotCount(CKDWORD count) { - m_MtlSlotCount = count; + m_MaterialSlotCount = count; m_MaterialSlot.resize(count, nullptr); } CKMaterial** CKMesh::GetMaterialSlots() { + if (m_MaterialSlotCount == 0) return nullptr; return m_MaterialSlot.data(); } @@ -629,15 +635,19 @@ namespace LibCmo::CK2::ObjImpls { } CKWORD* CKMesh::GetFaceIndices() { + if (m_FaceCount == 0) return nullptr; return m_FaceIndices.data(); } CKWORD* CKMesh::GetFaceMaterialSlotIndexs() { - return m_FaceIndices.data(); + if (m_FaceCount == 0) return nullptr; + return m_FaceMtlIndex.data(); } VxMath::VxVector3* CKMesh::GetFaceNormals(CKDWORD& stride) { stride = CKSizeof(FaceData_t); + + if (m_FaceCount == 0) return nullptr; return &m_FaceOthers.data()->m_Normal; } @@ -655,6 +665,7 @@ namespace LibCmo::CK2::ObjImpls { } CKWORD* CKMesh::GetLineIndices() { + if (m_LineCount == 0) return nullptr; return m_LineIndices.data(); } diff --git a/LibCmo/CK2/ObjImpls/CKMesh.hpp b/LibCmo/CK2/ObjImpls/CKMesh.hpp index 09e1d26..498d24a 100644 --- a/LibCmo/CK2/ObjImpls/CKMesh.hpp +++ b/LibCmo/CK2/ObjImpls/CKMesh.hpp @@ -83,7 +83,7 @@ namespace LibCmo::CK2::ObjImpls { VxMath::VXMESH_FLAGS m_Flags; CKDWORD m_VertexCount; CKDWORD m_LineCount; - CKDWORD m_MtlSlotCount; + CKDWORD m_MaterialSlotCount; CKDWORD m_FaceCount; XContainer::XArray m_VertexPosition;