forked from ericwa/Quakespasm
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathgl_mesh.c
2109 lines (1848 loc) · 66.4 KB
/
gl_mesh.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
/*
Copyright (C) 1996-2001 Id Software, Inc.
Copyright (C) 2002-2009 John Fitzgibbons and others
Copyright (C) 2010-2014 QuakeSpasm developers
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// gl_mesh.c: triangle model functions
#include "quakedef.h"
/*
=================================================================
ALIAS MODEL DISPLAY LIST GENERATION
=================================================================
*/
/*
================
GL_MakeAliasModelDisplayLists
Saves data needed to build the VBO for this model on the hunk. Afterwards this
is copied to Mod_Extradata.
Original code by MH from RMQEngine
================
*/
void GL_MakeAliasModelDisplayLists (qmodel_t *m, aliashdr_t *paliashdr)
{
int i, j;
int maxverts_vbo;
unsigned short *indexes;
trivertx_t *verts;
aliasmesh_t *desc;
// there can never be more than this number of verts and we just put them all on the hunk
// front/back logic says we can never have more than numverts*2
maxverts_vbo = paliashdr->numverts * 2;
desc = (aliasmesh_t *) Hunk_Alloc (sizeof (aliasmesh_t) * maxverts_vbo);
// there will always be this number of indexes
indexes = (unsigned short *) Hunk_Alloc (sizeof (unsigned short) * paliashdr->numtris * 3);
paliashdr->indexes = (intptr_t) indexes - (intptr_t) paliashdr;
paliashdr->meshdesc = (intptr_t) desc - (intptr_t) paliashdr;
paliashdr->numindexes = 0;
paliashdr->numverts_vbo = 0;
for (i = 0; i < paliashdr->numtris; i++)
{
for (j = 0; j < 3; j++)
{
int v;
// index into hdr->vertexes
unsigned short vertindex = triangles[i].vertindex[j];
// basic s/t coords
int s = stverts[vertindex].s;
int t = stverts[vertindex].t;
// check for back side and adjust texcoord s
if (!triangles[i].facesfront && stverts[vertindex].onseam) s += paliashdr->skinwidth / 2;
// see does this vert already exist
for (v = 0; v < paliashdr->numverts_vbo; v++)
{
// it could use the same xyz but have different s and t
if (desc[v].vertindex == vertindex && (int) desc[v].st[0] == s && (int) desc[v].st[1] == t)
{
// exists; emit an index for it
indexes[paliashdr->numindexes++] = v;
// no need to check any more
break;
}
}
if (v == paliashdr->numverts_vbo)
{
// doesn't exist; emit a new vert and index
indexes[paliashdr->numindexes++] = paliashdr->numverts_vbo;
desc[paliashdr->numverts_vbo].vertindex = vertindex;
desc[paliashdr->numverts_vbo].st[0] = s;
desc[paliashdr->numverts_vbo++].st[1] = t;
}
}
}
switch(paliashdr->poseverttype)
{
case PV_QUAKEFORGE:
verts = (trivertx_t *) Hunk_Alloc (paliashdr->nummorphposes * paliashdr->numverts_vbo*2 * sizeof(*verts));
paliashdr->vertexes = (byte *)verts - (byte *)paliashdr;
for (i=0 ; i<paliashdr->nummorphposes ; i++)
for (j=0 ; j<paliashdr->numverts_vbo ; j++)
{
verts[i*paliashdr->numverts_vbo*2 + j] = poseverts_mdl[i][desc[j].vertindex];
verts[i*paliashdr->numverts_vbo*2 + j + paliashdr->numverts_vbo] = poseverts_mdl[i][desc[j].vertindex + paliashdr->numverts_vbo];
}
break;
case PV_QUAKE1:
verts = (trivertx_t *) Hunk_Alloc (paliashdr->nummorphposes * paliashdr->numverts_vbo * sizeof(*verts));
paliashdr->vertexes = (byte *)verts - (byte *)paliashdr;
for (i=0 ; i<paliashdr->nummorphposes ; i++)
for (j=0 ; j<paliashdr->numverts_vbo ; j++)
verts[i*paliashdr->numverts_vbo + j] = poseverts_mdl[i][desc[j].vertindex];
break;
case PV_IQM:
case PV_QUAKE3:
break; //invalid here.
}
}
#define NUMVERTEXNORMALS 162
extern float r_avertexnormals[NUMVERTEXNORMALS][3];
/*
================
GLMesh_LoadVertexBuffer
Upload the given alias model's mesh to a VBO
Original code by MH from RMQEngine
may update the mesh vbo/ebo offsets.
================
*/
void GLMesh_LoadVertexBuffer (qmodel_t *m, aliashdr_t *mainhdr)
{
//we always need vertex array data.
//if we don't support vbos(gles?) then we just use system memory.
//if we're not using glsl(gles1?), then we don't actually need all the data, but we do still need some so its easier to just alloc the lot.
int totalvbosize = 0;
const aliasmesh_t *desc;
const void *trivertexes;
byte *ebodata;
byte *vbodata;
int f;
aliashdr_t *hdr;
unsigned int numindexes, numverts;
intptr_t stofs;
intptr_t vertofs;
//count how much space we're going to need.
for(hdr = mainhdr, numverts = 0, numindexes = 0; ; )
{
switch(hdr->poseverttype)
{
case PV_QUAKE1:
totalvbosize += (hdr->nummorphposes * hdr->numverts_vbo * sizeof (meshxyz_mdl_t)); // ericw -- what RMQEngine called nummeshframes is called numposes in QuakeSpasm
break;
case PV_QUAKEFORGE:
totalvbosize += (hdr->nummorphposes * hdr->numverts_vbo * sizeof (meshxyz_mdl16_t));
break;
case PV_QUAKE3:
totalvbosize += (hdr->nummorphposes * hdr->numverts_vbo * sizeof (meshxyz_md3_t));
break;
case PV_IQM:
totalvbosize += (hdr->nummorphposes * hdr->numverts_vbo * sizeof (iqmvert_t));
break;
}
numverts += hdr->numverts_vbo;
numindexes += hdr->numindexes;
if (hdr->nextsurface)
hdr = (aliashdr_t*)((byte*)hdr + hdr->nextsurface);
else
break;
}
hdr = NULL;
vertofs = 0;
totalvbosize = (totalvbosize+7)&~7; //align it.
stofs = totalvbosize;
totalvbosize += (numverts * sizeof (meshst_t));
if (!totalvbosize) return;
if (!numindexes) return;
//create an elements buffer
ebodata = (byte *) malloc(numindexes * sizeof(unsigned short));
if (!ebodata)
return; //fatal
// create the vertex buffer (empty)
vbodata = (byte *) malloc(totalvbosize);
if (!vbodata)
{ //fatal
free(ebodata);
return;
}
memset(vbodata, 0, totalvbosize);
numindexes = 0;
for(hdr = mainhdr, numverts = 0, numindexes = 0; ; )
{
// grab the pointers to data in the extradata
desc = (aliasmesh_t *) ((byte *) hdr + hdr->meshdesc);
trivertexes = (void *) ((byte *)hdr + hdr->vertexes);
//submit the index data.
hdr->eboofs = numindexes * sizeof (unsigned short);
numindexes += hdr->numindexes;
memcpy(ebodata + hdr->eboofs, (short *) ((byte *) hdr + hdr->indexes), hdr->numindexes * sizeof (unsigned short));
hdr->vbovertofs = vertofs;
// fill in the vertices at the start of the buffer
switch(hdr->poseverttype)
{
case PV_QUAKE1:
for (f = 0; f < hdr->nummorphposes; f++) // ericw -- what RMQEngine called nummeshframes is called numposes in QuakeSpasm
{
int v;
meshxyz_mdl_t *xyz = (meshxyz_mdl_t *) (vbodata + vertofs);
const trivertx_t *tv = (const trivertx_t*)trivertexes + (hdr->numverts_vbo * f);
vertofs += hdr->numverts_vbo * sizeof (*xyz);
for (v = 0; v < hdr->numverts_vbo; v++, tv++)
{
xyz[v].xyz[0] = tv->v[0];
xyz[v].xyz[1] = tv->v[1];
xyz[v].xyz[2] = tv->v[2];
xyz[v].xyz[3] = 1; // need w 1 for 4 byte vertex compression
// map the normal coordinates in [-1..1] to [-127..127] and store in an unsigned char.
// this introduces some error (less than 0.004), but the normals were very coarse
// to begin with
xyz[v].normal[0] = 127 * r_avertexnormals[tv->lightnormalindex][0];
xyz[v].normal[1] = 127 * r_avertexnormals[tv->lightnormalindex][1];
xyz[v].normal[2] = 127 * r_avertexnormals[tv->lightnormalindex][2];
xyz[v].normal[3] = 0; // unused; for 4-byte alignment
}
}
break;
case PV_QUAKEFORGE:
for (f = 0; f < hdr->nummorphposes; f++) // ericw -- what RMQEngine called nummeshframes is called numposes in QuakeSpasm
{
int v;
meshxyz_mdl16_t *xyz = (meshxyz_mdl16_t *) (vbodata + vertofs);
const trivertx_t *tv = (const trivertx_t*)trivertexes + (hdr->numverts_vbo*2 * f);
vertofs += hdr->numverts_vbo * sizeof (*xyz);
for (v = 0; v < hdr->numverts_vbo; v++, tv++)
{
xyz[v].xyz[0] = (tv->v[0]<<8) | tv[hdr->numverts_vbo].v[0];
xyz[v].xyz[1] = (tv->v[1]<<8) | tv[hdr->numverts_vbo].v[0];
xyz[v].xyz[2] = (tv->v[2]<<8) | tv[hdr->numverts_vbo].v[0];
xyz[v].xyz[3] = 1; // need w 1 for 4 byte vertex compression
// map the normal coordinates in [-1..1] to [-127..127] and store in an unsigned char.
// this introduces some error (less than 0.004), but the normals were very coarse
// to begin with
xyz[v].normal[0] = 127 * r_avertexnormals[tv->lightnormalindex][0];
xyz[v].normal[1] = 127 * r_avertexnormals[tv->lightnormalindex][1];
xyz[v].normal[2] = 127 * r_avertexnormals[tv->lightnormalindex][2];
xyz[v].normal[3] = 0; // unused; for 4-byte alignment
}
}
break;
case PV_QUAKE3:
for (f = 0; f < hdr->nummorphposes; f++) // ericw -- what RMQEngine called nummeshframes is called numposes in QuakeSpasm
{
int v;
meshxyz_md3_t *xyz = (meshxyz_md3_t *) (vbodata + vertofs);
const md3XyzNormal_t *tv = (const md3XyzNormal_t*)trivertexes + (hdr->numverts_vbo * f);
float lat,lng;
vertofs += hdr->numverts_vbo * sizeof (*xyz);
for (v = 0; v < hdr->numverts_vbo; v++, tv++)
{
xyz[v].xyz[0] = tv->xyz[0];
xyz[v].xyz[1] = tv->xyz[1];
xyz[v].xyz[2] = tv->xyz[2];
xyz[v].xyz[3] = 1; // need w 1 for 4 byte vertex compression
// map the normal coordinates in [-1..1] to [-127..127] and store in an unsigned char.
// this introduces some error (less than 0.004), but the normals were very coarse
// to begin with
lat = (float)tv->latlong[0] * (2 * M_PI)*(1.0 / 255.0);
lng = (float)tv->latlong[1] * (2 * M_PI)*(1.0 / 255.0);
xyz[v].normal[0] = 127 * cos ( lng ) * sin ( lat );
xyz[v].normal[1] = 127 * sin ( lng ) * sin ( lat );
xyz[v].normal[2] = 127 * cos ( lat );
xyz[v].normal[3] = 0; // unused; for 4-byte alignment
}
}
break;
case PV_IQM:
for (f = 0; f < hdr->nummorphposes; f++) // ericw -- what RMQEngine called nummeshframes is called numposes in QuakeSpasm
{
int v;
iqmvert_t *xyz = (iqmvert_t *) (vbodata + vertofs);
const iqmvert_t *tv = (const iqmvert_t*)trivertexes + (hdr->numverts_vbo * f);
vertofs += hdr->numverts_vbo * sizeof (*xyz);
for (v = 0; v < hdr->numverts_vbo; v++, tv++)
xyz[v] = *tv;
}
break;
}
// fill in the ST coords at the end of the buffer
{
meshst_t *st;
float hscale, vscale;
//johnfitz -- padded skins
hscale = (float)hdr->skinwidth/(float)TexMgr_PadConditional(hdr->skinwidth);
vscale = (float)hdr->skinheight/(float)TexMgr_PadConditional(hdr->skinheight);
//johnfitz
hdr->vbostofs = stofs;
st = (meshst_t *) (vbodata + stofs);
stofs += hdr->numverts_vbo*sizeof(*st);
switch(hdr->poseverttype)
{
case PV_QUAKE3:
for (f = 0; f < hdr->numverts_vbo; f++)
{ //md3 has floating-point skin coords. use the values directly.
st[f].st[0] = hscale * desc[f].st[0];
st[f].st[1] = vscale * desc[f].st[1];
}
break;
case PV_QUAKEFORGE:
case PV_QUAKE1:
for (f = 0; f < hdr->numverts_vbo; f++)
{
st[f].st[0] = hscale * ((float) desc[f].st[0] + 0.5f) / (float) hdr->skinwidth;
st[f].st[1] = vscale * ((float) desc[f].st[1] + 0.5f) / (float) hdr->skinheight;
}
break;
case PV_IQM:
//st coords are interleaved.
break;
}
}
if (hdr->nextsurface)
hdr = (aliashdr_t*)((byte*)hdr + hdr->nextsurface);
else
break;
}
hdr = NULL;
if (gl_vbo_able)
{
// upload indexes buffer
GL_DeleteBuffersFunc (1, &m->meshindexesvbo);
GL_GenBuffersFunc (1, &m->meshindexesvbo);
GL_BindBufferFunc (GL_ELEMENT_ARRAY_BUFFER, m->meshindexesvbo);
GL_BufferDataFunc (GL_ELEMENT_ARRAY_BUFFER, numindexes * sizeof (unsigned short), ebodata, GL_STATIC_DRAW);
// upload vertexes buffer
GL_DeleteBuffersFunc (1, &m->meshvbo);
GL_GenBuffersFunc (1, &m->meshvbo);
GL_BindBufferFunc (GL_ARRAY_BUFFER, m->meshvbo);
GL_BufferDataFunc (GL_ARRAY_BUFFER, totalvbosize, vbodata, GL_STATIC_DRAW);
free (vbodata);
free (ebodata);
m->meshvboptr = NULL;
m->meshindexesvboptr = NULL;
}
else
{
m->meshvboptr = vbodata;
m->meshindexesvboptr = ebodata;
}
// invalidate the cached bindings
GL_ClearBufferBindings ();
}
/*
================
GLMesh_LoadVertexBuffers
Loop over all precached alias models, and upload each one to a VBO.
================
*/
void GLMesh_LoadVertexBuffers (void)
{
int j;
qmodel_t *m;
aliashdr_t *hdr;
for (j = 1; j < MAX_MODELS; j++)
{
if (!(m = cl.model_precache[j])) break;
if (m->type != mod_alias) continue;
hdr = (aliashdr_t *) Mod_Extradata (m);
GLMesh_LoadVertexBuffer (m, hdr);
}
}
/*
================
GLMesh_DeleteVertexBuffers
Delete VBOs for all loaded alias models
================
*/
void GLMesh_DeleteVertexBuffers (void)
{
int j;
qmodel_t *m;
if (!gl_vbo_able)
return;
for (j = 1; j < MAX_MODELS; j++)
{
if (!(m = cl.model_precache[j])) break;
if (m->type != mod_alias) continue;
if (m->meshvbo)
GL_DeleteBuffersFunc (1, &m->meshvbo);
m->meshvbo = 0;
free(m->meshvboptr);
m->meshvboptr = NULL;
if (m->meshindexesvbo)
GL_DeleteBuffersFunc (1, &m->meshindexesvbo);
m->meshindexesvbo = 0;
free(m->meshindexesvboptr);
m->meshindexesvboptr = NULL;
}
GL_ClearBufferBindings ();
}
//from gl_model.c
extern char loadname[]; // for hunk tags
extern char diskname[]; // for skin files/etc.
void Mod_CalcAliasBounds (aliashdr_t *a);
#define MD3_VERSION 15
//structures from Tenebrae
typedef struct {
int ident;
int version;
char name[64];
int flags; //assumed to match quake1 models, for lack of somewhere better.
int numFrames;
int numTags;
int numSurfaces;
int numSkins;
int ofsFrames;
int ofsTags;
int ofsSurfaces;
int ofsEnd;
} md3Header_t;
//then has header->numFrames of these at header->ofs_Frames
typedef struct md3Frame_s {
vec3_t bounds[2];
vec3_t localOrigin;
float radius;
char name[16];
} md3Frame_t;
//there are header->numSurfaces of these at header->ofsSurfaces, following from ofsEnd
typedef struct {
int ident; //
char name[64]; // polyset name
int flags;
int numFrames; // all surfaces in a model should have the same
int numShaders; // all surfaces in a model should have the same
int numVerts;
int numTriangles;
int ofsTriangles;
int ofsShaders; // offset from start of md3Surface_t
int ofsSt; // texture coords are common for all frames
int ofsXyzNormals; // numVerts * numFrames
int ofsEnd; // next surface follows
} md3Surface_t;
//at surf+surf->ofsXyzNormals
/*typedef struct {
short xyz[3];
byte latlong[2];
} md3XyzNormal_t;*/
//surf->numTriangles at surf+surf->ofsTriangles
typedef struct {
int indexes[3];
} md3Triangle_t;
//surf->numVerts at surf+surf->ofsSt
typedef struct {
float s;
float t;
} md3St_t;
typedef struct {
char name[64];
int shaderIndex;
} md3Shader_t;
qboolean Mod_GetShaderNameForSurface(const char *skinfile, const char *surfacename, char *texturename, size_t texturenamesize)
{
const char *linestart = skinfile;
//lots of `surfacename,shadername` pair lines.
while (*skinfile)
{
if (*skinfile == ',')
{
while (*linestart == ' ' || *linestart == '\t')
linestart++;
if (skinfile - linestart == strlen(surfacename))
if (!strncmp(linestart, surfacename, skinfile-linestart))
{ //okay, this is the one we're after.
skinfile++;
while(*skinfile == ' ' || *skinfile == '\t')
skinfile++;
linestart = skinfile;
while (*skinfile && *skinfile != '\n')
skinfile++;
while (skinfile > linestart && (skinfile[-1] == '\r' || skinfile[-1] == ' ' || skinfile[-1] == '\t'))
skinfile--;
if (skinfile-linestart >= texturenamesize)
return false; //too long.
memcpy(texturename, linestart, skinfile-linestart);
texturename[skinfile-linestart] = 0;
return true;
}
//walk to the end of the line.
while (*skinfile && *skinfile != '\n')
skinfile++;
}
else if (*skinfile++ == '\n')
linestart = skinfile;
}
return false;
}
void Mod_LoadMD3Model (qmodel_t *mod, void *buffer)
{
md3Header_t *pinheader;
md3Surface_t *pinsurface;
md3Frame_t *pinframes;
md3Triangle_t *pintriangle;
unsigned short *poutindexes;
md3XyzNormal_t *pinvert;
md3XyzNormal_t *poutvert;
md3St_t *pinst;
aliasmesh_t *poutst;
md3Shader_t *pinshader;
int size;
int start, end, total;
int ival, j;
int numsurfs, surf;
int numframes;
aliashdr_t *outhdr;
char *skinfile[countof(outhdr->textures)];
unsigned int numskinfiles;
start = Hunk_LowMark ();
pinheader = (md3Header_t *)buffer;
ival = LittleLong (pinheader->version);
if (ival != MD3_VERSION)
Sys_Error ("%s has wrong version number (%i should be %i)",
diskname, ival, MD3_VERSION);
numsurfs = LittleLong (pinheader->numSurfaces);
numframes = LittleLong(pinheader->numFrames);
if (numframes > MAXALIASFRAMES)
Sys_Error ("%s has too many frames (%i vs %i)",
diskname, numframes, MAXALIASFRAMES);
if (!numsurfs)
Sys_Error ("%s has nosurfaces", diskname);
pinframes = (md3Frame_t*)((byte*)buffer + LittleLong(pinheader->ofsFrames));
//
// allocate space for a working header, plus all the data except the frames,
// skin and group info
//
size = sizeof(aliashdr_t) + (numframes-1) * sizeof (outhdr->frames[0]);
outhdr = (aliashdr_t *) Hunk_AllocName (size * numsurfs, loadname);
for (numskinfiles = 0; numskinfiles < countof(skinfile); numskinfiles++)
{ //q3 doesn't really support multiple skins any other way. plus this lets us fix up some texture paths...
skinfile[numskinfiles] = (char*)COM_LoadMallocFile(va("%s_%i.skin", diskname, numskinfiles), NULL);
if (!skinfile[numskinfiles])
break;
}
for (surf = 0, pinsurface = (md3Surface_t*)((byte*)buffer + LittleLong(pinheader->ofsSurfaces)); surf < numsurfs; surf++, pinsurface = (md3Surface_t*)((byte*)pinsurface + LittleLong(pinsurface->ofsEnd)))
{
aliashdr_t *osurf = (aliashdr_t*)((byte*)outhdr + size*surf);
if (LittleLong(pinsurface->ident) != (('I'<<0)|('D'<<8)|('P'<<16)|('3'<<24)))
Sys_Error ("%s corrupt surface ident", diskname);
if (LittleLong(pinsurface->numFrames) != numframes)
Sys_Error ("%s mismatched framecounts", diskname);
if (surf+1 < numsurfs)
osurf->nextsurface = size;
else
osurf->nextsurface = 0;
osurf->poseverttype = PV_QUAKE3;
osurf->numverts_vbo = osurf->numverts = LittleLong(pinsurface->numVerts);
pinvert = (md3XyzNormal_t*)((byte*)pinsurface + LittleLong(pinsurface->ofsXyzNormals));
poutvert = (md3XyzNormal_t *) Hunk_Alloc (numframes * osurf->numverts * sizeof(*poutvert));
osurf->vertexes = (byte *)poutvert - (byte *)osurf;
for (ival = 0; ival < numframes; ival++)
{
osurf->frames[ival].firstpose = ival;
osurf->frames[ival].numposes = 1;
osurf->frames[ival].interval = 0.1;
q_strlcpy(osurf->frames[ival].name, pinframes->name, sizeof(osurf->frames[ival].name));
for (j = 0; j < 3; j++)
{ //fixme...
osurf->frames[ival].bboxmin.v[j] = 0;
osurf->frames[ival].bboxmax.v[j] = 255;
}
for (j=0 ; j<osurf->numverts ; j++)
poutvert[j] = pinvert[j];
poutvert += osurf->numverts;
pinvert += osurf->numverts;
}
osurf->nummorphposes = osurf->numframes = numframes;
osurf->numtris = LittleLong(pinsurface->numTriangles);
osurf->numindexes = osurf->numtris*3;
pintriangle = (md3Triangle_t*)((byte*)pinsurface + LittleLong(pinsurface->ofsTriangles));
poutindexes = (unsigned short *) Hunk_Alloc (sizeof (*poutindexes) * osurf->numindexes);
osurf->indexes = (intptr_t) poutindexes - (intptr_t) osurf;
for (ival = 0; ival < osurf->numtris; ival++, pintriangle++, poutindexes+=3)
{
for (j = 0; j < 3; j++)
poutindexes[j] = LittleLong(pintriangle->indexes[j]);
}
for (j = 0; j < 3; j++)
{
osurf->scale_origin[j] = 0;
osurf->scale[j] = 1/64.0;
}
//guess at skin sizes
osurf->skinwidth = 320;
osurf->skinheight = 200;
//load the textures
if (!isDedicated)
{
int numshaders = pinsurface->numShaders;
pinshader = (md3Shader_t*)((byte*)pinsurface + LittleLong(pinsurface->ofsShaders));
if (numskinfiles)
osurf->numskins = numskinfiles;
else
osurf->numskins = q_max(1, q_min(countof(osurf->textures), numshaders));
for (j = 0; j < osurf->numskins; j++, pinshader++)
{
char texturename[MAX_QPATH];
char fullbrightname[MAX_QPATH];
char uppername[MAX_QPATH];
char lowername[MAX_QPATH];
char *ext;
if (j >= numshaders)
{ //invalid skin index for the model... not a major issue I guess
q_strlcpy(texturename, diskname, sizeof(texturename));
*(char*)COM_SkipPath(texturename) = 0;
//and concat the specified name
q_strlcat(texturename, pinsurface->name, sizeof(texturename));
}
else
{
//texture names in md3s are kinda fucked. they could be just names relative to the mdl, or full paths, or just simple shader names.
//our texture manager is too lame to scan all 1000 possibilities
if (strchr(pinshader->name, '/') || strchr(pinshader->name, '\\'))
{ //so if there's a path then we want to use that.
q_strlcpy(texturename, pinshader->name, sizeof(texturename));
}
else
{ //and if there's no path then we want to prefix it with our own.
q_strlcpy(texturename, diskname, sizeof(texturename));
*(char*)COM_SkipPath(texturename) = 0;
//and concat the specified name
q_strlcat(texturename, pinshader->name, sizeof(texturename));
}
}
if (j < numskinfiles)
Mod_GetShaderNameForSurface(skinfile[j], pinsurface->name, texturename, sizeof(texturename)); //swap it out for the skinned texture..
//and make sure there's no extensions. these get ignored in q3, which is kinda annoying, but this is an md3 and standards are standards (and it makes luma easier).
ext = (char*)COM_FileGetExtension(texturename);
if (*ext)
*--ext = 0;
//luma has an extra postfix.
q_snprintf(uppername, sizeof(uppername), "%s_shirt", texturename);
q_snprintf(lowername, sizeof(lowername), "%s_pants", texturename);
q_snprintf(fullbrightname, sizeof(fullbrightname), "%s_luma", texturename);
osurf->textures[j][0].base = TexMgr_LoadImage(mod, texturename, osurf->skinwidth, osurf->skinheight, SRC_EXTERNAL, NULL, texturename, 0, TEXPREF_PAD|TEXPREF_ALPHA|TEXPREF_NOBRIGHT|TEXPREF_MIPMAP);
osurf->textures[j][0].lower = TexMgr_LoadImage(mod, lowername, osurf->skinwidth, osurf->skinheight, SRC_EXTERNAL, NULL, lowername, 0, TEXPREF_ALLOWMISSING|TEXPREF_PAD|TEXPREF_MIPMAP);
osurf->textures[j][0].upper = TexMgr_LoadImage(mod, uppername, osurf->skinwidth, osurf->skinheight, SRC_EXTERNAL, NULL, uppername, 0, TEXPREF_ALLOWMISSING|TEXPREF_PAD|TEXPREF_MIPMAP);
osurf->textures[j][0].luma = TexMgr_LoadImage(mod, fullbrightname, osurf->skinwidth, osurf->skinheight, SRC_EXTERNAL, NULL, fullbrightname, 0, TEXPREF_ALLOWMISSING|TEXPREF_PAD|TEXPREF_ALPHA|TEXPREF_FULLBRIGHT|TEXPREF_MIPMAP);
osurf->textures[j][3] = osurf->textures[j][2] = osurf->textures[j][1] = osurf->textures[j][0];
}
if (osurf->numskins)
{
osurf->skinwidth = osurf->textures[0][0].base->source_width;
osurf->skinheight = osurf->textures[0][0].base->source_height;
}
}
//and figure out the texture coords properly, now we know the actual sizes.
pinst = (md3St_t*)((byte*)pinsurface + LittleLong(pinsurface->ofsSt));
poutst = (aliasmesh_t *) Hunk_Alloc (sizeof (*poutst) * osurf->numverts);
osurf->meshdesc = (intptr_t) poutst - (intptr_t) osurf;
for (j = 0; j < osurf->numverts; j++)
{
poutst[j].vertindex = j; //how is this useful?
poutst[j].st[0] = pinst[j].s;
poutst[j].st[1] = pinst[j].t;
}
}
GLMesh_LoadVertexBuffer (mod, outhdr);
//small violation of the spec, but it seems like noone else uses it.
mod->flags = LittleLong (pinheader->flags);
mod->type = mod_alias;
Mod_CalcAliasBounds (outhdr); //johnfitz
//
// move the complete, relocatable alias model to the cache
//
end = Hunk_LowMark ();
total = end - start;
Cache_Alloc (&mod->cache, total, loadname);
if (!mod->cache.data)
return;
memcpy (mod->cache.data, outhdr, total);
Hunk_FreeToLowMark (start);
}
/*
=================================================================
InterQuake Models.
=================================================================
Header:
*/
//Copyright (c) 2010-2019 Lee Salzman
//MIT License etc at: https://github.com/lsalzman/iqm
#define IQM_MAGIC "INTERQUAKEMODEL"
#define IQM_VERSION 2
struct iqmheader
{
char magic[16];
unsigned int version;
unsigned int filesize;
unsigned int flags;
unsigned int num_text, ofs_text; //text strings
unsigned int num_meshes, ofs_meshes; //surface info
unsigned int num_vertexarrays, num_vertexes, ofs_vertexarrays; //for loading vertex data
unsigned int num_triangles, ofs_triangles, ofs_adjacency; //the index data+neighbours(which we ignore)
unsigned int num_joints, ofs_joints; //mesh joints (base pose info)
unsigned int num_poses, ofs_poses; //animated joints (num_poses should match num_joints)
unsigned int num_anims, ofs_anims; //animations info
unsigned int num_frames, num_framechannels, ofs_frames, ofs_bounds; //the actual per-pose(aka:single-frame) data
unsigned int num_comment, ofs_comment; //extra stuff
unsigned int num_extensions, ofs_extensions;//extra stuff
};
struct iqmmesh
{
unsigned int name;
unsigned int material;
unsigned int first_vertex, num_vertexes;
unsigned int first_triangle, num_triangles;
};
enum
{
IQM_POSITION = 0,
IQM_TEXCOORD = 1,
IQM_NORMAL = 2,
IQM_TANGENT = 3,
IQM_BLENDINDEXES = 4,
IQM_BLENDWEIGHTS = 5,
IQM_COLOR = 6,
IQM_CUSTOM = 0x10
};
enum
{
IQM_BYTE = 0,
IQM_UBYTE = 1,
IQM_SHORT = 2,
IQM_USHORT = 3,
IQM_INT = 4,
IQM_UINT = 5,
IQM_HALF = 6,
IQM_FLOAT = 7,
IQM_DOUBLE = 8
};
/*struct iqmtriangle
{
unsigned int vertex[3];
};
struct iqmadjacency
{
unsigned int triangle[3];
};
struct iqmjointv1
{
unsigned int name;
int parent;
float translate[3], rotate[3], scale[3];
};*/
struct iqmjoint
{
unsigned int name;
int parent;
float translate[3], rotate[4], scale[3];
};
/*struct iqmposev1
{
int parent;
unsigned int mask;
float channeloffset[9];
float channelscale[9];
};*/
struct iqmpose
{
int parent;
unsigned int mask;
float channeloffset[10];
float channelscale[10];
};
struct iqmanim
{
unsigned int name;
unsigned int first_frame, num_frames;
float framerate;
unsigned int flags;
};
enum
{
IQM_LOOP = 1<<0
};
struct iqmvertexarray
{
unsigned int type;
unsigned int flags;
unsigned int format;
unsigned int size;
unsigned int offset;
};
/*struct iqmbounds
{
float bbmin[3], bbmax[3];
float xyradius, radius;
};*/
struct iqmextension
{
unsigned int name;
unsigned int num_data, ofs_data;
unsigned int ofs_extensions; // pointer to next extension
};
//skin lump is made of 3 parts
struct iqmext_fte_skin
{
unsigned int numskinframes;
unsigned int nummeshskins;
//unsigned int numskins[nummeshes];
//iqmext_fte_skin_skinframe[numskinframes];
//iqmext_fte_skin_meshskin mesh0[numskins[0]];
//iqmext_fte_skin_meshskin mesh1[numskins[1]]; etc
};
struct iqmext_fte_skin_skinframe
{ //as many as needed
unsigned int material_idx;
unsigned int shadertext_idx;
};
struct iqmext_fte_skin_meshskin
{
unsigned int firstframe; //index into skinframes
unsigned int countframes; //skinframes
float interval;
};
//IQM Implementation: Copyright 2019 spike, licensed like the rest of quakespasm.
static void IQM_LoadVertexes_Float(float *o, size_t c, size_t numverts, const byte *buffer, const struct iqmvertexarray *va)
{
size_t j, k;
if (c != va->size)
return; //erk, too lazy to handle weirdness.
switch(va->format)
{
// case IQM_BYTE:
case IQM_UBYTE:
{ //weights+colours are often normalised bytes.
const byte *in = (const byte*)(buffer+va->offset);
for (j = 0; j < numverts; j++, in+=va->size, o+=sizeof(iqmvert_t)/sizeof(*o))
{
for (k = 0; k < c; k++)
o[k] = in[k]/255.0;
}
}
break;
// case IQM_SHORT:
// case IQM_USHORT:
// case IQM_INT:
// case IQM_UINT:
// case IQM_HALF:
case IQM_FLOAT:
{
const float *in = (const float*)(buffer+va->offset);
for (j = 0; j < numverts; j++, in+=va->size, o+=sizeof(iqmvert_t)/sizeof(*o))
{
for (k = 0; k < c; k++)
o[k] = in[k];
}
}
break;
case IQM_DOUBLE:
{ //truncate, sorry...
const double *in = (const double*)(buffer+va->offset);
for (j = 0; j < numverts; j++, in+=va->size, o+=sizeof(iqmvert_t)/sizeof(*o))
{
for (k = 0; k < c; k++)
o[k] = in[k];
}
}
break;
default:
return; //oh bum. my laziness strikes again.
}
}