forked from assimp/assimp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASEParser.cpp
2150 lines (1985 loc) · 54.8 KB
/
ASEParser.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
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
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------
Copyright (c) 2006-2012, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/
/** @file ASEParser.cpp
* @brief Implementation of the ASE parser class
*/
#include "AssimpPCH.h"
// internal headers
#include "TextureTransform.h"
#include "ASELoader.h"
#include "MaterialSystem.h"
#include "fast_atof.h"
using namespace Assimp;
using namespace Assimp::ASE;
// ------------------------------------------------------------------------------------------------
// Begin an ASE parsing function
#define AI_ASE_PARSER_INIT() \
int iDepth = 0;
// ------------------------------------------------------------------------------------------------
// Handle a "top-level" section in the file. EOF is no error in this case.
#define AI_ASE_HANDLE_TOP_LEVEL_SECTION() \
else if ('{' == *filePtr)iDepth++; \
else if ('}' == *filePtr) \
{ \
if (0 == --iDepth) \
{ \
++filePtr; \
SkipToNextToken(); \
return; \
} \
} \
else if ('\0' == *filePtr) \
{ \
return; \
} \
if(IsLineEnd(*filePtr) && !bLastWasEndLine) \
{ \
++iLineNumber; \
bLastWasEndLine = true; \
} else bLastWasEndLine = false; \
++filePtr;
// ------------------------------------------------------------------------------------------------
// Handle a nested section in the file. EOF is an error in this case
// @param level "Depth" of the section
// @param msg Full name of the section (including the asterisk)
#define AI_ASE_HANDLE_SECTION(level, msg) \
if ('{' == *filePtr)iDepth++; \
else if ('}' == *filePtr) \
{ \
if (0 == --iDepth) \
{ \
++filePtr; \
SkipToNextToken(); \
return; \
} \
} \
else if ('\0' == *filePtr) \
{ \
LogError("Encountered unexpected EOL while parsing a " msg \
" chunk (Level " level ")"); \
} \
if(IsLineEnd(*filePtr) && !bLastWasEndLine) \
{ \
++iLineNumber; \
bLastWasEndLine = true; \
} else bLastWasEndLine = false; \
++filePtr;
// ------------------------------------------------------------------------------------------------
Parser::Parser (const char* szFile, unsigned int fileFormatDefault)
{
ai_assert(NULL != szFile);
filePtr = szFile;
iFileFormat = fileFormatDefault;
// make sure that the color values are invalid
m_clrBackground.r = get_qnan();
m_clrAmbient.r = get_qnan();
// setup some default values
iLineNumber = 0;
iFirstFrame = 0;
iLastFrame = 0;
iFrameSpeed = 30; // use 30 as default value for this property
iTicksPerFrame = 1; // use 1 as default value for this property
bLastWasEndLine = false; // need to handle \r\n seqs due to binary file mapping
}
// ------------------------------------------------------------------------------------------------
void Parser::LogWarning(const char* szWarn)
{
ai_assert(NULL != szWarn);
char szTemp[1024];
#if _MSC_VER >= 1400
sprintf_s(szTemp,"Line %i: %s",iLineNumber,szWarn);
#else
snprintf(szTemp,1024,"Line %i: %s",iLineNumber,szWarn);
#endif
// output the warning to the logger ...
DefaultLogger::get()->warn(szTemp);
}
// ------------------------------------------------------------------------------------------------
void Parser::LogInfo(const char* szWarn)
{
ai_assert(NULL != szWarn);
char szTemp[1024];
#if _MSC_VER >= 1400
sprintf_s(szTemp,"Line %i: %s",iLineNumber,szWarn);
#else
snprintf(szTemp,1024,"Line %i: %s",iLineNumber,szWarn);
#endif
// output the information to the logger ...
DefaultLogger::get()->info(szTemp);
}
// ------------------------------------------------------------------------------------------------
void Parser::LogError(const char* szWarn)
{
ai_assert(NULL != szWarn);
char szTemp[1024];
#if _MSC_VER >= 1400
sprintf_s(szTemp,"Line %i: %s",iLineNumber,szWarn);
#else
snprintf(szTemp,1024,"Line %i: %s",iLineNumber,szWarn);
#endif
// throw an exception
throw DeadlyImportError(szTemp);
}
// ------------------------------------------------------------------------------------------------
bool Parser::SkipToNextToken()
{
while (true)
{
char me = *filePtr;
// increase the line number counter if necessary
if (IsLineEnd(me) && !bLastWasEndLine)
{
++iLineNumber;
bLastWasEndLine = true;
}
else bLastWasEndLine = false;
if ('*' == me || '}' == me || '{' == me)return true;
if ('\0' == me)return false;
++filePtr;
}
}
// ------------------------------------------------------------------------------------------------
bool Parser::SkipSection()
{
// must handle subsections ...
int iCnt = 0;
while (true)
{
if ('}' == *filePtr)
{
--iCnt;
if (0 == iCnt)
{
// go to the next valid token ...
++filePtr;
SkipToNextToken();
return true;
}
}
else if ('{' == *filePtr)
{
++iCnt;
}
else if ('\0' == *filePtr)
{
LogWarning("Unable to parse block: Unexpected EOF, closing bracket \'}\' was expected [#1]");
return false;
}
else if(IsLineEnd(*filePtr))++iLineNumber;
++filePtr;
}
}
// ------------------------------------------------------------------------------------------------
void Parser::Parse()
{
AI_ASE_PARSER_INIT();
while (true)
{
if ('*' == *filePtr)
{
++filePtr;
// Version should be 200. Validate this ...
if (TokenMatch(filePtr,"3DSMAX_ASCIIEXPORT",18))
{
unsigned int fmt;
ParseLV4MeshLong(fmt);
if (fmt > 200)
{
LogWarning("Unknown file format version: *3DSMAX_ASCIIEXPORT should \
be <= 200");
}
// *************************************************************
// - fmt will be 0 if we're unable to read the version number
// there are some faulty files without a version number ...
// in this case we'll guess the exact file format by looking
// at the file extension (ASE, ASK, ASC)
// *************************************************************
if (fmt)iFileFormat = fmt;
continue;
}
// main scene information
if (TokenMatch(filePtr,"SCENE",5))
{
ParseLV1SceneBlock();
continue;
}
// "group" - no implementation yet, in facte
// we're just ignoring them for the moment
if (TokenMatch(filePtr,"GROUP",5))
{
Parse();
continue;
}
// material list
if (TokenMatch(filePtr,"MATERIAL_LIST",13))
{
ParseLV1MaterialListBlock();
continue;
}
// geometric object (mesh)
if (TokenMatch(filePtr,"GEOMOBJECT",10))
{
m_vMeshes.push_back(Mesh());
ParseLV1ObjectBlock(m_vMeshes.back());
continue;
}
// helper object = dummy in the hierarchy
if (TokenMatch(filePtr,"HELPEROBJECT",12))
{
m_vDummies.push_back(Dummy());
ParseLV1ObjectBlock(m_vDummies.back());
continue;
}
// light object
if (TokenMatch(filePtr,"LIGHTOBJECT",11))
{
m_vLights.push_back(Light());
ParseLV1ObjectBlock(m_vLights.back());
continue;
}
// camera object
if (TokenMatch(filePtr,"CAMERAOBJECT",12))
{
m_vCameras.push_back(Camera());
ParseLV1ObjectBlock(m_vCameras.back());
continue;
}
// comment - print it on the console
if (TokenMatch(filePtr,"COMMENT",7))
{
std::string out = "<unknown>";
ParseString(out,"*COMMENT");
LogInfo(("Comment: " + out).c_str());
continue;
}
// ASC bone weights
if (AI_ASE_IS_OLD_FILE_FORMAT() && TokenMatch(filePtr,"MESH_SOFTSKINVERTS",18))
{
ParseLV1SoftSkinBlock();
}
}
AI_ASE_HANDLE_TOP_LEVEL_SECTION();
}
return;
}
// ------------------------------------------------------------------------------------------------
void Parser::ParseLV1SoftSkinBlock()
{
// TODO: fix line counting here
// **************************************************************
// The soft skin block is formatted differently. There are no
// nested sections supported and the single elements aren't
// marked by keywords starting with an asterisk.
/**
FORMAT BEGIN
*MESH_SOFTSKINVERTS {
<nodename>
<number of vertices>
[for <number of vertices> times:]
<number of weights> [for <number of weights> times:] <bone name> <weight>
}
FORMAT END
*/
// **************************************************************
while (true)
{
if (*filePtr == '}' ) {++filePtr;return;}
else if (*filePtr == '\0') return;
else if (*filePtr == '{' ) ++filePtr;
else // if (!IsSpace(*filePtr) && !IsLineEnd(*filePtr))
{
ASE::Mesh* curMesh = NULL;
unsigned int numVerts = 0;
const char* sz = filePtr;
while (!IsSpaceOrNewLine(*filePtr))++filePtr;
const unsigned int diff = (unsigned int)(filePtr-sz);
if (diff)
{
std::string name = std::string(sz,diff);
for (std::vector<ASE::Mesh>::iterator it = m_vMeshes.begin();
it != m_vMeshes.end(); ++it)
{
if ((*it).mName == name)
{
curMesh = & (*it);
break;
}
}
if (!curMesh)
{
LogWarning("Encountered unknown mesh in *MESH_SOFTSKINVERTS section");
// Skip the mesh data - until we find a new mesh
// or the end of the *MESH_SOFTSKINVERTS section
while (true)
{
SkipSpacesAndLineEnd(&filePtr);
if (*filePtr == '}')
{++filePtr;return;}
else if (!IsNumeric(*filePtr))
break;
SkipLine(&filePtr);
}
}
else
{
SkipSpacesAndLineEnd(&filePtr);
ParseLV4MeshLong(numVerts);
// Reserve enough storage
curMesh->mBoneVertices.reserve(numVerts);
for (unsigned int i = 0; i < numVerts;++i)
{
SkipSpacesAndLineEnd(&filePtr);
unsigned int numWeights;
ParseLV4MeshLong(numWeights);
curMesh->mBoneVertices.push_back(ASE::BoneVertex());
ASE::BoneVertex& vert = curMesh->mBoneVertices.back();
// Reserve enough storage
vert.mBoneWeights.reserve(numWeights);
for (unsigned int w = 0; w < numWeights;++w)
{
std::string bone;
ParseString(bone,"*MESH_SOFTSKINVERTS.Bone");
// Find the bone in the mesh's list
std::pair<int,float> me;
me.first = -1;
for (unsigned int n = 0; n < curMesh->mBones.size();++n)
{
if (curMesh->mBones[n].mName == bone)
{
me.first = n;
break;
}
}
if (-1 == me.first)
{
// We don't have this bone yet, so add it to the list
me.first = (int)curMesh->mBones.size();
curMesh->mBones.push_back(ASE::Bone(bone));
}
ParseLV4MeshFloat( me.second );
// Add the new bone weight to list
vert.mBoneWeights.push_back(me);
}
}
}
}
}
++filePtr;
SkipSpacesAndLineEnd(&filePtr);
}
}
// ------------------------------------------------------------------------------------------------
void Parser::ParseLV1SceneBlock()
{
AI_ASE_PARSER_INIT();
while (true)
{
if ('*' == *filePtr)
{
++filePtr;
if (TokenMatch(filePtr,"SCENE_BACKGROUND_STATIC",23))
{
// parse a color triple and assume it is really the bg color
ParseLV4MeshFloatTriple( &m_clrBackground.r );
continue;
}
if (TokenMatch(filePtr,"SCENE_AMBIENT_STATIC",20))
{
// parse a color triple and assume it is really the bg color
ParseLV4MeshFloatTriple( &m_clrAmbient.r );
continue;
}
if (TokenMatch(filePtr,"SCENE_FIRSTFRAME",16))
{
ParseLV4MeshLong(iFirstFrame);
continue;
}
if (TokenMatch(filePtr,"SCENE_LASTFRAME",15))
{
ParseLV4MeshLong(iLastFrame);
continue;
}
if (TokenMatch(filePtr,"SCENE_FRAMESPEED",16))
{
ParseLV4MeshLong(iFrameSpeed);
continue;
}
if (TokenMatch(filePtr,"SCENE_TICKSPERFRAME",19))
{
ParseLV4MeshLong(iTicksPerFrame);
continue;
}
}
AI_ASE_HANDLE_TOP_LEVEL_SECTION();
}
}
// ------------------------------------------------------------------------------------------------
void Parser::ParseLV1MaterialListBlock()
{
AI_ASE_PARSER_INIT();
unsigned int iMaterialCount = 0;
unsigned int iOldMaterialCount = (unsigned int)m_vMaterials.size();
while (true)
{
if ('*' == *filePtr)
{
++filePtr;
if (TokenMatch(filePtr,"MATERIAL_COUNT",14))
{
ParseLV4MeshLong(iMaterialCount);
// now allocate enough storage to hold all materials
m_vMaterials.resize(iOldMaterialCount+iMaterialCount);
continue;
}
if (TokenMatch(filePtr,"MATERIAL",8))
{
unsigned int iIndex = 0;
ParseLV4MeshLong(iIndex);
if (iIndex >= iMaterialCount)
{
LogWarning("Out of range: material index is too large");
iIndex = iMaterialCount-1;
}
// get a reference to the material
Material& sMat = m_vMaterials[iIndex+iOldMaterialCount];
// parse the material block
ParseLV2MaterialBlock(sMat);
continue;
}
}
AI_ASE_HANDLE_TOP_LEVEL_SECTION();
}
}
// ------------------------------------------------------------------------------------------------
void Parser::ParseLV2MaterialBlock(ASE::Material& mat)
{
AI_ASE_PARSER_INIT();
unsigned int iNumSubMaterials = 0;
while (true)
{
if ('*' == *filePtr)
{
++filePtr;
if (TokenMatch(filePtr,"MATERIAL_NAME",13))
{
if (!ParseString(mat.mName,"*MATERIAL_NAME"))
SkipToNextToken();
continue;
}
// ambient material color
if (TokenMatch(filePtr,"MATERIAL_AMBIENT",16))
{
ParseLV4MeshFloatTriple(&mat.mAmbient.r);
continue;
}
// diffuse material color
if (TokenMatch(filePtr,"MATERIAL_DIFFUSE",16) )
{
ParseLV4MeshFloatTriple(&mat.mDiffuse.r);
continue;
}
// specular material color
if (TokenMatch(filePtr,"MATERIAL_SPECULAR",17))
{
ParseLV4MeshFloatTriple(&mat.mSpecular.r);
continue;
}
// material shading type
if (TokenMatch(filePtr,"MATERIAL_SHADING",16))
{
if (TokenMatch(filePtr,"Blinn",5))
{
mat.mShading = Discreet3DS::Blinn;
}
else if (TokenMatch(filePtr,"Phong",5))
{
mat.mShading = Discreet3DS::Phong;
}
else if (TokenMatch(filePtr,"Flat",4))
{
mat.mShading = Discreet3DS::Flat;
}
else if (TokenMatch(filePtr,"Wire",4))
{
mat.mShading = Discreet3DS::Wire;
}
else
{
// assume gouraud shading
mat.mShading = Discreet3DS::Gouraud;
SkipToNextToken();
}
continue;
}
// material transparency
if (TokenMatch(filePtr,"MATERIAL_TRANSPARENCY",21))
{
ParseLV4MeshFloat(mat.mTransparency);
mat.mTransparency = 1.0f - mat.mTransparency;continue;
}
// material self illumination
if (TokenMatch(filePtr,"MATERIAL_SELFILLUM",18))
{
float f = 0.0f;
ParseLV4MeshFloat(f);
mat.mEmissive.r = f;
mat.mEmissive.g = f;
mat.mEmissive.b = f;
continue;
}
// material shininess
if (TokenMatch(filePtr,"MATERIAL_SHINE",14) )
{
ParseLV4MeshFloat(mat.mSpecularExponent);
mat.mSpecularExponent *= 15;
continue;
}
// two-sided material
if (TokenMatch(filePtr,"MATERIAL_TWOSIDED",17) )
{
mat.mTwoSided = true;
continue;
}
// material shininess strength
if (TokenMatch(filePtr,"MATERIAL_SHINESTRENGTH",22))
{
ParseLV4MeshFloat(mat.mShininessStrength);
continue;
}
// diffuse color map
if (TokenMatch(filePtr,"MAP_DIFFUSE",11))
{
// parse the texture block
ParseLV3MapBlock(mat.sTexDiffuse);
continue;
}
// ambient color map
if (TokenMatch(filePtr,"MAP_AMBIENT",11))
{
// parse the texture block
ParseLV3MapBlock(mat.sTexAmbient);
continue;
}
// specular color map
if (TokenMatch(filePtr,"MAP_SPECULAR",12))
{
// parse the texture block
ParseLV3MapBlock(mat.sTexSpecular);
continue;
}
// opacity map
if (TokenMatch(filePtr,"MAP_OPACITY",11))
{
// parse the texture block
ParseLV3MapBlock(mat.sTexOpacity);
continue;
}
// emissive map
if (TokenMatch(filePtr,"MAP_SELFILLUM",13))
{
// parse the texture block
ParseLV3MapBlock(mat.sTexEmissive);
continue;
}
// bump map
if (TokenMatch(filePtr,"MAP_BUMP",8))
{
// parse the texture block
ParseLV3MapBlock(mat.sTexBump);
}
// specular/shininess map
if (TokenMatch(filePtr,"MAP_SHINESTRENGTH",17))
{
// parse the texture block
ParseLV3MapBlock(mat.sTexShininess);
continue;
}
// number of submaterials
if (TokenMatch(filePtr,"NUMSUBMTLS",10))
{
ParseLV4MeshLong(iNumSubMaterials);
// allocate enough storage
mat.avSubMaterials.resize(iNumSubMaterials);
}
// submaterial chunks
if (TokenMatch(filePtr,"SUBMATERIAL",11))
{
unsigned int iIndex = 0;
ParseLV4MeshLong(iIndex);
if (iIndex >= iNumSubMaterials)
{
LogWarning("Out of range: submaterial index is too large");
iIndex = iNumSubMaterials-1;
}
// get a reference to the material
Material& sMat = mat.avSubMaterials[iIndex];
// parse the material block
ParseLV2MaterialBlock(sMat);
continue;
}
}
AI_ASE_HANDLE_SECTION("2","*MATERIAL");
}
}
// ------------------------------------------------------------------------------------------------
void Parser::ParseLV3MapBlock(Texture& map)
{
AI_ASE_PARSER_INIT();
// ***********************************************************
// *BITMAP should not be there if *MAP_CLASS is not BITMAP,
// but we need to expect that case ... if the path is
// empty the texture won't be used later.
// ***********************************************************
bool parsePath = true;
while (true)
{
if ('*' == *filePtr)
{
++filePtr;
// type of map
if (TokenMatch(filePtr,"MAP_CLASS" ,9))
{
std::string temp;
if(!ParseString(temp,"*MAP_CLASS"))
SkipToNextToken();
if (temp != "Bitmap" && temp != "Normal Bump")
{
DefaultLogger::get()->warn("ASE: Skipping unknown map type: " + temp);
parsePath = false;
}
continue;
}
// path to the texture
if (parsePath && TokenMatch(filePtr,"BITMAP" ,6))
{
if(!ParseString(map.mMapName,"*BITMAP"))
SkipToNextToken();
if (map.mMapName == "None")
{
// Files with 'None' as map name are produced by
// an Maja to ASE exporter which name I forgot ..
DefaultLogger::get()->warn("ASE: Skipping invalid map entry");
map.mMapName = "";
}
continue;
}
// offset on the u axis
if (TokenMatch(filePtr,"UVW_U_OFFSET" ,12))
{
ParseLV4MeshFloat(map.mOffsetU);
continue;
}
// offset on the v axis
if (TokenMatch(filePtr,"UVW_V_OFFSET" ,12))
{
ParseLV4MeshFloat(map.mOffsetV);
continue;
}
// tiling on the u axis
if (TokenMatch(filePtr,"UVW_U_TILING" ,12))
{
ParseLV4MeshFloat(map.mScaleU);
continue;
}
// tiling on the v axis
if (TokenMatch(filePtr,"UVW_V_TILING" ,12))
{
ParseLV4MeshFloat(map.mScaleV);
continue;
}
// rotation around the z-axis
if (TokenMatch(filePtr,"UVW_ANGLE" ,9))
{
ParseLV4MeshFloat(map.mRotation);
continue;
}
// map blending factor
if (TokenMatch(filePtr,"MAP_AMOUNT" ,10))
{
ParseLV4MeshFloat(map.mTextureBlend);
continue;
}
}
AI_ASE_HANDLE_SECTION("3","*MAP_XXXXXX");
}
return;
}
// ------------------------------------------------------------------------------------------------
bool Parser::ParseString(std::string& out,const char* szName)
{
char szBuffer[1024];
if (!SkipSpaces(&filePtr))
{
sprintf(szBuffer,"Unable to parse %s block: Unexpected EOL",szName);
LogWarning(szBuffer);
return false;
}
// there must be '"'
if ('\"' != *filePtr)
{
sprintf(szBuffer,"Unable to parse %s block: Strings are expected "
"to be enclosed in double quotation marks",szName);
LogWarning(szBuffer);
return false;
}
++filePtr;
const char* sz = filePtr;
while (true)
{
if ('\"' == *sz)break;
else if ('\0' == *sz)
{
sprintf(szBuffer,"Unable to parse %s block: Strings are expected to "
"be enclosed in double quotation marks but EOF was reached before "
"a closing quotation mark was encountered",szName);
LogWarning(szBuffer);
return false;
}
sz++;
}
out = std::string(filePtr,(uintptr_t)sz-(uintptr_t)filePtr);
filePtr = sz+1;
return true;
}
// ------------------------------------------------------------------------------------------------
void Parser::ParseLV1ObjectBlock(ASE::BaseNode& node)
{
AI_ASE_PARSER_INIT();
while (true)
{
if ('*' == *filePtr)
{
++filePtr;
// first process common tokens such as node name and transform
// name of the mesh/node
if (TokenMatch(filePtr,"NODE_NAME" ,9))
{
if(!ParseString(node.mName,"*NODE_NAME"))
SkipToNextToken();
continue;
}
// name of the parent of the node
if (TokenMatch(filePtr,"NODE_PARENT" ,11) )
{
if(!ParseString(node.mParent,"*NODE_PARENT"))
SkipToNextToken();
continue;
}
// transformation matrix of the node
if (TokenMatch(filePtr,"NODE_TM" ,7))
{
ParseLV2NodeTransformBlock(node);
continue;
}
// animation data of the node
if (TokenMatch(filePtr,"TM_ANIMATION" ,12))
{
ParseLV2AnimationBlock(node);
continue;
}
if (node.mType == BaseNode::Light)
{
// light settings
if (TokenMatch(filePtr,"LIGHT_SETTINGS" ,14))
{
ParseLV2LightSettingsBlock((ASE::Light&)node);
continue;
}
// type of the light source
if (TokenMatch(filePtr,"LIGHT_TYPE" ,10))
{
if (!ASSIMP_strincmp("omni",filePtr,4))
{
((ASE::Light&)node).mLightType = ASE::Light::OMNI;
}
else if (!ASSIMP_strincmp("target",filePtr,6))
{
((ASE::Light&)node).mLightType = ASE::Light::TARGET;
}
else if (!ASSIMP_strincmp("free",filePtr,4))
{
((ASE::Light&)node).mLightType = ASE::Light::FREE;
}
else if (!ASSIMP_strincmp("directional",filePtr,11))
{
((ASE::Light&)node).mLightType = ASE::Light::DIRECTIONAL;
}
else
{
LogWarning("Unknown kind of light source");
}
continue;
}
}
else if (node.mType == BaseNode::Camera)
{
// Camera settings
if (TokenMatch(filePtr,"CAMERA_SETTINGS" ,15))
{
ParseLV2CameraSettingsBlock((ASE::Camera&)node);
continue;
}
else if (TokenMatch(filePtr,"CAMERA_TYPE" ,11))
{
if (!ASSIMP_strincmp("target",filePtr,6))
{
((ASE::Camera&)node).mCameraType = ASE::Camera::TARGET;
}
else if (!ASSIMP_strincmp("free",filePtr,4))
{
((ASE::Camera&)node).mCameraType = ASE::Camera::FREE;
}
else
{
LogWarning("Unknown kind of camera");
}
continue;
}
}
else if (node.mType == BaseNode::Mesh)
{
// mesh data
// FIX: Older files use MESH_SOFTSKIN
if (TokenMatch(filePtr,"MESH" ,4) ||
TokenMatch(filePtr,"MESH_SOFTSKIN",13))
{
ParseLV2MeshBlock((ASE::Mesh&)node);
continue;
}
// mesh material index
if (TokenMatch(filePtr,"MATERIAL_REF" ,12))
{
ParseLV4MeshLong(((ASE::Mesh&)node).iMaterialIndex);
continue;
}
}
}
AI_ASE_HANDLE_TOP_LEVEL_SECTION();
}
return;
}
// ------------------------------------------------------------------------------------------------
void Parser::ParseLV2CameraSettingsBlock(ASE::Camera& camera)
{
AI_ASE_PARSER_INIT();
while (true)
{
if ('*' == *filePtr)
{
++filePtr;
if (TokenMatch(filePtr,"CAMERA_NEAR" ,11))
{
ParseLV4MeshFloat(camera.mNear);
continue;
}
if (TokenMatch(filePtr,"CAMERA_FAR" ,10))
{
ParseLV4MeshFloat(camera.mFar);
continue;
}
if (TokenMatch(filePtr,"CAMERA_FOV" ,10))
{
ParseLV4MeshFloat(camera.mFOV);