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