2023-09-15 13:21:49 +08:00
|
|
|
#include "CKMesh.hpp"
|
|
|
|
#include "../CKStateChunk.hpp"
|
|
|
|
#include "../CKContext.hpp"
|
|
|
|
#include "CKMaterial.hpp"
|
|
|
|
|
|
|
|
namespace LibCmo::CK2::ObjImpls {
|
|
|
|
|
|
|
|
CKMesh::CKMesh(CKContext* ctx, CK_ID ckid, CKSTRING name) :
|
2023-09-20 14:42:44 +08:00
|
|
|
CKBeObject(ctx, ckid, name),
|
|
|
|
// init vertex
|
|
|
|
m_VertexCount(0),
|
|
|
|
m_VertexPosition(), m_VertexNormal(), m_VertexUV(),
|
|
|
|
m_VertexColor(), m_VertexSpecularColor(),
|
|
|
|
m_VertexWeight(), m_NoVertexWeight(true),
|
|
|
|
// init mtl slots
|
|
|
|
m_MtlSlotCount(0),
|
|
|
|
m_MaterialSlot(),
|
|
|
|
// init face data
|
|
|
|
m_FaceCount(0),
|
|
|
|
m_FaceIndices(), m_FaceMtlIndex(), m_Faces(),
|
|
|
|
// init line
|
|
|
|
m_LineCount(0),
|
|
|
|
m_LineIndices(),
|
|
|
|
// init mtl channels
|
|
|
|
m_MtlChannelCount(0),
|
|
|
|
m_MaterialChannels(),
|
|
|
|
// init flags
|
|
|
|
m_Flags(EnumsHelper::Merge({
|
|
|
|
VxMath::VXMESH_FLAGS::VXMESH_FORCETRANSPARENCY,
|
|
|
|
VxMath::VXMESH_FLAGS::VXMESH_HASTRANSPARENCY
|
|
|
|
})) {
|
|
|
|
// set visible in default
|
|
|
|
EnumsHelper::Add(m_ObjectFlags, CK_OBJECT_FLAGS::CK_OBJECT_VISIBLE);
|
|
|
|
}
|
2023-09-15 13:21:49 +08:00
|
|
|
|
|
|
|
CKMesh::~CKMesh() {}
|
|
|
|
|
2023-09-20 15:25:43 +08:00
|
|
|
void CKMesh::CheckPreDeletion() {
|
|
|
|
CKBeObject::CheckPreDeletion();
|
|
|
|
|
|
|
|
// check material slots
|
|
|
|
for (auto& slot : m_MaterialSlot) {
|
|
|
|
if (slot != nullptr && slot->IsToBeDeleted()) {
|
|
|
|
slot = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check mtl channels
|
|
|
|
for (auto& chl : m_MaterialChannels) {
|
|
|
|
if (chl.m_Material != nullptr && chl.m_Material->IsToBeDeleted()) {
|
|
|
|
chl.m_Material = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 13:21:49 +08:00
|
|
|
bool CKMesh::Save(CKStateChunk* chunk, CKFileVisitor* file, CKDWORD flags) {
|
|
|
|
bool suc = CKBeObject::Save(chunk, file, flags);
|
|
|
|
if (!suc) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CKMesh::Load(CKStateChunk* chunk, CKFileVisitor* file) {
|
|
|
|
bool suc = CKBeObject::Load(chunk, file);
|
|
|
|
if (!suc) return false;
|
|
|
|
|
2023-09-15 17:03:36 +08:00
|
|
|
// clear all data
|
|
|
|
CleanMesh();
|
2023-09-15 16:15:07 +08:00
|
|
|
|
2023-09-15 17:03:36 +08:00
|
|
|
// check data version.
|
|
|
|
// MARK: too low data is not supported.
|
|
|
|
// because my work are not related to them
|
|
|
|
if (chunk->GetDataVersion() < CK_STATECHUNK_DATAVERSION::CHUNK_MESHCHANGE_VERSION) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read flag
|
|
|
|
if (chunk->SeekIdentifier(CK_STATESAVEFLAGS_MESH::CK_STATESAVE_MESHFLAGS)) {
|
2023-09-20 13:13:08 +08:00
|
|
|
chunk->ReadStruct(m_Flags);
|
2023-09-15 17:03:36 +08:00
|
|
|
EnumsHelper::Mask(m_Flags, VxMath::VXMESH_FLAGS::VXMESH_ALLFLAGS);
|
|
|
|
|
|
|
|
// I don't know why, just interpter the IDA code.
|
|
|
|
EnumsHelper::Rm(m_Flags, EnumsHelper::Merge({
|
|
|
|
VxMath::VXMESH_FLAGS::VXMESH_BOUNDINGUPTODATE,
|
|
|
|
VxMath::VXMESH_FLAGS::VXMESH_OPTIMIZED
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// read material slots
|
|
|
|
if (chunk->SeekIdentifier(CK_STATESAVEFLAGS_MESH::CK_STATESAVE_MESHMATERIALS)) {
|
|
|
|
// get and set material count
|
|
|
|
CKDWORD mtlCount;
|
|
|
|
chunk->ReadStruct(mtlCount);
|
|
|
|
SetMaterialSlotCount(mtlCount);
|
|
|
|
|
|
|
|
// read slot
|
|
|
|
CKDWORD ph;
|
2023-09-20 14:42:44 +08:00
|
|
|
CKObject* objptr = nullptr;
|
2023-09-15 17:03:36 +08:00
|
|
|
for (auto& mtlSlot : m_MaterialSlot) {
|
|
|
|
// read id
|
2023-09-20 14:42:44 +08:00
|
|
|
chunk->ReadObjectPointer(objptr);
|
2023-09-15 17:03:36 +08:00
|
|
|
// and read a place holder idk what the fuck it is.
|
|
|
|
chunk->ReadStruct(ph);
|
|
|
|
|
2023-09-20 14:42:44 +08:00
|
|
|
// try to assign
|
2023-09-15 17:03:36 +08:00
|
|
|
if (objptr != nullptr && objptr->GetClassID() == CK_CLASSID::CKCID_MATERIAL) {
|
|
|
|
mtlSlot = static_cast<CKMaterial*>(objptr);
|
|
|
|
} else {
|
|
|
|
mtlSlot = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// read vertex data
|
2023-09-18 21:06:34 +08:00
|
|
|
VertexSaveFlags saveflags = VertexSaveFlags::None;
|
2023-09-15 17:03:36 +08:00
|
|
|
if (chunk->SeekIdentifier(CK_STATESAVEFLAGS_MESH::CK_STATESAVE_MESHVERTICES)) {
|
|
|
|
// read and set vertex count
|
|
|
|
CKDWORD vertexCount;
|
|
|
|
chunk->ReadStruct(vertexCount);
|
2023-09-18 23:11:33 +08:00
|
|
|
SetVertexCount(vertexCount);
|
2023-09-15 17:03:36 +08:00
|
|
|
|
|
|
|
if (vertexCount != 0) {
|
2023-09-18 21:06:34 +08:00
|
|
|
// read save flags
|
2023-09-15 17:03:36 +08:00
|
|
|
chunk->ReadStruct(saveflags);
|
|
|
|
|
2023-09-18 23:11:33 +08:00
|
|
|
// read size in dword (including it self)
|
2023-09-18 21:06:34 +08:00
|
|
|
CKDWORD sizeInDword;
|
|
|
|
chunk->ReadStruct(sizeInDword);
|
2023-09-18 23:11:33 +08:00
|
|
|
--sizeInDword; // remove self.
|
2023-09-15 17:03:36 +08:00
|
|
|
|
2023-09-18 21:06:34 +08:00
|
|
|
// lock read buffer
|
|
|
|
auto buf = chunk->LockReadBufferWrapper(sizeInDword * CKSizeof(CKDWORD));
|
|
|
|
const CKBYTE* rawbuf = static_cast<const CKBYTE*>(buf.get());
|
2023-09-15 17:03:36 +08:00
|
|
|
|
2023-09-18 21:06:34 +08:00
|
|
|
// copy position if it have
|
|
|
|
if (!EnumsHelper::Has(saveflags, VertexSaveFlags::NoPos)) {
|
|
|
|
CKDWORD consumed = CKSizeof(VxMath::VxVector3) * vertexCount;
|
|
|
|
std::memcpy(m_VertexPosition.data(), rawbuf, consumed);
|
|
|
|
rawbuf += consumed;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy color or apply single color
|
|
|
|
if (!EnumsHelper::Has(saveflags, VertexSaveFlags::SingleColor)) {
|
|
|
|
CKDWORD consumed = CKSizeof(CKDWORD) * vertexCount;
|
|
|
|
std::memcpy(m_VertexColor.data(), rawbuf, consumed);
|
|
|
|
rawbuf += consumed;
|
|
|
|
} else {
|
|
|
|
VxMath::VxCopyStructure(
|
|
|
|
vertexCount,
|
|
|
|
m_VertexColor.data(),
|
|
|
|
CKSizeof(CKDWORD),
|
|
|
|
CKSizeof(CKDWORD),
|
|
|
|
rawbuf,
|
|
|
|
0 // InStride = 0 to make sure copy this single value to every elements.
|
|
|
|
);
|
|
|
|
rawbuf += CKSizeof(CKDWORD);
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy specular color or apply a single color
|
|
|
|
if (!EnumsHelper::Has(saveflags, VertexSaveFlags::SingleSpecularColor)) {
|
|
|
|
CKDWORD consumed = CKSizeof(CKDWORD) * vertexCount;
|
|
|
|
std::memcpy(m_VertexSpecularColor.data(), rawbuf, consumed);
|
|
|
|
rawbuf += consumed;
|
|
|
|
} else {
|
|
|
|
VxMath::VxCopyStructure(
|
|
|
|
vertexCount,
|
|
|
|
m_VertexSpecularColor.data(),
|
|
|
|
CKSizeof(CKDWORD),
|
|
|
|
CKSizeof(CKDWORD),
|
|
|
|
rawbuf,
|
|
|
|
0 // InStride = 0 to make sure copy this single value to every elements.
|
|
|
|
);
|
|
|
|
rawbuf += CKSizeof(CKDWORD);
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy normals if it has
|
|
|
|
if (!EnumsHelper::Has(saveflags, VertexSaveFlags::NoNormal)) {
|
|
|
|
CKDWORD consumed = CKSizeof(VxMath::VxVector3) * vertexCount;
|
|
|
|
std::memcpy(m_VertexNormal.data(), rawbuf, consumed);
|
|
|
|
rawbuf += consumed;
|
|
|
|
}
|
2023-09-15 17:03:36 +08:00
|
|
|
|
2023-09-18 21:06:34 +08:00
|
|
|
// copy uv or apply single uv
|
|
|
|
if (!EnumsHelper::Has(saveflags, VertexSaveFlags::SingleUV)) {
|
|
|
|
CKDWORD consumed = CKSizeof(VxMath::VxVector2) * vertexCount;
|
|
|
|
std::memcpy(m_VertexUV.data(), rawbuf, consumed);
|
|
|
|
rawbuf += consumed;
|
|
|
|
} else {
|
|
|
|
VxMath::VxCopyStructure(
|
|
|
|
vertexCount,
|
|
|
|
m_VertexUV.data(),
|
|
|
|
CKSizeof(VxMath::VxVector2),
|
|
|
|
CKSizeof(VxMath::VxVector2),
|
|
|
|
rawbuf,
|
|
|
|
0 // InStride = 0 to make sure copy this single value to every elements.
|
|
|
|
);
|
|
|
|
rawbuf += CKSizeof(VxMath::VxVector2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// free buf
|
|
|
|
buf.reset();
|
2023-09-15 17:03:36 +08:00
|
|
|
}
|
|
|
|
}
|
2023-09-15 16:15:07 +08:00
|
|
|
|
2023-09-18 21:06:34 +08:00
|
|
|
// read face data
|
|
|
|
if (chunk->SeekIdentifier(CK_STATESAVEFLAGS_MESH::CK_STATESAVE_MESHFACES)) {
|
|
|
|
// read face count and set
|
|
|
|
CKDWORD faceCount;
|
|
|
|
chunk->ReadStruct(faceCount);
|
|
|
|
SetFaceCount(faceCount);
|
|
|
|
|
|
|
|
// lock buffer
|
|
|
|
auto buf = chunk->LockReadBufferWrapper(faceCount * CKSizeof(CKDWORD) * 2);
|
|
|
|
const CKWORD* rawbuf = static_cast<const CKWORD*>(buf.get());
|
|
|
|
// each face use 2 CKDWORD to describe
|
|
|
|
// first CKDWORD describe first 2 face vertex indices
|
|
|
|
// HIGH >>> 0xFFFF(indice 1) 0xFFFF(indice 0) <<< LOW
|
|
|
|
// second CKDWORD describe the third indices and used material slot index
|
|
|
|
// HIGH >>> 0xFFFF(mtl slot index) 0xFFFF(indice 2) <<< LOW
|
|
|
|
|
|
|
|
// due to little endian, the data listed before are placed in memory like this:
|
|
|
|
// (indice 0) (indice 1) (indice 2) (mtl idx)
|
|
|
|
|
|
|
|
// copy indice
|
|
|
|
VxMath::VxCopyStructure(
|
|
|
|
faceCount,
|
|
|
|
m_FaceIndices.data(),
|
|
|
|
3 * CKSizeof(CKWORD),
|
|
|
|
3 * CKSizeof(CKWORD),
|
|
|
|
rawbuf,
|
|
|
|
2 * CKSizeof(CKDWORD)
|
|
|
|
);
|
|
|
|
// copy mtl index
|
|
|
|
VxMath::VxCopyStructure(
|
|
|
|
faceCount,
|
|
|
|
m_FaceMtlIndex.data(),
|
|
|
|
CKSizeof(CKWORD),
|
|
|
|
CKSizeof(CKWORD),
|
|
|
|
rawbuf + 3,
|
|
|
|
2 * CKSizeof(CKDWORD)
|
|
|
|
);
|
|
|
|
|
|
|
|
// free buf
|
|
|
|
buf.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// read line data
|
|
|
|
if (chunk->SeekIdentifier(CK_STATESAVEFLAGS_MESH::CK_STATESAVE_MESHLINES)) {
|
|
|
|
// read and set line count;
|
|
|
|
CKDWORD lineCount;
|
|
|
|
chunk->ReadStruct(lineCount);
|
|
|
|
SetLineCount(lineCount);
|
|
|
|
|
2023-09-18 23:11:33 +08:00
|
|
|
chunk->ReadAndFillBuffer(m_LineIndices.data(), CKSizeof(CKWORD) * lineCount * 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// build normals
|
|
|
|
if (EnumsHelper::Has(saveflags, VertexSaveFlags::NoNormal)) {
|
|
|
|
BuildNormals();
|
|
|
|
} else {
|
|
|
|
BuildFaceNormals();
|
2023-09-18 21:06:34 +08:00
|
|
|
}
|
|
|
|
|
2023-09-20 14:42:44 +08:00
|
|
|
// read material channels
|
|
|
|
if (chunk->SeekIdentifier(CK_STATESAVEFLAGS_MESH::CK_STATESAVE_MESHCHANNELS)) {
|
|
|
|
// read size and resize it
|
|
|
|
CKDWORD chlSize;
|
|
|
|
chunk->ReadStruct(chlSize);
|
|
|
|
SetMtlChannelCount(chlSize);
|
|
|
|
|
|
|
|
for (auto& chl : m_MaterialChannels) {
|
|
|
|
// read material
|
|
|
|
CKObject* mtlobj = nullptr;
|
|
|
|
chunk->ReadObjectPointer(mtlobj);
|
|
|
|
if (mtlobj != nullptr && mtlobj->GetClassID() == CK_CLASSID::CKCID_MATERIAL) {
|
|
|
|
chl.m_Material = static_cast<CKMaterial*>(mtlobj);
|
|
|
|
}
|
|
|
|
|
|
|
|
// read flags and call function to make sure a custom uv can be created if existed.
|
|
|
|
chunk->ReadStruct(chl.m_Flags);
|
|
|
|
SyncVertexCountToMtlChannel();
|
|
|
|
|
|
|
|
// read blend modes
|
|
|
|
chunk->ReadStruct(chl.m_SourceBlend);
|
|
|
|
chunk->ReadStruct(chl.m_DestBlend);
|
|
|
|
|
|
|
|
// read custom vertex
|
|
|
|
CKDWORD uvcount;
|
|
|
|
chunk->ReadStruct(uvcount);
|
|
|
|
if (uvcount != 0) {
|
|
|
|
// make sure no overflow
|
2023-09-22 22:31:51 +08:00
|
|
|
uvcount = std::min(uvcount, static_cast<CKDWORD>(chl.m_CustomUV.size()));
|
2023-09-20 14:42:44 +08:00
|
|
|
|
|
|
|
CKDWORD bufsize = uvcount * CKSizeof(VxMath::VxVector2);
|
|
|
|
auto locker = chunk->LockReadBufferWrapper(bufsize);
|
|
|
|
std::memcpy(chl.m_CustomUV.data(), locker.get(), bufsize);
|
|
|
|
locker.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// vertex weight
|
|
|
|
CKDWORD weightSize;
|
|
|
|
m_NoVertexWeight = true;
|
|
|
|
if (chunk->SeekIdentifierAndReturnSize(CK_STATESAVEFLAGS_MESH::CK_STATESAVE_MESHWEIGHTS, &weightSize)) {
|
|
|
|
// set it has
|
|
|
|
m_NoVertexWeight = false;
|
|
|
|
// set count
|
|
|
|
CKDWORD weightCount;
|
|
|
|
chunk->ReadStruct(weightCount);
|
|
|
|
|
|
|
|
if (weightSize > CKSizeof(CKFLOAT)) {
|
|
|
|
// a float series
|
|
|
|
// read as a copy, to make sure no memory overflow
|
|
|
|
// because i couldn't understand how original CKMesh operate vertex weight count
|
|
|
|
// seperated with vertex count.
|
|
|
|
auto buf = chunk->ReadBufferWrapper();
|
|
|
|
CKDWORD bufsize = std::min(buf.get_deleter().GetBufferSize(), static_cast<CKDWORD>(m_VertexWeight.size()) * CKSizeof(CKFLOAT));
|
|
|
|
std::memcpy(m_VertexWeight.data(), buf.get(), bufsize);
|
|
|
|
buf.reset();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// a single float
|
|
|
|
CKFLOAT single;
|
|
|
|
chunk->ReadStruct(single);
|
|
|
|
|
|
|
|
for (auto& weight : m_VertexWeight) {
|
|
|
|
weight = single;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// face mask
|
|
|
|
if (chunk->SeekIdentifier(CK_STATESAVEFLAGS_MESH::CK_STATESAVE_MESHFACECHANMASK)) {
|
|
|
|
// 2 face mask (2 WORD) are compressed into a single DWORD.
|
|
|
|
// and if there is a remained WORD, read it as a single WORD.
|
|
|
|
// according to little endian, the actually stored data is just the mask placed
|
|
|
|
// one by one.
|
|
|
|
// so we just need to allocated it directly
|
|
|
|
|
|
|
|
// read mask count, and limit it to face count
|
|
|
|
CKDWORD maskCount;
|
|
|
|
chunk->ReadStruct(maskCount);
|
|
|
|
maskCount = std::min(maskCount, m_FaceCount);
|
|
|
|
|
|
|
|
auto locker = chunk->LockReadBufferWrapper(maskCount * CKSizeof(CKWORD));
|
|
|
|
const CKWORD* rawptr = static_cast<const CKWORD*>(locker.get());
|
|
|
|
for (auto& f : m_Faces) {
|
|
|
|
f.m_ChannelMask = *rawptr;
|
|
|
|
++rawptr;
|
|
|
|
}
|
|
|
|
locker.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: progressive mesh data is dropper.
|
|
|
|
|
2023-09-15 13:21:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-20 13:13:08 +08:00
|
|
|
void CKMesh::Show(CK_OBJECT_SHOWOPTION show) {
|
|
|
|
CKObject::Show(show);
|
|
|
|
|
|
|
|
if (show == CK_OBJECT_SHOWOPTION::CKSHOW) {
|
|
|
|
EnumsHelper::Add(m_Flags, VxMath::VXMESH_FLAGS::VXMESH_VISIBLE);
|
|
|
|
} else {
|
|
|
|
EnumsHelper::Rm(m_Flags, VxMath::VXMESH_FLAGS::VXMESH_VISIBLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-19 15:20:40 +08:00
|
|
|
#pragma region Misc Section
|
|
|
|
|
2023-09-15 16:15:07 +08:00
|
|
|
void CKMesh::CleanMesh() {
|
|
|
|
// clear material channel first
|
|
|
|
SetMtlChannelCount(0);
|
|
|
|
// then clear other
|
|
|
|
SetVertexCount(0);
|
2023-09-20 14:42:44 +08:00
|
|
|
m_NoVertexWeight = true;
|
|
|
|
|
2023-09-15 16:15:07 +08:00
|
|
|
SetMaterialSlotCount(0);
|
|
|
|
SetFaceCount(0);
|
|
|
|
SetLineCount(0);
|
|
|
|
}
|
|
|
|
|
2023-09-22 16:40:10 +08:00
|
|
|
VxMath::VXMESH_FLAGS CKMesh::GetMeshFlags() const {
|
|
|
|
return m_Flags;
|
|
|
|
}
|
|
|
|
|
2023-09-19 15:20:40 +08:00
|
|
|
void CKMesh::BuildNormals() {
|
|
|
|
if (m_FaceCount == 0 || m_VertexCount == 0) return;
|
|
|
|
|
|
|
|
// build face normal first
|
|
|
|
BuildFaceNormals();
|
|
|
|
|
|
|
|
// iterate all face and add face normal to each point's normal
|
|
|
|
for (CKDWORD fid = 0; fid < m_FaceCount; ++fid) {
|
|
|
|
m_VertexNormal[m_FaceIndices[fid * 3]] += m_Faces[fid].m_Normal;
|
|
|
|
m_VertexNormal[m_FaceIndices[fid * 3 + 1]] += m_Faces[fid].m_Normal;
|
|
|
|
m_VertexNormal[m_FaceIndices[fid * 3 + 2]] += m_Faces[fid].m_Normal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// then normalize all vertex normal
|
|
|
|
for (auto& nml : m_VertexNormal) {
|
|
|
|
nml.Normalized();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CKMesh::BuildFaceNormals() {
|
|
|
|
if (m_FaceCount == 0 || m_VertexCount == 0) return;
|
|
|
|
|
|
|
|
// iertate all face to build face normal according to position data
|
|
|
|
for (CKDWORD fid = 0; fid < m_FaceCount; ++fid) {
|
|
|
|
VxMath::VxVector3 *p0 = &m_VertexPosition[m_FaceIndices[fid * 3]];
|
|
|
|
|
|
|
|
VxMath::VxVector3 p0_p1 = m_VertexPosition[m_FaceIndices[fid * 3 + 1]] - *p0,
|
|
|
|
p0_p2 = m_VertexPosition[m_FaceIndices[fid * 3 + 2]] - *p0;
|
|
|
|
|
|
|
|
// cross product to get normal
|
|
|
|
// and normalize it
|
|
|
|
VxMath::VxVector3 nml = VxMath::NSVxVector::CrossProduct(p0_p1, p0_p2);
|
|
|
|
nml.Normalized();
|
|
|
|
|
|
|
|
// assign it
|
|
|
|
m_Faces[fid].m_Normal = nml;
|
|
|
|
}
|
|
|
|
}
|
2023-09-18 23:11:33 +08:00
|
|
|
|
2023-09-19 15:20:40 +08:00
|
|
|
#pragma endregion
|
2023-09-18 23:11:33 +08:00
|
|
|
|
2023-09-15 16:15:07 +08:00
|
|
|
#pragma region Vertex Section
|
|
|
|
|
2023-09-22 14:48:45 +08:00
|
|
|
CKDWORD CKMesh::GetVertexCount() const {
|
2023-09-15 16:15:07 +08:00
|
|
|
return m_VertexCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CKMesh::SetVertexCount(CKDWORD count) {
|
|
|
|
m_VertexCount = count;
|
|
|
|
m_VertexPosition.resize(count);
|
|
|
|
m_VertexNormal.resize(count);
|
|
|
|
m_VertexUV.resize(count);
|
|
|
|
m_VertexColor.resize(count, 0xFFFFFFFF);
|
|
|
|
m_VertexSpecularColor.resize(count, 0x00000000);
|
|
|
|
m_VertexWeight.resize(count, 0.0f);
|
|
|
|
|
|
|
|
// notify mtl channels refresh its custom uv
|
|
|
|
SyncVertexCountToMtlChannel();
|
|
|
|
}
|
|
|
|
|
|
|
|
VxMath::VxVector3* CKMesh::GetVertexPositions() {
|
|
|
|
return m_VertexPosition.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
VxMath::VxVector3* CKMesh::GetVertexNormals() {
|
|
|
|
return m_VertexNormal.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
VxMath::VxVector2* CKMesh::GetVertexUVs() {
|
|
|
|
return m_VertexUV.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
CKDWORD* CKMesh::GetVertexColors() {
|
|
|
|
return m_VertexColor.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
CKDWORD* CKMesh::GetVertexSpecularColors() {
|
|
|
|
return m_VertexSpecularColor.data();
|
|
|
|
}
|
|
|
|
|
2023-09-16 18:31:25 +08:00
|
|
|
CKFLOAT* CKMesh::GetVertexWeights() {
|
2023-09-15 16:15:07 +08:00
|
|
|
return m_VertexWeight.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma endregion
|
|
|
|
|
|
|
|
#pragma region Material Slot Section
|
|
|
|
|
2023-09-22 14:48:45 +08:00
|
|
|
CKDWORD CKMesh::GetMaterialSlotCount() const {
|
2023-09-15 16:15:07 +08:00
|
|
|
return m_MtlSlotCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CKMesh::SetMaterialSlotCount(CKDWORD count) {
|
|
|
|
m_MtlSlotCount = count;
|
|
|
|
m_MaterialSlot.resize(count, nullptr);
|
|
|
|
}
|
|
|
|
|
2023-09-22 14:48:45 +08:00
|
|
|
CKMaterial** CKMesh::GetMaterialSlots() {
|
|
|
|
return m_MaterialSlot.data();
|
2023-09-15 16:15:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma endregion
|
|
|
|
|
|
|
|
#pragma region Face Section
|
|
|
|
|
2023-09-22 14:48:45 +08:00
|
|
|
CKDWORD CKMesh::GetFaceCount() const {
|
2023-09-15 16:15:07 +08:00
|
|
|
return m_FaceCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CKMesh::SetFaceCount(CKDWORD count) {
|
|
|
|
m_FaceCount = count;
|
|
|
|
m_FaceIndices.resize(count * 3, 0);
|
|
|
|
m_FaceMtlIndex.resize(count, 0);
|
|
|
|
m_Faces.resize(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
CKWORD* CKMesh::GetFaceIndices() {
|
|
|
|
return m_FaceIndices.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
CKWORD* CKMesh::GetFaceMaterialSlotIndexs() {
|
|
|
|
return m_FaceIndices.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
VxMath::VxVector3* CKMesh::GetFaceNormals(CKDWORD& stride) {
|
|
|
|
stride = CKSizeof(FaceData_t);
|
|
|
|
return &m_Faces.data()->m_Normal;
|
|
|
|
}
|
|
|
|
|
|
|
|
CKWORD* CKMesh::GetFaceChannelMasks(CKDWORD& stride) {
|
|
|
|
stride = CKSizeof(FaceData_t);
|
|
|
|
return &m_Faces.data()->m_ChannelMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma endregion
|
|
|
|
|
|
|
|
#pragma region Line Section
|
|
|
|
|
2023-09-22 14:48:45 +08:00
|
|
|
CKDWORD CKMesh::GetLineCount() const {
|
2023-09-15 16:15:07 +08:00
|
|
|
return m_LineCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CKMesh::SetLineCount(CKDWORD count) {
|
|
|
|
m_LineCount = count;
|
|
|
|
m_LineIndices.resize(count * 2, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
CKWORD* CKMesh::GetLineIndices() {
|
|
|
|
return m_LineIndices.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma endregion
|
|
|
|
|
|
|
|
#pragma region Mtl Channel Section
|
|
|
|
|
2023-09-22 14:48:45 +08:00
|
|
|
CKDWORD CKMesh::GetMtlChannelCount() const {
|
2023-09-15 16:15:07 +08:00
|
|
|
return m_MtlChannelCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CKMesh::SetMtlChannelCount(CKDWORD count) {
|
2023-09-20 14:42:44 +08:00
|
|
|
// backup old count
|
|
|
|
CKDWORD oldcount = m_MtlChannelCount;
|
|
|
|
|
|
|
|
// set and resize
|
2023-09-15 16:15:07 +08:00
|
|
|
m_MtlChannelCount = count;
|
|
|
|
m_MaterialChannels.resize(count);
|
|
|
|
|
|
|
|
// sync mask to each face.
|
|
|
|
// each face accept all mask in default
|
2023-09-20 14:42:44 +08:00
|
|
|
SyncMtlChannelToFaceMask(oldcount, count);
|
2023-09-15 16:15:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CKMaterial** CKMesh::GetMtlChannelMaterials(CKDWORD& stride) {
|
|
|
|
stride = CKSizeof(MaterialChannel_t);
|
|
|
|
return &m_MaterialChannels.data()->m_Material;
|
|
|
|
}
|
|
|
|
|
|
|
|
VxMath::VXBLEND_MODE* CKMesh::GetMtlChannelSourceBlends(CKDWORD& stride) {
|
|
|
|
stride = CKSizeof(MaterialChannel_t);
|
|
|
|
return &m_MaterialChannels.data()->m_SourceBlend;
|
|
|
|
}
|
|
|
|
|
|
|
|
VxMath::VXBLEND_MODE* CKMesh::GetMtlChannelDestBlends(CKDWORD& stride) {
|
|
|
|
stride = CKSizeof(MaterialChannel_t);
|
|
|
|
return &m_MaterialChannels.data()->m_DestBlend;
|
|
|
|
}
|
|
|
|
|
|
|
|
VxMath::VxVector2* CKMesh::GetMtlChannelCustomUVs(CKDWORD idx) {
|
|
|
|
if (idx >= m_MtlChannelCount) return nullptr;
|
|
|
|
return m_MaterialChannels[idx].m_CustomUV.data();
|
|
|
|
}
|
|
|
|
|
2023-09-22 14:48:45 +08:00
|
|
|
VxMath::VXCHANNEL_FLAGS CKMesh::GetMtlChannelFlags(CKDWORD idx) const {
|
2023-09-15 16:15:07 +08:00
|
|
|
if (idx >= m_MtlChannelCount) return static_cast<VxMath::VXCHANNEL_FLAGS>(0);
|
|
|
|
return m_MaterialChannels[idx].m_Flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CKMesh::SetMtlChannelFlags(CKDWORD idx, VxMath::VXCHANNEL_FLAGS flags) {
|
|
|
|
if (idx >= m_MtlChannelCount) return;
|
|
|
|
m_MaterialChannels[idx].m_Flags = flags;
|
|
|
|
|
|
|
|
// refresh self custom uv
|
|
|
|
SyncVertexCountToMtlChannel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CKMesh::SyncVertexCountToMtlChannel() {
|
|
|
|
for (auto& channel : m_MaterialChannels) {
|
|
|
|
if (!EnumsHelper::Has(channel.m_Flags, VxMath::VXCHANNEL_FLAGS::VXCHANNEL_SAMEUV)) {
|
|
|
|
channel.m_CustomUV.resize(m_VertexCount);
|
|
|
|
} else {
|
|
|
|
channel.m_CustomUV.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-20 14:42:44 +08:00
|
|
|
void CKMesh::SyncMtlChannelToFaceMask(CKDWORD oldsize, CKDWORD newsize) {
|
|
|
|
// use oldsize and newsize to build mask
|
|
|
|
if (oldsize == newsize) return;
|
|
|
|
|
|
|
|
CKWORD mask = 0xFFFF;
|
|
|
|
if (oldsize > newsize) {
|
|
|
|
// channels shrinks
|
|
|
|
// set already removed bits to 1
|
|
|
|
mask = static_cast<CKWORD>(~(0xFFFF << newsize));
|
|
|
|
} else {
|
|
|
|
// channels expand
|
|
|
|
// set new added bits to 1
|
|
|
|
mask = static_cast<CKWORD>(~(0xFFFF << oldsize));
|
|
|
|
}
|
2023-09-15 16:15:07 +08:00
|
|
|
for (auto& face : m_Faces) {
|
|
|
|
face.m_ChannelMask |= mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma endregion
|
|
|
|
|
|
|
|
|
2023-09-15 13:21:49 +08:00
|
|
|
}
|