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