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