-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConverter.cpp
More file actions
1093 lines (867 loc) · 45.2 KB
/
Converter.cpp
File metadata and controls
1093 lines (867 loc) · 45.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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "include/ShapeLoaderAPI.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <memory>
#include <iomanip>
#include <filesystem>
#include <string>
#include <cstring>
#include <map>
#include <array>
#include <cmath>
#include <set>
#include <algorithm>
using namespace ShapeLoader;
struct Triangle {
int v1, v2, v3;
Triangle(int a, int b, int c) : v1(a), v2(b), v3(c) {}
};
class Converter {
private:
std::ofstream objFile;
std::ofstream mtlFile;
std::string baseName;
std::string materialName;
struct ChunkInfo {
std::string name;
size_t position;
size_t size;
};
public:
Converter(const std::string& outputPath) {
baseName = outputPath;
if (baseName.length() >= 4) {
std::string extension = baseName.substr(baseName.length() - 4);
std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
if (extension == ".obj") {
baseName = baseName.substr(0, baseName.length() - 4);
}
}
materialName = std::filesystem::path(baseName).filename().string();
std::transform(materialName.begin(), materialName.end(), materialName.begin(), [](char c) {
return (c == '.' || c == '-' || c == ' ') ? '_' : c;
});
objFile.open(baseName + ".obj", std::ios::out | std::ios::trunc);
if (!objFile.is_open()) {
throw std::runtime_error("Cannot create OBJ file: " + baseName + ".obj");
}
mtlFile.open(baseName + ".mtl", std::ios::out | std::ios::trunc);
if (!mtlFile.is_open()) {
throw std::runtime_error("Cannot create MTL file: " + baseName + ".mtl");
}
WriteHeaders();
}
~Converter() {
if (objFile.is_open()) objFile.close();
if (mtlFile.is_open()) mtlFile.close();
}
void WriteHeaders() {
std::string mtlName = std::filesystem::path(baseName).filename().string() + ".mtl";
objFile << "# 3GM to OBJ Converter" << std::endl;
objFile << "# Generated: " << __DATE__ << " " << __TIME__ << std::endl;
objFile << "mtllib " << mtlName << std::endl;
objFile << std::endl;
mtlFile << "# Material file for 3GM" << std::endl;
mtlFile << "newmtl " << materialName << std::endl;
mtlFile << "Ka 0.3 0.3 0.4" << std::endl;
mtlFile << "Kd 0.7 0.8 0.9" << std::endl;
mtlFile << "Ks 0.2 0.2 0.3" << std::endl;
mtlFile << "Ns 50.0" << std::endl;
mtlFile << "d 1.0" << std::endl;
mtlFile << std::endl;
}
bool ConvertFrom3GM(const std::vector<uint8_t>& data, const std::string& shapeName) {
std::cout << "\n=== 3GM to OBJ Conversion ===" << std::endl;
std::cout << "Input file size: " << data.size() << " bytes" << std::endl;
std::map<std::string, ChunkInfo> chunks;
if (!FindAllChunks(data, chunks)) {
std::cerr << "ERROR: Could not find valid chunks in 3GM file" << std::endl;
return false;
}
std::vector<VertexData> vertices;
int totalVertices = ParseAllVertexChunks(data, chunks, vertices);
if (totalVertices == 0) {
std::cerr << "ERROR: No vertices found in any chunk" << std::endl;
return false;
}
std::vector<Triangle> faces;
// Handle Line chunks with original surface creation system
if (chunks.find("Line") != chunks.end()) {
ParseLineChunkWithSurfaceSystem(data, chunks, faces, vertices);
} else {
// Fallback to Prim chunks
ParsePrimChunk(data, chunks, faces, vertices.size());
}
int totalFaces = faces.size();
objFile << "# Total vertices: " << vertices.size() << std::endl;
objFile << "# Total faces: " << faces.size() << std::endl;
objFile << std::endl;
objFile << "o " << shapeName << std::endl;
objFile << "usemtl " << materialName << std::endl;
objFile << std::endl;
for (const auto& v : vertices) {
objFile << "v " << std::fixed << std::setprecision(6)
<< v.x << " " << v.y << " " << v.z << std::endl;
}
objFile << std::endl;
for (const auto& v : vertices) {
objFile << "vt " << std::fixed << std::setprecision(6)
<< v.u << " " << v.v << std::endl;
}
objFile << std::endl;
for (const auto& v : vertices) {
objFile << "vn " << std::fixed << std::setprecision(6)
<< v.nx << " " << v.ny << " " << v.nz << std::endl;
}
objFile << std::endl;
for (const auto& face : faces) {
objFile << "f " << (face.v1 + 1) << "/" << (face.v1 + 1)
<< " " << (face.v2 + 1) << "/" << (face.v2 + 1)
<< " " << (face.v3 + 1) << "/" << (face.v3 + 1) << std::endl;
}
std::cout << "\n✓ Conversion completed!" << std::endl;
std::cout << " - Vertices: " << vertices.size() << std::endl;
std::cout << " - Faces: " << faces.size() << std::endl;
std::cout << " - Output: " << baseName << ".obj" << std::endl;
return true;
}
void ConvertPackedVerticesUsingCppFunction(uint32_t* packedData, uint32_t vertexCount, std::vector<VertexData>& vertices) {
size_t outputSize = vertexCount * 8 + 1;
std::vector<float> floatBuffer(outputSize);
uint32_t result = Conversion::ConvertPackedToFloatVertices3Component(packedData, floatBuffer.data(), vertexCount);
for (uint32_t i = 0; i < vertexCount; i++) {
VertexData vertex = {};
vertex.x = floatBuffer[i * 8 + 0];
vertex.y = floatBuffer[i * 8 + 1];
vertex.z = floatBuffer[i * 8 + 2];
bool isValid = true;
if (std::isnan(vertex.x) || std::isnan(vertex.y) || std::isnan(vertex.z)) {
isValid = false;
}
if (std::isinf(vertex.x) || std::isinf(vertex.y) || std::isinf(vertex.z)) {
isValid = false;
}
if (abs(vertex.x) > 100000.0f || abs(vertex.y) > 100000.0f || abs(vertex.z) > 100000.0f) {
isValid = false;
}
if (!isValid) {
if (i < 5) {
std::cout << " SKIPPING Invalid C++ vertex " << i << ": (" << vertex.x << ", " << vertex.y << ", " << vertex.z << ")" << std::endl;
}
continue;
}
vertex.u = (vertex.x + 25.0f) / 50.0f;
vertex.v = (vertex.y + 25.0f) / 50.0f;
float norm = sqrt(vertex.x * vertex.x + vertex.y * vertex.y + vertex.z * vertex.z);
if (norm > 0.001f) {
vertex.nx = vertex.x / norm;
vertex.ny = vertex.y / norm;
vertex.nz = vertex.z / norm;
} else {
vertex.nx = 0.0f;
vertex.ny = 1.0f;
vertex.nz = 0.0f;
}
vertex.color = 0xFFFFFFFF;
vertices.push_back(vertex);
}
}
private:
bool FindAllChunks(const std::vector<uint8_t>& data, std::map<std::string, ChunkInfo>& chunks) {
std::cout << "\nSearching for chunks..." << std::endl;
// Check for standard "3DGM" magic number, but don't require it
if (data.size() >= 4 && memcmp(data.data(), "3DGM", 4) == 0) {
std::cout << "✓ Valid 3DGM magic number found" << std::endl;
} else {
std::cout << "ℹ No 3DGM header found - checking for level file format" << std::endl;
}
std::vector<std::string> knownChunks = {
"3DGM", "FDot", "Dot2", "Dots", "cDot", "Prim", "Line",
"Pos ", "fPos", "Grp2", "Atr2", "TxNm", "SmGr", "End "
};
for (size_t pos = 0; pos <= data.size() - 4; pos++) {
std::string chunkName(reinterpret_cast<const char*>(&data[pos]), 4);
for (const auto& known : knownChunks) {
if (chunkName == known) {
ChunkInfo chunk;
chunk.name = chunkName;
chunk.position = pos;
chunk.size = data.size() - pos;
for (size_t nextPos = pos + 4; nextPos <= data.size() - 4; nextPos++) {
std::string nextChunk(reinterpret_cast<const char*>(&data[nextPos]), 4);
for (const auto& knownNext : knownChunks) {
if (nextChunk == knownNext && nextPos > pos + 4) {
chunk.size = nextPos - pos;
break;
}
}
if (chunk.size != data.size() - pos) break;
}
chunks[chunkName] = chunk;
std::cout << "Found chunk: '" << chunkName << "' at position " << pos
<< ", size: " << chunk.size << " bytes" << std::endl;
break;
}
}
}
std::cout << "Total chunks found: " << chunks.size() << std::endl;
return !chunks.empty();
}
int ParseAllVertexChunks(const std::vector<uint8_t>& data, const std::map<std::string, ChunkInfo>& chunks, std::vector<VertexData>& vertices) {
std::cout << "\nParsing vertex chunks..." << std::endl;
int totalVertices = 0;
if (chunks.find("Dot2") != chunks.end()) {
totalVertices += ParseDot2Chunk(data, chunks.at("Dot2"), vertices);
}
if (chunks.find("FDot") != chunks.end()) {
totalVertices += ParseFDotChunk(data, chunks.at("FDot"), vertices);
}
if (chunks.find("Dots") != chunks.end()) {
totalVertices += ParseDotsChunk(data, chunks.at("Dots"), vertices);
}
if (chunks.find("cDot") != chunks.end()) {
totalVertices += ParseCDotChunk(data, chunks.at("cDot"), vertices);
}
std::cout << "Total vertices parsed: " << totalVertices << std::endl;
return totalVertices;
}
int ParseDot2Chunk(const std::vector<uint8_t>& data, const ChunkInfo& chunk, std::vector<VertexData>& vertices) {
std::cout << "Parsing Dot2 chunk at position " << chunk.position << std::endl;
size_t pos = chunk.position + 4;
if (pos + 4 > data.size()) {
std::cout << "ERROR: Not enough data for Dot2 size header" << std::endl;
return 0;
}
uint32_t dataSize = (data[pos] << 24) | (data[pos + 1] << 16) | (data[pos + 2] << 8) | data[pos + 3];
pos += 4;
std::cout << "Dot2 data size: " << dataSize << " bytes" << std::endl;
// Korrekte Berechnung wie im Original:
// vertexCount = ((chunk.size / 4) - 1) / 3
size_t chunkSize = chunk.size;
uint32_t vertexCount = 0;
if (chunkSize >= 4) {
vertexCount = static_cast<uint32_t>(((chunkSize / 4) - 1) / 3);
}
std::cout << "Calculated vertex count (Dot2-Original): " << vertexCount << std::endl;
if (pos + (vertexCount * 12) > data.size()) {
std::cout << "ERROR: Not enough data for packed vertices" << std::endl;
return 0;
}
// Dot2 verwendet convertPackedToFloatVertices (nicht _3Component)
// Die eigentliche Konvertierung ist hier nur symbolisch, da die Originalfunktion nicht vorliegt.
// Wir nehmen die 3 DWORDs pro Vertex und interpretieren sie als float (wie bisher).
for (uint32_t i = 0; i < vertexCount; i++) {
VertexData vertex = {};
size_t vertexPos = pos + (i * 12);
uint32_t packedX = (data[vertexPos + 0] << 24) | (data[vertexPos + 1] << 16) | (data[vertexPos + 2] << 8) | data[vertexPos + 3];
uint32_t packedY = (data[vertexPos + 4] << 24) | (data[vertexPos + 5] << 16) | (data[vertexPos + 6] << 8) | data[vertexPos + 7];
uint32_t packedZ = (data[vertexPos + 8] << 24) | (data[vertexPos + 9] << 16) | (data[vertexPos + 10] << 8) | data[vertexPos + 11];
// Convert packed integers to floats with proper scaling
int32_t signedX = static_cast<int32_t>(packedX);
int32_t signedY = static_cast<int32_t>(packedY);
int32_t signedZ = static_cast<int32_t>(packedZ);
vertex.x = static_cast<float>(signedX) / 10.0f;
vertex.y = static_cast<float>(signedY) / 10.0f;
vertex.z = static_cast<float>(signedZ) / 10.0f;
bool isValid = true;
if (std::isnan(vertex.x) || std::isnan(vertex.y) || std::isnan(vertex.z)) {
isValid = false;
std::cout << "WARNING: Vertex " << i << " has NaN coordinates" << std::endl;
}
if (std::isinf(vertex.x) || std::isinf(vertex.y) || std::isinf(vertex.z)) {
isValid = false;
std::cout << "WARNING: Vertex " << i << " has infinite coordinates" << std::endl;
}
if (!isValid) {
vertex.x = 0.0f;
vertex.y = 0.0f;
vertex.z = 0.0f;
std::cout << "INFO: Invalid vertex " << i << " replaced with (0,0,0)" << std::endl;
}
vertex.u = (vertex.x + 25.0f) / 50.0f;
vertex.v = (vertex.y + 25.0f) / 50.0f;
float norm = sqrt(vertex.x * vertex.x + vertex.y * vertex.y + vertex.z * vertex.z);
if (norm > 0.001f) {
vertex.nx = vertex.x / norm;
vertex.ny = vertex.y / norm;
vertex.nz = vertex.z / norm;
} else {
vertex.nx = 0.0f;
vertex.ny = 1.0f;
vertex.nz = 0.0f;
}
vertex.color = 0xFFFFFFFF;
vertices.push_back(vertex);
}
return vertexCount;
}
int ParseFDotChunk(const std::vector<uint8_t>& data, const ChunkInfo& chunk, std::vector<VertexData>& vertices) {
std::cout << "Parsing FDot chunk at position " << chunk.position << std::endl;
size_t pos = chunk.position + 4; // Skip "FDot" header
if (pos + 4 > data.size()) {
std::cout << "ERROR: Not enough data for FDot size header" << std::endl;
return 0;
}
// Read data size (big-endian)
uint32_t dataSize = (data[pos] << 24) | (data[pos + 1] << 16) | (data[pos + 2] << 8) | data[pos + 3];
pos += 4;
std::cout << "FDot data size: " << dataSize << " bytes" << std::endl;
// FDot contains 32-bit float vertices (3 floats per vertex = 12 bytes per vertex)
if (dataSize < 4) {
std::cout << "ERROR: FDot data too small" << std::endl;
return 0;
}
// Calculate vertex count (subtract 4 for size header, divide by 12 for xyz floats)
uint32_t vertexCount = (dataSize - 4) / 12;
std::cout << "Calculated vertex count: " << vertexCount << std::endl;
if (pos + (vertexCount * 12) > data.size()) {
std::cout << "ERROR: Not enough data for FDot vertices" << std::endl;
return 0;
}
// Parse vertices as 32-bit floats
for (uint32_t i = 0; i < vertexCount; i++) {
if (pos + 12 > data.size()) break;
// Read coordinates as big-endian 32-bit floats
uint32_t xBits = (data[pos] << 24) | (data[pos + 1] << 16) | (data[pos + 2] << 8) | data[pos + 3];
uint32_t yBits = (data[pos + 4] << 24) | (data[pos + 5] << 16) | (data[pos + 6] << 8) | data[pos + 7];
uint32_t zBits = (data[pos + 8] << 24) | (data[pos + 9] << 16) | (data[pos + 10] << 8) | data[pos + 11];
VertexData vertex = {};
vertex.x = *reinterpret_cast<float*>(&xBits);
vertex.y = *reinterpret_cast<float*>(&yBits);
vertex.z = *reinterpret_cast<float*>(&zBits);
// Validate coordinates
bool isValid = true;
if (std::isnan(vertex.x) || std::isnan(vertex.y) || std::isnan(vertex.z)) {
std::cout << "WARNING: Invalid coordinates (NaN) at vertex " << i << std::endl;
isValid = false;
}
if (std::abs(vertex.x) > 1000000.0f || std::abs(vertex.y) > 1000000.0f || std::abs(vertex.z) > 1000000.0f) {
std::cout << "WARNING: Extreme coordinates at vertex " << i << ": ("
<< vertex.x << ", " << vertex.y << ", " << vertex.z << ")" << std::endl;
isValid = false;
}
if (isValid) {
// Set default values for other attributes
vertex.u = 0.0f;
vertex.v = 0.0f;
// Calculate normal (default pointing up)
float norm = std::sqrt(vertex.x * vertex.x + vertex.y * vertex.y + vertex.z * vertex.z);
if (norm > 0.0001f) {
vertex.nx = vertex.x / norm;
vertex.ny = vertex.y / norm;
vertex.nz = vertex.z / norm;
} else {
vertex.nx = 0.0f;
vertex.ny = 1.0f;
vertex.nz = 0.0f;
}
vertex.color = 0xFFFFFFFF;
vertices.push_back(vertex);
std::cout << "Added FDot vertex " << vertices.size() << ": ("
<< vertex.x << ", " << vertex.y << ", " << vertex.z << ")" << std::endl;
}
pos += 12;
}
std::cout << "Successfully parsed " << vertices.size() << " FDot vertices" << std::endl;
return static_cast<int>(vertices.size());
}
int ParseDotsChunk(const std::vector<uint8_t>& data, const ChunkInfo& chunk, std::vector<VertexData>& vertices) {
std::cout << "Parsing Dots chunk at position " << chunk.position << std::endl;
size_t pos = chunk.position + 4; // Skip "Dots" header
if (pos + 4 > data.size()) {
std::cout << "ERROR: Not enough data for Dots size header" << std::endl;
return 0;
}
// Calculate data size
size_t nextChunkPos = data.size();
size_t dotsDataSize = nextChunkPos - pos - 4;
pos += 4; // Skip size header
size_t remainingData = dotsDataSize - 4;
// Parse as 32-bit floats (3 per vertex = 12 bytes per vertex)
uint32_t vertexCount = remainingData / 12;
std::cout << "Using 32-bit float format: " << vertexCount << " vertices" << std::endl;
for (uint32_t i = 0; i < vertexCount; i++) {
if (pos + 12 > data.size()) break;
VertexData vertex = {};
// Read 3 x 32-bit floats (try big-endian first, like Python parser)
uint32_t xData = (data[pos] << 24) | (data[pos + 1] << 16) | (data[pos + 2] << 8) | data[pos + 3];
uint32_t yData = (data[pos + 4] << 24) | (data[pos + 5] << 16) | (data[pos + 6] << 8) | data[pos + 7];
uint32_t zData = (data[pos + 8] << 24) | (data[pos + 9] << 16) | (data[pos + 10] << 8) | data[pos + 11];
pos += 12;
vertex.x = *reinterpret_cast<float*>(&xData);
vertex.y = *reinterpret_cast<float*>(&yData);
vertex.z = *reinterpret_cast<float*>(&zData);
// Validate coordinates
if (abs(vertex.x) < 10000 && abs(vertex.y) < 10000 && abs(vertex.z) < 10000) {
// Generate texture coordinates and normals
vertex.u = (vertex.x + 25.0f) / 50.0f;
vertex.v = (vertex.y + 25.0f) / 50.0f;
float norm = sqrt(vertex.x * vertex.x + vertex.y * vertex.y + vertex.z * vertex.z);
if (norm > 0.001f) {
vertex.nx = vertex.x / norm;
vertex.ny = vertex.y / norm;
vertex.nz = vertex.z / norm;
} else {
vertex.nx = 0.0f;
vertex.ny = 1.0f;
vertex.nz = 0.0f;
}
vertex.color = 0xFFFFFFFF;
vertices.push_back(vertex);
}
}
return vertices.size();
}
int ParseCDotChunk(const std::vector<uint8_t>& data, const ChunkInfo& chunk, std::vector<VertexData>& vertices) {
size_t pos = chunk.position + 4; // Skip "cDot" header
if (pos + 8 > data.size()) {
std::cout << "ERROR: cDot data too small" << std::endl;
return 0;
}
// Calculate data size
size_t nextChunkPos = data.size();
size_t cdotDataSize = nextChunkPos - pos;
uint32_t countLE = data[pos] | (data[pos + 1] << 8) | (data[pos + 2] << 16) | (data[pos + 3] << 24);
uint32_t countBE = (data[pos] << 24) | (data[pos + 1] << 16) | (data[pos + 2] << 8) | data[pos + 3];
uint32_t vertexCount;
if (countBE < 100000 && countBE > 0) {
vertexCount = countBE;
} else if (countLE < 100000 && countLE > 0) {
vertexCount = countLE;
} else {
size_t remainingData = cdotDataSize - 4;
vertexCount = remainingData / 6;
}
pos += 4;
for (uint32_t i = 0; i < vertexCount; i++) {
if (pos + 6 > chunk.position + 4 + cdotDataSize) break;
VertexData vertex = {};
int16_t xComp = static_cast<int16_t>(data[pos] | (data[pos + 1] << 8));
int16_t yComp = static_cast<int16_t>(data[pos + 2] | (data[pos + 3] << 8));
int16_t zComp = static_cast<int16_t>(data[pos + 4] | (data[pos + 5] << 8));
pos += 6;
vertex.x = (xComp != -1) ? static_cast<float>(xComp) / 100.0f : 0.0f;
vertex.y = (yComp != -1) ? static_cast<float>(yComp) / 100.0f : 0.0f;
vertex.z = (zComp != -1) ? static_cast<float>(zComp) / 100.0f : 0.0f;
vertex.u = (vertex.x + 25.0f) / 50.0f;
vertex.v = (vertex.y + 25.0f) / 50.0f;
float norm = sqrt(vertex.x * vertex.x + vertex.y * vertex.y + vertex.z * vertex.z);
if (norm > 0.001f) {
vertex.nx = vertex.x / norm;
vertex.ny = vertex.y / norm;
vertex.nz = vertex.z / norm;
} else {
vertex.nx = 0.0f;
vertex.ny = 1.0f;
vertex.nz = 0.0f;
}
vertex.color = 0xFFFFFFFF;
vertices.push_back(vertex);
}
return vertices.size();
}
// Original Surface System from working Converter_Surface_Test.cpp - RESTORED
void ParseLineChunkWithSurfaceSystem(const std::vector<uint8_t>& data, const std::map<std::string, ChunkInfo>& chunks,
std::vector<Triangle>& faces, const std::vector<VertexData>& vertices) {
auto lineIt = chunks.find("Line");
if (lineIt == chunks.end()) return;
const ChunkInfo& lineChunk = lineIt->second;
size_t pos = lineChunk.position + 8;
size_t endPos = pos + lineChunk.size - 8;
std::cout << "Parsing Line chunk with original surface system" << std::endl;
int debugCount = 0;
while (pos < endPos - 2 && debugCount < 5) { // Limit debug output
uint16_t chunkType = (data[pos] << 8) | data[pos + 1];
pos += 2;
if (chunkType == 0x6000) break;
uint8_t chunkSize = chunkType & 0xFF;
std::vector<uint16_t> surfaceParams;
for (int i = 0; i < chunkSize && pos < endPos - 2; i++) {
uint16_t param = (data[pos] << 8) | data[pos + 1];
pos += 2;
if (param == 0x7000) break;
surfaceParams.push_back(param);
}
// Debug output to understand the parameters
if (debugCount < 3) {
std::cout << "ChunkType: 0x" << std::hex << chunkType << std::dec
<< ", Size: " << static_cast<int>(chunkSize)
<< ", Params: ";
for (size_t i = 0; i < surfaceParams.size() && i < 6; ++i) {
std::cout << surfaceParams[i] << " ";
}
std::cout << std::endl;
}
// NEW: Process as primitive geometry data instead of surface parameters
ProcessPrimitiveGeometry(surfaceParams, chunkType, faces, vertices.size());
debugCount++;
}
// Continue without debug output
while (pos < endPos - 2) {
uint16_t chunkType = (data[pos] << 8) | data[pos + 1];
pos += 2;
if (chunkType == 0x6000) break;
uint8_t chunkSize = chunkType & 0xFF;
std::vector<uint16_t> surfaceParams;
for (int i = 0; i < chunkSize && pos < endPos - 2; i++) {
uint16_t param = (data[pos] << 8) | data[pos + 1];
pos += 2;
if (param == 0x7000) break;
surfaceParams.push_back(param);
}
// NEW: Process as primitive geometry data instead of surface parameters
ProcessPrimitiveGeometry(surfaceParams, chunkType, faces, vertices.size());
}
std::cout << "Generated " << faces.size() << " faces from Line chunk (corrected primitive system)" << std::endl;
}
void ProcessPrimitiveGeometry(const std::vector<uint16_t>& geometryData, uint16_t primitiveType,
std::vector<Triangle>& faces, size_t vertexCount) {
if (geometryData.empty() || vertexCount == 0) return;
std::cout << "Processing primitive type 0x" << std::hex << primitiveType << std::dec
<< " with " << geometryData.size() << " geometry elements" << std::endl;
// Handle different primitive types based on RFC specification
switch (primitiveType) {
case 0x470E: // 18190 - Quad Processed (from RFC: 18189->18190 conversion)
ProcessQuadPrimitive(geometryData, faces, vertexCount);
break;
case 0x6F2B: // 28427 - Line Strip (from RFC: special line primitive)
case 0x470D: // 18189 - Original Quad Input
ProcessQuadPrimitive(geometryData, faces, vertexCount);
break;
case 0x1: // Simple geometry element
ProcessSimpleElement(geometryData, faces, vertexCount);
break;
default:
// Fallback: treat as indexed triangle/quad data
ProcessIndexedPrimitive(geometryData, faces, vertexCount);
break;
}
}
void ProcessQuadPrimitive(const std::vector<uint16_t>& indices, std::vector<Triangle>& faces, size_t vertexCount) {
std::cout << " Processing as Quad primitive with indices: ";
for (size_t i = 0; i < std::min((size_t)6, indices.size()); i++) {
std::cout << indices[i] << " ";
}
std::cout << std::endl;
if (indices.size() >= 4) {
// Take first 4 elements as vertex indices for a quad
uint16_t v0 = indices[0] % vertexCount;
uint16_t v1 = indices[1] % vertexCount;
uint16_t v2 = indices[2] % vertexCount;
uint16_t v3 = indices[3] % vertexCount;
// Create quad as two triangles with consistent winding
if (v0 != v1 && v1 != v2 && v2 != v3 && v0 != v3) {
faces.push_back(Triangle(v0, v2, v1)); // First triangle
faces.push_back(Triangle(v0, v3, v2)); // Second triangle
std::cout << " Created quad: (" << v0 << "," << v1 << "," << v2 << "," << v3 << ") -> "
<< "Triangle(" << v0 << "," << v2 << "," << v1 << ") + "
<< "Triangle(" << v0 << "," << v3 << "," << v2 << ")" << std::endl;
}
}
// Handle additional geometry data if present
if (indices.size() > 4) {
ProcessIndexedPrimitive(std::vector<uint16_t>(indices.begin() + 4, indices.end()), faces, vertexCount);
}
}
void ProcessSimpleElement(const std::vector<uint16_t>& elements, std::vector<Triangle>& faces, size_t vertexCount) {
std::cout << " Processing simple element with " << elements.size() << " parameters" << std::endl;
// Simple elements might be material/texture parameters, not geometry
// For now, just log them without creating faces
for (size_t i = 0; i < elements.size(); i++) {
std::cout << " Element[" << i << "] = " << elements[i] << std::endl;
}
}
void ProcessIndexedPrimitive(const std::vector<uint16_t>& indices, std::vector<Triangle>& faces, size_t vertexCount) {
std::cout << " Processing indexed primitive with " << indices.size() << " indices" << std::endl;
// Create triangles from index data
for (size_t i = 0; i + 2 < indices.size(); i += 3) {
uint16_t v0 = indices[i] % vertexCount;
uint16_t v1 = indices[i + 1] % vertexCount;
uint16_t v2 = indices[i + 2] % vertexCount;
if (v0 != v1 && v1 != v2 && v0 != v2) {
faces.push_back(Triangle(v0, v2, v1)); // Consistent winding
}
}
}
void CreateBoxFaces(std::vector<Triangle>& faces, size_t vertexCount) {
if (vertexCount != 16) return;
// Create a proper closed box with correct winding order (counter-clockwise for outward normals)
// Group vertices by their approximate positions
// Front face (Z ≈ 12.2) - vertices 13,14,15,16 (12,13,14,15 in 0-indexed)
faces.push_back(Triangle(12, 13, 15)); // Top-left, Top-right, Bottom-right
faces.push_back(Triangle(12, 15, 14)); // Top-left, Bottom-right, Bottom-left
// Back face (Z ≈ 0-4) - vertices with lowest Z
faces.push_back(Triangle(1, 10, 4)); // Counter-clockwise from outside
faces.push_back(Triangle(1, 7, 10)); // Complete the quad
// Top face (Y ≈ 12.2 or high Y)
faces.push_back(Triangle(0, 5, 13)); // Counter-clockwise from above
faces.push_back(Triangle(0, 13, 12)); // Complete the quad
// Bottom face (Y ≈ -12.2 or low Y)
faces.push_back(Triangle(6, 11, 15)); // Counter-clockwise from below
faces.push_back(Triangle(6, 15, 14)); // Complete the quad
// Right side (X ≈ 6-12)
faces.push_back(Triangle(4, 14, 7)); // Counter-clockwise from right
faces.push_back(Triangle(4, 12, 14)); // Complete the quad
// Left side (X ≈ -6 to -12)
faces.push_back(Triangle(2, 9, 15)); // Counter-clockwise from left
faces.push_back(Triangle(2, 15, 13)); // Complete the quad
// Additional faces to close gaps and create solid box
faces.push_back(Triangle(0, 2, 5)); // Connect corners
faces.push_back(Triangle(5, 4, 8)); // Connect edges
faces.push_back(Triangle(8, 6, 11)); // Bottom connections
faces.push_back(Triangle(9, 10, 11)); // Back bottom edge
}
void CreateConvexHullFaces(std::vector<Triangle>& faces, size_t vertexCount) {
// Create a simple convex hull approximation
size_t half = vertexCount / 2;
// Bottom half triangle fan
for (size_t i = 1; i < half - 1; ++i) {
faces.push_back(Triangle(0, static_cast<int>(i), static_cast<int>(i + 1)));
}
// Top half triangle fan
for (size_t i = half + 1; i < vertexCount - 1; ++i) {
faces.push_back(Triangle(static_cast<int>(half), static_cast<int>(i), static_cast<int>(i + 1)));
}
// Connect bottom to top
faces.push_back(Triangle(0, static_cast<int>(half), static_cast<int>(half - 1)));
faces.push_back(Triangle(static_cast<int>(half - 1), static_cast<int>(half), static_cast<int>(vertexCount - 1)));
}
void CreateSurfacesFromParameters(const std::vector<uint16_t>& surfaceParams, uint16_t chunkType,
std::vector<Triangle>& faces, const std::vector<VertexData>& vertices) {
if (surfaceParams.size() < 3) return;
for (size_t i = 0; i + 2 < surfaceParams.size(); i += 3) {
uint16_t param1 = surfaceParams[i];
uint16_t param2 = surfaceParams[i + 1];
uint16_t param3 = surfaceParams[i + 2];
// More refined parameter filtering to improve surface quality
if (param1 == 0x0E47 || param1 == 0x70 || param1 > 50000 ||
param2 == 0x0E47 || param2 == 0x70 || param2 > 50000 ||
param3 == 0x0E47 || param3 == 0x70 || param3 > 50000) continue;
// Skip degenerate parameter combinations
if (param1 == param2 || param2 == param3 || param1 == param3) continue;
CreateSurfaceFromParameters(param1, param2, param3, chunkType, faces, vertices);
}
}
void CreateSurfaceFromParameters(uint16_t param1, uint16_t param2, uint16_t param3, uint16_t chunkType,
std::vector<Triangle>& faces, const std::vector<VertexData>& vertices) {
if (vertices.size() < 3) return;
size_t localVertexCount = vertices.size();
// CRITICAL FIX: Large parameters need to be scaled down to fit vertex count
if (param1 >= localVertexCount) param1 = param1 % localVertexCount;
if (param2 >= localVertexCount) param2 = param2 % localVertexCount;
if (param3 >= localVertexCount) param3 = param3 % localVertexCount;
// Always use small param logic with corrected parameters
CreateFacesFromSmallParams(param1, param2, param3, localVertexCount, faces);
}
void CreateFacesFromSmallParams(uint16_t p1, uint16_t p2, uint16_t p3, size_t localVertexCount, std::vector<Triangle>& faces) {
if (localVertexCount == 0) return;
for (int i = 0; i < 6; i++) {
// Safe modulo to ensure result is < localVertexCount
uint32_t v1 = static_cast<uint32_t>((p1 + i) % localVertexCount);
uint32_t v2 = static_cast<uint32_t>((p2 + i) % localVertexCount);
uint32_t v3 = static_cast<uint32_t>((p3 + i) % localVertexCount);
// Final safety check
if (v1 < localVertexCount && v2 < localVertexCount && v3 < localVertexCount) {
if (v1 != v2 && v2 != v3 && v1 != v3) {
// Ensure consistent counter-clockwise winding for outward normals
faces.push_back(Triangle(static_cast<int>(v1), static_cast<int>(v3), static_cast<int>(v2)));
}
}
}
}
void CreateFacesFromLargeParams(uint16_t p1, uint16_t p2, uint16_t p3, size_t localVertexCount, std::vector<Triangle>& faces) {
if (localVertexCount == 0) return;
uint16_t v1_base = p1 & 0xFF;
uint16_t v2_base = p2 & 0xFF;
uint16_t v3_base = p3 & 0xFF;
for (int i = 0; i < 4; i++) {
// Safe modulo to ensure result is < localVertexCount
uint32_t v1 = static_cast<uint32_t>((v1_base + i) % localVertexCount);
uint32_t v2 = static_cast<uint32_t>((v2_base + i + 1) % localVertexCount);
uint32_t v3 = static_cast<uint32_t>((v3_base + i + 2) % localVertexCount);
// Final safety check
if (v1 < localVertexCount && v2 < localVertexCount && v3 < localVertexCount) {
if (v1 != v2 && v2 != v3 && v1 != v3) {
// Consistent winding and proper indexing (already 0-based, add 1 for OBJ format)
faces.push_back(Triangle(static_cast<int>(v1), static_cast<int>(v3), static_cast<int>(v2)));
}
}
}
}
int ParsePrimChunk(const std::vector<uint8_t>& data, const std::map<std::string, ChunkInfo>& chunks, std::vector<Triangle>& faces, size_t vertexCount) {
if (chunks.find("Prim") == chunks.end()) {
for (size_t i = 0; i + 2 < vertexCount; i += 3) {
faces.push_back(Triangle(static_cast<int>(i), static_cast<int>(i + 1), static_cast<int>(i + 2)));
}
return faces.size();
}
const ChunkInfo& primChunk = chunks.at("Prim");
size_t pos = primChunk.position + 4;
if (pos + 4 > data.size()) {
std::cout << "ERROR: Not enough data for Prim size header" << std::endl;
return 0;
}
uint32_t primSize = (data[pos] << 24) | (data[pos + 1] << 16) | (data[pos + 2] << 8) | data[pos + 3];
pos += 4;
// HEX ANALYSIS FINDINGS:
// Pattern: 0x470E → [data] → END_OF_PRIMITIVE (-1) → [4 vertex indices before -1]
const int32_t END_OF_PRIMITIVE = -1; // 0xFFFFFFFF
const int32_t PRIMITIVE_0x470E = 18190; // 0x470E
std::set<std::string> uniqueFaces; // Avoid duplicates
int primitiveCount = 0;
for (size_t offset = 0; offset + 4 <= primSize; offset += 4) {
int32_t value = (data[pos + offset] << 24) | (data[pos + offset + 1] << 16) | (data[pos + offset + 2] << 8) | data[pos + offset + 3];
if (value == END_OF_PRIMITIVE) {
if (offset >= 16) {
std::vector<int32_t> vertices;
std::cout << " Looking backwards for vertex indices:" << std::endl;
for (int back = 16; back >= 4; back -= 4) {
size_t vertexOffset = offset - back;
int32_t vertexIndex = (data[pos + vertexOffset] << 24) |
(data[pos + vertexOffset + 1] << 16) |
(data[pos + vertexOffset + 2] << 8) |
data[pos + vertexOffset + 3];
if(vertexIndex >= 0 && vertexIndex < static_cast<int32_t>(vertexCount)) {
vertices.push_back(vertexIndex);
}
}
if (vertices.size() == 4) {
int v0 = vertices[0], v1 = vertices[1], v2 = vertices[2], v3 = vertices[3];
if (v0 == v3) {
faces.push_back(Triangle(v0, v1, v2));
std::cout << " Triangle: " << v0 << " " << v1 << " " << v2 << std::endl;
} else {
bool isValidQuad = (v0 != v1 && v0 != v2 && v0 != v3 && v1 != v2 && v1 != v3 && v2 != v3);
if (isValidQuad) {
std::string face1Str = std::to_string(v0) + "/" + std::to_string(v1) + "/" + std::to_string(v2);
std::string face2Str = std::to_string(v0) + "/" + std::to_string(v2) + "/" + std::to_string(v3);
if (uniqueFaces.find(face1Str) == uniqueFaces.end()) {
faces.push_back(Triangle(v0, v1, v2));
uniqueFaces.insert(face1Str);
}
if (uniqueFaces.find(face2Str) == uniqueFaces.end()) {
faces.push_back(Triangle(v0, v2, v3));
uniqueFaces.insert(face2Str);
}
}
}
primitiveCount++;
}
}
}
}
return faces.size();
}
};
// Main function
int main(int argc, char* argv[]) {
std::cout << "🎮 3D Game Machine - 3GM to OBJ Converter v1.0" << std::endl;
std::cout << "===========================================" << std::endl;
// Parameter parsing
std::string inputFile = "";
std::string outputFile = "";
bool verbose = false;
bool showHelp = false;
bool showVersion = false;
std::string format = "obj";
// Parse command line arguments
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "-h" || arg == "--help") {
showHelp = true;
}
else if (arg == "-v" || arg == "--version") {
showVersion = true;
}
else if (arg == "-d" || arg == "--debug") {
verbose = true;
}
else if ((arg == "-o" || arg == "--output") && i + 1 < argc) {
outputFile = argv[++i];
}
else if ((arg == "-f" || arg == "--format") && i + 1 < argc) {
format = argv[++i];
}
else if (arg[0] != '-' && inputFile.empty()) {
inputFile = arg;
}
else {