-
Notifications
You must be signed in to change notification settings - Fork 30
/
mload.c
executable file
·1925 lines (1662 loc) · 53.5 KB
/
mload.c
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
/*==========================================================================
*
* File: Mload.c
* loads in a bin....
* extracts the names of the ppm's and creates a suface and material...
* make an execution list per group of....
* make a list of D3DVERTEX's.....and TRIANGLE's....
***************************************************************************/
/*
Execution list format with visibility info and vertex index (*.mxv):
num_texture_files : u_int16_t
texture_file_name[num_texture_files] : '\0' separated strings
num_groups : u_int16_t
{
num_exec_lists : u_int16_t
{
exec_type : u_int16_t // 0 = entirely opaque, 1 = contains transparencies
num_vertices : u_int16_t
vertex(x,y,z,reserved,colour,specular,tu,tv)[num_vertices] : x, y, z float, others u_int32_t
num_texture_groups : u_int16_t
{
texture_type : u_int16_t // 0 = normal, 1 = environment mapped
start_vertex : u_int16_t
num_vertices : u_int16_t
texture_no : u_int16_t
num_triangles : u_int16_t
triangles(v0,v1,v2,pad16,nx,ny,nz)[num_triangles] : v? u_int16_t, pad16 u_int16_t, n? float
}[num_texture_groups]
num_animating_polys : u_int16_t
{
animation : u_int16_t
frames : u_int16_t
vertices : u_int16_t
vertex[ vertices ] : u_int16_t
{
{
tu, tv : float
}[ vertices ]
}[ frames ]
}[ num_animating_polys ]
}[num_exec_lists]
}[num_groups]
num_animations : u_int16_t
{
frames : u_int16_t
maxloops : u_int16_t
animsize : u_int16_t
animdata[ animsize ] : u_int16_t
}[ num_animations ]
mxvflag : u_int16_t // always 1 for mxv format
{
group_name : '\0' separated string
group_center(x, y, z) : all float
group_half_size(x, y, z) : all float
num_portals_in_group : u_int16_t
{
num_vertices_in_portal : u_int16_t
vertex(x, y, z)[num_vertices_in_portal] : all float
num_groups_visible_from_portal : u_int16_t
groups_visible_from_portal[num_groups_visible_from_portal] : u_int16_t
}[num_portals_in_group]
}[num_groups]
mxviflag : u_int16_t // always 1 for mxv format with vertex index
{
num_exec_lists : u_int16_t
{
cell_origin(x, y, z) : all float
xcells, ycells, zcells : all u_int16_t
num_vertex_indices : u_int16_t
vertex_index[num_vertex_indices] : u_int16_t // points into exec buffer vertex[]
vertex_cell(num_verts_in_cell : u_int16_t
start_vert_in_cell : u_int16_t)[num_vertex_cells] // points into vertex_index[]
}[num_exec_lists]
}[num_groups]
num_start_points : u_int16_t
{
x, y, z : float
in_group : u_int16_t
}[num_start_points]
*/
/*===================================================================
Include File...
===================================================================*/
#include "main.h"
#include <stdio.h>
#include "new3d.h"
#include "quat.h"
#include "local.h"
#include "compobjects.h"
#include "bgobjects.h"
#include "object.h"
#include "networking.h"
#include "mload.h"
#include "lights.h"
#include "triggers.h"
#include "util.h"
#include "oct2.h"
#include "render.h"
/*===================================================================
Externals...
===================================================================*/
extern TRIGGERMOD * TrigMods;
extern MATRIX ProjMatrix;
extern TLOADHEADER Tloadheader;
extern MLOADHEADER Mloadheader;
extern DWORD CurrentSrcBlend;
extern DWORD CurrentDestBlend;
extern DWORD CurrentTextureBlend;
extern float framelag;
extern u_int32_t AnimOncePerFrame; // used for stuff that is displayed more than once in a single frame..
bool FindGroupConnections( MLOADHEADER *m );
bool ReadGroupConnections( MLOADHEADER *m, char **pbuf );
void FreeGroupConnections( void );
void ReadSoundInfo( MLOADHEADER *m, char **pbuf );
/*===================================================================
Defines
===================================================================*/
#define MXV_VERSION_NUMBER 3
#define UV_FUDGE (0.5F)
#define SAME_UV (2.0F)
/*===================================================================
Globals...
===================================================================*/
float UV_Fix = UV_FUDGE;
u_int16_t num_start_positions = 0;
u_int16_t last_start_position = 0;
GAMESTARTPOS StartPositions[MAXSTARTPOSITIONS]; // pos and group info...
u_int16_t FirstStartPositionInGroup[MAXGROUPS];
float SoundInfo[MAXGROUPS][MAXGROUPS];
bool TempGroups[MAXGROUPS];
void InitSoundInfo( MLOADHEADER * Mloadheader );
u_int16_t GroupTris[ MAXGROUPS ];
/*===================================================================
Procedure : Dont Know....
Input : Who can tell...
Output : int93???
===================================================================*/
static bool read_visible( MLOADHEADER * Mloadheader, VISTREE *v, u_int16_t group, u_int16_t **Uint16PntPtr )
{
bool ok;
u_int16_t *ptr;
u_int16_t vnum;
ok = true;
ptr = *Uint16PntPtr;
if ( *ptr < MAXPORTALSPERGROUP ) // nb cannot check against Mloadheader->Group[group].num_portals yet...
{
v->portal = &Mloadheader->Group[group].Portal[*ptr++];
v->group = *ptr++;
if ( v->group < Mloadheader->num_groups )
{
v->num_visible = *ptr++;
if ( v->num_visible )
{
v->visible = (VISTREE *) calloc( v->num_visible, sizeof( VISTREE ) );
if ( v->visible )
{
for ( vnum = 0; vnum < v->num_visible; vnum++ )
{
if ( !read_visible( Mloadheader, &v->visible[ vnum ], v->group, &ptr ) )
{
ok = false;
break;
}
}
}
else
ok = false;
}
else
v->visible = NULL;
}
else
ok = false;
}
else
ok = false;
*Uint16PntPtr = ptr;
return ok;
}
void FixUV( LPTRIANGLE Tri, LPLVERTEX Vert, u_int16_t Tpage, LPLVERTEX Orig_Vert )
{
static LPLVERTEX TriVert[ 3 ], Orig_TriVert[ 3 ];
static float u[ 3 ], v[ 3 ];
float du = 0.0f, dv = 0.0f;
int j = 0, k = 0;
int zu = 0, zv = 0, uz = 0, vz = 0;
float su = 0.0f, sv = 0.0f;
int Xsize = 0, Ysize = 0;
Xsize = Tloadheader.Xsize[Tpage] / ( 1 << Tloadheader.CurScale[Tpage] );
Ysize = Tloadheader.Ysize[Tpage] / ( 1 << Tloadheader.CurScale[Tpage] );
if( bSquareOnly )
{
if( Xsize != Ysize )
{
if( Xsize > Ysize )
{
Xsize = Ysize;
// DebugPrintf("-- here -- shrinking x to y = %d\n",Xsize);
}
if( Ysize > Xsize )
{
Ysize = Xsize;
// DebugPrintf("-- here -- shrinking y to x = %d\n",Xsize);
}
}
}
u[ 0 ] = Vert[ Tri->v1 ].tu;
u[ 1 ] = Vert[ Tri->v2 ].tu;
u[ 2 ] = Vert[ Tri->v3 ].tu;
v[ 0 ] = Vert[ Tri->v1 ].tv;
v[ 1 ] = Vert[ Tri->v2 ].tv;
v[ 2 ] = Vert[ Tri->v3 ].tv;
zu = zv = 0;
for ( j = 0; j < 3; j++ )
{
k = ( j + 1 ) % 3;
du = u[ k ] - u[ j ];
dv = v[ k ] - v[ j ];
if ( fabs( du * Xsize ) < SAME_UV )
{
uz = j;
zu++;
}
if ( fabs( dv * Ysize ) < SAME_UV )
{
vz = j;
zv++;
}
}
/*
if ( zu != 1 || zv != 1 )
{
DebugPrintf("-- here -- not a square texture\n");
// return; // not a square texture
}
*/
TriVert[ 0 ] = &Vert[ Tri->v1 ];
TriVert[ 1 ] = &Vert[ Tri->v2 ];
TriVert[ 2 ] = &Vert[ Tri->v3 ];
Orig_TriVert[ 0 ] = &Orig_Vert[ Tri->v1 ];
Orig_TriVert[ 1 ] = &Orig_Vert[ Tri->v2 ];
Orig_TriVert[ 2 ] = &Orig_Vert[ Tri->v3 ];
du = ( UV_Fix / Xsize );
dv = ( UV_Fix / Ysize );
su = du * ( ( TriVert[ ( uz + 2 ) % 3 ]->tu > TriVert[ uz ]->tu ) ? 1 : -1 );
sv = dv * ( ( TriVert[ ( vz + 2 ) % 3 ]->tv > TriVert[ vz ]->tv ) ? 1 : -1 );
if ( fabs( TriVert[ uz ]->tu + su - Orig_TriVert[ uz ]->tu ) <= du )
TriVert[ uz ]->tu += su;
k = ( uz + 1 ) % 3;
if ( fabs( TriVert[ k ]->tu + su - Orig_TriVert[ k ]->tu ) <= du )
TriVert[ k ]->tu += su;
if ( fabs( TriVert[ vz ]->tv + sv - Orig_TriVert[ vz ]->tv ) <= dv )
TriVert[ vz ]->tv += sv;
k = ( vz + 1 ) % 3;
if ( fabs( TriVert[ k ]->tv + sv - Orig_TriVert[ k ]->tv ) <= dv )
TriVert[ k ]->tv += sv;
}
void
FixUV_Anim( POLYANIM *PolyAnim, LPLVERTEX Vert, LPLVERTEX Orig_Vert )
{
int vnum, frame, vi;
TANIMUV *UV;
UV = PolyAnim->UVs;
for ( frame = 0; frame < PolyAnim->frames; frame++ )
{
for ( vnum = 0; vnum < PolyAnim->vertices; vnum++ )
{
vi = PolyAnim->vert[ vnum ];
UV->u += Vert[ vi ].tu - Orig_Vert[ vi ].tu;
UV->v += Vert[ vi ].tv - Orig_Vert[ vi ].tv;
UV++;
}
}
}
/*===================================================================
Procedure : Load .Mxv File
Input : char * Filename , MLOADHEADER *
Output : Nothing
===================================================================*/
bool Mload( char * Filename, MLOADHEADER * Mloadheader )
{
char * FileNamePnt;
char * Buffer;
char * tempBuffer;
u_int16_t * Uint16Pnt;
u_int16_t * Uint16Pnt2;
int16_t * int16Pnt;
float * FloatPnt;
MFACE * MFacePnt;
VERT * VertPnt;
VERTEXCELL * VertexCellPnt;
TRIANGLE FacePnt; // was a pointer
LPTRIANGLE TempFacePnt;
u_int16_t exec_type; // the type of execute buffer
u_int16_t texture_type; // the type of texture...0 normal 1 env
u_int16_t num_vertices; // overall number of verts
u_int16_t num_triangle_groups;// number of triangle groups
u_int16_t num_triangles; // number of triangles in group
u_int16_t group_vertex_start; // where in the vert list to start processing
u_int16_t group_vertex_num; // and how many to do...
u_int16_t verts;
u_int16_t portal;
u_int16_t ExecSize;
LPOLDLVERTEX lpLVERTEX2 = NULL;
LPLVERTEX lpLVERTEX;
LPLVERTEX lpBufStart = NULL;
WORD *lpIndices = NULL;
NORMAL * lpNormals = NULL;
int indexOffset = 0;
int ibIndex = 0;
int numTriangles = 0;
int tempInt = 0;
int triangleCount = 0;
STARTPOS * StartPosPnt;
int i,e,o;
u_int16_t tpage;
int execbuf;
int group;
int r,g,b,a;
COLOR color;
float * up;
u_int16_t animation,frames,vertices;
POLYANIM * PolyAnim;
TANIMUV * TanimUV;
int * intPnt;
u_int16_t num_animations;
u_int16_t maxloops;
u_int16_t animsize;
u_int16_t NumColours;
u_int32_t TempColour;
u_int32_t * Uint32Pnt;
TEXTUREANIMINFOINDEX * TAnimInfoIndexPnt;
u_int16_t polyanim;
u_int16_t initstate;
int16_t whenstoppedtriggermod;
COLOR *ambient;
int colourkey = 0;
DebugPrintf("Mload - %s\n",Filename);
// Mloadheader is not valid until everything has been done..
Mloadheader->state = false;
Buffer = Mloadheader->Buffer;
if( Buffer == NULL)
{
Msg( "Mload : Error Allocating Buffer\n" );
return false;
}
/* get the number of groups */
Uint16Pnt = (u_int16_t *) Buffer;
Mloadheader->num_groups = *Uint16Pnt++;
Buffer = (char *) Uint16Pnt;
if ( Mloadheader->num_groups > MAXGROUPS )
{
Msg( "Mload : NumGroups > MAXGROUPS\n" );
return false;
}
for( group = 0; group < Mloadheader->num_groups; group++)
{
GroupTris[ group ] = 0;
/* get the number of execbufs in this group */
Uint16Pnt = (u_int16_t *) Buffer;
Mloadheader->Group[group].num_execbufs = *Uint16Pnt++;
Buffer = (char *) Uint16Pnt;
if ( Mloadheader->Group[group].num_execbufs > MAXEXECBUFSPERGROUP )
{
Msg( "Mload : NumExecBufs > MAXEXECBUFSPERGROUP\n" );
return false;
}
for( execbuf = 0; execbuf < Mloadheader->Group[group].num_execbufs; execbuf++)
{
Mloadheader->Group[group].num_animating_polys[execbuf] = 0;
int16Pnt = (int16_t *) Buffer;
ExecSize = *int16Pnt++;
ExecSize += 512;
Buffer = (char *) int16Pnt;
Uint16Pnt = (u_int16_t *) Buffer;
/* get the type of execution buffer */
exec_type = *Uint16Pnt++;
/* get the number of verts in execution buffer */
num_vertices = *Uint16Pnt++;
Buffer = (char *) Uint16Pnt;
/* record how what type of exec buffer */
Mloadheader->Group[group].exec_type[execbuf] = exec_type;
/* record how many verts there are in the exec buffer */
Mloadheader->Group[group].num_verts_per_execbuf[execbuf] = num_vertices;
lpLVERTEX2 = (LPOLDLVERTEX ) Buffer;
if (!FSCreateVertexBuffer((RENDEROBJECT*)&Mloadheader->Group[group].renderObject[execbuf], num_vertices))
{
Msg( "Mload : MakeExecBuffer Failed\n" );
return false;
}
DebugPrintf("created buffer to hold :%d verts\n", num_vertices);
if (!(FSLockVertexBuffer((RENDEROBJECT*)&Mloadheader->Group[group].renderObject[execbuf], &lpLVERTEX)))
{
Msg( "Mload() lock failed in %s\n", Filename );
return false;
}
lpBufStart = lpLVERTEX;
/* copy the vertex data into the execute buffer */
for ( i=0 ; i<num_vertices; i++)
{
LPOLDLVERTEX old = lpLVERTEX2;
lpLVERTEX[i].x = old->x;
lpLVERTEX[i].y = old->y;
lpLVERTEX[i].z = old->z;
lpLVERTEX[i].tu = old->tu;
lpLVERTEX[i].tv = old->tv;
color = old->color;
a = (color>>24)&255;
r = (color>>16)&255;
g = (color>>8)&255;
b = color&255;
/* bjd curr driver = 0 use to be software mode
if(render_info.CurrDriver == 0) // is it or ramp mode..
{
a = 128;
}
else
*/
{
//r = Tab[r];
//g = Tab[g];
//b = Tab[b];
#if ACTUAL_TRANS
a = 128;
#else
a = 255;
#endif
}
// right here you could override texture coloring if you wanted
color = RGBA_MAKE( r , g , b , a );
lpLVERTEX[i].color = color;
lpLVERTEX2->color = color;
// bjd - CHECK lpLVERTEX2->dwReserved |= 0xff000000;
// nothing used specular so we removed it from LVERTEX
//lpLVERTEX->specular = lpLVERTEX2->specular;
lpLVERTEX2++;
}
/* bjd - allows us to retrieve copies of the original vertices in the new format! */
Mloadheader->Group[group].originalVerts[execbuf] = malloc(sizeof(LVERTEX) * num_vertices);
memmove(Mloadheader->Group[group].originalVerts[execbuf], &lpLVERTEX[0], sizeof(LVERTEX) * num_vertices);//memcpy
Buffer = (char *) lpLVERTEX2;
Uint16Pnt = (u_int16_t *) Buffer;
num_triangle_groups = *Uint16Pnt++;
Buffer = (char *) Uint16Pnt;
triangleCount = 0;
tempInt = 0;
tempBuffer = Buffer;
/* find out how many vertexes we'll need to load into our vertex buffer */
for ( i=0 ; i<num_triangle_groups; i++)
{
u_int16_t *temp = (u_int16_t *) tempBuffer;
temp += 4;
numTriangles = *temp++;
tempBuffer = (char*) temp;
MFacePnt = (MFACE *) tempBuffer;
MFacePnt += numTriangles;
tempBuffer = (char *) MFacePnt;
triangleCount += numTriangles;
}
/* create an index buffer */
if (!FSCreateIndexBuffer((RENDEROBJECT*)&Mloadheader->Group[group].renderObject[execbuf], triangleCount * 3))
{
return false;
}
/* lock the index buffer */
if (!(FSLockIndexBuffer((RENDEROBJECT*)&Mloadheader->Group[group].renderObject[execbuf], &lpIndices)))
{
Msg( "Mload() lock failed in %s\n", Filename );
return false;
}
if (!FSCreateNormalBuffer((RENDEROBJECT*)&Mloadheader->Group[group].renderObject[execbuf], triangleCount))
{
Msg( "Mload() failed to create normal buffer %s\n", Filename );
return false;
}
if (!(FSLockNormalBuffer((RENDEROBJECT*)&Mloadheader->Group[group].renderObject[execbuf], &lpNormals)))
{
Msg( "Mload() failed to lock normal buffer %s\n", Filename );
return false;
}
ibIndex = 0;
indexOffset = 0;
for ( i=0 ; i<num_triangle_groups; i++)
{
Uint16Pnt = (u_int16_t *) Buffer;
texture_type = *Uint16Pnt++;
group_vertex_start = *Uint16Pnt++;
group_vertex_num = *Uint16Pnt++;
tpage = *Uint16Pnt++;
num_triangles = *Uint16Pnt++;
Buffer = (char *) Uint16Pnt;
GroupTris[ group ] += num_triangles;
MFacePnt = (MFACE *) Buffer;
TempFacePnt = (LPTRIANGLE ) lpIndices;
/* copy the faces data into the execute buffer */
for( e=0; e<num_triangles; e++)
{
FacePnt.v1 = MFacePnt->v1;
FacePnt.v2 = MFacePnt->v2;
FacePnt.v3 = MFacePnt->v3;
lpIndices[ibIndex] = FacePnt.v1;
ibIndex++;
lpIndices[ibIndex] = FacePnt.v2;
ibIndex++;
lpIndices[ibIndex] = FacePnt.v3;
ibIndex++;
/*
FacePnt->v1 = MFacePnt->v1;
FacePnt->v2 = MFacePnt->v2;
FacePnt->v3 = MFacePnt->v3;
*/
lpNormals->nx = MFacePnt->nx;
lpNormals->ny = MFacePnt->ny;
lpNormals->nz = MFacePnt->nz;
if ( MFacePnt->pad & 1 )
{
// colourkey triangle found
colourkey++;
MFacePnt->pad &= ~1;
}
//if ( AllWires )
//FacePnt->wFlags = D3DTRIFLAG_EDGEENABLETRIANGLE;
//else
//FacePnt->wFlags = MFacePnt->pad;
FixUV( &FacePnt, lpBufStart, tpage, Mloadheader->Group[group].originalVerts[execbuf] );
//FacePnt++;
//MFacePnt++;
MFacePnt++;
lpNormals++;
tempInt+=3;
}
//lpPointer = (LPVOID) FacePnt;
Buffer = (char *) MFacePnt;
Mloadheader->Group[group].renderObject[execbuf].textureGroups[i].numVerts = num_vertices;
Mloadheader->Group[group].renderObject[execbuf].textureGroups[i].numTriangles = num_triangles;
/* keep track of our offset into our index buffer */
Mloadheader->Group[ group ].renderObject[execbuf].textureGroups[i].startIndex = indexOffset;
indexOffset += num_triangles * 3;
Mloadheader->Group[group].renderObject[execbuf].textureGroups[i].texture = Tloadheader.lpTexture[Mloadheader->TloadIndex[tpage]];
Mloadheader->Group[group].renderObject[execbuf].textureGroups[i].colourkey = Tloadheader.ColourKey[Mloadheader->TloadIndex[tpage]];
{
LEVELRENDEROBJECT * obj = &(Mloadheader->Group[group].renderObject[execbuf]);
INCREASE_TEXTURE_GROUPS( obj );
}
}
Mloadheader->Group[group].polyanim[execbuf] = NULL;
Mloadheader->Group[group].num_animating_polys[execbuf] = 0;
// this go here?
/* unlock the vertex buffer */
if (!(FSUnlockVertexBuffer((RENDEROBJECT*)&Mloadheader->Group[group].renderObject[execbuf])))
{
Msg( "Mxload() vb unlock failed in %s\n", Filename );
return false ;
}
/* unlock the index buffer */
if (!(FSUnlockIndexBuffer((RENDEROBJECT*)&Mloadheader->Group[group].renderObject[execbuf])))
{
Msg( "Mxload() ib unlock failed in %s\n", Filename );
return false ;
}
if (!(FSUnlockNormalBuffer((RENDEROBJECT*)&Mloadheader->Group[group].renderObject[execbuf])))
{
Msg( "Mxload() normal unlock failed in %s\n", Filename );
return false ;
}
}
}
Uint16Pnt = (u_int16_t *) Buffer;
// is there any vispoly info.....
if ( *Uint16Pnt != 0)
{
u_int16_t pnum, vnum;
if ( *Uint16Pnt != 2 )
{
Msg( "Mload : Not in new recursive format\n" );
return false; // error if not in new recursive group/portal visibility tree format
}
Uint16Pnt++;
/*===================================================================
* Particular Stuff Related To Visiploys
===================================================================*/
Buffer = ( char * ) Uint16Pnt;
for( group=0 ; group<Mloadheader->num_groups; group++)
{
/* copy over the groups Name */
FileNamePnt = (char*) &Mloadheader->Group[group].name;
while ( ( *FileNamePnt++ = *Buffer++ ) != 0 );
// Make sure there are no start points initially in this group..
Mloadheader->Group[group].StartPosInThisGroup = (u_int16_t) -1;
VertPnt = ( VERT* ) Buffer;
/* get the center position of the group */
Mloadheader->Group[group].center = *VertPnt++;
/* get the size of the group */
Mloadheader->Group[group].half_size = *VertPnt++;
Buffer = ( char * ) VertPnt;
Uint16Pnt = (u_int16_t *) Buffer;
/* get the number of portals in the group */
Mloadheader->Group[group].num_portals = *Uint16Pnt++;
Buffer = (char *) Uint16Pnt;
if(Mloadheader->Group[group].num_portals > MAXPORTALSPERGROUP)
{
Msg( "Mload : NumPortals > MAXPORTALSPERGROUP\n" );
return false;
}
Mloadheader->Group[group].Portal = (PORTAL*) malloc( Mloadheader->Group[group].num_portals * sizeof(PORTAL) );
for( portal = 0 ; portal < Mloadheader->Group[group].num_portals ; portal ++ )
{
Uint16Pnt = (u_int16_t *) Buffer;
/* get the number of Verts in the Portal */
Mloadheader->Group[group].Portal[portal].num_vertices_in_portal = *Uint16Pnt++;
Buffer = (char *) Uint16Pnt;
if(Mloadheader->Group[group].Portal[portal].num_vertices_in_portal > MAXVERTSPERPORTAL)
{
Msg( "Mload : More than MAXVERTSPERPORTAL\n" );
return false;
}
{
VERT sum = {0,0,0};
VERT centroid = {0,0,0};
int length = Mloadheader->Group[group].Portal[portal].num_vertices_in_portal;
VertPnt = ( VERT* ) Buffer;
for( verts = 0 ; verts < length ; verts++ )
{
VERT vert = *VertPnt++;
sum.x += vert.x;
sum.y += vert.y;
sum.z += vert.z;
//DebugPrintf("portal %d vert %d = { %f, %f, %f }\n", portal, verts, vert->x, vert->y, vert->z);
Mloadheader->Group[group].Portal[portal].Verts[verts] = vert;
}
centroid.x = sum.x / length;
centroid.y = sum.y / length;
centroid.z = sum.z / length;
Mloadheader->Group[group].Portal[portal].centroid = centroid;
//DebugPrintf("group %d portal %d centroid = { %f, %f, %f }\n",
// group, portal, centroid.x, centroid.y, centroid.z);
}
Buffer = (char *) VertPnt;
Uint16Pnt = (u_int16_t *) Buffer;
// get the polys in the portal
Mloadheader->Group[group].Portal[portal].num_polys_in_portal = *Uint16Pnt++;
if ( Mloadheader->Group[group].Portal[portal].num_polys_in_portal > MAXPOLYSPERPORTAL )
{
Msg( "Mload : More than MAXPOLYSPERPORTAL\n" );
return false;
}
for ( pnum = 0; pnum < Mloadheader->Group[group].Portal[portal].num_polys_in_portal; pnum++ )
{
#if 1
Mloadheader->Group[group].Portal[portal].Poly[pnum] = * (MCFACE *) Uint16Pnt;
Uint16Pnt = (u_int16_t *) ( ( (MCFACE *) Uint16Pnt) + 1 );
#else
Mloadheader->Group[group].Portal[portal].Poly[pnum].num_vertices_in_poly = *Uint16Pnt++;
if ( Mloadheader->Group[group].Portal[portal].Poly[pnum].num_vertices_in_poly > MAXVERTSPERPORTALPOLY )
{
Msg( "Mload : More than MAXVERTSPERPORTALPOLY\n" );
return false;
}
for ( verts = 0; verts < Mloadheader->Group[group].Portal[portal].Poly[pnum].num_vertices_in_poly; verts++ )
{
vnum = *Uint16Pnt++;
if ( vnum >= Mloadheader->Group[group].Portal[portal].num_vertices_in_portal )
{
Msg( "Mload : Different num of verts in portal\n" );
return false;
}
Mloadheader->Group[group].Portal[portal].Poly[pnum].Verts[verts] = Mloadheader->Group[group].Portal[portal].Verts[vnum];
}
#endif
}
Buffer = (char *) Uint16Pnt;
}
}
// get the recursive group/portal visibility tree
for( group=0 ; group<Mloadheader->num_groups; group++)
{
for( portal = 0 ; portal < Mloadheader->Group[group].num_portals ; portal ++ )
{
Mloadheader->Group[group].Portal[portal].visible.portal = &Mloadheader->Group[group].Portal[portal];
Mloadheader->Group[group].Portal[portal].visible.group = *Uint16Pnt++;
if ( Mloadheader->Group[group].Portal[portal].visible.group >= Mloadheader->num_groups )
{
Msg( "Mload : Group > Num groups\n" );
return false;
}
Mloadheader->Group[group].Portal[portal].visible.num_visible = *Uint16Pnt++;
if ( Mloadheader->Group[group].Portal[portal].visible.num_visible > 0 )
{
Mloadheader->Group[group].Portal[portal].visible.visible =
(VISTREE *) calloc( Mloadheader->Group[group].Portal[portal].visible.num_visible, sizeof( VISTREE ) );
if ( !Mloadheader->Group[group].Portal[portal].visible.visible )
{
Msg( "Mload : no visible portal\n" );
return false;
}
for ( vnum = 0; vnum < Mloadheader->Group[group].Portal[portal].visible.num_visible; vnum++ )
{
if ( !read_visible( Mloadheader, &Mloadheader->Group[group].Portal[portal].visible.visible[vnum],
Mloadheader->Group[group].Portal[portal].visible.group, &Uint16Pnt ) )
{
Msg( "Mload : Read visible failed\n" );
return false;
}
}
}
else
Mloadheader->Group[group].Portal[portal].visible.visible = NULL;
}
}
Buffer = (char *) Uint16Pnt;
/*===================================================================
* End Of Visiploy Stuff
===================================================================*/
#if MXV_VERSION_NUMBER >= 3
if ( !ReadGroupConnections( Mloadheader, &Buffer ) )
return false;
ReadSoundInfo( Mloadheader, &Buffer );
#endif
/*===================================================================
* Start of Cell Index Stuff
===================================================================*/
Uint16Pnt = (u_int16_t *) Buffer;
// is there any cell index info.....
if ( *Uint16Pnt++ != 0)
{
Buffer = (char *) Uint16Pnt;
FloatPnt = ( float * ) Buffer;
Mloadheader->CellSize = 1.0F / *FloatPnt++;
Buffer = (char * ) FloatPnt;
for( group=0 ; group<Mloadheader->num_groups; group++)
{
Uint16Pnt = (u_int16_t *) Buffer;
Uint16Pnt++; //dont need the number of exec buffs......
Buffer = (char *) Uint16Pnt;
for( execbuf=0 ; execbuf<Mloadheader->Group[group].num_execbufs; execbuf++)
{
FloatPnt = (float * ) Buffer;
Mloadheader->Group[group].cell_origin[execbuf].x = *FloatPnt++;
Mloadheader->Group[group].cell_origin[execbuf].y = *FloatPnt++;
Mloadheader->Group[group].cell_origin[execbuf].z = *FloatPnt++;
Buffer = (char * ) FloatPnt;
Uint16Pnt = (u_int16_t *) Buffer;
Mloadheader->Group[group].xcells[execbuf] = *Uint16Pnt++;
Mloadheader->Group[group].ycells[execbuf] = *Uint16Pnt++;
Mloadheader->Group[group].zcells[execbuf] = *Uint16Pnt++;
Mloadheader->Group[group].numofcells[execbuf] = Mloadheader->Group[group].xcells[execbuf] *
Mloadheader->Group[group].ycells[execbuf] *
Mloadheader->Group[group].zcells[execbuf];
#if MXV_VERSION_NUMBER >= 2
if ( !execbuf )
Mloadheader->Group[group].colour_cell_pnt[0] = (COLOR*) malloc( Mloadheader->Group[group].numofcells[execbuf] * sizeof(COLOR) );
else
Mloadheader->Group[group].colour_cell_pnt[execbuf] = NULL;
if( !Mloadheader->Group[group].colour_cell_pnt[0] )
{
Msg( "Mload : Couldnt allocate enough memory for the cell colour info\n" );
return false;
}
#else
Mloadheader->Group[group].colour_cell_pnt[execbuf] = (COLOR*) malloc( Mloadheader->Group[group].numofcells[execbuf] * sizeof(COLOR) );
if( !Mloadheader->Group[group].colour_cell_pnt[execbuf] )
{
Msg( "Mload : Couldnt allocate enough memory for the cell colour info\n" );
return false;
}
#endif
Mloadheader->Group[group].num_vertex_indices[execbuf] = *Uint16Pnt++;
Mloadheader->Group[group].vertex_index_pnt[execbuf] = Uint16Pnt;
Uint16Pnt += Mloadheader->Group[group].num_vertex_indices[execbuf];
VertexCellPnt = (VERTEXCELL * ) Uint16Pnt;
Mloadheader->Group[group].vertex_cell_pnt[execbuf] = VertexCellPnt;
VertexCellPnt += Mloadheader->Group[group].numofcells[execbuf];
Buffer = (char *) VertexCellPnt;
}
#if MXV_VERSION_NUMBER >= 2
ambient = (COLOR *) Buffer;
for ( i = 0; i < Mloadheader->Group[group].numofcells[0]; i++ )
{
Mloadheader->Group[group].colour_cell_pnt[0][i] = *ambient++;
}
Buffer = (char *) ambient;
#endif
}
}
/*===================================================================
* Start of Start Pos Stuff
===================================================================*/
Uint16Pnt = (u_int16_t *) Buffer;
num_start_positions = *Uint16Pnt++;
Buffer = (char *) Uint16Pnt;
for( i = 0 ; i < MAXGROUPS ; i++ )
{
FirstStartPositionInGroup[i] = (u_int16_t) -1;
}
StartPosPnt = (STARTPOS * ) Buffer;
for( i = 0 ; i < num_start_positions ; i++ )
{
StartPositions[i].Pos.x = StartPosPnt->Pos.x;
StartPositions[i].Pos.y = StartPosPnt->Pos.y;
StartPositions[i].Pos.z = StartPosPnt->Pos.z;
StartPositions[i].Dir.x = StartPosPnt->Dir.x;
StartPositions[i].Dir.y = StartPosPnt->Dir.y;
StartPositions[i].Dir.z = StartPosPnt->Dir.z;
StartPositions[i].Up.x = StartPosPnt->Up.x;
StartPositions[i].Up.y = StartPosPnt->Up.y;
StartPositions[i].Up.z = StartPosPnt->Up.z;
StartPositions[i].Group = StartPosPnt->Group;
//Make a note in each group of which Start point is in there...
Mloadheader->Group[StartPositions[i].Group].StartPosInThisGroup = i;
if( FirstStartPositionInGroup[StartPositions[i].Group] == (u_int16_t) -1 )
{
StartPositions[i].NextInGroup = (u_int16_t) -1;
FirstStartPositionInGroup[StartPositions[i].Group] = i;
}else{
StartPositions[i].NextInGroup = FirstStartPositionInGroup[StartPositions[i].Group];
FirstStartPositionInGroup[StartPositions[i].Group] = i;
}
StartPosPnt++;
}
Buffer = (char *) StartPosPnt;
/*===================================================================
* Start of group up vector Stuff
===================================================================*/
Uint16Pnt = (u_int16_t *) Buffer;
// is there any up vector info.....
if ( *Uint16Pnt++ != 0)
{
Buffer = (char *) Uint16Pnt;
up = (float *) Buffer;
for ( i = 0; i < Mloadheader->num_groups; i++ )
{
Mloadheader->Group[ i ].up.x = *up++;
Mloadheader->Group[ i ].up.y = *up++;
Mloadheader->Group[ i ].up.z = *up++;
}
Buffer = (char *) up;
}
else
{
// no up vector info -> use default (0,1,0)
for ( i = 0; i < Mloadheader->num_groups; i++ )
{
Mloadheader->Group[ i ].up.x = 0.0F;
Mloadheader->Group[ i ].up.y = 1.0F;
Mloadheader->Group[ i ].up.z = 0.0F;
}
}
}
// Background Animation Stuff......
Uint16Pnt = (u_int16_t *) Buffer;
num_animations = *Uint16Pnt++;
Mloadheader->AnimData.num_animations = num_animations;
Buffer = (char *) Uint16Pnt;
if( num_animations )
{
for( i = 0 ; i < num_animations ; i++ )
{
frames = *Uint16Pnt++;
maxloops = *Uint16Pnt++;
animsize = *Uint16Pnt++;
Mloadheader->AnimData.AnimSeq[i] = (u_int16_t *) malloc( animsize * sizeof( u_int16_t ) );
Uint16Pnt2 = Mloadheader->AnimData.AnimSeq[i];
for( e = 0 ; e < animsize ; e++ )
{
*Uint16Pnt2++ = *Uint16Pnt++;
}
}
Buffer = ( char * ) Uint16Pnt;
for( group=0 ; group<Mloadheader->num_groups; group++)