-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgl_model.c
More file actions
3410 lines (2880 loc) · 86.2 KB
/
Copy pathgl_model.c
File metadata and controls
3410 lines (2880 loc) · 86.2 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
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.
*/
// models.c -- model loading and caching
// models are the only shared resource between a client and server running
// on the same machine.
#include "quakedef.h"
static qmodel_t* loadmodel;
static char loadname[32]; // for hunk tags
static void Mod_LoadSpriteModel (qmodel_t *mod, void *buffer);
static void Mod_LoadBrushModel (qmodel_t *mod, void *buffer);
static void Mod_LoadAliasModel (qmodel_t *mod, void *buffer);
static qmodel_t *Mod_LoadModel (qmodel_t *mod, qboolean crash);
static void Mod_Print (void);
static cvar_t external_ents = {"external_ents", "1", CVAR_ARCHIVE};
static cvar_t external_vis = {"external_vis", "1", CVAR_ARCHIVE};
static cvar_t external_textures = {"external_textures", "1", CVAR_ARCHIVE};
static byte *mod_novis;
static int mod_novis_capacity;
static byte *mod_decompressed;
static int mod_decompressed_capacity;
#define MAX_MOD_KNOWN 2048 /*johnfitz -- was 512 */
static qmodel_t mod_known[MAX_MOD_KNOWN];
static int mod_numknown;
texture_t *r_notexture_mip; //johnfitz -- moved here from r_main.c
texture_t *r_notexture_mip2; //johnfitz -- used for non-lightmapped surfs with a missing texture
/*
===============
Mod_Init
===============
*/
void Mod_Init (void)
{
Cvar_RegisterVariable (&gl_subdivide_size);
Cvar_RegisterVariable (&external_vis);
Cvar_RegisterVariable (&external_ents);
Cvar_RegisterVariable (&external_textures);
Cmd_AddCommand ("mcache", Mod_Print);
//johnfitz -- create notexture miptex
r_notexture_mip = (texture_t *) Hunk_AllocName (sizeof(texture_t), "r_notexture_mip");
strcpy (r_notexture_mip->name, "notexture");
r_notexture_mip->height = r_notexture_mip->width = 32;
r_notexture_mip2 = (texture_t *) Hunk_AllocName (sizeof(texture_t), "r_notexture_mip2");
strcpy (r_notexture_mip2->name, "notexture2");
r_notexture_mip2->height = r_notexture_mip2->width = 32;
//johnfitz
}
/*
===============
Mod_Extradata
Caches the data if needed
===============
*/
void *Mod_Extradata (qmodel_t *mod)
{
void *r;
r = Cache_Check (&mod->cache);
if (r)
return r;
Mod_LoadModel (mod, true);
if (!mod->cache.data)
Sys_Error ("Mod_Extradata: caching failed");
return mod->cache.data;
}
/*
===============
Mod_PointInLeaf
===============
*/
mleaf_t *Mod_PointInLeaf (vec3_t p, qmodel_t *model)
{
mnode_t *node;
float d;
mplane_t *plane;
if (!model || !model->nodes)
Sys_Error ("Mod_PointInLeaf: bad model");
node = model->nodes;
while (1)
{
if (node->contents < 0)
return (mleaf_t *)node;
plane = node->plane;
d = DotProduct (p,plane->normal) - plane->dist;
if (d > 0)
node = node->children[0];
else
node = node->children[1];
}
return NULL; // never reached
}
/*
===================
Mod_DecompressVis
===================
*/
static byte *Mod_DecompressVis (byte *in, qmodel_t *model)
{
int c;
byte *out;
byte *outend;
int row;
row = (model->numleafs+7)>>3;
if (mod_decompressed == NULL || row > mod_decompressed_capacity)
{
mod_decompressed_capacity = row;
mod_decompressed = (byte *) realloc (mod_decompressed, mod_decompressed_capacity);
if (!mod_decompressed)
Sys_Error ("Mod_DecompressVis: realloc() failed on %d bytes", mod_decompressed_capacity);
}
out = mod_decompressed;
outend = mod_decompressed + row;
if (!in)
{ // no vis info, so make all visible
while (row)
{
*out++ = 0xff;
row--;
}
return mod_decompressed;
}
do
{
if (*in)
{
*out++ = *in++;
continue;
}
c = in[1];
in += 2;
while (c)
{
if (out == outend)
{
if(!model->viswarn) {
model->viswarn = true;
Con_Warning("Mod_DecompressVis: output overrun on model \"%s\"\n", model->name);
}
return mod_decompressed;
}
*out++ = 0;
c--;
}
} while (out - mod_decompressed < row);
return mod_decompressed;
}
byte *Mod_LeafPVS (mleaf_t *leaf, qmodel_t *model)
{
if (leaf == model->leafs)
return Mod_NoVisPVS (model);
return Mod_DecompressVis (leaf->compressed_vis, model);
}
byte *Mod_NoVisPVS (qmodel_t *model)
{
int pvsbytes;
pvsbytes = (model->numleafs+7)>>3;
if (mod_novis == NULL || pvsbytes > mod_novis_capacity)
{
mod_novis_capacity = pvsbytes;
mod_novis = (byte *) realloc (mod_novis, mod_novis_capacity);
if (!mod_novis)
Sys_Error ("Mod_NoVisPVS: realloc() failed on %d bytes", mod_novis_capacity);
memset(mod_novis, 0xff, mod_novis_capacity);
}
return mod_novis;
}
/*
===================
Mod_ClearAll
===================
*/
void Mod_ClearAll (void)
{
int i;
qmodel_t *mod;
for (i=0 , mod=mod_known ; i<mod_numknown ; i++, mod++)
{
if (mod->type != mod_alias)
{
mod->needload = true;
TexMgr_FreeTexturesForOwner (mod); //johnfitz
}
}
}
void Mod_ResetAll (void)
{
int i;
qmodel_t *mod;
//ericw -- free alias model VBOs
GLMesh_DeleteVertexBuffers ();
for (i=0 , mod=mod_known ; i<mod_numknown ; i++, mod++)
{
if (!mod->needload) //otherwise Mod_ClearAll() did it already
TexMgr_FreeTexturesForOwner (mod);
memset(mod, 0, sizeof(qmodel_t));
}
mod_numknown = 0;
}
/*
==================
Mod_FindName
==================
*/
static qmodel_t *Mod_FindName (const char *name)
{
int i;
qmodel_t *mod;
if (!name[0])
Sys_Error ("Mod_FindName: NULL name"); //johnfitz -- was "Mod_ForName"
//
// search the currently loaded models
//
for (i=0 , mod=mod_known ; i<mod_numknown ; i++, mod++)
{
if (!strcmp (mod->name, name) )
break;
}
if (i == mod_numknown)
{
if (mod_numknown == MAX_MOD_KNOWN)
Sys_Error ("mod_numknown == MAX_MOD_KNOWN");
q_strlcpy (mod->name, name, MAX_QPATH);
mod->needload = true;
mod_numknown++;
}
return mod;
}
/*
==================
Mod_TouchModel
==================
*/
void Mod_TouchModel (const char *name)
{
qmodel_t *mod;
mod = Mod_FindName (name);
if (!mod->needload)
{
if (mod->type == mod_alias)
Cache_Check (&mod->cache);
}
}
/*
==================
Mod_LoadModel
Loads a model into the cache
==================
*/
static qmodel_t *Mod_LoadModel (qmodel_t *mod, qboolean crash)
{
byte *buf;
byte stackbuf[1024]; // avoid dirtying the cache heap
int mod_type;
if (!mod->needload)
{
if (mod->type == mod_alias)
{
if (Cache_Check (&mod->cache))
return mod;
}
else
return mod; // not cached at all
}
//
// because the world is so huge, load it one piece at a time
//
if (!crash)
{
}
//
// load the file
//
buf = COM_LoadStackFile (mod->name, stackbuf, sizeof(stackbuf), & mod->path_id);
if (!buf)
{
if (crash)
Host_Error ("Mod_LoadModel: %s not found", mod->name); //johnfitz -- was "Mod_NumForName"
return NULL;
}
//
// allocate a new model
//
COM_FileBase (mod->name, loadname, sizeof(loadname));
loadmodel = mod;
//
// fill it in
//
// call the apropriate loader
mod->needload = false;
mod_type = (buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24));
switch (mod_type)
{
case IDPOLYHEADER:
Mod_LoadAliasModel (mod, buf);
break;
case IDSPRITEHEADER:
Mod_LoadSpriteModel (mod, buf);
break;
default:
Mod_LoadBrushModel (mod, buf);
break;
}
return mod;
}
/*
==================
Mod_ForName
Loads in a model for the given name
==================
*/
qmodel_t *Mod_ForName (const char *name, qboolean crash)
{
qmodel_t *mod;
mod = Mod_FindName (name);
return Mod_LoadModel (mod, crash);
}
/*
===============================================================================
BRUSHMODEL LOADING
===============================================================================
*/
static byte *mod_base;
/*
=============
Mod_LoadWadFiles
load all of the wads listed in the worldspawn "wad" field
=============
*/
static wad_t *Mod_LoadWadFiles (qmodel_t *mod)
{
char key[128], value[4096];
const char *data;
if (!external_textures.value)
return NULL;
// disregard if this isn't the world model
if (strcmp (mod->name, sv.modelname))
return NULL;
data = COM_Parse (mod->entities);
if (!data)
return NULL; // error
if (com_token[0] != '{')
return NULL; // error
while (1)
{
data = COM_Parse (data);
if (!data)
return NULL; // error
if (com_token[0] == '}')
break; // end of worldspawn
if (com_token[0] == '_')
q_strlcpy (key, com_token + 1, sizeof (key));
else
q_strlcpy (key, com_token, sizeof (key));
while (key[0] && key[strlen (key) - 1] == ' ') // remove trailing spaces
key[strlen (key) - 1] = 0;
data = COM_ParseEx (data, CPE_ALLOWTRUNC);
if (!data)
return NULL; // error
q_strlcpy (value, com_token, sizeof (value));
if (!strcmp ("wad", key))
{
return W_LoadWadList (value);
}
}
return NULL;
}
/*
=================
Mod_LoadWadTexture
look for an external texture in any of the loaded map wads
=================
*/
static texture_t *Mod_LoadWadTexture (qmodel_t *mod, wad_t *wads, const char *name, qboolean *out_pal, int *out_pixels)
{
int i, pixels, palette, tex_bytes, lump_bytes, pixels_pos, palette_pos;
lumpinfo_t *info;
wad_t *wad;
miptex_t mt;
texture_t *tx;
qboolean pal;
unsigned short colors;
// look for the lump in any of the loaded wads
info = W_GetLumpinfoList (wads, name, &wad);
// ensure we're dealing with a miptex
if (!info || (info->type != TYP_MIPTEX && (wad->id != WADID_VALVE || info->type != TYP_MIPTEX_PALETTE)))
{
Con_Warning ("Missing texture %s in %s!\n", name, mod->name);
return NULL;
}
// override the texture from the bsp file
FS_fseek (&wad->fh, info->filepos, SEEK_SET);
FS_fread (&mt, 1, sizeof (miptex_t), &wad->fh);
mt.width = LittleLong (mt.width);
mt.height = LittleLong (mt.height);
for (i = 0; i < MIPLEVELS; i++)
mt.offsets[i] = LittleLong (mt.offsets[i]);
if (mt.width == 0 || mt.height == 0)
{
Con_Warning ("Zero sized texture %s in %s!\n", mt.name, wad->name);
return NULL;
}
if ((mt.width & 15) || (mt.height & 15))
{
if (mod->bspversion != BSPVERSION_QUAKE64)
Con_Warning ("Texture %s (%d x %d) is not 16 aligned\n", mt.name, mt.width, mt.height);
return NULL;
}
pal = wad->id == WADID_VALVE && info->type == TYP_MIPTEX_PALETTE;
pixels = mt.width * mt.height; // only copy the first mip, the rest are auto-generated
pixels_pos = info->filepos + sizeof (miptex_t);
// valve textures have a color palette immediately following the pixels
if (pal)
{
palette_pos = pixels_pos + pixels / 64 * 85;
// add space for the color count
palette = 2;
if ((pixels / 64 * 85 + 2) <= info->size)
{
// the palette is basically garunteed to be 256 colors but,
// we might as well use the value since it *does* exist
FS_fseek (&wad->fh, palette_pos, SEEK_SET);
FS_fread (&colors, 1, 2, &wad->fh);
colors = LittleShort (colors);
// add space for the color palette
palette += colors * 3;
}
tex_bytes = pixels + palette;
lump_bytes = pixels / 64 * 85 + palette;
}
else
{
palette_pos = 0;
palette = 0;
tex_bytes = pixels;
lump_bytes = pixels;
}
tx = (texture_t *)Hunk_AllocName (sizeof (texture_t) + tex_bytes, loadname);
memcpy (tx->name, mt.name, sizeof (tx->name));
tx->width = mt.width;
tx->height = mt.height;
// the pixels immediately follow the structures
// check for pixels extending past the end of the lump
if (lump_bytes > info->size)
{
Con_DPrintf ("Texture %s extends past end of lump\n", mt.name);
lump_bytes = info->size;
if (pal)
palette = q_min(palette, q_max(0, lump_bytes - pixels / 64 * 85));
pixels = q_min(pixels, lump_bytes);
}
tx->update_warp = false; //johnfitz
tx->warpimage = NULL; //johnfitz
tx->fullbright = NULL; //johnfitz
tx->shift = 0; // Q64 only
FS_fseek (&wad->fh, pixels_pos, SEEK_SET);
FS_fread (tx + 1, 1, pixels, &wad->fh);
if (pal && palette)
{
FS_fseek (&wad->fh, palette_pos, SEEK_SET);
FS_fread ((byte *)(tx + 1) + pixels, 1, palette, &wad->fh);
}
*out_pal = pal;
*out_pixels = pixels;
return tx;
}
/*
=================
Mod_CheckFullbrights -- johnfitz
=================
*/
static qboolean Mod_CheckFullbrights (byte *pixels, int count)
{
int i;
for (i = 0; i < count; i++)
{
if (*pixels++ > 223)
return true;
}
return false;
}
/*
=================
Mod_CheckFullbrightsValve
=================
*/
static qboolean Mod_CheckFullbrightsValve (char *name, byte *pixels, int count)
{
if (name[0] == '~' || (name[2] == '~' && name[0] == '+'))
return Mod_CheckFullbrights (pixels, count);
return false;
}
/*
=================
Mod_CheckAnimTextureArrayQ64
Quake64 bsp
Check if we have any missing textures in the array
=================
*/
static qboolean Mod_CheckAnimTextureArrayQ64(texture_t *anims[], int numTex)
{
int i;
for (i = 0; i < numTex; i++)
{
if (!anims[i])
return false;
}
return true;
}
/*
=================
Mod_LoadTextures
=================
*/
static void Mod_LoadTextures (lump_t *l)
{
int i, j, pixels, num, maxanim, altmax, tex_bytes, lump_bytes;
miptex_t *mt;
texture_t *tx, *tx2;
texture_t *anims[10];
texture_t *altanims[10];
dmiptexlump_t *m;
//johnfitz -- more variables
char texturename[64];
int nummiptex;
src_offset_t offset;
int mark, fwidth, fheight;
char filename[MAX_OSPATH], mapname[MAX_OSPATH];
byte *data;
extern byte *hunk_base;
//johnfitz
unsigned int flags;
wad_t *wads;
qboolean from_wad;
enum srcformat fmt;
qboolean fbright;
const char *source_file;
#ifdef BSP29_VALVE
int palette;
byte *palette_p;
unsigned short colors;
#endif
qboolean pal;
//johnfitz -- don't return early if no textures; still need to create dummy texture
if (!l->filelen)
{
Con_Printf ("Mod_LoadTextures: no textures in bsp file\n");
nummiptex = 0;
m = NULL; // avoid bogus compiler warning
}
else
{
m = (dmiptexlump_t *)(mod_base + l->fileofs);
m->nummiptex = LittleLong (m->nummiptex);
nummiptex = m->nummiptex;
}
//johnfitz
loadmodel->numtextures = nummiptex + 2; //johnfitz -- need 2 dummy texture chains for missing textures
loadmodel->textures = (texture_t **) Hunk_AllocName (loadmodel->numtextures * sizeof(*loadmodel->textures) , loadname);
// load any wads this map may need to load external textures from
wads = Mod_LoadWadFiles (loadmodel);
for (i=0 ; i<nummiptex ; i++)
{
m->dataofs[i] = LittleLong(m->dataofs[i]);
if (m->dataofs[i] == -1)
continue;
mt = (miptex_t *)((byte *)m + m->dataofs[i]);
mt->width = LittleLong (mt->width);
mt->height = LittleLong (mt->height);
for (j=0 ; j<MIPLEVELS ; j++)
mt->offsets[j] = LittleLong (mt->offsets[j]);
if (mt->width == 0 || mt->height == 0)
{
Con_Warning ("Zero sized texture %s in %s!\n", mt->name, loadmodel->name);
continue;
}
if ( (mt->width & 15) || (mt->height & 15) )
{
if (loadmodel->bspversion != BSPVERSION_QUAKE64)
Con_Warning ("Texture %s (%d x %d) is not 16 aligned\n", mt->name, mt->width, mt->height);
}
// an offset of zero indicates an external texture
from_wad = mt->offsets[0] == 0;
if (from_wad)
{
tx = (texture_t *)Mod_LoadWadTexture (loadmodel, wads, mt->name, &pal, &pixels);
if (!tx)
continue;
loadmodel->textures[i] = tx;
goto _load_texture;
}
#ifdef BSP29_VALVE
pal = loadmodel->bspversion == BSPVERSION_VALVE;
#else
pal = false;
#endif
pixels = mt->width*mt->height; // only copy the first mip, the rest are auto-generated
#ifdef BSP29_VALVE
// valve textures have a color palette immediately following the pixels
if (pal)
{
palette_p = (byte *)(mt + 1) + pixels / 64 * 85;
// add space for the color count
palette = 2;
if ((palette_p + 2) <= (mod_base + l->fileofs + l->filelen))
{
// the palette is basically garunteed to be 256 colors but,
// we might as well use the value since it *does* exist
memcpy (&colors, palette_p, 2);
colors = LittleShort (colors);
// add space for the color palette
palette += colors * 3;
}
tex_bytes = pixels + palette;
lump_bytes = pixels / 64 * 85 + palette;
}
else
#endif
{
#ifdef BSP29_VALVE
palette_p = NULL;
palette = 0;
#endif
tex_bytes = pixels;
lump_bytes = pixels;
}
tx = (texture_t *) Hunk_AllocName (sizeof(texture_t) +tex_bytes, loadname );
loadmodel->textures[i] = tx;
memcpy (tx->name, mt->name, sizeof(tx->name));
tx->width = mt->width;
tx->height = mt->height;
// the pixels immediately follow the structures
// ericw -- check for pixels extending past the end of the lump.
// appears in the wild; e.g. jam2_tronyn.bsp (func_mapjam2),
// kellbase1.bsp (quoth), and can lead to a segfault if we read past
// the end of the .bsp file buffer
if (((byte*)(mt+1) + lump_bytes) > (mod_base + l->fileofs + l->filelen))
{
Con_DPrintf("Texture %s extends past end of lump\n", mt->name);
lump_bytes = q_max(0L, (long)((mod_base + l->fileofs + l->filelen) - (byte*)(mt+1)));
#ifdef BSP29_VALVE
if (pal)
palette = q_min(palette, q_max(0, lump_bytes - pixels / 64 * 85));
#endif
pixels = q_min(pixels, lump_bytes);
}
tx->update_warp = false; //johnfitz
tx->warpimage = NULL; //johnfitz
tx->fullbright = NULL; //johnfitz
tx->shift = 0; // Q64 only
if (loadmodel->bspversion != BSPVERSION_QUAKE64)
{
memcpy ( tx+1, mt+1, pixels);
#ifdef BSP29_VALVE
if (pal && palette)
memcpy ((byte *)(tx + 1) + pixels, palette_p, palette);
#endif
}
else
{ // Q64 bsp
miptex64_t *mt64 = (miptex64_t *)mt;
tx->shift = LittleLong (mt64->shift);
memcpy ( tx+1, mt64+1, pixels);
}
_load_texture:
//johnfitz -- lots of changes
if (!isDedicated) //no texture uploading for dedicated server
{
#ifdef BSP29_VALVE
if (loadmodel->bspversion != BSPVERSION_VALVE && !q_strncasecmp(tx->name,"sky",3))
#else
if (!q_strncasecmp(tx->name,"sky",3)) //sky texture //also note -- was Q_strncmp, changed to match qbsp
#endif
{
if (loadmodel->bspversion == BSPVERSION_QUAKE64)
Sky_LoadTextureQ64 (loadmodel, tx);
else
Sky_LoadTexture (loadmodel, tx);
}
else if (tx->name[0] == '*' || tx->name[0] == '!') //warping texture
{
//external textures -- first look in "textures/mapname/" then look in "textures/"
mark = Hunk_LowMark();
COM_StripExtension (loadmodel->name + 5, mapname, sizeof(mapname));
q_snprintf (filename, sizeof(filename), "textures/%s/#%s", mapname, tx->name+1); //this also replaces the '*' with a '#'
data = Image_LoadImage (filename, &fwidth, &fheight);
if (!data)
{
q_snprintf (filename, sizeof(filename), "textures/#%s", tx->name+1);
data = Image_LoadImage (filename, &fwidth, &fheight);
}
//now load whatever we found
if (data) //load external image
{
q_strlcpy (texturename, filename, sizeof(texturename));
tx->gltexture = TexMgr_LoadImage (loadmodel, texturename, fwidth, fheight,
SRC_RGBA, data, filename, 0, TEXPREF_NONE);
}
else //use the texture from the bsp file
{
q_snprintf (texturename, sizeof(texturename), "%s:%s", loadmodel->name, tx->name);
if (from_wad || pal)
{
source_file = "";
offset = (src_offset_t)(tx + 1);
}
else
{
source_file = loadmodel->name;
offset = (src_offset_t)(mt+1) - (src_offset_t)mod_base;
}
fmt = pal ? SRC_INDEXED_PALETTE : SRC_INDEXED;
tx->gltexture = TexMgr_LoadImage (loadmodel, texturename, tx->width, tx->height,
fmt, (byte *)(tx+1), source_file, offset, TEXPREF_NONE);
}
//now create the warpimage, using dummy data from the hunk to create the initial image
Hunk_Alloc (gl_warpimagesize*gl_warpimagesize*4); //make sure hunk is big enough so we don't reach an illegal address
Hunk_FreeToLowMark (mark);
q_snprintf (texturename, sizeof(texturename), "%s_warp", texturename);
flags = TEXPREF_NOPICMIP | TEXPREF_WARPIMAGE;
if (GL_GenerateMipmap)
flags |= TEXPREF_MIPMAP;
tx->warpimage = TexMgr_LoadImage (loadmodel, texturename, gl_warpimagesize,
gl_warpimagesize, SRC_RGBA, hunk_base, "", (src_offset_t)hunk_base, flags);
tx->update_warp = true;
}
else //regular texture
{
// ericw -- fence textures
int extraflags;
extraflags = 0;
if (tx->name[0] == '{')
extraflags |= TEXPREF_ALPHA;
// ericw
//external textures -- first look in "textures/mapname/" then look in "textures/"
mark = Hunk_LowMark ();
COM_StripExtension (loadmodel->name + 5, mapname, sizeof(mapname));
q_snprintf (filename, sizeof(filename), "textures/%s/%s", mapname, tx->name);
data = Image_LoadImage (filename, &fwidth, &fheight);
if (!data)
{
q_snprintf (filename, sizeof(filename), "textures/%s", tx->name);
data = Image_LoadImage (filename, &fwidth, &fheight);
}
//now load whatever we found
if (data) //load external image
{
char filename2[MAX_OSPATH];
tx->gltexture = TexMgr_LoadImage (loadmodel, filename, fwidth, fheight,
SRC_RGBA, data, filename, 0, TEXPREF_MIPMAP | extraflags );
//now try to load glow/luma image from the same place
Hunk_FreeToLowMark (mark);
q_snprintf (filename2, sizeof(filename2), "%s_glow", filename);
data = Image_LoadImage (filename2, &fwidth, &fheight);
if (!data)
{
q_snprintf (filename2, sizeof(filename2), "%s_luma", filename);
data = Image_LoadImage (filename2, &fwidth, &fheight);
}
if (data)
tx->fullbright = TexMgr_LoadImage (loadmodel, filename2, fwidth, fheight,
SRC_RGBA, data, filename2, 0, TEXPREF_MIPMAP | extraflags );
}
else //use the texture from the bsp file
{
q_snprintf (texturename, sizeof(texturename), "%s:%s", loadmodel->name, tx->name);
if (from_wad || pal)
{
source_file = "";
offset = (src_offset_t)(tx + 1);
}
else
{
source_file = loadmodel->name;
offset = (src_offset_t)(mt+1) - (src_offset_t)mod_base;
}
if (pal)
{
fmt = SRC_INDEXED_PALETTE;
fbright = Mod_CheckFullbrightsValve (tx->name, (byte *)(tx + 1), pixels);
}
else
{
fmt = SRC_INDEXED;
fbright = Mod_CheckFullbrights ((byte *)(tx + 1), pixels);
}
if (fbright)
{
tx->gltexture = TexMgr_LoadImage (loadmodel, texturename, tx->width, tx->height,
fmt, (byte *)(tx+1), source_file, offset, TEXPREF_MIPMAP | TEXPREF_NOBRIGHT | extraflags);
q_snprintf (texturename, sizeof(texturename), "%s:%s_glow", loadmodel->name, tx->name);
tx->fullbright = TexMgr_LoadImage (loadmodel, texturename, tx->width, tx->height,
fmt, (byte *)(tx+1), source_file, offset, TEXPREF_MIPMAP | TEXPREF_FULLBRIGHT | extraflags);
}
else
{
tx->gltexture = TexMgr_LoadImage (loadmodel, texturename, tx->width, tx->height,
fmt, (byte *)(tx+1), source_file, offset, TEXPREF_MIPMAP | extraflags);
}
}
Hunk_FreeToLowMark (mark);
}
}
//johnfitz
}
// we no longer need the wads after this point
W_FreeWadList (wads);
//johnfitz -- last 2 slots in array should be filled with dummy textures
loadmodel->textures[loadmodel->numtextures-2] = r_notexture_mip; //for lightmapped surfs
loadmodel->textures[loadmodel->numtextures-1] = r_notexture_mip2; //for SURF_DRAWTILED surfs
//
// sequence the animations
//
for (i=0 ; i<nummiptex ; i++)
{
tx = loadmodel->textures[i];
if (!tx || tx->name[0] != '+')
continue;
if (tx->anim_next)
continue; // already sequenced
// find the number of frames in the animation
memset (anims, 0, sizeof(anims));
memset (altanims, 0, sizeof(altanims));
maxanim = tx->name[1];
altmax = 0;
if (maxanim >= 'a' && maxanim <= 'z')
maxanim -= 'a' - 'A';
if (maxanim >= '0' && maxanim <= '9')
{
maxanim -= '0';
altmax = 0;
anims[maxanim] = tx;
maxanim++;
}
else if (maxanim >= 'A' && maxanim <= 'J')
{
altmax = maxanim - 'A';
maxanim = 0;
altanims[altmax] = tx;
altmax++;
}
else
Sys_Error ("Bad animating texture %s", tx->name);
for (j=i+1 ; j<nummiptex ; j++)
{