]> icculus.org git repositories - divverent/darkplaces.git/blob - model_brush.c
workaround for bounds checking error in loading texture lump
[divverent/darkplaces.git] / model_brush.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "quakedef.h"
22
23 byte    mod_novis[MAX_MAP_LEAFS/8];
24
25 qboolean        hlbsp; // LordHavoc: true if it is a HalfLife BSP file (version 30)
26
27 cvar_t gl_subdivide_size = {"gl_subdivide_size", "128", true};
28 cvar_t halflifebsp = {"halflifebsp", "0"};
29
30 /*
31 ===============
32 Mod_BrushInit
33 ===============
34 */
35 void Mod_BrushInit (void)
36 {
37         Cvar_RegisterVariable (&gl_subdivide_size);
38         Cvar_RegisterVariable (&halflifebsp);
39         memset (mod_novis, 0xff, sizeof(mod_novis));
40 }
41
42 // Mod_PointInLeaf moved to cpu_noasm.c
43
44 /*
45 ===================
46 Mod_DecompressVis
47 ===================
48 */
49 byte *Mod_DecompressVis (byte *in, model_t *model)
50 {
51         static byte     decompressed[MAX_MAP_LEAFS/8];
52         int             c;
53         byte    *out;
54         int             row;
55
56         row = (model->numleafs+7)>>3;   
57         out = decompressed;
58
59         if (!in)
60         {       // no vis info, so make all visible
61                 while (row)
62                 {
63                         *out++ = 0xff;
64                         row--;
65                 }
66                 return decompressed;            
67         }
68
69         do
70         {
71                 if (*in)
72                 {
73                         *out++ = *in++;
74                         continue;
75                 }
76         
77                 c = in[1];
78                 in += 2;
79                 while (c)
80                 {
81                         *out++ = 0;
82                         c--;
83                 }
84         } while (out - decompressed < row);
85         
86         return decompressed;
87 }
88
89 byte *Mod_LeafPVS (mleaf_t *leaf, model_t *model)
90 {
91         if (leaf == model->leafs)
92                 return mod_novis;
93         return Mod_DecompressVis (leaf->compressed_vis, model);
94 }
95
96 extern byte     *mod_base;
97
98 extern cvar_t r_fullbrights;
99
100 /*
101 =================
102 Mod_LoadTextures
103 =================
104 */
105 void Mod_LoadTextures (lump_t *l)
106 {
107         int             i, j, num, max, altmax, bytesperpixel, freeimage, transparent, fullbrights;
108         miptex_t        *mt;
109         texture_t       *tx, *tx2;
110         texture_t       *anims[10];
111         texture_t       *altanims[10];
112         dmiptexlump_t *m;
113         byte    *data;
114         int             *dofs;
115
116         if (!l->filelen)
117         {
118                 loadmodel->textures = NULL;
119                 return;
120         }
121
122         m = (dmiptexlump_t *)(mod_base + l->fileofs);
123         
124         m->nummiptex = LittleLong (m->nummiptex);
125         
126         loadmodel->numtextures = m->nummiptex;
127         loadmodel->textures = Hunk_AllocName (m->nummiptex * sizeof(*loadmodel->textures) , loadname);
128
129         // just to work around bounds checking when debugging with it (array index out of bounds error thing)
130         dofs = m->dataofs;
131         for (i=0 ; i<m->nummiptex ; i++)
132         {
133                 dofs[i] = LittleLong(dofs[i]);
134                 if (dofs[i] == -1)
135                         continue;
136                 mt = (miptex_t *)((byte *)m + dofs[i]);
137                 mt->width = LittleLong (mt->width);
138                 mt->height = LittleLong (mt->height);
139                 for (j=0 ; j<MIPLEVELS ; j++)
140                         mt->offsets[j] = LittleLong (mt->offsets[j]);
141                 
142                 if ( (mt->width & 15) || (mt->height & 15) )
143                         Host_Error ("Texture %s is not 16 aligned", mt->name);
144                 // LordHavoc: rewriting the map texture loader for GLQuake
145                 tx = Hunk_AllocName (sizeof(texture_t), loadname );
146                 loadmodel->textures[i] = tx;
147
148                 // LordHavoc: force all names to lowercase and make sure they are terminated while copying
149                 for (j = 0;mt->name[j] && j < 15;j++)
150                 {
151                         if (mt->name[j] >= 'A' && mt->name[j] <= 'Z')
152                                 tx->name[j] = mt->name[j] + ('a' - 'A');
153                         else
154                                 tx->name[j] = mt->name[j];
155                 }
156                 for (;j < 16;j++)
157                         tx->name[j] = 0;
158
159                 tx->width = mt->width;
160                 tx->height = mt->height;
161                 for (j=0 ; j<MIPLEVELS ; j++)
162                         tx->offsets[j] = 0;
163                 freeimage = TRUE;
164                 bytesperpixel = 4;
165                 fullbrights = FALSE;
166                 transparent = TRUE;
167                 data = loadimagepixels(tx->name, FALSE, 0, 0); //tx->width, tx->height);
168                 if (!data) // no external texture found
169                 {
170                         freeimage = FALSE;
171                         transparent = FALSE;
172                         bytesperpixel = 1;
173                         if (!hlbsp && mt->offsets[0]) // texture included
174                         {
175                                 data = (byte *)((int) mt + mt->offsets[0]);
176                                 if (r_fullbrights.value && tx->name[0] != '*')
177                                 {
178                                         for (j = 0;j < tx->width*tx->height;j++)
179                                                 if (data[j] >= 224) // fullbright
180                                                 {
181                                                         fullbrights = TRUE;
182                                                         break;
183                                                 }
184                                 }
185                         }
186                         else // no texture, and no external replacement texture was found
187                         {
188                                 tx->width = tx->height = 16;
189                                 data = (byte *)((int) r_notexture_mip + r_notexture_mip->offsets[0]);
190                         }
191                 }
192                 else
193                 {
194                         tx->width = image_width;
195                         tx->height = image_height;
196                 }
197                 if (!hlbsp && !strncmp(tx->name,"sky",3) && tx->width == 256 && tx->height == 128) // LordHavoc: HL sky textures are entirely unrelated
198                 {
199                         tx->transparent = FALSE;
200                         R_InitSky (data, bytesperpixel);
201                 }
202                 else
203                 {
204                         tx->transparent = transparent;
205                         if (fullbrights)
206                         {
207                                 char name[64];
208                                 byte *data2;
209                                 data2 = malloc(tx->width*tx->height);
210                                 for (j = 0;j < tx->width*tx->height;j++)
211                                         data2[j] = data[j] >= 224 ? 0 : data[j]; // no fullbrights
212                                 tx->gl_texturenum = GL_LoadTexture (tx->name, tx->width, tx->height, data2, true, transparent, 1);
213                                 strcpy(name, tx->name);
214                                 strcat(name, "_glow");
215                                 for (j = 0;j < tx->width*tx->height;j++)
216                                         data2[j] = data[j] >= 224 ? data[j] : 0; // only fullbrights
217                                 tx->gl_glowtexturenum = GL_LoadTexture (name, tx->width, tx->height, data2, true, transparent, 1);
218                                 free(data2);
219                         }
220                         else
221                         {
222                                 tx->gl_texturenum = GL_LoadTexture (tx->name, tx->width, tx->height, data, true, transparent, bytesperpixel);
223                                 tx->gl_glowtexturenum = 0;
224                         }
225                 }
226                 if (freeimage)
227                         free(data);
228         }
229
230 //
231 // sequence the animations
232 //
233         for (i=0 ; i<m->nummiptex ; i++)
234         {
235                 tx = loadmodel->textures[i];
236                 if (!tx || tx->name[0] != '+')
237                         continue;
238                 if (tx->anim_next)
239                         continue;       // allready sequenced
240
241         // find the number of frames in the animation
242                 memset (anims, 0, sizeof(anims));
243                 memset (altanims, 0, sizeof(altanims));
244
245                 max = tx->name[1];
246                 altmax = 0;
247                 if (max >= 'a' && max <= 'z')
248                         max -= 'a' - 'A';
249                 if (max >= '0' && max <= '9')
250                 {
251                         max -= '0';
252                         altmax = 0;
253                         anims[max] = tx;
254                         max++;
255                 }
256                 else if (max >= 'A' && max <= 'J')
257                 {
258                         altmax = max - 'A';
259                         max = 0;
260                         altanims[altmax] = tx;
261                         altmax++;
262                 }
263                 else
264                         Host_Error ("Bad animating texture %s", tx->name);
265
266                 for (j=i+1 ; j<m->nummiptex ; j++)
267                 {
268                         tx2 = loadmodel->textures[j];
269                         if (!tx2 || tx2->name[0] != '+')
270                                 continue;
271                         if (strcmp (tx2->name+2, tx->name+2))
272                                 continue;
273
274                         num = tx2->name[1];
275                         if (num >= 'a' && num <= 'z')
276                                 num -= 'a' - 'A';
277                         if (num >= '0' && num <= '9')
278                         {
279                                 num -= '0';
280                                 anims[num] = tx2;
281                                 if (num+1 > max)
282                                         max = num + 1;
283                         }
284                         else if (num >= 'A' && num <= 'J')
285                         {
286                                 num = num - 'A';
287                                 altanims[num] = tx2;
288                                 if (num+1 > altmax)
289                                         altmax = num+1;
290                         }
291                         else
292                                 Host_Error ("Bad animating texture %s", tx->name);
293                 }
294                 
295 #define ANIM_CYCLE      2
296         // link them all together
297                 for (j=0 ; j<max ; j++)
298                 {
299                         tx2 = anims[j];
300                         if (!tx2)
301                                 Host_Error ("Missing frame %i of %s",j, tx->name);
302                         tx2->anim_total = max * ANIM_CYCLE;
303                         tx2->anim_min = j * ANIM_CYCLE;
304                         tx2->anim_max = (j+1) * ANIM_CYCLE;
305                         tx2->anim_next = anims[ (j+1)%max ];
306                         if (altmax)
307                                 tx2->alternate_anims = altanims[0];
308                 }
309                 for (j=0 ; j<altmax ; j++)
310                 {
311                         tx2 = altanims[j];
312                         if (!tx2)
313                                 Host_Error ("Missing frame %i of %s",j, tx->name);
314                         tx2->anim_total = altmax * ANIM_CYCLE;
315                         tx2->anim_min = j * ANIM_CYCLE;
316                         tx2->anim_max = (j+1) * ANIM_CYCLE;
317                         tx2->anim_next = altanims[ (j+1)%altmax ];
318                         if (max)
319                                 tx2->alternate_anims = anims[0];
320                 }
321         }
322 }
323
324 /*
325 =================
326 Mod_LoadLighting
327 =================
328 */
329 void Mod_LoadLighting (lump_t *l)
330 {
331         int i;
332         byte *in, *out, *data;
333         byte d;
334         char litfilename[1024];
335         loadmodel->lightdata = NULL;
336         if (hlbsp) // LordHavoc: load the colored lighting data straight
337         {
338                 loadmodel->lightdata = Hunk_AllocName ( l->filelen, loadname);
339                 memcpy (loadmodel->lightdata, mod_base + l->fileofs, l->filelen);
340         }
341         else // LordHavoc: bsp version 29 (normal white lighting)
342         {
343                 // LordHavoc: hope is not lost yet, check for a .lit file to load
344                 strcpy(litfilename, loadmodel->name);
345                 COM_StripExtension(litfilename, litfilename);
346                 strcat(litfilename, ".lit");
347                 data = (byte*) COM_LoadHunkFile (litfilename, false);
348                 if (data)
349                 {
350                         if (data[0] == 'Q' && data[1] == 'L' && data[2] == 'I' && data[3] == 'T')
351                         {
352                                 i = LittleLong(((int *)data)[1]);
353                                 if (i == 1)
354                                 {
355                                         Con_DPrintf("%s loaded", litfilename);
356                                         loadmodel->lightdata = data + 8;
357                                         return;
358                                 }
359                                 else
360                                         Con_Printf("Unknown .lit file version (%d)\n", i);
361                         }
362                         else
363                                 Con_Printf("Corrupt .lit file (old version?), ignoring\n");
364                 }
365                 // LordHavoc: oh well, expand the white lighting data
366                 if (!l->filelen)
367                         return;
368                 loadmodel->lightdata = Hunk_AllocName ( l->filelen*3, litfilename);
369                 in = loadmodel->lightdata + l->filelen*2; // place the file at the end, so it will not be overwritten until the very last write
370                 out = loadmodel->lightdata;
371                 memcpy (in, mod_base + l->fileofs, l->filelen);
372                 for (i = 0;i < l->filelen;i++)
373                 {
374                         d = *in++;
375                         *out++ = d;
376                         *out++ = d;
377                         *out++ = d;
378                 }
379         }
380 }
381
382
383 /*
384 =================
385 Mod_LoadVisibility
386 =================
387 */
388 void Mod_LoadVisibility (lump_t *l)
389 {
390         if (!l->filelen)
391         {
392                 loadmodel->visdata = NULL;
393                 return;
394         }
395         loadmodel->visdata = Hunk_AllocName ( l->filelen, loadname);    
396         memcpy (loadmodel->visdata, mod_base + l->fileofs, l->filelen);
397 }
398
399 void CL_ParseEntityLump(char *entdata);
400
401 extern qboolean isworldmodel;
402
403 /*
404 =================
405 Mod_LoadEntities
406 =================
407 */
408 void Mod_LoadEntities (lump_t *l)
409 {
410         if (!l->filelen)
411         {
412                 loadmodel->entities = NULL;
413                 return;
414         }
415         loadmodel->entities = Hunk_AllocName ( l->filelen, loadname);   
416         memcpy (loadmodel->entities, mod_base + l->fileofs, l->filelen);
417
418         if (isworldmodel)
419                 CL_ParseEntityLump(loadmodel->entities);
420 }
421
422
423 /*
424 =================
425 Mod_LoadVertexes
426 =================
427 */
428 void Mod_LoadVertexes (lump_t *l)
429 {
430         dvertex_t       *in;
431         mvertex_t       *out;
432         int                     i, count;
433
434         in = (void *)(mod_base + l->fileofs);
435         if (l->filelen % sizeof(*in))
436                 Host_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
437         count = l->filelen / sizeof(*in);
438         out = Hunk_AllocName ( count*sizeof(*out), loadname);   
439
440         loadmodel->vertexes = out;
441         loadmodel->numvertexes = count;
442
443         for ( i=0 ; i<count ; i++, in++, out++)
444         {
445                 out->position[0] = LittleFloat (in->point[0]);
446                 out->position[1] = LittleFloat (in->point[1]);
447                 out->position[2] = LittleFloat (in->point[2]);
448         }
449 }
450
451 /*
452 =================
453 Mod_LoadSubmodels
454 =================
455 */
456 void Mod_LoadSubmodels (lump_t *l)
457 {
458         dmodel_t        *in;
459         dmodel_t        *out;
460         int                     i, j, count;
461
462         in = (void *)(mod_base + l->fileofs);
463         if (l->filelen % sizeof(*in))
464                 Host_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
465         count = l->filelen / sizeof(*in);
466         out = Hunk_AllocName ( count*sizeof(*out), loadname);   
467
468         loadmodel->submodels = out;
469         loadmodel->numsubmodels = count;
470
471         for ( i=0 ; i<count ; i++, in++, out++)
472         {
473                 for (j=0 ; j<3 ; j++)
474                 {       // spread the mins / maxs by a pixel
475                         out->mins[j] = LittleFloat (in->mins[j]) - 1;
476                         out->maxs[j] = LittleFloat (in->maxs[j]) + 1;
477                         out->origin[j] = LittleFloat (in->origin[j]);
478                 }
479                 for (j=0 ; j<MAX_MAP_HULLS ; j++)
480                         out->headnode[j] = LittleLong (in->headnode[j]);
481                 out->visleafs = LittleLong (in->visleafs);
482                 out->firstface = LittleLong (in->firstface);
483                 out->numfaces = LittleLong (in->numfaces);
484         }
485 }
486
487 /*
488 =================
489 Mod_LoadEdges
490 =================
491 */
492 void Mod_LoadEdges (lump_t *l)
493 {
494         dedge_t *in;
495         medge_t *out;
496         int     i, count;
497
498         in = (void *)(mod_base + l->fileofs);
499         if (l->filelen % sizeof(*in))
500                 Host_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
501         count = l->filelen / sizeof(*in);
502         out = Hunk_AllocName ( (count + 1) * sizeof(*out), loadname);   
503
504         loadmodel->edges = out;
505         loadmodel->numedges = count;
506
507         for ( i=0 ; i<count ; i++, in++, out++)
508         {
509                 out->v[0] = (unsigned short)LittleShort(in->v[0]);
510                 out->v[1] = (unsigned short)LittleShort(in->v[1]);
511         }
512 }
513
514 /*
515 =================
516 Mod_LoadTexinfo
517 =================
518 */
519 void Mod_LoadTexinfo (lump_t *l)
520 {
521         texinfo_t *in;
522         mtexinfo_t *out;
523         int     i, j, count;
524         int             miptex;
525         float   len1, len2;
526
527         in = (void *)(mod_base + l->fileofs);
528         if (l->filelen % sizeof(*in))
529                 Host_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
530         count = l->filelen / sizeof(*in);
531         out = Hunk_AllocName ( count*sizeof(*out), loadname);   
532
533         loadmodel->texinfo = out;
534         loadmodel->numtexinfo = count;
535
536         for ( i=0 ; i<count ; i++, in++, out++)
537         {
538                 for (j=0 ; j<8 ; j++)
539                         out->vecs[0][j] = LittleFloat (in->vecs[0][j]);
540                 len1 = Length (out->vecs[0]);
541                 len2 = Length (out->vecs[1]);
542                 len1 = (len1 + len2)/2;
543                 if (len1 < 0.32)
544                         out->mipadjust = 4;
545                 else if (len1 < 0.49)
546                         out->mipadjust = 3;
547                 else if (len1 < 0.99)
548                         out->mipadjust = 2;
549                 else
550                         out->mipadjust = 1;
551 #if 0
552                 if (len1 + len2 < 0.001)
553                         out->mipadjust = 1;             // don't crash
554                 else
555                         out->mipadjust = 1 / floor( (len1+len2)/2 + 0.1 );
556 #endif
557
558                 miptex = LittleLong (in->miptex);
559                 out->flags = LittleLong (in->flags);
560         
561                 if (!loadmodel->textures)
562                 {
563                         out->texture = r_notexture_mip; // checkerboard texture
564                         out->flags = 0;
565                         out->texture->transparent = FALSE;
566                 }
567                 else
568                 {
569                         if (miptex >= loadmodel->numtextures)
570                                 Host_Error ("miptex >= loadmodel->numtextures");
571                         out->texture = loadmodel->textures[miptex];
572                         if (!out->texture)
573                         {
574                                 out->texture = r_notexture_mip; // texture not found
575                                 out->flags = 0;
576                                 out->texture->transparent = FALSE;
577                         }
578                 }
579         }
580 }
581
582 /*
583 ================
584 CalcSurfaceExtents
585
586 Fills in s->texturemins[] and s->extents[]
587 ================
588 */
589 void CalcSurfaceExtents (msurface_t *s)
590 {
591         float   mins[2], maxs[2], val;
592         int             i,j, e;
593         mvertex_t       *v;
594         mtexinfo_t      *tex;
595         int             bmins[2], bmaxs[2];
596
597         mins[0] = mins[1] = 999999;
598         maxs[0] = maxs[1] = -99999;
599
600         tex = s->texinfo;
601         
602         for (i=0 ; i<s->numedges ; i++)
603         {
604                 e = loadmodel->surfedges[s->firstedge+i];
605                 if (e >= 0)
606                         v = &loadmodel->vertexes[loadmodel->edges[e].v[0]];
607                 else
608                         v = &loadmodel->vertexes[loadmodel->edges[-e].v[1]];
609                 
610                 for (j=0 ; j<2 ; j++)
611                 {
612                         val = v->position[0] * tex->vecs[j][0] + 
613                                 v->position[1] * tex->vecs[j][1] +
614                                 v->position[2] * tex->vecs[j][2] +
615                                 tex->vecs[j][3];
616                         if (val < mins[j])
617                                 mins[j] = val;
618                         if (val > maxs[j])
619                                 maxs[j] = val;
620                 }
621         }
622
623         for (i=0 ; i<2 ; i++)
624         {       
625                 bmins[i] = floor(mins[i]/16);
626                 bmaxs[i] = ceil(maxs[i]/16);
627
628                 s->texturemins[i] = bmins[i] * 16;
629                 s->extents[i] = (bmaxs[i] - bmins[i]) * 16;
630                 if ( !(tex->flags & TEX_SPECIAL) && s->extents[i] > 512 /* 256 */ )
631                         Host_Error ("Bad surface extents");
632         }
633 }
634
635 void GL_SubdivideSurface (msurface_t *fa);
636
637 extern char skyname[];
638
639 /*
640 =================
641 Mod_LoadFaces
642 =================
643 */
644 void Mod_LoadFaces (lump_t *l)
645 {
646         dface_t         *in;
647         msurface_t      *out;
648         int                     i, count, surfnum;
649         int                     planenum, side;
650
651         in = (void *)(mod_base + l->fileofs);
652         if (l->filelen % sizeof(*in))
653                 Host_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
654         count = l->filelen / sizeof(*in);
655         out = Hunk_AllocName ( count*sizeof(*out), loadname);   
656
657         loadmodel->surfaces = out;
658         loadmodel->numsurfaces = count;
659
660         for ( surfnum=0 ; surfnum<count ; surfnum++, in++, out++)
661         {
662                 out->firstedge = LittleLong(in->firstedge);
663                 out->numedges = LittleShort(in->numedges);              
664                 out->flags = 0;
665
666                 planenum = LittleShort(in->planenum);
667                 side = LittleShort(in->side);
668                 if (side)
669                         out->flags |= SURF_PLANEBACK;                   
670
671                 out->plane = loadmodel->planes + planenum;
672
673                 out->texinfo = loadmodel->texinfo + LittleShort (in->texinfo);
674
675                 CalcSurfaceExtents (out);
676                                 
677         // lighting info
678
679                 for (i=0 ; i<MAXLIGHTMAPS ; i++)
680                         out->styles[i] = in->styles[i];
681                 i = LittleLong(in->lightofs);
682                 if (i == -1)
683                         out->samples = NULL;
684                 else if (hlbsp) // LordHavoc: HalfLife map (bsp version 30)
685                         out->samples = loadmodel->lightdata + i;
686                 else // LordHavoc: white lighting (bsp version 29)
687                         out->samples = loadmodel->lightdata + (i * 3); 
688                 
689         // set the drawing flags flag
690                 
691 //              if (!strncmp(out->texinfo->texture->name,"sky",3))      // sky
692                 // LordHavoc: faster check
693                 if ((out->texinfo->texture->name[0] == 's' || out->texinfo->texture->name[0] == 'S')
694                  && (out->texinfo->texture->name[1] == 'k' || out->texinfo->texture->name[1] == 'K')
695                  && (out->texinfo->texture->name[2] == 'y' || out->texinfo->texture->name[2] == 'Y'))
696                 {
697                         // LordHavoc: for consistency reasons, mark sky as fullbright and solid as well
698                         out->flags |= (SURF_DRAWSKY | SURF_DRAWTILED | SURF_DRAWFULLBRIGHT | SURF_DRAWNOALPHA);
699                         GL_SubdivideSurface (out);      // cut up polygon for warps
700                         continue;
701                 }
702                 
703 //              if (!strncmp(out->texinfo->texture->name,"*",1))                // turbulent
704                 if (out->texinfo->texture->name[0] == '*') // LordHavoc: faster check
705                 {
706                         out->flags |= (SURF_DRAWTURB | SURF_DRAWTILED);
707                         // LordHavoc: some turbulent textures should be fullbright and solid
708                         if (!strncmp(out->texinfo->texture->name,"*lava",5)
709                          || !strncmp(out->texinfo->texture->name,"*teleport",9)
710                          || !strncmp(out->texinfo->texture->name,"*rift",5)) // Scourge of Armagon texture
711                                 out->flags |= (SURF_DRAWFULLBRIGHT | SURF_DRAWNOALPHA);
712                         for (i=0 ; i<2 ; i++)
713                         {
714                                 out->extents[i] = 16384;
715                                 out->texturemins[i] = -8192;
716                         }
717                         GL_SubdivideSurface (out);      // cut up polygon for warps
718                         continue;
719                 }
720
721         }
722 }
723
724
725 /*
726 =================
727 Mod_SetParent
728 =================
729 */
730 void Mod_SetParent (mnode_t *node, mnode_t *parent)
731 {
732         node->parent = parent;
733         if (node->contents < 0)
734                 return;
735         Mod_SetParent (node->children[0], node);
736         Mod_SetParent (node->children[1], node);
737 }
738
739 /*
740 =================
741 Mod_LoadNodes
742 =================
743 */
744 void Mod_LoadNodes (lump_t *l)
745 {
746         int                     i, j, count, p;
747         dnode_t         *in;
748         mnode_t         *out;
749
750         in = (void *)(mod_base + l->fileofs);
751         if (l->filelen % sizeof(*in))
752                 Host_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
753         count = l->filelen / sizeof(*in);
754         out = Hunk_AllocName ( count*sizeof(*out), loadname);   
755
756         loadmodel->nodes = out;
757         loadmodel->numnodes = count;
758
759         for ( i=0 ; i<count ; i++, in++, out++)
760         {
761                 for (j=0 ; j<3 ; j++)
762                 {
763                         out->minmaxs[j] = LittleShort (in->mins[j]);
764                         out->minmaxs[3+j] = LittleShort (in->maxs[j]);
765                 }
766         
767                 p = LittleLong(in->planenum);
768                 out->plane = loadmodel->planes + p;
769
770                 out->firstsurface = LittleShort (in->firstface);
771                 out->numsurfaces = LittleShort (in->numfaces);
772                 
773                 for (j=0 ; j<2 ; j++)
774                 {
775                         p = LittleShort (in->children[j]);
776                         if (p >= 0)
777                                 out->children[j] = loadmodel->nodes + p;
778                         else
779                                 out->children[j] = (mnode_t *)(loadmodel->leafs + (-1 - p));
780                 }
781         }
782         
783         Mod_SetParent (loadmodel->nodes, NULL); // sets nodes and leafs
784 }
785
786 /*
787 =================
788 Mod_LoadLeafs
789 =================
790 */
791 void Mod_LoadLeafs (lump_t *l)
792 {
793         dleaf_t         *in;
794         mleaf_t         *out;
795         int                     i, j, count, p;
796
797         in = (void *)(mod_base + l->fileofs);
798         if (l->filelen % sizeof(*in))
799                 Host_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
800         count = l->filelen / sizeof(*in);
801         out = Hunk_AllocName ( count*sizeof(*out), loadname);   
802
803         loadmodel->leafs = out;
804         loadmodel->numleafs = count;
805
806         for ( i=0 ; i<count ; i++, in++, out++)
807         {
808                 for (j=0 ; j<3 ; j++)
809                 {
810                         out->minmaxs[j] = LittleShort (in->mins[j]);
811                         out->minmaxs[3+j] = LittleShort (in->maxs[j]);
812                 }
813
814                 p = LittleLong(in->contents);
815                 out->contents = p;
816
817                 out->firstmarksurface = loadmodel->marksurfaces +
818                         LittleShort(in->firstmarksurface);
819                 out->nummarksurfaces = LittleShort(in->nummarksurfaces);
820                 
821                 p = LittleLong(in->visofs);
822                 if (p == -1)
823                         out->compressed_vis = NULL;
824                 else
825                         out->compressed_vis = loadmodel->visdata + p;
826                 out->efrags = NULL;
827                 
828                 for (j=0 ; j<4 ; j++)
829                         out->ambient_sound_level[j] = in->ambient_level[j];
830
831                 // gl underwater warp
832                 // LordHavoc: disabled underwater warping
833                 /*
834                 if (out->contents != CONTENTS_EMPTY)
835                 {
836                         for (j=0 ; j<out->nummarksurfaces ; j++)
837                                 out->firstmarksurface[j]->flags |= SURF_UNDERWATER;
838                 }
839                 */
840         }       
841 }
842
843 /*
844 =================
845 Mod_LoadClipnodes
846 =================
847 */
848 void Mod_LoadClipnodes (lump_t *l)
849 {
850         dclipnode_t *in, *out;
851         int                     i, count;
852         hull_t          *hull;
853
854         in = (void *)(mod_base + l->fileofs);
855         if (l->filelen % sizeof(*in))
856                 Host_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
857         count = l->filelen / sizeof(*in);
858         out = Hunk_AllocName ( count*sizeof(*out), loadname);   
859
860         loadmodel->clipnodes = out;
861         loadmodel->numclipnodes = count;
862
863         if (hlbsp)
864         {
865                 hull = &loadmodel->hulls[1];
866                 hull->clipnodes = out;
867                 hull->firstclipnode = 0;
868                 hull->lastclipnode = count-1;
869                 hull->planes = loadmodel->planes;
870                 hull->clip_mins[0] = -16;
871                 hull->clip_mins[1] = -16;
872                 hull->clip_mins[2] = -36;
873                 hull->clip_maxs[0] = 16;
874                 hull->clip_maxs[1] = 16;
875                 hull->clip_maxs[2] = 36;
876
877                 hull = &loadmodel->hulls[2];
878                 hull->clipnodes = out;
879                 hull->firstclipnode = 0;
880                 hull->lastclipnode = count-1;
881                 hull->planes = loadmodel->planes;
882                 hull->clip_mins[0] = -32;
883                 hull->clip_mins[1] = -32;
884                 hull->clip_mins[2] = -32;
885                 hull->clip_maxs[0] = 32;
886                 hull->clip_maxs[1] = 32;
887                 hull->clip_maxs[2] = 32;
888
889                 hull = &loadmodel->hulls[3];
890                 hull->clipnodes = out;
891                 hull->firstclipnode = 0;
892                 hull->lastclipnode = count-1;
893                 hull->planes = loadmodel->planes;
894                 hull->clip_mins[0] = -16;
895                 hull->clip_mins[1] = -16;
896                 hull->clip_mins[2] = -18;
897                 hull->clip_maxs[0] = 16;
898                 hull->clip_maxs[1] = 16;
899                 hull->clip_maxs[2] = 18;
900         }
901         else
902         {
903                 hull = &loadmodel->hulls[1];
904                 hull->clipnodes = out;
905                 hull->firstclipnode = 0;
906                 hull->lastclipnode = count-1;
907                 hull->planes = loadmodel->planes;
908                 hull->clip_mins[0] = -16;
909                 hull->clip_mins[1] = -16;
910                 hull->clip_mins[2] = -24;
911                 hull->clip_maxs[0] = 16;
912                 hull->clip_maxs[1] = 16;
913                 hull->clip_maxs[2] = 32;
914
915                 hull = &loadmodel->hulls[2];
916                 hull->clipnodes = out;
917                 hull->firstclipnode = 0;
918                 hull->lastclipnode = count-1;
919                 hull->planes = loadmodel->planes;
920                 hull->clip_mins[0] = -32;
921                 hull->clip_mins[1] = -32;
922                 hull->clip_mins[2] = -24;
923                 hull->clip_maxs[0] = 32;
924                 hull->clip_maxs[1] = 32;
925                 hull->clip_maxs[2] = 64;
926         }
927
928         for (i=0 ; i<count ; i++, out++, in++)
929         {
930                 out->planenum = LittleLong(in->planenum);
931                 out->children[0] = LittleShort(in->children[0]);
932                 out->children[1] = LittleShort(in->children[1]);
933                 if (out->children[0] >= count || out->children[1] >= count)
934                         Host_Error("Corrupt clipping hull (out of range child)\n");
935         }
936 }
937
938 /*
939 =================
940 Mod_MakeHull0
941
942 Duplicate the drawing hull structure as a clipping hull
943 =================
944 */
945 void Mod_MakeHull0 (void)
946 {
947         mnode_t         *in, *child;
948         dclipnode_t *out;
949         int                     i, j, count;
950         hull_t          *hull;
951         
952         hull = &loadmodel->hulls[0];    
953         
954         in = loadmodel->nodes;
955         count = loadmodel->numnodes;
956         out = Hunk_AllocName ( count*sizeof(*out), loadname);   
957
958         hull->clipnodes = out;
959         hull->firstclipnode = 0;
960         hull->lastclipnode = count-1;
961         hull->planes = loadmodel->planes;
962
963         for (i=0 ; i<count ; i++, out++, in++)
964         {
965                 out->planenum = in->plane - loadmodel->planes;
966                 for (j=0 ; j<2 ; j++)
967                 {
968                         child = in->children[j];
969                         if (child->contents < 0)
970                                 out->children[j] = child->contents;
971                         else
972                                 out->children[j] = child - loadmodel->nodes;
973                 }
974         }
975 }
976
977 /*
978 =================
979 Mod_LoadMarksurfaces
980 =================
981 */
982 void Mod_LoadMarksurfaces (lump_t *l)
983 {       
984         int             i, j, count;
985         short           *in;
986         msurface_t **out;
987         
988         in = (void *)(mod_base + l->fileofs);
989         if (l->filelen % sizeof(*in))
990                 Host_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
991         count = l->filelen / sizeof(*in);
992         out = Hunk_AllocName ( count*sizeof(*out), loadname);   
993
994         loadmodel->marksurfaces = out;
995         loadmodel->nummarksurfaces = count;
996
997         for ( i=0 ; i<count ; i++)
998         {
999                 j = LittleShort(in[i]);
1000                 if (j >= loadmodel->numsurfaces)
1001                         Host_Error ("Mod_ParseMarksurfaces: bad surface number");
1002                 out[i] = loadmodel->surfaces + j;
1003         }
1004 }
1005
1006 /*
1007 =================
1008 Mod_LoadSurfedges
1009 =================
1010 */
1011 void Mod_LoadSurfedges (lump_t *l)
1012 {       
1013         int             i, count;
1014         int             *in, *out;
1015         
1016         in = (void *)(mod_base + l->fileofs);
1017         if (l->filelen % sizeof(*in))
1018                 Host_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
1019         count = l->filelen / sizeof(*in);
1020         out = Hunk_AllocName ( count*sizeof(*out), loadname);   
1021
1022         loadmodel->surfedges = out;
1023         loadmodel->numsurfedges = count;
1024
1025         for ( i=0 ; i<count ; i++)
1026                 out[i] = LittleLong (in[i]);
1027 }
1028
1029
1030 /*
1031 =================
1032 Mod_LoadPlanes
1033 =================
1034 */
1035 void Mod_LoadPlanes (lump_t *l)
1036 {
1037         int                     i, j;
1038         mplane_t        *out;
1039         dplane_t        *in;
1040         int                     count;
1041         int                     bits;
1042         
1043         in = (void *)(mod_base + l->fileofs);
1044         if (l->filelen % sizeof(*in))
1045                 Host_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
1046         count = l->filelen / sizeof(*in);
1047         out = Hunk_AllocName ( count*2*sizeof(*out), loadname); 
1048
1049         loadmodel->planes = out;
1050         loadmodel->numplanes = count;
1051
1052         for ( i=0 ; i<count ; i++, in++, out++)
1053         {
1054                 bits = 0;
1055                 for (j=0 ; j<3 ; j++)
1056                 {
1057                         out->normal[j] = LittleFloat (in->normal[j]);
1058 //                      if (out->normal[j] < 0)
1059 //                              bits |= 1<<j;
1060                 }
1061
1062                 out->dist = LittleFloat (in->dist);
1063                 out->type = LittleLong (in->type);
1064 //              out->signbits = bits;
1065                 BoxOnPlaneSideClassify(out);
1066         }
1067 }
1068
1069 /*
1070 =================
1071 Mod_LoadBrushModel
1072 =================
1073 */
1074 void Mod_LoadBrushModel (model_t *mod, void *buffer)
1075 {
1076         int                     i, j;
1077         dheader_t       *header;
1078         dmodel_t        *bm;
1079         
1080         loadmodel->type = mod_brush;
1081         
1082         header = (dheader_t *)buffer;
1083
1084         i = LittleLong (header->version);
1085         if (i != BSPVERSION && i != 30)
1086                 Host_Error ("Mod_LoadBrushModel: %s has wrong version number (%i should be %i or 30 (HalfLife))", mod->name, i, BSPVERSION);
1087         hlbsp = i == 30;
1088         halflifebsp.value = hlbsp;
1089
1090 // swap all the lumps
1091         mod_base = (byte *)header;
1092
1093         for (i=0 ; i<sizeof(dheader_t)/4 ; i++)
1094                 ((int *)header)[i] = LittleLong ( ((int *)header)[i]);
1095
1096 // load into heap
1097         
1098         // LordHavoc: had to move entity loading above everything to allow parsing various settings from worldspawn
1099         Mod_LoadEntities (&header->lumps[LUMP_ENTITIES]);
1100
1101         Mod_LoadVertexes (&header->lumps[LUMP_VERTEXES]);
1102         Mod_LoadEdges (&header->lumps[LUMP_EDGES]);
1103         Mod_LoadSurfedges (&header->lumps[LUMP_SURFEDGES]);
1104         Mod_LoadTextures (&header->lumps[LUMP_TEXTURES]);
1105         Mod_LoadLighting (&header->lumps[LUMP_LIGHTING]);
1106         Mod_LoadPlanes (&header->lumps[LUMP_PLANES]);
1107         Mod_LoadTexinfo (&header->lumps[LUMP_TEXINFO]);
1108         Mod_LoadFaces (&header->lumps[LUMP_FACES]);
1109         Mod_LoadMarksurfaces (&header->lumps[LUMP_MARKSURFACES]);
1110         Mod_LoadVisibility (&header->lumps[LUMP_VISIBILITY]);
1111         Mod_LoadLeafs (&header->lumps[LUMP_LEAFS]);
1112         Mod_LoadNodes (&header->lumps[LUMP_NODES]);
1113         Mod_LoadClipnodes (&header->lumps[LUMP_CLIPNODES]);
1114 //      Mod_LoadEntities (&header->lumps[LUMP_ENTITIES]);
1115         Mod_LoadSubmodels (&header->lumps[LUMP_MODELS]);
1116
1117         Mod_MakeHull0 ();
1118         
1119         mod->numframes = 2;             // regular and alternate animation
1120         
1121 //
1122 // set up the submodels (FIXME: this is confusing)
1123 //
1124         for (i=0 ; i<mod->numsubmodels ; i++)
1125         {
1126                 bm = &mod->submodels[i];
1127
1128                 mod->hulls[0].firstclipnode = bm->headnode[0];
1129                 for (j=1 ; j<MAX_MAP_HULLS ; j++)
1130                 {
1131                         mod->hulls[j].firstclipnode = bm->headnode[j];
1132                         mod->hulls[j].lastclipnode = mod->numclipnodes-1;
1133                 }
1134                 
1135                 mod->firstmodelsurface = bm->firstface;
1136                 mod->nummodelsurfaces = bm->numfaces;
1137                 
1138                 VectorCopy (bm->maxs, mod->maxs);
1139                 VectorCopy (bm->mins, mod->mins);
1140
1141                 mod->radius = RadiusFromBounds (mod->mins, mod->maxs);
1142
1143                 mod->numleafs = bm->visleafs;
1144
1145                 if (isworldmodel && i < (mod->numsubmodels-1)) // LordHavoc: only register submodels if it is the world (prevents bsp models from replacing world submodels)
1146                 {       // duplicate the basic information
1147                         char    name[10];
1148
1149                         sprintf (name, "*%i", i+1);
1150                         loadmodel = Mod_FindName (name);
1151                         *loadmodel = *mod;
1152                         strcpy (loadmodel->name, name);
1153                         mod = loadmodel;
1154                 }
1155         }
1156 }