2022-04-02 00:19:26 +00:00
|
|
|
#include "dynos.cpp.h"
|
2022-05-04 07:14:33 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "engine/graph_node.h"
|
|
|
|
}
|
2022-04-02 00:19:26 +00:00
|
|
|
|
2022-05-03 00:31:29 +00:00
|
|
|
#define F32VTX_SENTINEL_0 0x3346
|
|
|
|
#define F32VTX_SENTINEL_1 0x5632
|
|
|
|
#define F32VTX_SENTINEL_2 0x5854
|
|
|
|
|
|
|
|
static inline bool ShouldUseF32Vtx(DataNode<Vtx>* aNode) {
|
2024-05-19 13:53:18 +00:00
|
|
|
for (u32 i = 0; i != aNode->mSize; ++i) {
|
|
|
|
for (u32 j = 0; j != 3; ++j) {
|
|
|
|
if (aNode->mData[i].n.ob[j] < -0x7FFF ||
|
|
|
|
aNode->mData[i].n.ob[j] > +0x7FFF) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2022-05-03 00:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool IsUsingF32Vtx(Vec3f ob) {
|
|
|
|
return ob[0] == F32VTX_SENTINEL_0 &&
|
|
|
|
ob[1] == F32VTX_SENTINEL_1 &&
|
|
|
|
ob[2] == F32VTX_SENTINEL_2;
|
|
|
|
}
|
|
|
|
|
2022-04-02 00:19:26 +00:00
|
|
|
/////////////
|
|
|
|
// Parsing //
|
|
|
|
/////////////
|
|
|
|
|
|
|
|
DataNode<Vtx>* DynOS_Vtx_Parse(GfxData* aGfxData, DataNode<Vtx>* aNode) {
|
|
|
|
if (aNode->mData) return aNode;
|
|
|
|
|
|
|
|
// Vertex data
|
|
|
|
aNode->mSize = (u32) (aNode->mTokens.Count() / 10);
|
|
|
|
aNode->mData = New<Vtx>(aNode->mSize);
|
|
|
|
for (u32 i = 0; i != aNode->mSize; ++i) {
|
|
|
|
f32 px = (f32) aNode->mTokens[10 * i + 0].ParseFloat();
|
|
|
|
f32 py = (f32) aNode->mTokens[10 * i + 1].ParseFloat();
|
|
|
|
f32 pz = (f32) aNode->mTokens[10 * i + 2].ParseFloat();
|
|
|
|
u8 fl = (u8) aNode->mTokens[10 * i + 3].ParseInt();
|
|
|
|
s16 tu = (s16) aNode->mTokens[10 * i + 4].ParseInt();
|
|
|
|
s16 tv = (s16) aNode->mTokens[10 * i + 5].ParseInt();
|
|
|
|
u8 nx = (u8) aNode->mTokens[10 * i + 6].ParseInt();
|
|
|
|
u8 ny = (u8) aNode->mTokens[10 * i + 7].ParseInt();
|
|
|
|
u8 nz = (u8) aNode->mTokens[10 * i + 8].ParseInt();
|
|
|
|
u8 a = (u8) aNode->mTokens[10 * i + 9].ParseInt();
|
|
|
|
aNode->mData[i] = { { { px, py, pz }, fl, { tu, tv }, { nx, ny, nz, a } } };
|
|
|
|
}
|
|
|
|
aNode->mLoadIndex = aGfxData->mLoadIndex++;
|
|
|
|
return aNode;
|
|
|
|
}
|
2022-04-02 02:50:42 +00:00
|
|
|
|
|
|
|
/////////////
|
|
|
|
// Writing //
|
|
|
|
/////////////
|
|
|
|
|
2022-06-25 07:52:53 +00:00
|
|
|
void DynOS_Vtx_Write(BinFile* aFile, GfxData* aGfxData, DataNode<Vtx> *aNode) {
|
2022-04-02 02:50:42 +00:00
|
|
|
if (!aNode->mData) return;
|
|
|
|
|
|
|
|
// Header
|
2022-06-25 07:52:53 +00:00
|
|
|
aFile->Write<u8>(DATA_TYPE_VERTEX);
|
2022-04-02 02:50:42 +00:00
|
|
|
aNode->mName.Write(aFile);
|
|
|
|
|
|
|
|
// Data
|
2022-05-03 00:31:29 +00:00
|
|
|
bool shouldUseF32Vtx = ShouldUseF32Vtx(aNode);
|
|
|
|
if (shouldUseF32Vtx) {
|
2022-06-25 07:52:53 +00:00
|
|
|
aFile->Write<u32>(aNode->mSize + 1);
|
2022-05-03 00:31:29 +00:00
|
|
|
|
|
|
|
// Write sentinel
|
2022-06-25 07:52:53 +00:00
|
|
|
aFile->Write<s16>(F32VTX_SENTINEL_0);
|
|
|
|
aFile->Write<s16>(F32VTX_SENTINEL_1);
|
|
|
|
aFile->Write<s16>(F32VTX_SENTINEL_2);
|
|
|
|
aFile->Write<s16>(0);
|
|
|
|
aFile->Write<s16>(0);
|
|
|
|
aFile->Write<s16>(0);
|
|
|
|
aFile->Write<s8> (0);
|
|
|
|
aFile->Write<s8> (0);
|
|
|
|
aFile->Write<s8> (0);
|
|
|
|
aFile->Write<u8> (0);
|
2022-05-03 00:31:29 +00:00
|
|
|
} else {
|
2022-06-25 07:52:53 +00:00
|
|
|
aFile->Write<u32>(aNode->mSize);
|
2022-05-03 00:31:29 +00:00
|
|
|
}
|
2022-04-02 02:50:42 +00:00
|
|
|
for (u32 i = 0; i != aNode->mSize; ++i) {
|
2022-05-03 00:31:29 +00:00
|
|
|
if (shouldUseF32Vtx) {
|
2022-06-25 07:52:53 +00:00
|
|
|
aFile->Write<f32>(aNode->mData[i].n.ob[0]);
|
|
|
|
aFile->Write<f32>(aNode->mData[i].n.ob[1]);
|
|
|
|
aFile->Write<f32>(aNode->mData[i].n.ob[2]);
|
2022-05-03 00:31:29 +00:00
|
|
|
} else {
|
2022-06-25 07:52:53 +00:00
|
|
|
aFile->Write<s16>(aNode->mData[i].n.ob[0]);
|
|
|
|
aFile->Write<s16>(aNode->mData[i].n.ob[1]);
|
|
|
|
aFile->Write<s16>(aNode->mData[i].n.ob[2]);
|
2022-05-03 00:31:29 +00:00
|
|
|
}
|
2022-06-25 07:52:53 +00:00
|
|
|
aFile->Write<s16>(aNode->mData[i].n.flag);
|
|
|
|
aFile->Write<s16>(aNode->mData[i].n.tc[0]);
|
|
|
|
aFile->Write<s16>(aNode->mData[i].n.tc[1]);
|
|
|
|
aFile->Write<s8> (aNode->mData[i].n.n[0]);
|
|
|
|
aFile->Write<s8> (aNode->mData[i].n.n[1]);
|
|
|
|
aFile->Write<s8> (aNode->mData[i].n.n[2]);
|
|
|
|
aFile->Write<u8> (aNode->mData[i].n.a);
|
2022-04-02 02:50:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////
|
|
|
|
// Reading //
|
|
|
|
/////////////
|
|
|
|
|
2022-06-25 07:52:53 +00:00
|
|
|
void DynOS_Vtx_Load(BinFile *aFile, GfxData *aGfxData) {
|
2022-04-02 02:50:42 +00:00
|
|
|
DataNode<Vtx> *_Node = New<DataNode<Vtx>>();
|
|
|
|
|
|
|
|
// Name
|
|
|
|
_Node->mName.Read(aFile);
|
|
|
|
|
|
|
|
// Data
|
2022-05-03 00:31:29 +00:00
|
|
|
bool isUsingF32Vtx = false;
|
2022-06-25 07:52:53 +00:00
|
|
|
_Node->mSize = aFile->Read<u32>();
|
2022-04-02 02:50:42 +00:00
|
|
|
_Node->mData = New<Vtx>(_Node->mSize);
|
|
|
|
for (u32 i = 0; i != _Node->mSize; ++i) {
|
2022-05-03 00:31:29 +00:00
|
|
|
if (isUsingF32Vtx) {
|
2022-06-25 07:52:53 +00:00
|
|
|
_Node->mData[i].n.ob[0] = aFile->Read<f32>();
|
|
|
|
_Node->mData[i].n.ob[1] = aFile->Read<f32>();
|
|
|
|
_Node->mData[i].n.ob[2] = aFile->Read<f32>();
|
2022-05-03 00:31:29 +00:00
|
|
|
} else {
|
2022-06-25 07:52:53 +00:00
|
|
|
_Node->mData[i].n.ob[0] = aFile->Read<s16>();
|
|
|
|
_Node->mData[i].n.ob[1] = aFile->Read<s16>();
|
|
|
|
_Node->mData[i].n.ob[2] = aFile->Read<s16>();
|
2022-05-03 00:31:29 +00:00
|
|
|
}
|
2022-06-25 07:52:53 +00:00
|
|
|
_Node->mData[i].n.flag = aFile->Read<s16>();
|
|
|
|
_Node->mData[i].n.tc[0] = aFile->Read<s16>();
|
|
|
|
_Node->mData[i].n.tc[1] = aFile->Read<s16>();
|
|
|
|
_Node->mData[i].n.n[0] = aFile->Read<s8> ();
|
|
|
|
_Node->mData[i].n.n[1] = aFile->Read<s8> ();
|
|
|
|
_Node->mData[i].n.n[2] = aFile->Read<s8> ();
|
|
|
|
_Node->mData[i].n.a = aFile->Read<u8> ();
|
2022-05-03 00:31:29 +00:00
|
|
|
|
|
|
|
// Check sentinel on first vertex
|
|
|
|
if (!isUsingF32Vtx && i == 0 && IsUsingF32Vtx(_Node->mData[i].n.ob)) {
|
|
|
|
_Node->mSize--; i--;
|
|
|
|
isUsingF32Vtx = true;
|
|
|
|
}
|
2022-04-02 02:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append
|
|
|
|
aGfxData->mVertices.Add(_Node);
|
|
|
|
}
|