update BMap.

- BMMeshConverter now use CK_ID as CKMaterial references.
- Add CKObject, CKGroup visitor.
This commit is contained in:
yyc12345 2023-10-05 21:08:58 +08:00
parent 9cb57adf6e
commit b03806e3c9
4 changed files with 145 additions and 45 deletions

View File

@ -116,9 +116,9 @@ LibCmo::CK2::CK_ID BMFile_GetGroup(BMap::BMFile* bmfile, LibCmo::CKDWORD idx) {
if (!CheckBMFile(bmfile)) return 0;
return bmfile->GetGroup(idx);
}
LibCmo::CK2::CK_ID BMFile_CreateGroup(BMap::BMFile* bmfile, LibCmo::CKSTRING name) {
LibCmo::CK2::CK_ID BMFile_CreateGroup(BMap::BMFile* bmfile) {
if (!CheckBMFile(bmfile)) return 0;
return bmfile->CreateGroup(name);
return bmfile->CreateGroup();
}
LibCmo::CKDWORD BMFile_Get3dObjectCount(BMap::BMFile* bmfile) {
if (!CheckBMFile(bmfile)) return 0;
@ -128,9 +128,9 @@ LibCmo::CK2::CK_ID BMFile_Get3dObject(BMap::BMFile* bmfile, LibCmo::CKDWORD idx)
if (!CheckBMFile(bmfile)) return 0;
return bmfile->Get3dObject(idx);
}
LibCmo::CK2::CK_ID BMFile_Create3dObject(BMap::BMFile* bmfile, LibCmo::CKSTRING name) {
LibCmo::CK2::CK_ID BMFile_Create3dObject(BMap::BMFile* bmfile) {
if (!CheckBMFile(bmfile)) return 0;
return bmfile->Create3dObject(name);
return bmfile->Create3dObject();
}
LibCmo::CKDWORD BMFile_GetMeshCount(BMap::BMFile* bmfile) {
if (!CheckBMFile(bmfile)) return 0;
@ -140,9 +140,9 @@ LibCmo::CK2::CK_ID BMFile_GetMesh(BMap::BMFile* bmfile, LibCmo::CKDWORD idx) {
if (!CheckBMFile(bmfile)) return 0;
return bmfile->GetMesh(idx);
}
LibCmo::CK2::CK_ID BMFile_CreateMesh(BMap::BMFile* bmfile, LibCmo::CKSTRING name) {
LibCmo::CK2::CK_ID BMFile_CreateMesh(BMap::BMFile* bmfile) {
if (!CheckBMFile(bmfile)) return 0;
return bmfile->CreateMesh(name);
return bmfile->CreateMesh();
}
LibCmo::CKDWORD BMFile_GetMaterialCount(BMap::BMFile* bmfile) {
if (!CheckBMFile(bmfile)) return 0;
@ -152,9 +152,9 @@ LibCmo::CK2::CK_ID BMFile_GetMaterial(BMap::BMFile* bmfile, LibCmo::CKDWORD idx)
if (!CheckBMFile(bmfile)) return 0;
return bmfile->GetMaterial(idx);
}
LibCmo::CK2::CK_ID BMFile_CreateMaterial(BMap::BMFile* bmfile, LibCmo::CKSTRING name) {
LibCmo::CK2::CK_ID BMFile_CreateMaterial(BMap::BMFile* bmfile) {
if (!CheckBMFile(bmfile)) return 0;
return bmfile->CreateMaterial(name);
return bmfile->CreateMaterial();
}
LibCmo::CKDWORD BMFile_GetTextureCount(BMap::BMFile* bmfile) {
if (!CheckBMFile(bmfile)) return 0;
@ -164,9 +164,9 @@ LibCmo::CK2::CK_ID BMFile_GetTexture(BMap::BMFile* bmfile, LibCmo::CKDWORD idx)
if (!CheckBMFile(bmfile)) return 0;
return bmfile->GetTexture(idx);
}
LibCmo::CK2::CK_ID BMFile_CreateTexture(BMap::BMFile* bmfile, LibCmo::CKSTRING name) {
LibCmo::CK2::CK_ID BMFile_CreateTexture(BMap::BMFile* bmfile) {
if (!CheckBMFile(bmfile)) return 0;
return bmfile->CreateTexture(name);
return bmfile->CreateTexture();
}
#pragma endregion
@ -218,7 +218,7 @@ bool BMMeshTrans_PrepareMtlSlotCount(BMap::BMMeshTransition* trans, LibCmo::CKDW
if (!CheckBMMeshTrans(trans)) return false;
return trans->PrepareMtlSlotCount(count);
}
LibCmo::CK2::ObjImpls::CKMaterial** BMMeshTrans_PrepareMtlSlot(BMap::BMMeshTransition* trans) {
LibCmo::CK2::CK_ID* BMMeshTrans_PrepareMtlSlot(BMap::BMMeshTransition* trans) {
if (!CheckBMMeshTrans(trans)) return nullptr;
return trans->PrepareMtlSlot();
}
@ -249,3 +249,63 @@ bool BMMeshTrans_Parse(BMap::BMMeshTransition* trans, LibCmo::CK2::ObjImpls::CKM
#pragma endregion
#pragma region CKObject
LibCmo::CKSTRING BMCKObject_GetName(BMap::BMFile* bmfile, LibCmo::CK2::CK_ID objid) {
auto obj = CheckCKObject<LibCmo::CK2::ObjImpls::CKObject*>(bmfile, objid, LibCmo::CK2::CK_CLASSID::CKCID_OBJECT);
if (obj == nullptr) return nullptr;
return obj->GetName();
}
bool BMCKObject_SetName(BMap::BMFile* bmfile, LibCmo::CK2::CK_ID objid, LibCmo::CKSTRING name) {
auto obj = CheckCKObject<LibCmo::CK2::ObjImpls::CKObject*>(bmfile, objid, LibCmo::CK2::CK_CLASSID::CKCID_OBJECT);
if (obj == nullptr) return false;
obj->SetName(name);
return true;
}
#pragma endregion
#pragma region CKGroup
bool BMCKGroup_AddObject(BMap::BMFile* bmfile, LibCmo::CK2::CK_ID objid, LibCmo::CK2::CK_ID memberid) {
auto obj = CheckCKObject<LibCmo::CK2::ObjImpls::CKGroup*>(bmfile, objid, LibCmo::CK2::CK_CLASSID::CKCID_GROUP);
auto memberobj = CheckCKObject<LibCmo::CK2::ObjImpls::CK3dObject*>(bmfile, memberid, LibCmo::CK2::CK_CLASSID::CKCID_3DOBJECT);
if (obj == nullptr || memberobj == nullptr) return false;
return obj->AddObject(memberobj) == LibCmo::CK2::CKERROR::CKERR_OK;
}
LibCmo::CKDWORD BMCKGroup_GetObjectCount(BMap::BMFile* bmfile, LibCmo::CK2::CK_ID objid) {
auto obj = CheckCKObject<LibCmo::CK2::ObjImpls::CKGroup*>(bmfile, objid, LibCmo::CK2::CK_CLASSID::CKCID_GROUP);
if (obj == nullptr) return false;
return obj->GetObjectCount();
}
LibCmo::CK2::CK_ID BMCKGroup_GetObject(BMap::BMFile* bmfile, LibCmo::CK2::CK_ID objid, LibCmo::CKDWORD pos) {
auto obj = CheckCKObject<LibCmo::CK2::ObjImpls::CKGroup*>(bmfile, objid, LibCmo::CK2::CK_CLASSID::CKCID_GROUP);
if (obj == nullptr) return 0;
auto cache = obj->GetObject(pos);
if (cache == nullptr) return 0;
return cache->GetID();
}
#pragma endregion
#pragma region CKTexture
#pragma endregion
#pragma region CKMaterial
#pragma endregion
#pragma region CKMesh
#pragma endregion
#pragma region CK3dObject
#pragma endregion

View File

@ -2,6 +2,19 @@
#include "BMap.hpp"
/*
Design Note:
+ We use raw pointer for BMFile and BMMeshTransition
- These 2 class do no involve class inheritance so we can safely pass them to this DLL.
- These 2 class is not a part of CK core. It is more easy to check them validation. It is enough checking them in a std::set.
+ We use CK_ID as CKObject visitor to ensure the visiting is safe.
- We choose CK_ID because checking a valid CK_ID is more easy and cost lower performance than checking a valid CKObject*.
- Another reason is that CKObject* relate to class inheritance and need cast. Use CK_ID can leyt them get safe cast in C++, not in other binding languages.
- Each CK_ID also should be used with its corresponding BMFile*, because each BMfile* will create a unique CKContext*. CK_ID between different BMFile* is not shared.
*/
#pragma region Init / Dispose
LIBCMO_EXPORT void BMInit();
@ -18,19 +31,19 @@ LIBCMO_EXPORT bool BMFile_Free(BMap::BMFile* map_file);
LIBCMO_EXPORT LibCmo::CKDWORD BMFile_GetGroupCount(BMap::BMFile* bmfile);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMFile_GetGroup(BMap::BMFile* bmfile, LibCmo::CKDWORD idx);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMFile_CreateGroup(BMap::BMFile* bmfile, LibCmo::CKSTRING name);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMFile_CreateGroup(BMap::BMFile* bmfile);
LIBCMO_EXPORT LibCmo::CKDWORD BMFile_Get3dObjectCount(BMap::BMFile* bmfile);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMFile_Get3dObject(BMap::BMFile* bmfile, LibCmo::CKDWORD idx);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMFile_Create3dObject(BMap::BMFile* bmfile, LibCmo::CKSTRING name);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMFile_Create3dObject(BMap::BMFile* bmfile);
LIBCMO_EXPORT LibCmo::CKDWORD BMFile_GetMeshCount(BMap::BMFile* bmfile);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMFile_GetMesh(BMap::BMFile* bmfile, LibCmo::CKDWORD idx);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMFile_CreateMesh(BMap::BMFile* bmfile, LibCmo::CKSTRING name);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMFile_CreateMesh(BMap::BMFile* bmfile);
LIBCMO_EXPORT LibCmo::CKDWORD BMFile_GetMaterialCount(BMap::BMFile* bmfile);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMFile_GetMaterial(BMap::BMFile* bmfile, LibCmo::CKDWORD idx);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMFile_CreateMaterial(BMap::BMFile* bmfile, LibCmo::CKSTRING name);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMFile_CreateMaterial(BMap::BMFile* bmfile);
LIBCMO_EXPORT LibCmo::CKDWORD BMFile_GetTextureCount(BMap::BMFile* bmfile);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMFile_GetTexture(BMap::BMFile* bmfile, LibCmo::CKDWORD idx);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMFile_CreateTexture(BMap::BMFile* bmfile, LibCmo::CKSTRING name);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMFile_CreateTexture(BMap::BMFile* bmfile);
#pragma endregion
@ -46,13 +59,43 @@ LIBCMO_EXPORT LibCmo::VxMath::VxVector3* BMMeshTrans_PrepareNormal(BMap::BMMeshT
LIBCMO_EXPORT bool BMMeshTrans_PrepareUVCount(BMap::BMMeshTransition* trans, LibCmo::CKDWORD count);
LIBCMO_EXPORT LibCmo::VxMath::VxVector2* BMMeshTrans_PrepareUV(BMap::BMMeshTransition* trans);
LIBCMO_EXPORT bool BMMeshTrans_PrepareMtlSlotCount(BMap::BMMeshTransition* trans, LibCmo::CKDWORD count);
LIBCMO_EXPORT LibCmo::CK2::ObjImpls::CKMaterial** BMMeshTrans_PrepareMtlSlot(BMap::BMMeshTransition* trans);
LIBCMO_EXPORT LibCmo::CK2::CK_ID* BMMeshTrans_PrepareMtlSlot(BMap::BMMeshTransition* trans);
LIBCMO_EXPORT bool BMMeshTrans_PrepareFaceCount(BMap::BMMeshTransition* trans, LibCmo::CKDWORD count);
LIBCMO_EXPORT LibCmo::CKDWORD* BMMeshTrans_PrepareFaceVertexIndices(BMap::BMMeshTransition* trans);
LIBCMO_EXPORT LibCmo::CKDWORD* BMMeshTrans_PrepareFaceNormalIndices(BMap::BMMeshTransition* trans);
LIBCMO_EXPORT LibCmo::CKDWORD* BMMeshTrans_PrepareFaceUVIndices(BMap::BMMeshTransition* trans);
LIBCMO_EXPORT LibCmo::CKDWORD* BMMeshTrans_PrepareFaceMtlSlot(BMap::BMMeshTransition* trans);
LIBCMO_EXPORT bool BMMeshTrans_Parse(BMap::BMMeshTransition* trans, LibCmo::CK2::ObjImpls::CKMesh* write_into_mesh);
#pragma endregion
#pragma region CKObject
LIBCMO_EXPORT LibCmo::CKSTRING BMCKObject_GetName(BMap::BMFile* bmfile, LibCmo::CK2::CK_ID objid);
LIBCMO_EXPORT bool BMCKObject_SetName(BMap::BMFile* bmfile, LibCmo::CK2::CK_ID objid, LibCmo::CKSTRING name);
#pragma endregion
#pragma region CKGroup
LIBCMO_EXPORT bool BMCKGroup_AddObject(BMap::BMFile* bmfile, LibCmo::CK2::CK_ID objid, LibCmo::CK2::CK_ID memberid);
LIBCMO_EXPORT LibCmo::CKDWORD BMCKGroup_GetObjectCount(BMap::BMFile* bmfile, LibCmo::CK2::CK_ID objid);
LIBCMO_EXPORT LibCmo::CK2::CK_ID BMCKGroup_GetObject(BMap::BMFile* bmfile, LibCmo::CK2::CK_ID objid, LibCmo::CKDWORD pos);
#pragma endregion
#pragma region CKTexture
#pragma endregion
#pragma region CKMaterial
#pragma endregion
#pragma region CKMesh
#pragma endregion
#pragma region CK3dObject
#pragma endregion

View File

@ -67,12 +67,12 @@ namespace BMap {
bool BMMeshTransition::PrepareMtlSlotCount(LibCmo::CKDWORD count) {
if (m_IsParsed) return false;
m_MtlSlots.resize(count, nullptr);
m_MtlSlots.resize(count, 0);
m_IsMtlSlotOK = true;
return true;
}
LibCmo::CK2::ObjImpls::CKMaterial** BMMeshTransition::PrepareMtlSlot() {
LibCmo::CK2::CK_ID* BMMeshTransition::PrepareMtlSlot() {
if (m_IsParsed || !m_IsMtlSlotOK) return nullptr;
return m_MtlSlots.data();
}
@ -209,10 +209,17 @@ namespace BMap {
}
// set mtl slot
LibCmo::CK2::CKContext* correspondingCtx = write_into_mesh->GetCKContext();
write_into_mesh->SetMaterialSlotCount(mtl_count);
auto pMtlSlot = write_into_mesh->GetMaterialSlots();
LibCmo::CK2::ObjImpls::CKMaterial** pMtlSlot = write_into_mesh->GetMaterialSlots();
for (LibCmo::CKDWORD i = 0; i < mtl_count; ++i) {
*(pMtlSlot++) = m_MtlSlots[i];
// convert id to CKMaterial* and check its type
LibCmo::CK2::ObjImpls::CKObject* mtlptr = correspondingCtx->GetObject(m_MtlSlots[i]);
if (mtlptr != nullptr && LibCmo::CK2::CKIsChildClassOf(mtlptr->GetClassID(), LibCmo::CK2::CK_CLASSID::CKCID_MATERIAL)) {
*(pMtlSlot++) = static_cast<LibCmo::CK2::ObjImpls::CKMaterial*>(mtlptr);
} else {
*(pMtlSlot++) = nullptr;
}
}
}
@ -268,29 +275,19 @@ namespace BMap {
LibCmo::CKDWORD BMFile::GetGroupCount() { return CommonGetObjectCount(m_ObjGroups); }
LibCmo::CK2::CK_ID BMFile::GetGroup(LibCmo::CKDWORD idx) { return CommonGetObject(m_ObjGroups, idx); }
LibCmo::CK2::CK_ID BMFile::CreateGroup(LibCmo::CKSTRING name) {
return CommonCreateObject(m_ObjGroups, LibCmo::CK2::CK_CLASSID::CKCID_GROUP, name);
}
LibCmo::CK2::CK_ID BMFile::CreateGroup() { return CommonCreateObject(m_ObjGroups, LibCmo::CK2::CK_CLASSID::CKCID_GROUP); }
LibCmo::CKDWORD BMFile::Get3dObjectCount() { return CommonGetObjectCount(m_Obj3dObjects); }
LibCmo::CK2::CK_ID BMFile::Get3dObject(LibCmo::CKDWORD idx) { return CommonGetObject(m_Obj3dObjects, idx); }
LibCmo::CK2::CK_ID BMFile::Create3dObject(LibCmo::CKSTRING name) {
return CommonCreateObject(m_Obj3dObjects, LibCmo::CK2::CK_CLASSID::CKCID_3DOBJECT, name);
}
LibCmo::CK2::CK_ID BMFile::Create3dObject() { return CommonCreateObject(m_Obj3dObjects, LibCmo::CK2::CK_CLASSID::CKCID_3DOBJECT); }
LibCmo::CKDWORD BMFile::GetMeshCount() { return CommonGetObjectCount(m_ObjMeshs); }
LibCmo::CK2::CK_ID BMFile::GetMesh(LibCmo::CKDWORD idx) { return CommonGetObject(m_ObjMeshs, idx); }
LibCmo::CK2::CK_ID BMFile::CreateMesh(LibCmo::CKSTRING name) {
return CommonCreateObject(m_ObjMeshs, LibCmo::CK2::CK_CLASSID::CKCID_MESH, name);
}
LibCmo::CK2::CK_ID BMFile::CreateMesh() { return CommonCreateObject(m_ObjMeshs, LibCmo::CK2::CK_CLASSID::CKCID_MESH); }
LibCmo::CKDWORD BMFile::GetMaterialCount() { return CommonGetObjectCount(m_ObjMaterials); }
LibCmo::CK2::CK_ID BMFile::GetMaterial(LibCmo::CKDWORD idx) { return CommonGetObject(m_ObjMaterials, idx); }
LibCmo::CK2::CK_ID BMFile::CreateMaterial(LibCmo::CKSTRING name) {
return CommonCreateObject(m_ObjMaterials, LibCmo::CK2::CK_CLASSID::CKCID_MATERIAL, name);
}
LibCmo::CK2::CK_ID BMFile::CreateMaterial() { return CommonCreateObject(m_ObjMaterials, LibCmo::CK2::CK_CLASSID::CKCID_MATERIAL); }
LibCmo::CKDWORD BMFile::GetTextureCount() { return CommonGetObjectCount(m_ObjTextures); }
LibCmo::CK2::CK_ID BMFile::GetTexture(LibCmo::CKDWORD idx) { return CommonGetObject(m_ObjTextures, idx); }
LibCmo::CK2::CK_ID BMFile::CreateTexture(LibCmo::CKSTRING name) {
return CommonCreateObject(m_ObjTextures, LibCmo::CK2::CK_CLASSID::CKCID_TEXTURE, name);
}
LibCmo::CK2::CK_ID BMFile::CreateTexture() { return CommonCreateObject(m_ObjTextures, LibCmo::CK2::CK_CLASSID::CKCID_TEXTURE); }
#pragma endregion

View File

@ -40,7 +40,7 @@ namespace BMap {
bool PrepareUVCount(LibCmo::CKDWORD count);
LibCmo::VxMath::VxVector2* PrepareUV();
bool PrepareMtlSlotCount(LibCmo::CKDWORD count);
LibCmo::CK2::ObjImpls::CKMaterial** PrepareMtlSlot();
LibCmo::CK2::CK_ID* PrepareMtlSlot();
bool PrepareFaceCount(LibCmo::CKDWORD count);
LibCmo::CKDWORD* PrepareFaceVertexIndices();
LibCmo::CKDWORD* PrepareFaceNormalIndices();
@ -60,7 +60,7 @@ namespace BMap {
std::vector<LibCmo::VxMath::VxVector2> m_UVs;
std::vector<LibCmo::CKDWORD> m_FaceVertexs, m_FaceNormals, m_FaceUVs;
std::vector<LibCmo::CKDWORD> m_FaceMtlSlotIdxs;
std::vector<LibCmo::CK2::ObjImpls::CKMaterial*> m_MtlSlots;
std::vector<LibCmo::CK2::CK_ID> m_MtlSlots;
std::vector<TransitionVertex> m_ProcVertexs;
std::vector<TransitionFace> m_ProcFaces;
@ -101,12 +101,12 @@ namespace BMap {
if (m_IsFreezed || !m_IsReader || idx >= container.size()) return 0;
return container[idx];
}
LibCmo::CK2::CK_ID CommonCreateObject(std::vector<LibCmo::CK2::CK_ID>& container, LibCmo::CK2::CK_CLASSID cid, LibCmo::CKSTRING name) {
LibCmo::CK2::CK_ID CommonCreateObject(std::vector<LibCmo::CK2::CK_ID>& container, LibCmo::CK2::CK_CLASSID cid) {
// only available in writer
if (m_IsFreezed || m_IsReader) return 0;
// try create object and get its pointer
LibCmo::CK2::ObjImpls::CKObject* obj = m_Context->CreateObject(cid, name);
LibCmo::CK2::ObjImpls::CKObject* obj = m_Context->CreateObject(cid, nullptr);
// check creation validation
if (obj == nullptr) return 0;
@ -118,19 +118,19 @@ namespace BMap {
public:
LibCmo::CKDWORD GetGroupCount();
LibCmo::CK2::CK_ID GetGroup(LibCmo::CKDWORD idx);
LibCmo::CK2::CK_ID CreateGroup(LibCmo::CKSTRING name);
LibCmo::CK2::CK_ID CreateGroup();
LibCmo::CKDWORD Get3dObjectCount();
LibCmo::CK2::CK_ID Get3dObject(LibCmo::CKDWORD idx);
LibCmo::CK2::CK_ID Create3dObject(LibCmo::CKSTRING name);
LibCmo::CK2::CK_ID Create3dObject();
LibCmo::CKDWORD GetMeshCount();
LibCmo::CK2::CK_ID GetMesh(LibCmo::CKDWORD idx);
LibCmo::CK2::CK_ID CreateMesh(LibCmo::CKSTRING name);
LibCmo::CK2::CK_ID CreateMesh();
LibCmo::CKDWORD GetMaterialCount();
LibCmo::CK2::CK_ID GetMaterial(LibCmo::CKDWORD idx);
LibCmo::CK2::CK_ID CreateMaterial(LibCmo::CKSTRING name);
LibCmo::CK2::CK_ID CreateMaterial();
LibCmo::CKDWORD GetTextureCount();
LibCmo::CK2::CK_ID GetTexture(LibCmo::CKDWORD idx);
LibCmo::CK2::CK_ID CreateTexture(LibCmo::CKSTRING name);
LibCmo::CK2::CK_ID CreateTexture();
private:
/**