-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathlwoReader.cpp
executable file
·671 lines (530 loc) · 15.1 KB
/
lwoReader.cpp
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
// LWO File format reader
// By Jean-René Bédard (https://github.com/jrbedard/3d-converter), 2005
#include "StdAfx.h"
#include "lwoReader.h"
// LWO reader Constructor
CLwoReader::CLwoReader(CFile* pFile, const fs::path& fileName):
m_pCurLayer(NULL)
{
CLwoFile* pLwoFile = static_cast<CLwoFile*>(pFile);
OBJ_ASSERT(pLwoFile);
m_pLwoFile = pLwoFile;
m_fileName = fileName;
}
// LWO Global Read
bool CLwoReader::Read()
{
OBJ_ASSERT(m_pLwoFile);
if(!m_pLwoFile)
return false;
std::string fileName(m_fileName.string()); // Extract native file path string
m_ifs.open(fileName.c_str(), ios::binary);
MSG_INFO("Reading .LWO File '" << fileName << "'");
OBJ_ASSERT(m_ifs.is_open());
if(!m_ifs.is_open())
{
MSG_ERROR("Couldn't open .LWO file '" << fileName << "'");
return false;
}
bool bStatus = false;
// Compute the file size
FileSize();
// Read the header of the LWO file
bStatus = ReadHeader();
if(!bStatus)
{
MSG_ERROR("Invalid LWO Header");
m_ifs.close();
return false;
}
// Parse the LWO file chunks
bStatus = ParseChunks();
if(!bStatus)
{
MSG_ERROR("Error parsing LWO chunks");
m_ifs.close();
return false;
}
// End reading
EndReading();
m_ifs.close();
return true;
}
// LWO Read Header
bool CLwoReader::ReadHeader()
{
// FORM chunk
CLwoFile::SChunk formChunk = ReadChunk();
if(formChunk.Flag != CLwoFile::CHUNK_FORM) // FORM check
return false;
m_curChunk = formChunk;
// LWO2 tag
ulong lwo2 = ReadLong();
if(lwo2 != CLwoFile::CHUNK_LWO2) // LWO check
return false;
return true;
}
// LWO Chunks Parsing
bool CLwoReader::ParseChunks()
{
if(!m_ifs.is_open())
return false;
if(m_ifs.eof())
return true;
CLwoFile::SChunk chunk = ReadChunk();
m_curChunk = chunk;
if(chunk.Flag == CLwoFile::CHUNK_TAGS) // TAGS
{
MSG_DEBUG("Chunk TAGS detected");
ReadTagStrings();
ParseChunks();
}
else if(chunk.Flag == CLwoFile::CHUNK_LAYR) // LAYR
{
MSG_DEBUG("Chunk LAYR detected");
ReadLayer();
ParseChunks();
}
else if(chunk.Flag == CLwoFile::CHUNK_PNTS) // PNTS
{
MSG_DEBUG("Chunk PNTS detected");
ReadPoints();
ParseChunks();
}
else if(chunk.Flag == CLwoFile::CHUNK_VMAP) // VMAP
{
MSG_DEBUG("Chunk VMAP detected");
ReadVertexMapping();
ParseChunks();
}
else if(chunk.Flag == CLwoFile::CHUNK_POLS) // POLS
{
MSG_DEBUG("Chunk POLS detected");
ReadPolygons();
ParseChunks();
}
else if(chunk.Flag == CLwoFile::CHUNK_PTAG) // PTAG
{
MSG_DEBUG("Chunk PTAG detected");
ReadPolygonTagMapping();
ParseChunks();
}
else if(chunk.Flag == CLwoFile::CHUNK_VMAD) // VMAD
{
MSG_DEBUG("Chunk VMAD detected");
ReadDiscVertexMapping();
ParseChunks();
}
else if(chunk.Flag == CLwoFile::CHUNK_CLIP) // CLIP
{
MSG_DEBUG("Chunk CLIP detected");
ReadImageDefinition();
ParseChunks();
}
else if(chunk.Flag == CLwoFile::CHUNK_SURF) // SURF
{
MSG_DEBUG("Chunk SURF detected");
ReadSurface();
ParseChunks();
}
else
{
MSG_DEBUG("Unknown Chunk '" << GetTagString(chunk.Flag) << "' at " << m_ifs.tellg() << " offset");
SkipChunk(chunk.Size);
ParseChunks();;
}
return true;
}
// Read LWO Tags
bool CLwoReader::ReadTagStrings()
{
OBJ_ASSERT(m_curChunk.Flag == CLwoFile::CHUNK_TAGS);
while(m_curChunk.Size > 0) // for each tag
{
std::string name;
ReadString(name);
m_curChunk.Size -= GetStringSize(name);
MSG_DEBUG(" tag name: '" << name << "'");
m_tagVector.push_back(name);
}
return true;
}
// Read LWO Layers
bool CLwoReader::ReadLayer()
{
OBJ_ASSERT(m_curChunk.Flag == CLwoFile::CHUNK_LAYR);
ushort layerNumber = ReadShort();
m_curChunk.Size -= 2;
CLwoFile::CLayer layer(layerNumber);
layer.m_flag = ReadShort();
m_curChunk.Size -= 2;
Vector3D pivot = ReadVector3D();
layer.m_pivot = pivot;
m_curChunk.Size -= 3*4;
std::string layerName;
ReadString(layerName);
layer.SetLayerName(layerName);
m_curChunk.Size -= GetStringSize(layerName);
if(m_curChunk.Size > 2)
{
layer.m_parent = ReadShort();
m_curChunk.Size -= 2;
}
MSG_DEBUG("LAYER '" << layer.GetLayerID() << "', Name: '" << layer.GetLayerName() << "'");
SkipChunk(m_curChunk.Size);
// remember current layer for parse methods
m_pCurLayer = m_pLwoFile->AddLayer(layer); // Store current layer
return true;
}
// Read LWO Point List
bool CLwoReader::ReadPoints()
{
OBJ_ASSERT(m_pCurLayer && m_curChunk.Flag == CLwoFile::CHUNK_PNTS);
int vertexCount = m_curChunk.Size / 12;
MSG_DEBUG( vertexCount << " vertices");
while(vertexCount--) // for each vertex position
{
Vector3D vert = ReadVector3D();
m_pCurLayer->GetVertexPositionVector().push_back(vert);
//MSG_DEBUG("X: " << vert[0] << " Y: " << vert[1] << " Z:" << vert[2]);
}
return true;
}
// Read LWO Vertex Mapping
bool CLwoReader::ReadVertexMapping()
{
OBJ_ASSERT(m_pCurLayer && m_curChunk.Flag == CLwoFile::CHUNK_VMAP); // VMAP
ulong type = ReadLong();
m_curChunk.Size -= 4;
ushort dimension = ReadShort();
m_curChunk.Size -= 2;
std::string name;
ReadString(name); // TODO : merge multiple UV sets
m_curChunk.Size -= GetStringSize(name);
MSG_DEBUG("VMAP: '" << GetTypeString(type) << "', name: '" << name << "' dimension: " << dimension);
if(type == CLwoFile::CHUNK_TXUV && dimension == 2) // TXUV
{
ulong uvCount = m_curChunk.Size / 10;
Vector3DVector& vertPos = m_pCurLayer->GetVertexPositionVector();
CLwoFile::CLayer::TexCoordMap& texCoords = m_pCurLayer->GetTexCoordMap();
ushort vertexIndex = 0;
Vector3D uv(3);
while(uvCount--) // For each UV coordinate
{
ushort vertexIndex = ReadShort();
uv = ReadVector2D();
CLwoFile::CLayer::CTexCoord lwoTexCoord;
lwoTexCoord.m_faceIndex = -1; // TODO : what do we do with this
lwoTexCoord.m_texCoord = uv;
m_pCurLayer->GetTexCoordMap()[vertexIndex].push_back(lwoTexCoord); // push CTexCoord at the vertex position index
}
}
else
{
// not recognized yet
MSG_WARNING("VMAP type '" << GetTypeString(type) << "' not recognized" );
SkipChunk(m_curChunk.Size);
}
return true;
}
// Read LWO Discontinuous Vertex Mapping
bool CLwoReader::ReadDiscVertexMapping()
{
OBJ_ASSERT(m_pCurLayer && m_curChunk.Flag == CLwoFile::CHUNK_VMAD);
ulong type = ReadLong();
m_curChunk.Size -= 4;
ushort dimension = ReadShort();
m_curChunk.Size -= 2;
std::string name;
ReadString(name); // TODO : merge multiple UV sets
m_curChunk.Size -= GetStringSize(name);
MSG_DEBUG("VMAD: '" << name << "', type: '" << GetTypeString(type) << "', dimension: " << dimension);
if(type == CLwoFile::CHUNK_TXUV && dimension == 2)
{
int count = m_curChunk.Size / 12;
ushort point_index = 0;
ushort polygon_index = 0;
Vector3D uv(3);
ushort uv_index = 0;
CLwoFile::CLayer::TexCoordMap& texCoords = m_pCurLayer->GetTexCoordMap();
while(count--) // For each UV mapping
{
point_index = ReadShort();
polygon_index = ReadShort();
uv = ReadVector2D();
//MSG_DEBUG("point: " << point_index << ", polygon: " << polygon_index << ", uv: (" << uv[0] << "," << uv[1] << ")");
CLwoFile::CLayer::CTexCoord lwoTexCoord;
lwoTexCoord.m_faceIndex = polygon_index;
lwoTexCoord.m_texCoord = uv;
m_pCurLayer->GetTexCoordMap()[point_index].push_back(lwoTexCoord); // push CTexCoord at the vertex position index
}
}
else
{
// not recognized yet
MSG_WARNING("VMAD type '" << GetTypeString(type) << "' not recognized" );
SkipChunk(m_curChunk.Size);
}
return true;
}
// Read LWO Polygon List
bool CLwoReader::ReadPolygons()
{
OBJ_ASSERT(m_pCurLayer && m_curChunk.Flag == CLwoFile::CHUNK_POLS);
ulong type = ReadLong();
m_curChunk.Size -= 4;
MSG_DEBUG("POLS: '" << GetTypeString(type) << "'");
if(type == CLwoFile::CHUNK_FACE)
{
ushort vertexCount;
while(m_curChunk.Size > 0) // For each face
{
vertexCount = ReadShort() & 0x03FF;
m_curChunk.Size -= 2;
CLwoFile::CLayer::CFace face;
while(vertexCount--) // For each vertex
{
ushort vertexIndex = ReadShort();
face.GetVertexIndexVector().push_back(vertexIndex);
m_curChunk.Size -= 2;
}
m_pCurLayer->AddFace(face);
}
}
else if(type == CLwoFile::CHUNK_PTCH)
{
ushort vertexCount;
while(m_curChunk.Size > 0) // For each face
{
vertexCount = ReadShort() & 0x03FF;
m_curChunk.Size -= 2;
CLwoFile::CLayer::CFace face;
while(vertexCount--) // For each vertex
{
ushort vertexIndex = ReadShort();
face.GetVertexIndexVector().push_back(vertexIndex);
m_curChunk.Size -= 2;
}
m_pCurLayer->AddFace(face);
}
}
else
{
// not recognized
MSG_WARNING("POLS type '" << GetTypeString(type) << "' not recognized" );
SkipChunk(m_curChunk.Size);
}
return true;
}
// Read LWO Polygon Tag Mapping
bool CLwoReader::ReadPolygonTagMapping()
{
OBJ_ASSERT(m_pCurLayer && m_curChunk.Flag == CLwoFile::CHUNK_PTAG);
ulong type = ReadLong();
m_curChunk.Size -= 4;
MSG_DEBUG("PTAG: '" << GetTypeString(type) << "'");
CLwoFile::CLayer::FaceVector& faces = m_pCurLayer->GetFaceVector();
uint faceSize = faces.size();
int ptagCount = m_curChunk.Size / 4;
ushort polygon_index;
// Note: the "&& faces.size()" below are to skip the subchunk if there is no faces in the layer.
// That can be caused by a non-"FACE" "POLS" list. Like for subpatches "PTCH", "MBAL", "BONE" that we skipped.
if(type == CLwoFile::CHUNK_SURF && faces.size()) // Surface Tag
{
ushort surf_index;
while(ptagCount--) // for each polygon tag
{
polygon_index = ReadShort();
surf_index = ReadShort();
OBJ_ASSERT(polygon_index < (int)faces.size());
if(polygon_index < (int)faces.size())
{
faces[polygon_index].m_surface = surf_index;
}
}
}
else if(type == CLwoFile::CHUNK_PART && faces.size()) // Part Tag
{
ushort part_index;
while(ptagCount--) // for each polygon tag
{
polygon_index = ReadShort();
part_index = ReadShort();
OBJ_ASSERT(polygon_index < (int)faces.size());
if(polygon_index < (int)faces.size())
{
faces[polygon_index].m_part = part_index;
}
}
}
else if(type == CLwoFile::CHUNK_SMGP && faces.size()) // Smoothing Group tag
{
ushort smoothingGroup;
while(ptagCount--) // for each polygon tag
{
polygon_index = ReadShort();
smoothingGroup = ReadShort();
OBJ_ASSERT(polygon_index < (int)faces.size());
if(polygon_index < (int)faces.size())
{
faces[polygon_index].m_smoothingGroup = std::make_pair(true, smoothingGroup);
}
}
}
else
{
// not existing or POLS is not of the FACE type
MSG_WARNING("PTAG type '" << GetTypeString(type) << "' not recognized" );
SkipChunk(m_curChunk.Size);
}
return true;
}
// Read LWO Image Definition
bool CLwoReader::ReadImageDefinition()
{
OBJ_ASSERT(m_pCurLayer && m_curChunk.Flag == CLwoFile::CHUNK_CLIP);
ulong index = ReadLong();
m_curChunk.Size -= 4;
MSG_DEBUG("Image index: " << index);
while(m_curChunk.Size > 0)
{
ulong type = ReadLong();
m_curChunk.Size -= 4;
// size of still
ushort current_tag_size = ReadShort();
m_curChunk.Size -= 2;
if(type == CLwoFile::CHUNK_STIL)
{
std::string imagePath;
ReadString(imagePath);
m_curChunk.Size -= GetStringSize(imagePath);
m_pCurLayer->AddImage(index, imagePath);
MSG_DEBUG("\t type: '" << GetTypeString(type) << "', image name: '" << imagePath << "'");
}
else
{
m_curChunk.Size -= SkipChunk(current_tag_size);
MSG_DEBUG("\t type: '" << GetTypeString(type) << "', not processed...");
}
}
return true;
}
// Read LWO Surface
bool CLwoReader::ReadSurface()
{
OBJ_ASSERT(m_curChunk.Flag == CLwoFile::CHUNK_SURF);
std::string surfaceName;
ReadString(surfaceName);
m_curChunk.Size -= GetStringSize(surfaceName);
// Create new surface
CLwoFile::CLayer::CSurface surface;
surface.SetSurfaceName(surfaceName);
std::string source;
ReadString(source);
m_curChunk.Size -= GetStringSize(source);
MSG_DEBUG("SURF: '" << surfaceName << "', " << "source: '" << source << "'");
long current_tag_name;
ushort current_tag_size;
while(m_curChunk.Size > 0 && !m_ifs.eof())
{
current_tag_name = ReadLong();
m_curChunk.Size -= 4;
current_tag_size = ReadShort();
m_curChunk.Size -= 2;
MSG_DEBUG("\ttag: '" << GetTagString(current_tag_name) << "', size: " << current_tag_size);
if(current_tag_name == CLwoFile::CHUNK_BLOK) // BLOK
{
int blok_size = current_tag_size;
m_curChunk.Size -= blok_size;
while(blok_size > 0 && !m_ifs.eof())
{
current_tag_name = ReadLong();
blok_size -= 4;
current_tag_size = ReadShort();
blok_size -= 2;
MSG_DEBUG("\t\ttag: '" << GetTagString(current_tag_name) << "', size: " << current_tag_size);
if(current_tag_name == CLwoFile::CHUNK_IMAG) // BLOK | IMAG
{
surface.m_imageIndex = ReadShort();
MSG_DEBUG("\t\t\timage index: " << surface.m_imageIndex);
blok_size -= 2;
}
else if(current_tag_name == CLwoFile::CHUNK_IMAP) // BLOK | IMAP
{
int imap_size = current_tag_size;
blok_size -= imap_size;
std::string ordinal;
ReadString(ordinal);
imap_size -= GetStringSize(ordinal);
MSG_DEBUG("\t\t\tordinal: '" << ordinal << "'");
while(imap_size > 0)
{
current_tag_name = ReadLong();
imap_size -= 4;
current_tag_size = ReadShort();
imap_size -= 2;
MSG_DEBUG("\t\t\ttag: '" << GetTagString(current_tag_name) << "', size: " << current_tag_size);
imap_size -= SkipChunk(current_tag_size);
}
}
else
{
MSG_WARNING("Unknown tag in BLOK: '" << GetTagString(current_tag_name) << "'");
blok_size -= SkipChunk(current_tag_size );
}
}
}
else if(current_tag_name == CLwoFile::CHUNK_COLR) // COLR
{
Vector3D color = ReadVector3D();
surface.m_vertexColor = color;
current_tag_size -= 12;
m_curChunk.Size -= 12;
MSG_DEBUG("\tcolor: (" << color[0] << "," << color[1] << "," << color[2] << ")" );
// envelope
ushort envl = ReadShort();
current_tag_size -= 2;
m_curChunk.Size -= 2;
}
else
{
m_curChunk.Size -= SkipChunk(current_tag_size);
}
}
m_pCurLayer->GetSurfaceMap()[surfaceName] = surface; // Add new surface
return true;
}
// print 4-char tag to debug out
std::string CLwoReader::GetTagString(uint tag)
{
std::string sTag;
sTag += char(tag >> 24);
sTag += char(tag >> 16);
sTag += char(tag >> 8);
sTag += char(tag);
return sTag;
}
// print 4-char type
std::string CLwoReader::GetTypeString(uint type)
{
std::string sType;
sType += char(type >> 24);
sType += char(type >> 16);
sType += char(type >> 8);
sType += char(type);
return sType;
}
ulong CLwoReader::SkipChunk(ulong size)
{
ulong newPos = size + size % 2;
m_ifs.seekg(newPos, ios::cur);
return newPos;
}
// Change endianess
CLwoFile::SChunk CLwoReader::ReadChunk()
{
CLwoFile::SChunk chunk;
chunk.Flag = ReadLong();
chunk.Size = ReadLong();
return chunk;
}