-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathac3d_reader.m
1721 lines (1471 loc) · 52.7 KB
/
ac3d_reader.m
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
/* ======================================================================
* AC3D reader lib for iPhone
* See license.txt (BSD license)
* Author: Edward Patel/Memention AB
* ====================================================================== */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/sysctl.h>
#include <TargetConditionals.h>
static int __unused is_iPhone3GS = 0;
#if TARGET_IPHONE_SIMULATOR
static int tris;
static int strips;
static int strips_pts;
# define INIT_STATS tris = strips = strips_pts = 0
# define ADD_TRIS(_v) tris += _v
# define ADD_STRIPS(_v) strips += _v
# define ADD_STRIPS_PTS(_v) strips_pts += _v
# define SHOW_STATS NSLog(@"\nTris: %d\nCreated tri strips: %d\nPoints removed: %d", tris, strips, strips_pts)
#else
# define INIT_STATS
# define ADD_TRIS(_v)
# define ADD_STRIPS(_v)
# define ADD_STRIPS_PTS(_v)
# define SHOW_STATS
#endif
//#define USE_VBO
#define USE_FLOATS
#import "AC3DTexture.h"
#include "ac3d_reader.h"
static NSMutableDictionary *textures = nil;
static int lastMat = -1;
static
void load_textures_ac3d_object(AC3DObject *obj,
NSMutableDictionary *textures);
static
void init_ac3d_textures()
{
if (!textures) {
#if TARGET_IPHONE_SIMULATOR
/* empty */
#else
char machine[32];
size_t len=32;
sysctlbyname("hw.machine", machine, &len, NULL, 0);
if (!strcmp(machine, "iPhone2,1"))
is_iPhone3GS = 1;
#endif
textures = [[NSMutableDictionary alloc] init];
}
}
void free_ac3d_textures()
{
if (textures)
[textures release];
textures = nil;
}
enum {
SURF_SHADED = 0x10,
SURF_TWOSIDED = 0x20,
SURF_POLYGON = 0, // USING FAN
SURF_CLOSEDLINE = 1,
SURF_LINE = 2,
SURF_TRI_STRIP = 3,
OBJECT_WORLD = 0,
OBJECT_POLY,
OBJECT_GROUP,
OBJECT_LIGHT
};
struct AC3DFile_s;
struct AC3DMaterial_s;
struct AC3DObject_s;
struct AC3DSurf_s;
struct AC3DFile_s {
int nummats;
float *bbox;
struct AC3DMaterial_s **mats;
struct AC3DObject_s *obj;
};
struct AC3DMaterial_s {
char *name;
float rgb[4];
float amb[4];
float emis[4];
float spec[4];
float shi;
};
typedef float AC3DVert[6]; // pos+normal
typedef union {
float f;
int i;
short cmd[2];
struct {
unsigned char params[4];
} b;
} AC3Doptcmd;
struct AC3DObject_s {
bool texture_loaded;
bool enabled;
int type;
char *name;
char *texture;
int texid;
float *texrep; // 2
float *texoff; // 2
float *rot; // 9
float *loc; // 3
float angle;
float *rotvec; // 6
float *bbox; // 6
int numvert;
AC3DVert *verts;
int numcmds;
AC3Doptcmd *optcmds;
GLuint vbo;
int numsurf;
struct AC3DSurf_s **surfs;
int numkids;
struct AC3DObject_s **kids;
// data - not implemented
// url - not implemented
};
struct AC3Dtexref_s {
float texu;
float texv;
};
struct AC3DSurf_s {
int type;
int mat;
float normal[3];
int numrefs;
short *vrefs;
struct AC3Dtexref_s *texrefs;
};
typedef struct AC3DMaterial_s AC3DMaterial;
typedef struct AC3DSurf_s AC3DSurf;
typedef struct AC3Dtexref_s AC3Dtexref;
// ----------------------------------------------------------------------
#define CATCH_ERROR catch_error
#define THROW( _str ) do { *err = _str ; goto catch_error; } while (0)
// ----------------------------------------------------------------------
static
void free_ac3d_material(AC3DMaterial *mat)
{
if (mat) {
if (mat->name)
free(mat->name);
free(mat);
}
}
static
void free_ac3d_surf(AC3DSurf *surf)
{
if (surf) {
if (surf->vrefs)
free(surf->vrefs);
if (surf->texrefs)
free(surf->texrefs);
free(surf);
}
}
static
void free_ac3d_object(AC3DObject *obj)
{
if (obj) {
int i;
#ifdef USE_VBO
if (obj->vbo)
glDeleteBuffers(1, &obj->vbo);
#endif
if (obj->name)
free(obj->name);
if (obj->texture)
free(obj->texture);
if (obj->texrep)
free(obj->texrep);
if (obj->texoff)
free(obj->texoff);
if (obj->rot)
free(obj->rot);
if (obj->rotvec)
free(obj->rotvec);
if (obj->bbox)
free(obj->bbox);
if (obj->loc)
free(obj->loc);
if (obj->numvert > 0 && obj->verts)
free(obj->verts);
if (obj->numcmds > 0 && obj->optcmds)
free(obj->optcmds);
if (obj->numsurf > 0 && obj->surfs) {
for (i=0; i<obj->numsurf; i++) {
if (obj->surfs[i])
free_ac3d_surf(obj->surfs[i]);
}
free(obj->surfs);
}
if (obj->numkids > 0 && obj->kids) {
for (i=0; i<obj->numkids; i++) {
if (obj->kids[i])
free_ac3d_object(obj->kids[i]);
}
free(obj->kids);
}
free(obj);
}
}
void free_ac3d_file(AC3DFile *file)
{
if (file) {
if (file->obj)
free_ac3d_object(file->obj);
if (file->nummats && file->mats) {
int i;
for (i=0; i<file->nummats; i++) {
free_ac3d_material(file->mats[i]);
}
free(file->mats);
}
free(file);
}
}
// ----------------------------------------------------------------------
static
AC3DObject *find_ac3d_object_object(AC3DObject *obj, const char *name)
{
AC3DObject *o;
int i;
if (obj->name && !strcmp(obj->name, name))
return obj;
for (i=0; i<obj->numkids; i++)
if ((o = find_ac3d_object_object(obj->kids[i], name)))
return o;
return nil;
}
AC3DObject *find_ac3d_object(AC3DFile *file, const char *name)
{
if (file->obj)
return find_ac3d_object_object(file->obj, name);
return NULL;
}
void set_rotation_ac3d_object(AC3DObject *obj, float angle)
{
if (obj)
obj->angle = angle;
}
int is_enabled_ac3d_object(AC3DObject *obj)
{
if (obj)
return obj->enabled ? 1 : 0;
return -1;
}
void set_enabled_ac3d_object(AC3DObject *obj, int flag)
{
if (obj)
obj->enabled = flag ? true : false;
}
// ----------------------------------------------------------------------
void get_ac3d_material(AC3DFile *file,
int index,
float *rgb,
float *amb,
float *emis,
float *spec,
float *shi,
float *trans)
{
if (!file) return;
int j;
if (index >=0 &&
index < file->nummats) {
#define COPY( _field ) \
if (_field) for (j=0;j<3;j++) _field[j] = file->mats[index]->_field[j]
COPY( rgb );
COPY( amb );
COPY( emis );
COPY( spec );
if (shi) *shi = file->mats[index]->shi;
if (trans) *trans = 1.0 - file->mats[index]->rgb[3];
#undef COPY
}
}
void set_ac3d_material(AC3DFile *file,
int index,
float *rgb,
float *amb,
float *emis,
float *spec,
float shi,
float trans)
{
if (!file) return;
int j;
if (index >=0 &&
index < file->nummats) {
#define COPY( _field ) \
if (_field) for (j=0;j<3;j++) file->mats[index]->_field[j] = _field[j]
COPY( rgb );
COPY( amb );
COPY( emis );
COPY( spec );
if (shi >= 0.0)
file->mats[index]->shi = shi;
if (trans >= 0.0) {
file->mats[index]->rgb[3] = 1.0-trans;
file->mats[index]->amb[3] = 1.0-trans;
}
lastMat = -1;
#undef COPY
}
}
// ----------------------------------------------------------------------
static
void set_ac3d_texture_object(AC3DObject *obj,
char *texture_name,
int texid)
{
int i;
if (obj->texture && !strcmp(obj->texture, texture_name)) {
obj->texid = texid;
if (texid == -1)
obj->texture_loaded = 0;
}
for (i=0; i<obj->numkids; i++)
set_ac3d_texture_object(obj->kids[i], texture_name, texid);
}
void set_ac3d_texture(AC3DFile *file,
char *texture_name,
int texid)
{
if (!file->obj->texture_loaded) {
init_ac3d_textures();
load_textures_ac3d_object(file->obj, textures);
}
set_ac3d_texture_object(file->obj, texture_name, texid);
}
void set_ac3d_texture_named(AC3DFile *file,
char *texture_name_org,
char *texture_name_new)
{
int texid = -1;
NSString *texname = [NSString stringWithFormat:@"%s", texture_name_new];
AC3DTexture *texture = [textures objectForKey:texname];
if (texture) {
texid = [texture name];
} else {
texture = [[AC3DTexture alloc] initWithImagePath:texname];
if (texture) {
texid = [texture name];
[textures setObject:texture forKey:texname];
} else {
texname = [NSString stringWithFormat:@"Textures/%s", texture_name_new];
texture = [textures objectForKey:texname];
if (texture) {
texid = [texture name];
} else {
texture = [[AC3DTexture alloc] initWithImagePath:texname];
if (texture) {
texid = [texture name];
[textures setObject:texture forKey:texname];
}
}
}
}
if (texid > -1)
set_ac3d_texture(file, texture_name_org, texid);
}
void reset_ac3d_texture(AC3DFile *file,
char *texture_name)
{
if (!file->obj->texture_loaded) {
init_ac3d_textures();
load_textures_ac3d_object(file->obj, textures);
}
AC3DTexture *tex = [textures objectForKey:[NSString stringWithFormat:@"%s", texture_name]];
if (tex)
set_ac3d_texture(file, texture_name, tex.name);
}
// ----------------------------------------------------------------------
static
void normalize(float *v)
{
float len = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
if (len < 0.00000001) {
v[0] = v[1] = 0.0;
v[2] = 1.0;
return;
}
v[0] /= len;
v[1] /= len;
v[2] /= len;
}
static
void cross(float *dst, float *a, float *b)
{
dst[0] = a[1]*b[2] - a[2]*b[1];
dst[1] = a[2]*b[0] - a[0]*b[2];
dst[2] = a[0]*b[1] - a[1]*b[0];
}
static
void make_normal(float *dst, float *a, float *b, float *c)
{
float ab[3];
float ac[3];
ab[0] = b[0]-a[0];
ab[1] = b[1]-a[1];
ab[2] = b[2]-a[2];
normalize(ab);
ac[0] = c[0]-a[0];
ac[1] = c[1]-a[1];
ac[2] = c[2]-a[2];
normalize(ac);
cross(dst, ab, ac);
normalize(dst);
}
static
void fix_object_bbox(AC3DObject *obj)
{
if (obj->bbox && obj->loc) {
// TODO: Apply rotmatrix
obj->bbox[0] += obj->loc[0];
obj->bbox[1] += obj->loc[1];
obj->bbox[2] += obj->loc[2];
obj->bbox[3] += obj->loc[0];
obj->bbox[4] += obj->loc[1];
obj->bbox[5] += obj->loc[2];
}
}
static
void check_object_bbox(AC3DObject *obj, float *xyz)
{
if (obj->bbox) {
// Min
if (obj->bbox[0] > xyz[0]) obj->bbox[0] = xyz[0];
if (obj->bbox[1] > xyz[1]) obj->bbox[1] = xyz[1];
if (obj->bbox[2] > xyz[2]) obj->bbox[2] = xyz[2];
// Max
if (obj->bbox[3] < xyz[0]) obj->bbox[3] = xyz[0];
if (obj->bbox[4] < xyz[1]) obj->bbox[4] = xyz[1];
if (obj->bbox[5] < xyz[2]) obj->bbox[5] = xyz[2];
} else {
obj->bbox = (float*)malloc(sizeof(float)*6);
// Min
obj->bbox[0] = xyz[0];
obj->bbox[1] = xyz[1];
obj->bbox[2] = xyz[2];
// Max
obj->bbox[3] = xyz[0];
obj->bbox[4] = xyz[1];
obj->bbox[5] = xyz[2];
}
}
static
AC3DSurf *read_ac3d_surf(FILE *fp, AC3DObject *obj, char **err)
{
int do_read = 1;
char buf[256];
AC3DSurf *surf = (AC3DSurf*)malloc(sizeof(AC3DSurf));
if (!surf)
THROW( "malloc failed" );
memset(surf, 0, sizeof(AC3DSurf));
surf->mat = -1;
while (do_read) {
if (fscanf(fp, "%s", buf) != 1)
THROW( "SURF failed" );
if (!strcmp(buf, "SURF")) {
if (fscanf(fp, "%x", &surf->type) != 1)
THROW( "SURF type failed" );
} else if (!strcmp(buf, "mat")) {
if (fscanf(fp, "%d", &surf->mat) != 1)
THROW( "SURF mat failed" );
} else if (!strcmp(buf, "refs")) {
if (fscanf(fp, "%d", &surf->numrefs) != 1)
THROW( "SURF refs failed" );
if (surf->numrefs > 0) {
int i;
surf->vrefs = (short*)malloc(sizeof(short)*surf->numrefs);
surf->texrefs = (AC3Dtexref*)malloc(sizeof(AC3Dtexref)*surf->numrefs);
if (!surf->vrefs || !surf->texrefs)
THROW( "malloc failed" );
for (i=0; i<surf->numrefs; i++) {
if (fscanf(fp,
"%hd %f %f",
&surf->vrefs[i],
&surf->texrefs[i].texu,
&surf->texrefs[i].texv) != 3)
THROW( "SURF ref failed" );
}
if (surf->numrefs > 2) {
make_normal(surf->normal,
obj->verts[surf->vrefs[0]],
obj->verts[surf->vrefs[1]],
obj->verts[surf->vrefs[2]]);
}
}
do_read = 0; // done
} else {
THROW( "SURF unknown tag" );
}
}
return surf;
CATCH_ERROR:
free_ac3d_surf(surf);
return NULL;
}
static
void scan_string(FILE *fp, char *buf)
{
int c;
do {
c = fgetc(fp);
} while (c != EOF && c != '"');
do {
c = fgetc(fp);
if (c != EOF && c != '"')
*buf++ = c;
} while (c != EOF && c != '"');
*buf = '\0';
}
static
AC3DMaterial *read_ac3d_material(FILE *fp, char **err)
{
char buf[256];
AC3DMaterial *mat = (AC3DMaterial*)malloc(sizeof(AC3DMaterial));
float trans;
if (!mat)
THROW( "malloc failed" );
memset(mat, 0, sizeof(AC3DMaterial));
scan_string(fp, buf);
if (fscanf(fp,
" rgb %f %f %f "
"amb %f %f %f "
"emis %f %f %f "
"spec %f %f %f "
"shi %f "
"trans %f",
&mat->rgb[0], &mat->rgb[1], &mat->rgb[2],
&mat->amb[0], &mat->amb[1], &mat->amb[2],
&mat->emis[0], &mat->emis[1], &mat->emis[2],
&mat->spec[0], &mat->spec[1], &mat->spec[2],
&mat->shi,
&trans) != 14)
THROW( "MATERIAL error" );
mat->rgb[3]=1.0-trans;
mat->amb[3]=1.0-trans;
mat->emis[3]=1.0;
mat->spec[3]=1.0;
if (strlen(buf))
mat->name = strdup(buf);
return mat;
CATCH_ERROR:
free_ac3d_material(mat);
return NULL;
}
static
int find_surf_with_vertexes(int from,
int to,
AC3DSurf **surfs,
int mat,
short a, short b,
AC3Dtexref texa, AC3Dtexref texb)
{
for (;from < to; from++) {
AC3DSurf *surf = surfs[from];
if (surf->numrefs == 3 &&
(surf->type & 0x0f) == SURF_POLYGON &&
surf->mat == mat) {
if (surf->vrefs[0] == a &&
surf->vrefs[1] == b &&
fabs((surf->texrefs[0].texu-texa.texu) +
(surf->texrefs[0].texv-texa.texv)) < 0.0001 &&
fabs((surf->texrefs[1].texu-texb.texu) +
(surf->texrefs[1].texv-texb.texv)) < 0.0001) {
return (from << 2) + 2;
} else if (surf->vrefs[1] == a &&
surf->vrefs[2] == b &&
fabs((surf->texrefs[1].texu-texa.texu) +
(surf->texrefs[1].texv-texa.texv)) < 0.0001 &&
fabs((surf->texrefs[2].texu-texb.texu) +
(surf->texrefs[2].texv-texb.texv)) < 0.0001) {
return (from << 2) + 0;
} else if (surf->vrefs[2] == a &&
surf->vrefs[0] == b &&
fabs((surf->texrefs[2].texu-texa.texu) +
(surf->texrefs[2].texv-texa.texv)) < 0.0001 &&
fabs((surf->texrefs[0].texu-texb.texu) +
(surf->texrefs[0].texv-texb.texv)) < 0.0001) {
return (from << 2) + 1;
}
}
}
return 0;
}
static
int find_surf_with_vertexes_notex(int from,
int to,
AC3DSurf **surfs,
int mat,
short a, short b)
{
for (;from < to; from++) {
AC3DSurf *surf = surfs[from];
if (surf->numrefs == 3 &&
(surf->type & 0x0f) == SURF_POLYGON &&
surf->mat == mat) {
if (surf->vrefs[0] == a &&
surf->vrefs[1] == b) {
return (from << 2) + 2;
} else if (surf->vrefs[1] == a &&
surf->vrefs[2] == b) {
return (from << 2) + 0;
} else if (surf->vrefs[2] == a &&
surf->vrefs[0] == b) {
return (from << 2) + 1;
}
}
}
return 0;
}
// This is a naive method to find some triangle strips. Feel free to make a better (but keep in
// mind that it shouldn't take too long time to run)
static
void optimize_ac3d_object_step_1(AC3DObject *obj)
{
int i;
for (i=0; i<obj->numsurf-1; i++) {
AC3DSurf *surf = obj->surfs[i];
int a=2, b=1;
if (surf->numrefs == 3 &&
(surf->type & 0x0f) == SURF_POLYGON) {
int rc;
if (obj->texture)
rc = find_surf_with_vertexes(0, // from
obj->numsurf, // to
obj->surfs,
surf->mat,
surf->vrefs[a], surf->vrefs[b],
surf->texrefs[a], surf->texrefs[b]);
else
rc = find_surf_with_vertexes_notex(0, // from
obj->numsurf, // to
obj->surfs,
surf->mat,
surf->vrefs[a], surf->vrefs[b]);
if (rc) {
ADD_STRIPS(1);
} else {
ADD_TRIS((surf->numrefs>2) ? surf->numrefs-2 : 0);
}
while (rc) {
int idx1 = rc >> 2;
int idx2 = rc & 0x03;
surf->type = (surf->type & 0xf0) | SURF_TRI_STRIP;
surf->numrefs++;
surf->vrefs = (short*)realloc(surf->vrefs, sizeof(short)*surf->numrefs);
surf->texrefs = (AC3Dtexref*)realloc(surf->texrefs, sizeof(AC3Dtexref)*surf->numrefs);
surf->vrefs[surf->numrefs-1] = obj->surfs[idx1]->vrefs[idx2];
surf->texrefs[surf->numrefs-1] = obj->surfs[idx1]->texrefs[idx2];
{
AC3DSurf *tmp = obj->surfs[idx1];
obj->numsurf--;
obj->surfs[idx1] = obj->surfs[obj->numsurf];
free_ac3d_surf(tmp);
ADD_STRIPS_PTS(2);
}
if (a < b)
a += 2;
else
b += 2;
if (obj->texture)
rc = find_surf_with_vertexes(0, // from
obj->numsurf, // to
obj->surfs,
surf->mat,
surf->vrefs[a], surf->vrefs[b],
surf->texrefs[a], surf->texrefs[b]);
else
rc = find_surf_with_vertexes_notex(0, // from
obj->numsurf, // to
obj->surfs,
surf->mat,
surf->vrefs[a], surf->vrefs[b]);
}
if ((surf->type & 0x0f) == SURF_TRI_STRIP) {
ADD_TRIS(surf->numrefs-2);
}
}
}
}
static
void optimize_ac3d_object_step_2(AC3DObject *obj)
{
int i;
AC3Doptcmd *ptr;
obj->numcmds = 0;
for (i=0; i<obj->numsurf; i++) {
AC3DSurf *surf = obj->surfs[i];
if ((surf->type & 0x0f) == SURF_POLYGON ||
(surf->type & 0x0f) == SURF_TRI_STRIP) {
if (surf->numrefs > 2) {
obj->numcmds += 2; // type+len+mat
obj->numcmds += surf->numrefs*3; // vertex
if (obj->texture)
obj->numcmds += surf->numrefs*2; // texcoord
// single normal when...NONE SHADED and POLYGON
if (!(surf->type & SURF_SHADED) &&
(surf->type & 0x0f) == SURF_POLYGON) {
obj->numcmds += 3; // normal
} else {
obj->numcmds += surf->numrefs*3; // normals
}
}
} else {
obj->numcmds += 2; // type+len+mat
obj->numcmds += surf->numrefs*3; // vertex
}
}
ptr = obj->optcmds = (AC3Doptcmd*)malloc(sizeof(AC3Doptcmd)*obj->numcmds);
if (!obj) return;
memset(obj->optcmds, 0, sizeof(AC3Doptcmd)*obj->numcmds);
for (i=0; i<obj->numsurf; i++) {
int j;
AC3DSurf *surf = obj->surfs[i];
if (((surf->type & 0x0f) == SURF_POLYGON ||
(surf->type & 0x0f) == SURF_TRI_STRIP)) {
if (surf->numrefs > 2) {
ptr->cmd[0] = surf->type;
ptr->cmd[1] = surf->numrefs;
ptr++;
ptr->cmd[0] = surf->mat;
ptr++;
if (!(surf->type & SURF_SHADED) &&
(surf->type & 0x0f) == SURF_POLYGON) {
#ifdef USE_FLOATS
(ptr++)->f = surf->normal[0];
(ptr++)->f = surf->normal[1];
(ptr++)->f = surf->normal[2];
#else
(ptr++)->i = surf->normal[0] * 65536.0;
(ptr++)->i = surf->normal[1] * 65536.0;
(ptr++)->i = surf->normal[2] * 65536.0;
#endif
}
/*
Best Practices on the PowerVR MBX
▪ For best performance, you should interleave the standard vertex attributes in the following order: Position, Normal, Color, TexCoord0, TexCoord1, PointSize, Weight, MatrixIndex.
==>> V,N,T
*/
for (j=0; j<surf->numrefs; j++) {
int idx = surf->vrefs[j];
check_object_bbox(obj, obj->verts[idx]);
// VERTEX DATA
#ifdef USE_FLOATS
(ptr++)->f = obj->verts[idx][0];
(ptr++)->f = obj->verts[idx][1];
(ptr++)->f = obj->verts[idx][2];
#else
(ptr++)->i = obj->verts[idx][0] * 65536.0;
(ptr++)->i = obj->verts[idx][1] * 65536.0;
(ptr++)->i = obj->verts[idx][2] * 65536.0;
#endif
// NORMAL DATA
if (surf->type & SURF_SHADED) {
#ifdef USE_FLOATS
(ptr++)->f = obj->verts[idx][3];
(ptr++)->f = obj->verts[idx][4];
(ptr++)->f = obj->verts[idx][5];
#else
(ptr++)->i = obj->verts[idx][3] * 65536.0;
(ptr++)->i = obj->verts[idx][4] * 65536.0;
(ptr++)->i = obj->verts[idx][5] * 65536.0;
#endif
} else if ((surf->type & 0x0f) == SURF_TRI_STRIP) {
float n[3];
int i = j-2;
if (i < 0)
i = 0;
if (i%2)
make_normal(n,
obj->verts[surf->vrefs[i+1]],
obj->verts[surf->vrefs[i]],
obj->verts[surf->vrefs[i+2]]);
else
make_normal(n,
obj->verts[surf->vrefs[i]],
obj->verts[surf->vrefs[i+1]],
obj->verts[surf->vrefs[i+2]]);
#ifdef USE_FLOATS
(ptr++)->f = n[0];
(ptr++)->f = n[1];
(ptr++)->f = n[2];
#else
(ptr++)->i = n[0] * 65536.0;
(ptr++)->i = n[1] * 65536.0;
(ptr++)->i = n[2] * 65536.0;
#endif
}
// TEXTURE DATA
if (obj->texture) {
float repu = 1.0;
float repv = 1.0;
float offu = 0.0;
float offv = 0.0;
if (obj->texrep) {
repu = obj->texrep[0];
repv = obj->texrep[1];
}
if (obj->texoff) {
offu = obj->texoff[0];
offv = obj->texoff[1];
}
#ifdef USE_FLOATS
(ptr++)->f = (offu + surf->texrefs[j].texu*repu);
(ptr++)->f = (1.0 - (offv + surf->texrefs[j].texv*repv));
#else
(ptr++)->i = (offu + surf->texrefs[j].texu*repu) * 65536.0;
(ptr++)->i = (1.0 - (offv + surf->texrefs[j].texv*repv)) * 65536.0;
#endif
}
}
}
} else {
ptr->cmd[0] = surf->type;
ptr->cmd[1] = surf->numrefs;
ptr++;
ptr->cmd[0] = surf->mat;
ptr++;
for (j=0; j<surf->numrefs; j++) {
int idx = surf->vrefs[j];
check_object_bbox(obj, obj->verts[idx]);
#ifdef USE_FLOATS
(ptr++)->f = obj->verts[idx][0];
(ptr++)->f = obj->verts[idx][1];
(ptr++)->f = obj->verts[idx][2];
#else
(ptr++)->i = obj->verts[idx][0] * 65536.0;
(ptr++)->i = obj->verts[idx][1] * 65536.0;
(ptr++)->i = obj->verts[idx][2] * 65536.0;
#endif
}
}
}
if (obj->numvert > 0 && obj->verts) {
free(obj->verts);
obj->verts = NULL;
}
if (obj->numsurf > 0 && obj->surfs) {
for (i=0; i<obj->numsurf; i++) {
if (obj->surfs[i])
free_ac3d_surf(obj->surfs[i]);
}
free(obj->surfs);
obj->surfs = NULL;
}
}
static
void make_normals(AC3DObject *obj)
{
int i, j, k;
for (i=0; i<obj->numvert; i++) {
float n[3] = {0.0, 0.0, 0.0};
int ns = 0;
for (j=0; j<obj->numsurf; j++) {
AC3DSurf *surf = obj->surfs[j];
for (k=0; k<surf->numrefs; k++) {
if (surf->vrefs[k] == i) {
n[0] += surf->normal[0];
n[1] += surf->normal[1];
n[2] += surf->normal[2];
ns++;
}
}
}
if (ns > 0) {
n[0] /= ns;
n[1] /= ns;
n[2] /= ns;
}
obj->verts[i][3] = n[0];
obj->verts[i][4] = n[1];
obj->verts[i][5] = n[2];
}
}
static
AC3DObject *read_ac3d_object(FILE *fp, char **err)
{
char buf[256];
AC3DObject *obj = (AC3DObject*)malloc(sizeof(AC3DObject));
int do_read = 1;
memset(obj, 0, sizeof(AC3DObject));
obj->texid = -1;
obj->enabled = true;