Repository navigation
Expand file tree
/
Copy pathModelFactory.cpp
More file actions
858 lines (790 loc) · 35.2 KB
/
Copy pathModelFactory.cpp
File metadata and controls
858 lines (790 loc) · 35.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
#include "ModelFactory.h"
#include "spdlog/spdlog.h"
#include <libultraship/libultraship.h>
#include <ship/resource/type/Blob.h>
#include <fast/resource/type/Vertex.h>
#include <fast/resource/type/Texture.h>
#include "port/Resource/Alt/AltAnimTextures.h"
#include "port/Resource/Alt/AltPathPool.h"
#include "port/Enhancements/Events/Hooks/Events.h"
#include <cstdlib>
#include <cstring>
#include <string>
#include <unordered_map>
#include <vector>
#include "core2/model.h"
#include "BlobWriter.h"
namespace Factories {
namespace {
void PadTo8(std::vector<uint8_t>& dst) {
while (dst.size() & 7)
dst.push_back(0);
}
} // anonymous namespace
// The N of a G_SETTIMG image ReadResource points at "__OTR__<model>_tex_N", or -1 for any other image. With `palette`,
// "_tex_N_TLUT" counts too, and *palette says which one the image is.
int ModelTextureIndex(uintptr_t image, bool* palette) {
if (image == 0 || (image & 1) != 0 || std::strncmp((const char*)image, "__OTR__", 7) != 0) {
return -1;
}
const char* tag = nullptr;
for (const char* found = std::strstr((const char*)image, "_tex_"); found != nullptr;
found = std::strstr(found + 1, "_tex_")) {
tag = found;
}
if (tag == nullptr) {
return -1;
}
char* end;
const int index = (int)std::strtol(tag + 5, &end, 10);
const bool isPalette = std::strcmp(end, "_TLUT") == 0;
if (palette != nullptr) {
*palette = isPalette;
} else if (isPalette) {
return -1;
}
return index;
}
std::shared_ptr<Ship::IResource>
ResourceFactoryBinaryModelV0::ReadResource(std::shared_ptr<Ship::File> file,
std::shared_ptr<Ship::ResourceInitData> initData) {
if (!FileHasValidFormatAndReader(file, initData)) {
return nullptr;
}
auto reader = std::get<std::shared_ptr<Ship::BinaryReader>>(file->Reader);
auto resourceMgr = Ship::Context::GetRawInstance()->GetResourceManager();
// ── Read metadata written by ModelBinaryExporter::Export ────────────────
const uint16_t geoType = reader->ReadUInt16();
const uint16_t triCount = reader->ReadUInt16();
const uint16_t vertCount = reader->ReadUInt16();
(void)triCount;
(void)vertCount;
const bool hasGeo = reader->ReadUByte() != 0;
const bool hasVtx = reader->ReadUByte() != 0;
const bool hasDL = reader->ReadUByte() != 0;
const uint16_t texCount = reader->ReadUInt16();
const bool hasAnim = reader->ReadUByte() != 0;
const bool hasCol = reader->ReadUByte() != 0;
const bool hasCollisionShapes = reader->ReadUByte() != 0;
const bool hasCameraAreas = reader->ReadUByte() != 0;
const bool hasMeshList = reader->ReadUByte() != 0;
const bool hasVertexBoneMap = reader->ReadUByte() != 0;
const bool hasAnimTex = reader->ReadUByte() != 0;
// [port] Debug: dump parsed flags so we can verify stream alignment
SPDLOG_TRACE("[BKModel] '{}' flags: geoType={} tri={} vert={} geo={} vtx={} dl={} tex={} anim={} col={} shapes={} "
"cameraAreas={} meshes={} vertexBoneMap={} animTex={}",
initData->Path, geoType, triCount, vertCount, hasGeo, hasVtx, hasDL, texCount, hasAnim, hasCol,
hasCollisionShapes, hasCameraAreas, hasMeshList, hasVertexBoneMap, hasAnimTex);
// VTX header fields
int16_t vtxMin[3] = {}, vtxMax[3] = {}, vtxCenter[3] = {};
int16_t vtxLocalNorm = 0, vtxGlobalNorm = 0;
uint16_t vtxCount = 0;
if (hasVtx) {
vtxMin[0] = reader->ReadInt16();
vtxMin[1] = reader->ReadInt16();
vtxMin[2] = reader->ReadInt16();
vtxMax[0] = reader->ReadInt16();
vtxMax[1] = reader->ReadInt16();
vtxMax[2] = reader->ReadInt16();
vtxCenter[0] = reader->ReadInt16();
vtxCenter[1] = reader->ReadInt16();
vtxCenter[2] = reader->ReadInt16();
vtxLocalNorm = reader->ReadInt16();
vtxCount = reader->ReadUInt16();
vtxGlobalNorm = reader->ReadInt16();
}
// GFX info + raw N64 display list command words
uint32_t dlCount = 0, dlUnkInfo = 0, gfxSubCount = 0;
std::vector<uint32_t> rawDLWords;
if (hasDL) {
dlCount = reader->ReadUInt32();
dlUnkInfo = reader->ReadUInt32();
gfxSubCount = reader->ReadUInt32();
SPDLOG_TRACE("[BKModel] '{}' DL: count={} unkInfo=0x{:X} subCount={} (need {} more bytes for raw words)",
initData->Path, dlCount, dlUnkInfo, gfxSubCount, dlCount * 2 * 4);
// Read raw N64 DL words embedded by Torch (w0,w1 pairs, dlCount commands)
rawDLWords.resize(dlCount * 2);
for (uint32_t i = 0; i < dlCount * 2; i++) {
rawDLWords[i] = reader->ReadUInt32();
}
}
// Texture metadata
struct TexMeta {
uint16_t type;
uint8_t width, height;
uint16_t tlutColors;
uint32_t textureDataOffset;
};
std::vector<TexMeta> texMetas(texCount);
for (uint16_t ti = 0; ti < texCount; ti++) {
auto& tm = texMetas[ti];
tm.type = reader->ReadUInt16();
tm.width = reader->ReadUByte();
tm.height = reader->ReadUByte();
tm.tlutColors = reader->ReadUInt16();
tm.textureDataOffset = reader->ReadUInt32();
SPDLOG_TRACE("[BKModel] '{}' tex[{}]: type=0x{:X} {}x{} tlutColors={} romOff=0x{:X}", initData->Path, ti,
tm.type, tm.width, tm.height, tm.tlutColors, tm.textureDataOffset);
}
// Let enhancements patch the raw display list before it is widened.
// Command indices must be preserved; the geo layout references them by position.
if (!rawDLWords.empty() && texCount > 0) {
std::vector<ModelTexSize> texSizes;
texSizes.reserve(texMetas.size());
for (const auto& tm : texMetas) {
texSizes.push_back(ModelTexSize{ tm.width, tm.height });
}
CALL_EVENT(OnModelDisplayListLoad, initData->Path.c_str(), rawDLWords.data(),
static_cast<uint32_t>(rawDLWords.size()), texSizes.data(), texCount);
}
// Raw texture data blob — full contiguous pixel area from the ROM.
// Preserves animated texture frames and unlisted data between textures.
uint32_t rawTexDataSize = reader->ReadUInt32();
std::vector<uint8_t> rawTexBlob;
if (rawTexDataSize > 0) {
rawTexBlob.resize(rawTexDataSize);
for (uint32_t i = 0; i < rawTexDataSize; i++)
rawTexBlob[i] = reader->ReadInt8();
}
// Animation
float animScale = 0.0f;
uint16_t boneCount = 0;
struct BoneEntry {
float pos[3];
uint16_t id, parentId;
};
std::vector<BoneEntry> bones;
if (hasAnim) {
animScale = reader->ReadFloat();
boneCount = reader->ReadUInt16();
for (uint16_t i = 0; i < boneCount; i++) {
BoneEntry b{};
b.pos[0] = reader->ReadFloat();
b.pos[1] = reader->ReadFloat();
b.pos[2] = reader->ReadFloat();
b.id = reader->ReadUInt16();
b.parentId = reader->ReadUInt16();
bones.push_back(b);
}
}
// Collision
int16_t colMin[3] = {}, colMax[3] = {};
uint16_t colYStride = 0, colZStride = 0, colCubeScale = 0;
struct ColCubeEntry {
uint16_t startTri, triCount;
};
struct ColTriEntry {
uint16_t vtxIds[3], unk6;
uint32_t flags;
};
std::vector<ColCubeEntry> colCubes;
std::vector<ColTriEntry> colTris;
if (hasCol) {
SPDLOG_TRACE("[BKModel] '{}' reading collision...", initData->Path);
colMin[0] = reader->ReadInt16();
colMin[1] = reader->ReadInt16();
colMin[2] = reader->ReadInt16();
colMax[0] = reader->ReadInt16();
colMax[1] = reader->ReadInt16();
colMax[2] = reader->ReadInt16();
colYStride = reader->ReadUInt16();
colZStride = reader->ReadUInt16();
colCubeScale = reader->ReadUInt16();
const uint16_t cubeCnt = reader->ReadUInt16();
const uint16_t triCnt = reader->ReadUInt16();
SPDLOG_TRACE("[BKModel] '{}' collision: cubes={} tris={}", initData->Path, cubeCnt, triCnt);
for (uint16_t i = 0; i < cubeCnt; i++) {
ColCubeEntry e{};
e.startTri = reader->ReadUInt16();
e.triCount = reader->ReadUInt16();
colCubes.push_back(e);
}
for (uint16_t i = 0; i < triCnt; i++) {
ColTriEntry e{};
e.vtxIds[0] = reader->ReadUInt16();
e.vtxIds[1] = reader->ReadUInt16();
e.vtxIds[2] = reader->ReadUInt16();
e.unk6 = reader->ReadUInt16();
e.flags = reader->ReadUInt32();
colTris.push_back(e);
}
}
int16_t shapeBoxCount = 0, shapeCylinderCount = 0, shapeSphereCount = 0, shapeCullRadius = 0;
struct ShapeBox {
int16_t scale1[3], scale2[3], pos[3];
uint8_t rot[3], unk15;
int8_t animIdx;
uint8_t pad;
};
struct ShapeCylinder {
int16_t unk0, unk2, pos[3];
uint8_t rot[3], unkD;
int8_t animIdx;
uint8_t pad;
};
struct ShapeSphere {
int16_t unk0, unk2[3];
uint8_t unk8;
int8_t unk9;
uint8_t pad[2];
};
std::vector<ShapeBox> shapeBoxes;
std::vector<ShapeCylinder> shapeCylinders;
std::vector<ShapeSphere> shapeSpheres;
if (hasCollisionShapes) {
shapeBoxCount = reader->ReadInt16();
shapeCylinderCount = reader->ReadInt16();
shapeSphereCount = reader->ReadInt16();
shapeCullRadius = reader->ReadInt16();
for (int16_t i = 0; i < shapeBoxCount; i++) {
ShapeBox e{};
e.scale1[0] = reader->ReadInt16();
e.scale1[1] = reader->ReadInt16();
e.scale1[2] = reader->ReadInt16();
e.scale2[0] = reader->ReadInt16();
e.scale2[1] = reader->ReadInt16();
e.scale2[2] = reader->ReadInt16();
e.pos[0] = reader->ReadInt16();
e.pos[1] = reader->ReadInt16();
e.pos[2] = reader->ReadInt16();
e.rot[0] = reader->ReadUByte();
e.rot[1] = reader->ReadUByte();
e.rot[2] = reader->ReadUByte();
e.unk15 = reader->ReadUByte();
e.animIdx = reader->ReadInt8();
e.pad = reader->ReadUByte();
shapeBoxes.push_back(e);
}
for (int16_t i = 0; i < shapeCylinderCount; i++) {
ShapeCylinder e{};
e.unk0 = reader->ReadInt16();
e.unk2 = reader->ReadInt16();
e.pos[0] = reader->ReadInt16();
e.pos[1] = reader->ReadInt16();
e.pos[2] = reader->ReadInt16();
e.rot[0] = reader->ReadUByte();
e.rot[1] = reader->ReadUByte();
e.rot[2] = reader->ReadUByte();
e.unkD = reader->ReadUByte();
e.animIdx = reader->ReadInt8();
e.pad = reader->ReadUByte();
shapeCylinders.push_back(e);
}
for (int16_t i = 0; i < shapeSphereCount; i++) {
ShapeSphere e{};
e.unk0 = reader->ReadInt16();
e.unk2[0] = reader->ReadInt16();
e.unk2[1] = reader->ReadInt16();
e.unk2[2] = reader->ReadInt16();
e.unk8 = reader->ReadUByte();
e.unk9 = reader->ReadInt8();
e.pad[0] = reader->ReadUByte();
e.pad[1] = reader->ReadUByte();
shapeSpheres.push_back(e);
}
}
uint8_t cameraAreaCount = 0;
struct CameraArea {
int16_t unk0[3], unk6[3];
uint8_t unkC, pad;
};
std::vector<CameraArea> cameraAreas;
if (hasCameraAreas) {
cameraAreaCount = reader->ReadUByte();
for (uint8_t i = 0; i < cameraAreaCount; i++) {
CameraArea e{};
e.unk0[0] = reader->ReadInt16();
e.unk0[1] = reader->ReadInt16();
e.unk0[2] = reader->ReadInt16();
e.unk6[0] = reader->ReadInt16();
e.unk6[1] = reader->ReadInt16();
e.unk6[2] = reader->ReadInt16();
e.unkC = reader->ReadUByte();
e.pad = reader->ReadUByte();
cameraAreas.push_back(e);
}
}
uint16_t meshCount = 0;
struct MeshEntry {
uint16_t dataInfo;
std::vector<uint16_t> vtxIndices;
};
std::vector<MeshEntry> meshes;
if (hasMeshList) {
try {
meshCount = reader->ReadUInt16();
for (uint16_t i = 0; i < meshCount; i++) {
MeshEntry e{};
e.dataInfo = reader->ReadUInt16();
const uint16_t vtxCnt = reader->ReadUInt16();
for (uint16_t j = 0; j < vtxCnt; j++)
e.vtxIndices.push_back(reader->ReadUInt16());
meshes.push_back(e);
}
} catch (const std::out_of_range& ex) {
SPDLOG_WARN("[BKModel] Truncated mesh list in {}, ignoring it: {}", initData->Path, ex.what());
meshes.clear();
}
}
int16_t vertexBoneCount = 0;
struct VertexBoneEntry {
int16_t coord[3];
int8_t animIdx;
std::vector<int16_t> vtxList;
};
std::vector<VertexBoneEntry> vertexBoneMap;
if (hasVertexBoneMap) {
vertexBoneCount = reader->ReadInt16();
for (int16_t i = 0; i < vertexBoneCount; i++) {
VertexBoneEntry e{};
e.coord[0] = reader->ReadInt16();
e.coord[1] = reader->ReadInt16();
e.coord[2] = reader->ReadInt16();
e.animIdx = reader->ReadInt8();
const uint8_t vtxCnt = reader->ReadUByte();
for (uint8_t j = 0; j < vtxCnt; j++)
e.vtxList.push_back(reader->ReadInt16());
vertexBoneMap.push_back(e);
}
}
// AnimTextures (always 4 slots)
struct AnimTexEntry {
uint16_t frameSize, frameCount;
float frameRate;
};
std::vector<AnimTexEntry> animTextures;
if (hasAnimTex) {
for (int i = 0; i < 4; i++) {
AnimTexEntry at{};
at.frameSize = reader->ReadUInt16();
at.frameCount = reader->ReadUInt16();
at.frameRate = reader->ReadFloat();
animTextures.push_back(at);
}
}
BkAnimTextureAlt::Slot animSlots[4];
// ── Load GEO sub-resource (GeoLayout render tree) ─────────────────────────
std::shared_ptr<Ship::Blob> geoBlob;
if (hasGeo) {
SPDLOG_TRACE("[BKModel] '{}' loading _GEO sub-resource...", initData->Path);
geoBlob = std::static_pointer_cast<Ship::Blob>(resourceMgr->LoadResourceProcess(initData->Path + "_GEO"));
if (!geoBlob) {
SPDLOG_WARN("[BKModel] _GEO not found: {}", initData->Path);
}
}
// ── Assemble contiguous BKModelBin buffer ─────────────────────────────────
//
// Layout (each section padded to 8-byte alignment):
// [ 0 ] BKModelBin header (zero-init, geo_type set)
// [ T ] BKTextureList + BKTextureInfo[] + pixel data -> texture_list_offset
// [ GL ] GeoLayout command tree -> geo_list_offset
// [ A ] BKAnimationList + BKAnimation[] -> animation_list_offset
// [ B ] BKCollisionList + ColGeo[] + ColTri[] -> collision_list_offset
// [ CV ] collision volumes, boxes/cylinders/spheres -> unk14_list_offset
// [ CA ] BKCameraAreaList + entries -> camera_area_list_offset
// [ ML ] mesh count (s16) + named vertex groups -> mesh_list_offset
// [ VB ] vertex to bone binding + entries -> anim_vertices_list_offset
// [ AT ] AnimTexture[4] -> animated_texture_list_offset
// [ V ] BKVertexList header + Vtx[] -> vtx_list_offset
// [ G ] BKGfxList header + Gfx[] -> gfx_list_offset
auto out = std::vector<uint8_t>(sizeof(BKModelBin), 0);
auto hdr = [&]() -> BKModelBin* { return reinterpret_cast<BKModelBin*>(out.data()); };
hdr()->geo_type = static_cast<int16_t>(geoType);
// Texture section
size_t texturesAt = 0;
if (texCount > 0) {
PadTo8(out);
hdr()->texture_list_offset = static_cast<int16_t>(out.size());
texturesAt = out.size() + 8 + texCount * 16; // modelRender_draw's animated texture base
// Use raw texture blob directly — preserves animated frames and
// all unlisted data. OTEX overlay for alt assets can be added later.
const uint8_t* blobPtr = rawTexBlob.empty() ? nullptr : rawTexBlob.data();
uint32_t totalPixSize = rawTexDataSize;
// BKTextureList: s32 size_0, s16 cnt_4, u8 pad[2]
AppendValue<int32_t>(out, static_cast<int32_t>(totalPixSize));
AppendValue<int16_t>(out, static_cast<int16_t>(texCount));
AppendValue<uint8_t>(out, 0);
AppendValue<uint8_t>(out, 0);
// BKTextureInfo[] (16 bytes each) — use original ROM offsets
for (uint16_t i = 0; i < texCount; i++) {
const auto& tm = texMetas[i];
AppendValue<int32_t>(out, static_cast<int32_t>(tm.textureDataOffset));
AppendValue<int16_t>(out, static_cast<int16_t>(tm.type));
AppendValue<uint8_t>(out, 0);
AppendValue<uint8_t>(out, 0);
AppendValue<uint8_t>(out, tm.width);
AppendValue<uint8_t>(out, tm.height);
for (int p = 0; p < 6; p++)
AppendValue<uint8_t>(out, 0);
}
// Write pixel data area (raw blob from Torch)
if (blobPtr) {
AppendBytes(out, blobPtr, rawTexDataSize);
}
}
// GeoLayout section
if (geoBlob && !geoBlob->Data.empty()) {
PadTo8(out);
hdr()->geo_list_offset = static_cast<int32_t>(out.size());
AppendBytes(out, geoBlob->Data.data(), geoBlob->Data.size());
}
// Animation section
if (hasAnim) {
PadTo8(out);
hdr()->animation_list_offset = static_cast<int32_t>(out.size());
// BKAnimationList: f32 scalingFactor, s16 boneCount, u8 pad[2], BKAnimation[]
AppendValue<float>(out, animScale);
AppendValue<int16_t>(out, static_cast<int16_t>(boneCount));
AppendValue<uint8_t>(out, 0);
AppendValue<uint8_t>(out, 0);
for (const auto& b : bones) {
AppendValue<float>(out, b.pos[0]);
AppendValue<float>(out, b.pos[1]);
AppendValue<float>(out, b.pos[2]);
AppendValue<int16_t>(out, static_cast<int16_t>(b.id));
AppendValue<int16_t>(out, static_cast<int16_t>(b.parentId));
}
}
// Collision section
if (hasCol) {
PadTo8(out);
hdr()->collision_list_offset = static_cast<int32_t>(out.size());
// BKCollisionList header (24 bytes)
AppendValue<int16_t>(out, colMin[0]);
AppendValue<int16_t>(out, colMin[1]);
AppendValue<int16_t>(out, colMin[2]);
AppendValue<int16_t>(out, colMax[0]);
AppendValue<int16_t>(out, colMax[1]);
AppendValue<int16_t>(out, colMax[2]);
AppendValue<int16_t>(out, static_cast<int16_t>(colYStride));
AppendValue<int16_t>(out, static_cast<int16_t>(colZStride));
AppendValue<int16_t>(out, static_cast<int16_t>(colCubes.size()));
AppendValue<int16_t>(out, static_cast<int16_t>(colCubeScale));
AppendValue<int16_t>(out, static_cast<int16_t>(colTris.size()));
AppendValue<uint8_t>(out, 0);
AppendValue<uint8_t>(out, 0);
for (const auto& gc : colCubes) {
AppendValue<int16_t>(out, static_cast<int16_t>(gc.startTri));
AppendValue<int16_t>(out, static_cast<int16_t>(gc.triCount));
}
for (const auto& ct : colTris) {
AppendValue<int16_t>(out, static_cast<int16_t>(ct.vtxIds[0]));
AppendValue<int16_t>(out, static_cast<int16_t>(ct.vtxIds[1]));
AppendValue<int16_t>(out, static_cast<int16_t>(ct.vtxIds[2]));
AppendValue<int16_t>(out, static_cast<int16_t>(ct.unk6));
AppendValue<int32_t>(out, static_cast<int32_t>(ct.flags));
}
}
if (hasCollisionShapes) {
PadTo8(out);
hdr()->unk14_list_offset = static_cast<int32_t>(out.size());
AppendValue<int16_t>(out, shapeBoxCount);
AppendValue<int16_t>(out, shapeCylinderCount);
AppendValue<int16_t>(out, shapeSphereCount);
AppendValue<int16_t>(out, shapeCullRadius);
for (const auto& e : shapeBoxes) {
AppendValue<int16_t>(out, e.scale1[0]);
AppendValue<int16_t>(out, e.scale1[1]);
AppendValue<int16_t>(out, e.scale1[2]);
AppendValue<int16_t>(out, e.scale2[0]);
AppendValue<int16_t>(out, e.scale2[1]);
AppendValue<int16_t>(out, e.scale2[2]);
AppendValue<int16_t>(out, e.pos[0]);
AppendValue<int16_t>(out, e.pos[1]);
AppendValue<int16_t>(out, e.pos[2]);
AppendValue<uint8_t>(out, e.rot[0]);
AppendValue<uint8_t>(out, e.rot[1]);
AppendValue<uint8_t>(out, e.rot[2]);
AppendValue<uint8_t>(out, e.unk15);
AppendValue<int8_t>(out, e.animIdx);
AppendValue<uint8_t>(out, e.pad);
}
for (const auto& e : shapeCylinders) {
AppendValue<int16_t>(out, e.unk0);
AppendValue<int16_t>(out, e.unk2);
AppendValue<int16_t>(out, e.pos[0]);
AppendValue<int16_t>(out, e.pos[1]);
AppendValue<int16_t>(out, e.pos[2]);
AppendValue<uint8_t>(out, e.rot[0]);
AppendValue<uint8_t>(out, e.rot[1]);
AppendValue<uint8_t>(out, e.rot[2]);
AppendValue<uint8_t>(out, e.unkD);
AppendValue<int8_t>(out, e.animIdx);
AppendValue<uint8_t>(out, e.pad);
}
for (const auto& e : shapeSpheres) {
AppendValue<int16_t>(out, e.unk0);
AppendValue<int16_t>(out, e.unk2[0]);
AppendValue<int16_t>(out, e.unk2[1]);
AppendValue<int16_t>(out, e.unk2[2]);
AppendValue<uint8_t>(out, e.unk8);
AppendValue<int8_t>(out, e.unk9);
AppendValue<uint8_t>(out, e.pad[0]);
AppendValue<uint8_t>(out, e.pad[1]);
}
}
if (hasCameraAreas) {
PadTo8(out);
hdr()->camera_area_list_offset = static_cast<int32_t>(out.size());
AppendValue<uint8_t>(out, cameraAreaCount);
AppendValue<uint8_t>(out, 0);
for (const auto& e : cameraAreas) {
AppendValue<int16_t>(out, e.unk0[0]);
AppendValue<int16_t>(out, e.unk0[1]);
AppendValue<int16_t>(out, e.unk0[2]);
AppendValue<int16_t>(out, e.unk6[0]);
AppendValue<int16_t>(out, e.unk6[1]);
AppendValue<int16_t>(out, e.unk6[2]);
AppendValue<uint8_t>(out, e.unkC);
AppendValue<uint8_t>(out, e.pad);
}
}
if (hasMeshList && !meshes.empty()) {
PadTo8(out);
hdr()->mesh_list_offset = static_cast<int32_t>(out.size());
AppendValue<int16_t>(out, static_cast<int16_t>(meshes.size()));
for (const auto& entry : meshes) {
AppendValue<uint16_t>(out, entry.dataInfo);
AppendValue<uint16_t>(out, static_cast<uint16_t>(entry.vtxIndices.size()));
for (auto idx : entry.vtxIndices)
AppendValue<uint16_t>(out, idx);
}
}
if (hasVertexBoneMap) {
PadTo8(out);
hdr()->anim_vertices_list_offset = static_cast<int32_t>(out.size());
AppendValue<int16_t>(out, vertexBoneCount);
AppendValue<uint8_t>(out, 0);
AppendValue<uint8_t>(out, 0);
for (const auto& e : vertexBoneMap) {
AppendValue<int16_t>(out, e.coord[0]);
AppendValue<int16_t>(out, e.coord[1]);
AppendValue<int16_t>(out, e.coord[2]);
AppendValue<int8_t>(out, e.animIdx);
AppendValue<int8_t>(out, static_cast<int8_t>(e.vtxList.size()));
for (auto idx : e.vtxList)
AppendValue<int16_t>(out, idx);
}
}
// AnimTexture section
if (hasAnimTex) {
PadTo8(out);
hdr()->animated_texture_list_offset = static_cast<int32_t>(out.size());
for (const auto& at : animTextures) {
AppendValue<int16_t>(out, static_cast<int16_t>(at.frameSize));
AppendValue<int16_t>(out, static_cast<int16_t>(at.frameCount));
AppendValue<float>(out, at.frameRate);
}
}
// GFX section — widen raw N64 commands (8 bytes each) to native Gfx (16 bytes each).
// This preserves the exact N64 command count and indices so geo layout references
// (list[cmd->unk8]) remain correct. No OTR markers or expanded commands.
if (hasDL && !rawDLWords.empty()) {
PadTo8(out);
hdr()->gfx_list_offset = static_cast<int32_t>(out.size());
// BKGfxList: s32 dlCount, s32 dlUnkInfo
AppendValue<int32_t>(out, static_cast<int32_t>(dlCount));
AppendValue<int32_t>(out, static_cast<int32_t>(dlUnkInfo));
// The archives' _tex_N_TLUT palettes, by where each one starts in the model.
std::unordered_map<uint32_t, std::pair<std::string, uint16_t>> palettes;
auto archives = resourceMgr->GetArchiveManager();
for (uint16_t ti = 0; ti < texCount; ti++) {
const std::string palette = initData->Path + "_tex_" + std::to_string(ti) + "_TLUT";
if (texMetas[ti].tlutColors > 0 && archives->HasFile(palette)) {
palettes[texMetas[ti].textureDataOffset] = { palette, texMetas[ti].tlutColors };
}
}
// Some models load 256 colors for a texture that lists 16. Only the model has the other 240, so those loads
// keep the model's palette.
const auto loadsAtMost = [&](uint32_t at, uint16_t colors) {
for (uint32_t j = at + 1; j < dlCount; j++) {
const uint8_t op = (uint8_t)(rawDLWords[j * 2] >> 24);
if (op == 0xF0) { // G_LOADTLUT
return ((rawDLWords[j * 2 + 1] >> 14) & 0x3FF) + 1 <= colors;
}
if (op == 0xFD) { // G_SETTIMG
return false;
}
}
return false;
};
const auto loadsPixels = [&](uint32_t at) {
for (uint32_t j = at + 1; j < dlCount; j++) {
const uint8_t op = (uint8_t)(rawDLWords[j * 2] >> 24);
if (op == 0xF3 || op == 0xF4) { // G_LOADBLOCK, G_LOADTILE
return true;
}
if (op == 0xF0 || op == 0xFD) { // G_LOADTLUT, G_SETTIMG
return false;
}
}
return false;
};
if (hasAnimTex && texCount > 0) {
int32_t loads[4] = {};
for (uint32_t i = 0; i < dlCount; i++) {
const uint32_t seg = (rawDLWords[i * 2 + 1] >> 24) & 0xFF;
if ((rawDLWords[i * 2] >> 24) != 0xFD || seg < 0x0B || seg > 0x0F || !loadsPixels(i)) {
continue;
}
auto& slot = animSlots[0x0F - seg];
const uint32_t offset = rawDLWords[i * 2 + 1] & 0x00FFFFFF;
if (loads[0x0F - seg]++ > 0 && offset != slot.pixelOffset) {
slot.texIndex = -2; // more than one load: leave the slot alone
continue;
}
slot.pixelOffset = offset;
slot.frameSize = animTextures[0x0F - seg].frameSize;
for (uint16_t ti = 0; ti < texCount && slot.texIndex != -2; ti++) {
if (texMetas[ti].textureDataOffset <= offset &&
(slot.texIndex < 0 ||
texMetas[ti].textureDataOffset > texMetas[slot.texIndex].textureDataOffset)) {
slot.texIndex = ti;
}
}
}
for (auto& slot : animSlots) {
if (slot.texIndex < 0 || slot.frameSize == 0) {
slot.texIndex = -1;
}
}
}
// Widen each N64 command: (u32 w0, u32 w1) → (uintptr_t w0, uintptr_t w1)
for (uint32_t i = 0; i < dlCount; i++) {
uintptr_t w0 = rawDLWords[i * 2];
uintptr_t w1 = rawDLWords[i * 2 + 1];
uint8_t opcode = (uint8_t)(w0 >> 24);
if (opcode == 0xFD && (uint32_t)((w1 >> 24) & 0xFF) == 0xFF) {
const uint32_t texIndex = (uint32_t)(w1 & 0x00FFFFFF);
w1 = (uintptr_t)InternAltPath("__OTR__" + initData->Path + "_tex_" + std::to_string(texIndex));
AppendValue<uintptr_t>(out, w0);
AppendValue<uintptr_t>(out, w1);
continue;
}
const auto palette = opcode == 0xFD && (uint32_t)((w1 >> 24) & 0xFF) == 0x02 // G_SETTIMG from segment 2
? palettes.find((uint32_t)(w1 & 0x00FFFFFF))
: palettes.end();
if (palette != palettes.end() && loadsAtMost(i, palette->second.second)) {
w1 = (uintptr_t)InternAltPath("__OTR__" + palette->second.first);
AppendValue<uintptr_t>(out, w0);
AppendValue<uintptr_t>(out, w1);
continue;
}
if (opcode == 0xFD && ((w1 >> 24) & 0xFF) >= 0x0B && ((w1 >> 24) & 0xFF) <= 0x0F) {
const uint32_t slot = 0x0F - (uint32_t)((w1 >> 24) & 0xFF);
if (animSlots[slot].texIndex >= 0 && (uint32_t)(w1 & 0x00FFFFFF) == animSlots[slot].pixelOffset) {
w1 = ((uintptr_t)(BkAnimTextureAlt::kHdSegmentBase + slot) << 24) | (w1 & 0x00FFFFFF);
}
}
// F3DEX opcodes that carry a segmented address in w1.
// G_DL byte offsets must be scaled for widened Gfx (16 bytes vs N64's 8).
if (opcode == 0x06 && sizeof(uintptr_t) > 4) { // G_DL
if (w1 != 0 && (w1 >> 24) != 0 && w1 <= 0xFFFFFFFF) {
uint32_t seg = (uint32_t)(w1 >> 24);
uint32_t off = (uint32_t)(w1 & 0x00FFFFFF);
off *= (sizeof(Gfx) / 8);
w1 = ((uintptr_t)seg << 24) | (off & 0x00FFFFFF);
}
}
// G_VTX (F3DEX opcode 0x04): w1 is a segmented address into the
// vertex buffer. With GBI_FLOATS, sizeof(Vtx) = 24 instead of N64's 16,
// so byte offsets must be scaled: new_off = (old_off / 16) * sizeof(Vtx).
if (opcode == 0x04 && sizeof(Vtx) != 16) {
if (w1 != 0 && (w1 >> 24) != 0 && w1 <= 0xFFFFFFFF) {
uint32_t seg = (uint32_t)(w1 >> 24);
uint32_t off = (uint32_t)(w1 & 0x00FFFFFF);
off = (off / 16) * sizeof(Vtx);
w1 = ((uintptr_t)seg << 24) | (off & 0x00FFFFFF);
}
}
// Tag segmented addresses with bit 0 so SegAddr() identifies them.
// Only tag opcodes that carry a segmented address in w1. Other commands
// (G_SETTILE, G_LOADBLOCK, G_SETCOMBINE, G_SETPRIMCOLOR, etc.) have
// literal values in w1 that must NOT be modified.
switch (opcode) {
case 0x01: // G_MTX
case 0x03: // G_MOVEMEM
case 0x04: // G_VTX
case 0x06: // G_DL
case 0xFD: // G_SETTIMG (gDPSetTextureImage)
case 0xFE: // G_SETZIMG
case 0xFF: // G_SETCIMG
if (w1 != 0 && w1 <= 0xFFFFFFFF && (w1 >> 24) != 0) {
w1 |= 1;
}
break;
default:
break;
}
AppendValue<uintptr_t>(out, w0);
AppendValue<uintptr_t>(out, w1);
}
// Append gSPEndDisplayList sentinel. Some BK models don't include
// the terminator in dlCount (the N64 game knew chunk sizes from the geo
// layout and didn't need it). The port's interpreter walks DLs sequentially
// until gSPEndDisplayList, so we must provide one.
uintptr_t endW0 = (uintptr_t)0xB8 << 24; // F3DEX G_ENDDL
uintptr_t endW1 = 0;
AppendValue<uintptr_t>(out, endW0);
AppendValue<uintptr_t>(out, endW1);
}
// Vertex section
if (hasVtx) {
PadTo8(out);
hdr()->vtx_list_offset = static_cast<int32_t>(out.size());
auto vtxRes = std::static_pointer_cast<Fast::Vertex>(resourceMgr->LoadResourceProcess(initData->Path + "_VTX"));
// Fast::Vertex resource already holds properly-sized Vtx structs
// (with GBI_FLOATS, sizeof(Vtx) = 24, not the N64's 16 bytes).
if (vtxRes) {
const size_t resBytes = vtxRes->GetPointerSize();
const uint16_t resCount = static_cast<uint16_t>(resBytes / sizeof(Vtx));
if (resCount != vtxCount) {
SPDLOG_TRACE("[BKModel] {} VTX count mismatch: model o2r says {} but _VTX resource has {} vertices "
"({} bytes, sizeof(Vtx)={}) — using resource-derived count.",
initData->Path, vtxCount, resCount, resBytes, sizeof(Vtx));
vtxCount = resCount;
}
}
// BKVertexList header — must match sizeof(BKVertexList) so vtx_18[] starts
// at the correct offset. sizeof(BKVertexList) includes trailing padding for
// Vtx alignment (8 bytes due to long long in Vtx union).
AppendValue<int16_t>(out, vtxMin[0]);
AppendValue<int16_t>(out, vtxMin[1]);
AppendValue<int16_t>(out, vtxMin[2]);
AppendValue<int16_t>(out, vtxMax[0]);
AppendValue<int16_t>(out, vtxMax[1]);
AppendValue<int16_t>(out, vtxMax[2]);
AppendValue<int16_t>(out, vtxCenter[0]);
AppendValue<int16_t>(out, vtxCenter[1]);
AppendValue<int16_t>(out, vtxCenter[2]);
AppendValue<int16_t>(out, vtxLocalNorm);
AppendValue<uint16_t>(out, vtxCount);
AppendValue<int16_t>(out, vtxGlobalNorm);
// Pad header to sizeof(BKVertexList) so vtx_18[] aligns correctly.
// BKVertexList has 24 bytes of fields but sizeof may be larger due to
// flexible array member alignment (Vtx requires 8-byte alignment).
const size_t headerFieldBytes = 24; // 12 s16 fields = 24 bytes
const size_t headerSize = sizeof(BKVertexList);
if (headerSize > headerFieldBytes) {
for (size_t p = 0; p < headerSize - headerFieldBytes; p++)
out.push_back(0);
}
if (vtxRes) {
// Fast::Vertex stores Vtx structs (with GBI_FLOATS, ob[] are
// floats, sizeof(Vtx) = 24 not N64's 16). Copy at native stride so
// the interpreter can index as F3DVtx* (same layout).
const Vtx* vtxArray = vtxRes->GetPointer();
const size_t vtxBytes = static_cast<size_t>(vtxCount) * sizeof(Vtx);
AppendBytes(out, vtxArray, vtxBytes);
} else {
SPDLOG_WARN("[BKModel] _VTX not found: {}", initData->Path);
}
}
auto blob = MakeBlob(initData, std::move(out));
if (hasAnimTex && texturesAt != 0) {
BkAnimTextureAlt::Register(blob->Data.data() + texturesAt, initData->Path, animSlots);
}
CALL_EVENT(OnModelBinBuilt, initData->Path.c_str(), blob->Data.data());
return blob;
}
} // namespace Factories