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