-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlwoload.cpp
More file actions
851 lines (675 loc) · 19.1 KB
/
lwoload.cpp
File metadata and controls
851 lines (675 loc) · 19.1 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
/*
Copyright (C) 2004-2005 Kolian^Spöntz
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <search.h>
#include <string.h>
#include <math.h>
#include "utils.h"
#include "3dsload.h"
// ******************************************************************
static FILE *inFile = 0;
#define dread(dest,len) (fread(dest,len,1,inFile))
#define dsetpos(pos) (fseek(inFile,pos,SEEK_CUR))
#define dgetpos() (ftell(inFile))
// ******************************************************************
void readS(char *buff) {
int readchars = 0;
unsigned char ch;
do {
dread(&ch,1);
buff[readchars++] = ch;
} while (ch != (unsigned char)0);
buff[readchars] = '0';
if ((readchars & 1) == 1) dread(&ch,1);
}
// ******************************************************************
char readB() {
unsigned char f[1];
if (dread(f,1) != 1) return -1;
return f[0];
}
// ******************************************************************
short readW() {
unsigned char f[2];
if (dread(f,2) != 1) return -1;
return (f[0] << 8) | f[1];
}
// ******************************************************************
long readDW() {
unsigned char f[4];
if (dread(f,4) != 1) return -1;
return (f[0] << 24) | (f[1] << 16) | (f[2] << 8) | f[3];
}
// ******************************************************************
int readVX() {
int i;
unsigned char c1, c2, c3;
if (dread(&c1,1) != 1) return -1;
// 2 bytes long
if (c1 != 0xFF) {
if (dread(&c2,1) != 1) return -1;
i = (c1 << 8) | c2;
}
// 4 bytes long (value on 3 last bytes)
else {
if (dread(&c1,1) != 1) return -1;
if (dread(&c2,1) != 1) return -1;
if (dread(&c3,1) != 1) return -1;
i = (c1 << 16) | (c2 << 8) | c3;
}
return i;
}
// ******************************************************************
float readF() {
float f;
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
unsigned char t1, t2, t3, t4;
char *p = (char *) &f;
#endif
if (dread(&f,4) != 1) return -1;
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
t1 = (unsigned char) p[0];
t2 = (unsigned char) p[1];
t3 = (unsigned char) p[2];
t4 = (unsigned char) p[3];
p[0] = t4;
p[1] = t3;
p[2] = t2;
p[3] = t1;
#endif
return f;
}
// ******************************************************************
#define LWO2_OK 0
#define LWO2_ERROR 1
#define LWO_ID(a,b,c,d) ((a<<24) | (b<<16) | (c<<8) | d)
// ******************************************************************
#define ID_FORM LWO_ID('F','O','R','M')
#define ID_LWO2 LWO_ID('L','W','O','2')
#define ID_LWOB LWO_ID('L','W','O','B')
/* top-level chunks */
#define ID_LAYR LWO_ID('L','A','Y','R')
#define ID_TAGS LWO_ID('T','A','G','S')
#define ID_PNTS LWO_ID('P','N','T','S')
#define ID_BBOX LWO_ID('B','B','O','X')
#define ID_VMAP LWO_ID('V','M','A','P')
#define ID_VMAD LWO_ID('V','M','A','D')
#define ID_POLS LWO_ID('P','O','L','S')
#define ID_PTAG LWO_ID('P','T','A','G')
#define ID_ENVL LWO_ID('E','N','V','L')
#define ID_CLIP LWO_ID('C','L','I','P')
#define ID_SURF LWO_ID('S','U','R','F')
#define ID_DESC LWO_ID('D','E','S','C')
#define ID_TEXT LWO_ID('T','E','X','T')
#define ID_ICON LWO_ID('I','C','O','N')
/* polygon types */
#define ID_FACE LWO_ID('F','A','C','E')
#define ID_CURV LWO_ID('C','U','R','V')
#define ID_PTCH LWO_ID('P','T','C','H')
#define ID_MBAL LWO_ID('M','B','A','L')
#define ID_BONE LWO_ID('B','O','N','E')
/* polygon tags */
#define ID_SURF LWO_ID('S','U','R','F')
#define ID_PART LWO_ID('P','A','R','T')
#define ID_SMGP LWO_ID('S','M','G','P')
#define ID_TXUV LWO_ID('T','X','U','V')
/* envelopes */
#define ID_PRE LWO_ID('P','R','E',' ')
#define ID_POST LWO_ID('P','O','S','T')
#define ID_KEY LWO_ID('K','E','Y',' ')
#define ID_SPAN LWO_ID('S','P','A','N')
#define ID_TCB LWO_ID('T','C','B',' ')
#define ID_HERM LWO_ID('H','E','R','M')
#define ID_BEZI LWO_ID('B','E','Z','I')
#define ID_BEZ2 LWO_ID('B','E','Z','2')
#define ID_LINE LWO_ID('L','I','N','E')
#define ID_STEP LWO_ID('S','T','E','P')
/* clips */
#define ID_STIL LWO_ID('S','T','I','L')
#define ID_ISEQ LWO_ID('I','S','E','Q')
#define ID_ANIM LWO_ID('A','N','I','M')
#define ID_XREF LWO_ID('X','R','E','F')
#define ID_STCC LWO_ID('S','T','C','C')
#define ID_TIME LWO_ID('T','I','M','E')
#define ID_CONT LWO_ID('C','O','N','T')
#define ID_BRIT LWO_ID('B','R','I','T')
#define ID_SATR LWO_ID('S','A','T','R')
#define ID_HUE LWO_ID('H','U','E',' ')
#define ID_GAMM LWO_ID('G','A','M','M')
#define ID_NEGA LWO_ID('N','E','G','A')
#define ID_IFLT LWO_ID('I','F','L','T')
#define ID_PFLT LWO_ID('P','F','L','T')
/* surfaces */
#define ID_COLR LWO_ID('C','O','L','R')
#define ID_LUMI LWO_ID('L','U','M','I')
#define ID_DIFF LWO_ID('D','I','F','F')
#define ID_SPEC LWO_ID('S','P','E','C')
#define ID_GLOS LWO_ID('G','L','O','S')
#define ID_REFL LWO_ID('R','E','F','L')
#define ID_RFOP LWO_ID('R','F','O','P')
#define ID_RIMG LWO_ID('R','I','M','G')
#define ID_RSAN LWO_ID('R','S','A','N')
#define ID_TRAN LWO_ID('T','R','A','N')
#define ID_TROP LWO_ID('T','R','O','P')
#define ID_TIMG LWO_ID('T','I','M','G')
#define ID_RIND LWO_ID('R','I','N','D')
#define ID_TRNL LWO_ID('T','R','N','L')
#define ID_BUMP LWO_ID('B','U','M','P')
#define ID_SMAN LWO_ID('S','M','A','N')
#define ID_SIDE LWO_ID('S','I','D','E')
#define ID_CLRH LWO_ID('C','L','R','H')
#define ID_CLRF LWO_ID('C','L','R','F')
#define ID_ADTR LWO_ID('A','D','T','R')
#define ID_SHRP LWO_ID('S','H','R','P')
#define ID_LINE LWO_ID('L','I','N','E')
#define ID_LSIZ LWO_ID('L','S','I','Z')
#define ID_ALPH LWO_ID('A','L','P','H')
#define ID_AVAL LWO_ID('A','V','A','L')
#define ID_GVAL LWO_ID('G','V','A','L')
#define ID_BLOK LWO_ID('B','L','O','K')
/* texture layer */
#define ID_TYPE LWO_ID('T','Y','P','E')
#define ID_CHAN LWO_ID('C','H','A','N')
#define ID_NAME LWO_ID('N','A','M','E')
#define ID_ENAB LWO_ID('E','N','A','B')
#define ID_OPAC LWO_ID('O','P','A','C')
#define ID_FLAG LWO_ID('F','L','A','G')
#define ID_PROJ LWO_ID('P','R','O','J')
#define ID_STCK LWO_ID('S','T','C','K')
#define ID_TAMP LWO_ID('T','A','M','P')
/* texture coordinates */
#define ID_TMAP LWO_ID('T','M','A','P')
#define ID_AXIS LWO_ID('A','X','I','S')
#define ID_CNTR LWO_ID('C','N','T','R')
#define ID_SIZE LWO_ID('S','I','Z','E')
#define ID_ROTA LWO_ID('R','O','T','A')
#define ID_OREF LWO_ID('O','R','E','F')
#define ID_FALL LWO_ID('F','A','L','L')
#define ID_CSYS LWO_ID('C','S','Y','S')
/* image map */
#define ID_IMAP LWO_ID('I','M','A','P')
#define ID_IMAG LWO_ID('I','M','A','G')
#define ID_WRAP LWO_ID('W','R','A','P')
#define ID_WRPW LWO_ID('W','R','P','W')
#define ID_WRPH LWO_ID('W','R','P','H')
#define ID_VMAP LWO_ID('V','M','A','P')
#define ID_AAST LWO_ID('A','A','S','T')
#define ID_PIXB LWO_ID('P','I','X','B')
/* procedural */
#define ID_PROC LWO_ID('P','R','O','C')
#define ID_COLR LWO_ID('C','O','L','R')
#define ID_VALU LWO_ID('V','A','L','U')
#define ID_FUNC LWO_ID('F','U','N','C')
#define ID_FTPS LWO_ID('F','T','P','S')
#define ID_ITPS LWO_ID('I','T','P','S')
#define ID_ETPS LWO_ID('E','T','P','S')
/* gradient */
#define ID_GRAD LWO_ID('G','R','A','D')
#define ID_GRST LWO_ID('G','R','S','T')
#define ID_GREN LWO_ID('G','R','E','N')
#define ID_PNAM LWO_ID('P','N','A','M')
#define ID_INAM LWO_ID('I','N','A','M')
#define ID_GRPT LWO_ID('G','R','P','T')
#define ID_FKEY LWO_ID('F','K','E','Y')
#define ID_IKEY LWO_ID('I','K','E','Y')
/* shader */
#define ID_SHDR LWO_ID('S','H','D','R')
#define ID_DATA LWO_ID('D','A','T','A')
// ******************************************************************
H3dsMeshObj *processLAYR(long dwSize, H3dsScene *scene) {
H3dsMeshObj *meshobj = GetMeshObj(scene);
return meshobj;
}
// ******************************************************************
void processTAGS(long dwSize, H3dsScene *scene) {
char name[256];
H3dsTag *tag;
long opos, index, readed;
opos = ftell(inFile);
readed = 0;
index = 1;
while (readed < dwSize) {
// read tag from file
readS(name);
// store tag
tag = (H3dsTag *) malloc(sizeof(H3dsTag));
tag->name = strdup(name);
tag->mat = NULL;
tag->next = (H3dsTag *) scene->taglist;
scene->taglist = tag;
scene->tags++;
readed = ftell(inFile) - opos;
}
fseek(inFile, opos, SEEK_SET);
}
// ******************************************************************
void processPNTS(long dwSize, H3dsMeshObj *meshobj) {
int i;
meshobj->verts = dwSize/12;
meshobj->vertlist = (H3dsVert *) malloc(sizeof(H3dsVert)*meshobj->verts);
memset(meshobj->vertlist, 0, sizeof(H3dsVert)*meshobj->verts);
for (i=0; i<meshobj->verts; i++) {
meshobj->vertlist[i].x = readF();
meshobj->vertlist[i].y = readF();
meshobj->vertlist[i].z = readF();
}
}
// ******************************************************************
void processVMAP_VMAD(long dwSize, H3dsMeshObj *meshobj, int perPoly) {
char name[256];
H3dsLinkedMap *linkedmap;
H3dsMap *map;
long type, opos, cpos, dwLoop;
int i, wCount;
short dim;
unsigned char c;
// get vmap properties
opos = ftell(inFile);
type = readDW();
dim = readW();
if ((type != ID_TXUV) || (dim != 2)){
meshobj->hasUnknownTexmaps = 1;
return;
}
readS(name);
cpos = ftell(inFile);
// count vmaps
wCount = 0;
dwLoop = cpos - opos;
while (dwLoop < dwSize) {
dread(&c,1);
// 2 bytes long
if (c != 0xFF) {
dread(&c,1);
dwLoop += 2;
}
// 4 bytes long (value on 3 last bytes)
else {
dread(&c,1);
dread(&c,1);
dread(&c,1);
dwLoop += 4;
}
if (perPoly) {
dread(&c,1);
// 2 bytes long
if (c != 0xFF) {
dread(&c,1);
dwLoop += 2;
}
// 4 bytes long (value on 3 last bytes)
else {
dread(&c,1);
dread(&c,1);
dread(&c,1);
dwLoop += 4;
}
}
readF();
readF();
dwLoop += 4*2;
wCount++;
}
// read vmaps
linkedmap = (H3dsLinkedMap *) malloc(sizeof(H3dsLinkedMap));
linkedmap->maps = wCount;
linkedmap->perPoly = perPoly;
linkedmap->map = (H3dsMap *) malloc(sizeof(H3dsMap)*linkedmap->maps);
linkedmap->next = meshobj->maplist;
meshobj->maplist = linkedmap;
meshobj->maps++;
map = linkedmap->map;
fseek(inFile, cpos, SEEK_SET);
for (i=0; i<linkedmap->maps; i++) {
map[i].vertex = readVX();
if (perPoly) map[i].face = readVX();
map[i].u = readF();
map[i].v = readF();
}
}
// ******************************************************************
void processPOLS(long dwSize, H3dsMeshObj *meshobj) {
long numFaces;
long dwType, dwLoop, i, cpos;
char q = 0;
unsigned char c;
dwType = readDW();
switch (dwType) {
case ID_FACE: {
int wCount, wFace;
cpos = ftell(inFile);
// count faces
numFaces = 0;
dwLoop = 4; // 4 bytes (type)
while (dwLoop < dwSize) {
wCount = readW();
wCount &= 1023;
dwLoop += 2;
numFaces++;
for (i=0; i<wCount; i++) {
dread(&c,1);
// 2 bytes long
if (c != 0xFF) {
dread(&c,1);
dwLoop += 2;
}
// 4 bytes long (value on 3 last bytes)
else {
dread(&c,1);
dread(&c,1);
dread(&c,1);
dwLoop += 4;
}
}
}
fseek(inFile, cpos, SEEK_SET);
// allocate faces
meshobj->faces = numFaces;
meshobj->facelist = (H3dsFace *) malloc(sizeof(H3dsFace)*meshobj->faces);
memset(meshobj->facelist, 0, sizeof(H3dsFace)*meshobj->faces);
// read faces
wFace = 0;
while (wFace < numFaces) {
wCount = readW();
wCount &= 1023;
meshobj->facelist[wFace].vertexes = wCount;
meshobj->facelist[wFace].v = (H3dsVFData *) malloc(sizeof(H3dsVFData)*wCount);
meshobj->facelist[wFace].tag = -1;
for (i=0; i<wCount; i++) {
meshobj->facelist[wFace].v[i].vertex = readVX();
meshobj->facelist[wFace].v[i].u = 0;
meshobj->facelist[wFace].v[i].v = 0;
}
wFace++;
}
break;
}
default:
meshobj->hasUnknownPolys = 1;
if (fseek(inFile, dwSize-4, SEEK_CUR) != 0) q = 1; // type has been readed
break;
}
}
// ******************************************************************
void processPTAG(long dwSize, H3dsMeshObj *meshobj) {
long dwLoop, opos, id;
int pol, tag;
if (meshobj->faces == 0) return;
opos = ftell(inFile);
id = readDW();
if (id != ID_SURF) return;
dwLoop = 4;
while (dwLoop < dwSize) {
pol = readVX();
tag = readVX();
if (pol < meshobj->faces) meshobj->facelist[pol].tag = tag;
dwLoop = ftell(inFile) - opos;
}
}
// ******************************************************************
void processCLIP(long dwSize, H3dsScene *scene) {
char text[256];
H3dsClip *clip;
long dwIndex, dwType;
short wLength;
dwIndex = readDW();
dwType = readDW();
wLength = readW();
switch (dwType) {
case ID_STIL:
readS(text);
clip = (H3dsClip *) malloc(sizeof(H3dsClip));
clip->index = dwIndex;
clip->name = strdup(text);
clip->next = (H3dsClip *) scene->cliplist;
scene->cliplist = clip;
scene->clips++;
break;
}
}
// ******************************************************************
void processSURF(long dwSize, H3dsScene *scene) {
char text[256];
H3dsMaterial *mat = GetMaterial();
long opos, cpos, opos2, cpos2, dwLoopBlok, dwLoop;
float percent;
unsigned char c;
opos = ftell(inFile);
readS(text);
mat->Matname = strdup(text);
c = 0;
while (c == 0) {
c = readB();
}
fseek(inFile, -1, SEEK_CUR);
dwLoop = ftell(inFile) - opos;
while (dwLoop < dwSize) {
long subid, id = readDW();
int subsize, size = readW();
cpos = ftell(inFile);
switch (id) {
case ID_COLR:
mat->Ambient.red = readF();
mat->Ambient.green = readF();
mat->Ambient.blue = readF();
readVX(); // envelope
break;
case ID_DIFF:
percent = readF();
mat->Diffuse.red = mat->Ambient.red * percent;
mat->Diffuse.green = mat->Ambient.green * percent;
mat->Diffuse.blue = mat->Ambient.blue * percent;
readVX(); // envelope
break;
case ID_GLOS:
percent = readF();
mat->Specular.red = mat->Ambient.red * percent;
mat->Specular.green = mat->Ambient.green * percent;
mat->Specular.blue = mat->Ambient.blue * percent;
readVX(); // envelope
break;
case ID_BLOK:
dwLoopBlok = 4;
opos2 = ftell(inFile);
while (dwLoopBlok < size) {
subid = readDW();
subsize = readW();
cpos2 = ftell(inFile);
switch (subid) {
case ID_IMAG:
mat->hTex = readVX();
break;
}
fseek(inFile, cpos2 + subsize, SEEK_SET);
dwLoopBlok = ftell(inFile) - opos2;
}
break;
}
fseek(inFile, cpos + size, SEEK_SET);
dwLoop = ftell(inFile) - opos;
}
// link new material to scene material list
mat->next = scene->materials;
scene->materials = mat;
scene->mats++;
}
// ******************************************************************
void updateSurfaces(H3dsScene *scene) {
H3dsMeshObj *mesh;
H3dsFace *face;
H3dsMaterial *mat;
H3dsTag *tag;
H3dsClip *clip;
int m, j, i;
// link tags to materials
tag = scene->taglist;
while (tag) {
mat = scene->materials;
while (mat) {
if (strcasecmp(mat->Matname, tag->name) == 0) break;
mat = (H3dsMaterial *) mat->next;
}
if (mat) tag->mat = mat;
tag = (H3dsTag *) tag->next;
}
// link faces to materials
for (m=0; m<scene->meshobjs; m++) {
mesh = &scene->meshobjlist[m];
for (j=0; j<mesh->faces; j++) {
face = &mesh->facelist[j];
if (face->tag != -1) {
tag = scene->taglist;
i = scene->tags-1;
while (tag && (i != face->tag)) {
tag = (H3dsTag *) tag->next;
i--;
}
if (tag) face->material = tag->mat;
}
}
}
// link materials to textures
mat = scene->materials;
while (mat) {
if (mat->hTex > 0) {
clip = scene->cliplist;
while (clip) {
if (clip->index == mat->hTex) break;
clip = (H3dsClip *) clip->next;
}
if (clip) {
if (clip->index == mat->hTex) {
mat->TexFilename = strdup(clip->name);
}
}
if (!mat->TexFilename) mat->hTex = 0;
}
mat = (H3dsMaterial *) mat->next;
}
}
// ******************************************************************
static void postProcessUV(H3dsScene *scene) {
H3dsMeshObj *mesh;
H3dsLinkedMap *linkedmap;
H3dsMap *map;
int nmesh, nface, nvert, nmap, id;
char hasUV;
for (nmesh=0; nmesh<scene->meshobjs; nmesh++) {
mesh = &scene->meshobjlist[nmesh];
for (nface=0; nface<mesh->faces; nface++) {
for (nvert=0; nvert<mesh->facelist[nface].vertexes; nvert++) {
id = mesh->facelist[nface].v[nvert].vertex;
hasUV = 0;
linkedmap = mesh->maplist;
while (linkedmap && !hasUV) {
// vmads only
if (linkedmap->perPoly) {
map = linkedmap->map;
for (nmap=0; nmap<linkedmap->maps; nmap++) {
if ((map[nmap].face == nface) && (map[nmap].vertex == id)) {
mesh->facelist[nface].v[nvert].u = map[nmap].u;
mesh->facelist[nface].v[nvert].v = map[nmap].v;
hasUV = 1;
break;
}
}
}
linkedmap = (H3dsLinkedMap *) linkedmap->next;
}
linkedmap = mesh->maplist;
while (linkedmap && !hasUV) {
// vmaps only
if (!linkedmap->perPoly) {
map = linkedmap->map;
for (nmap=0; nmap<linkedmap->maps; nmap++) {
if (map[nmap].vertex == id) {
mesh->facelist[nface].v[nvert].u = map[nmap].u;
mesh->facelist[nface].v[nvert].v = map[nmap].v;
hasUV = 1;
break;
}
}
}
linkedmap = (H3dsLinkedMap *) linkedmap->next;
}
}
}
}
}
// ******************************************************************
H3dsScene *readLWO(void *ptr) {
H3dsScene *scene;
long id, dwFileSize, type, cpos;
long dwChunkId = LWO2_OK, dwSize;
H3dsMeshObj *meshobj = NULL;
char q = 0;
inFile = (FILE *) ptr;
id = readDW();
dwFileSize = readDW();
type = readDW();
if (id != ID_FORM) return NULL;
if (type != ID_LWO2) return NULL;
scene = (H3dsScene *) malloc(sizeof(H3dsScene));
if (!scene) return 0;
memset(scene, 0, sizeof(H3dsScene));
while ((dwChunkId != LWO2_ERROR) && !q) {
dwChunkId = readDW();
dwSize = readDW();
// capture current position
cpos = ftell(inFile);
// process current chunk
switch (dwChunkId) {
case ID_LAYR:
meshobj = processLAYR(dwSize, scene);
break;
case ID_TAGS:
processTAGS(dwSize, scene);
break;
case ID_PNTS:
processPNTS(dwSize, meshobj);
break;
case ID_BBOX:
break;
case ID_VMAP:
processVMAP_VMAD(dwSize, meshobj, 0);
break;
case ID_VMAD:
processVMAP_VMAD(dwSize, meshobj, 1);
break;
case ID_POLS:
processPOLS(dwSize, meshobj);
break;
case ID_PTAG:
processPTAG(dwSize, meshobj);
break;
case ID_ENVL:
break;
case ID_CLIP:
processCLIP(dwSize, scene);
break;
case ID_SURF:
processSURF(dwSize, scene);
break;
}
// go to the next chunk
fseek(inFile, cpos + dwSize, SEEK_SET);
if (ftell(inFile) >= dwFileSize + 8) q = 1;
}
updateSurfaces(scene);
postProcessUV(scene);
scene->sourceFile = FILE_LWO;
return scene;
}