]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_brush.c
every malloc/calloc/free converted to qmalloc/qfree with tracking (memstats command...
[xonotic/darkplaces.git] / model_brush.c
index e62b5272bfd0491aa021b5ef6edf59f8ab600660..c070d4217a4126a3a554c47beb82b4d5f0984ac8 100644 (file)
@@ -39,36 +39,7 @@ void Mod_BrushInit (void)
        memset (mod_novis, 0xff, sizeof(mod_novis));
 }
 
-/*
-===============
-Mod_PointInLeaf
-===============
-*/
-mleaf_t *Mod_PointInLeaf (vec3_t p, model_t *model)
-{
-       mnode_t         *node;
-       float           d;
-       mplane_t        *plane;
-       
-       if (!model || !model->nodes)
-               Sys_Error ("Mod_PointInLeaf: bad model");
-
-       node = model->nodes;
-       while (1)
-       {
-               if (node->contents < 0)
-                       return (mleaf_t *)node;
-               plane = node->plane;
-               d = DotProduct (p,plane->normal) - plane->dist;
-               if (d > 0)
-                       node = node->children[0];
-               else
-                       node = node->children[1];
-       }
-       
-       return NULL;    // never reached
-}
-
+// Mod_PointInLeaf moved to cpu_noasm.c
 
 /*
 ===================
@@ -122,7 +93,7 @@ byte *Mod_LeafPVS (mleaf_t *leaf, model_t *model)
        return Mod_DecompressVis (leaf->compressed_vis, model);
 }
 
-byte   *mod_base;
+extern byte    *mod_base;
 
 extern cvar_t r_fullbrights;
 
@@ -139,7 +110,8 @@ void Mod_LoadTextures (lump_t *l)
        texture_t       *anims[10];
        texture_t       *altanims[10];
        dmiptexlump_t *m;
-       byte *data;
+       byte    *data;
+       int             *dofs;
 
        if (!l->filelen)
        {
@@ -154,12 +126,14 @@ void Mod_LoadTextures (lump_t *l)
        loadmodel->numtextures = m->nummiptex;
        loadmodel->textures = Hunk_AllocName (m->nummiptex * sizeof(*loadmodel->textures) , loadname);
 
+       // just to work around bounds checking when debugging with it (array index out of bounds error thing)
+       dofs = m->dataofs;
        for (i=0 ; i<m->nummiptex ; i++)
        {
-               m->dataofs[i] = LittleLong(m->dataofs[i]);
-               if (m->dataofs[i] == -1)
+               dofs[i] = LittleLong(dofs[i]);
+               if (dofs[i] == -1)
                        continue;
-               mt = (miptex_t *)((byte *)m + m->dataofs[i]);
+               mt = (miptex_t *)((byte *)m + dofs[i]);
                mt->width = LittleLong (mt->width);
                mt->height = LittleLong (mt->height);
                for (j=0 ; j<MIPLEVELS ; j++)
@@ -171,7 +145,17 @@ void Mod_LoadTextures (lump_t *l)
                tx = Hunk_AllocName (sizeof(texture_t), loadname );
                loadmodel->textures[i] = tx;
 
-               memcpy (tx->name, mt->name, sizeof(tx->name));
+               // LordHavoc: force all names to lowercase and make sure they are terminated while copying
+               for (j = 0;mt->name[j] && j < 15;j++)
+               {
+                       if (mt->name[j] >= 'A' && mt->name[j] <= 'Z')
+                               tx->name[j] = mt->name[j] + ('a' - 'A');
+                       else
+                               tx->name[j] = mt->name[j];
+               }
+               for (;j < 16;j++)
+                       tx->name[j] = 0;
+
                tx->width = mt->width;
                tx->height = mt->height;
                for (j=0 ; j<MIPLEVELS ; j++)
@@ -180,7 +164,7 @@ void Mod_LoadTextures (lump_t *l)
                bytesperpixel = 4;
                fullbrights = FALSE;
                transparent = TRUE;
-               data = loadimagepixels(mt->name, FALSE, 0, 0); //tx->width, tx->height);
+               data = loadimagepixels(tx->name, FALSE, 0, 0); //tx->width, tx->height);
                if (!data) // no external texture found
                {
                        freeimage = FALSE;
@@ -189,7 +173,7 @@ void Mod_LoadTextures (lump_t *l)
                        if (!hlbsp && mt->offsets[0]) // texture included
                        {
                                data = (byte *)((int) mt + mt->offsets[0]);
-                               if (r_fullbrights.value && mt->name[0] != '*')
+                               if (r_fullbrights.value && tx->name[0] != '*')
                                {
                                        for (j = 0;j < tx->width*tx->height;j++)
                                                if (data[j] >= 224) // fullbright
@@ -210,7 +194,7 @@ void Mod_LoadTextures (lump_t *l)
                        tx->width = image_width;
                        tx->height = image_height;
                }
-               if (!hlbsp && !strncmp(mt->name,"sky",3) && tx->width == 256 && tx->height == 128) // LordHavoc: HL sky textures are entirely unrelated
+               if (!hlbsp && !strncmp(tx->name,"sky",3) && tx->width == 256 && tx->height == 128) // LordHavoc: HL sky textures are entirely unrelated
                {
                        tx->transparent = FALSE;
                        R_InitSky (data, bytesperpixel);
@@ -222,7 +206,7 @@ void Mod_LoadTextures (lump_t *l)
                        {
                                char name[64];
                                byte *data2;
-                               data2 = malloc(tx->width*tx->height);
+                               data2 = qmalloc(tx->width*tx->height);
                                for (j = 0;j < tx->width*tx->height;j++)
                                        data2[j] = data[j] >= 224 ? 0 : data[j]; // no fullbrights
                                tx->gl_texturenum = GL_LoadTexture (tx->name, tx->width, tx->height, data2, true, transparent, 1);
@@ -231,7 +215,7 @@ void Mod_LoadTextures (lump_t *l)
                                for (j = 0;j < tx->width*tx->height;j++)
                                        data2[j] = data[j] >= 224 ? data[j] : 0; // only fullbrights
                                tx->gl_glowtexturenum = GL_LoadTexture (name, tx->width, tx->height, data2, true, transparent, 1);
-                               free(data2);
+                               qfree(data2);
                        }
                        else
                        {
@@ -240,27 +224,7 @@ void Mod_LoadTextures (lump_t *l)
                        }
                }
                if (freeimage)
-                       free(data);
-
-               /*
-               pixels = mt->width*mt->height/64*85;
-               tx = Hunk_AllocName (sizeof(texture_t) +pixels, loadname );
-               loadmodel->textures[i] = tx;
-
-               memcpy (tx->name, mt->name, sizeof(tx->name));
-               tx->width = mt->width;
-               tx->height = mt->height;
-               for (j=0 ; j<MIPLEVELS ; j++)
-                       tx->offsets[j] = mt->offsets[j] + sizeof(texture_t) - sizeof(miptex_t);
-               // the pixels immediately follow the structures
-               memcpy ( tx+1, mt+1, pixels);
-               
-
-               if (!strncmp(mt->name,"sky",3)) 
-                       R_InitSky (tx);
-               else
-                       tx->gl_texturenum = GL_LoadTexture (mt->name, tx->width, tx->height, (byte *)(tx+1), true, false, 1);
-               */
+                       qfree(data);
        }
 
 //
@@ -369,8 +333,6 @@ void Mod_LoadLighting (lump_t *l)
        byte d;
        char litfilename[1024];
        loadmodel->lightdata = NULL;
-       if (!l->filelen)
-               return;
        if (hlbsp) // LordHavoc: load the colored lighting data straight
        {
                loadmodel->lightdata = Hunk_AllocName ( l->filelen, loadname);
@@ -390,6 +352,7 @@ void Mod_LoadLighting (lump_t *l)
                                i = LittleLong(((int *)data)[1]);
                                if (i == 1)
                                {
+                                       Con_DPrintf("%s loaded", litfilename);
                                        loadmodel->lightdata = data + 8;
                                        return;
                                }
@@ -400,6 +363,8 @@ void Mod_LoadLighting (lump_t *l)
                                Con_Printf("Corrupt .lit file (old version?), ignoring\n");
                }
                // LordHavoc: oh well, expand the white lighting data
+               if (!l->filelen)
+                       return;
                loadmodel->lightdata = Hunk_AllocName ( l->filelen*3, litfilename);
                in = loadmodel->lightdata + l->filelen*2; // place the file at the end, so it will not be overwritten until the very last write
                out = loadmodel->lightdata;
@@ -555,7 +520,7 @@ void Mod_LoadTexinfo (lump_t *l)
 {
        texinfo_t *in;
        mtexinfo_t *out;
-       int     i, j, count;
+       int     i, j, k, count;
        int             miptex;
        float   len1, len2;
 
@@ -570,8 +535,9 @@ void Mod_LoadTexinfo (lump_t *l)
 
        for ( i=0 ; i<count ; i++, in++, out++)
        {
-               for (j=0 ; j<8 ; j++)
-                       out->vecs[0][j] = LittleFloat (in->vecs[0][j]);
+               for (k=0 ; k<2 ; k++)
+                       for (j=0 ; j<4 ; j++)
+                               out->vecs[k][j] = LittleFloat (in->vecs[k][j]);
                len1 = Length (out->vecs[0]);
                len2 = Length (out->vecs[1]);
                len1 = (len1 + len2)/2;
@@ -597,6 +563,7 @@ void Mod_LoadTexinfo (lump_t *l)
                {
                        out->texture = r_notexture_mip; // checkerboard texture
                        out->flags = 0;
+                       out->texture->transparent = FALSE;
                }
                else
                {
@@ -607,9 +574,8 @@ void Mod_LoadTexinfo (lump_t *l)
                        {
                                out->texture = r_notexture_mip; // texture not found
                                out->flags = 0;
-                       }
-                       else
                                out->texture->transparent = FALSE;
+                       }
                }
        }
 }
@@ -895,35 +861,78 @@ void Mod_LoadClipnodes (lump_t *l)
        loadmodel->clipnodes = out;
        loadmodel->numclipnodes = count;
 
-       hull = &loadmodel->hulls[1];
-       hull->clipnodes = out;
-       hull->firstclipnode = 0;
-       hull->lastclipnode = count-1;
-       hull->planes = loadmodel->planes;
-       hull->clip_mins[0] = -16;
-       hull->clip_mins[1] = -16;
-       hull->clip_mins[2] = -24;
-       hull->clip_maxs[0] = 16;
-       hull->clip_maxs[1] = 16;
-       hull->clip_maxs[2] = 32;
-
-       hull = &loadmodel->hulls[2];
-       hull->clipnodes = out;
-       hull->firstclipnode = 0;
-       hull->lastclipnode = count-1;
-       hull->planes = loadmodel->planes;
-       hull->clip_mins[0] = -32;
-       hull->clip_mins[1] = -32;
-       hull->clip_mins[2] = -24;
-       hull->clip_maxs[0] = 32;
-       hull->clip_maxs[1] = 32;
-       hull->clip_maxs[2] = 64;
+       if (hlbsp)
+       {
+               hull = &loadmodel->hulls[1];
+               hull->clipnodes = out;
+               hull->firstclipnode = 0;
+               hull->lastclipnode = count-1;
+               hull->planes = loadmodel->planes;
+               hull->clip_mins[0] = -16;
+               hull->clip_mins[1] = -16;
+               hull->clip_mins[2] = -36;
+               hull->clip_maxs[0] = 16;
+               hull->clip_maxs[1] = 16;
+               hull->clip_maxs[2] = 36;
+
+               hull = &loadmodel->hulls[2];
+               hull->clipnodes = out;
+               hull->firstclipnode = 0;
+               hull->lastclipnode = count-1;
+               hull->planes = loadmodel->planes;
+               hull->clip_mins[0] = -32;
+               hull->clip_mins[1] = -32;
+               hull->clip_mins[2] = -32;
+               hull->clip_maxs[0] = 32;
+               hull->clip_maxs[1] = 32;
+               hull->clip_maxs[2] = 32;
+
+               hull = &loadmodel->hulls[3];
+               hull->clipnodes = out;
+               hull->firstclipnode = 0;
+               hull->lastclipnode = count-1;
+               hull->planes = loadmodel->planes;
+               hull->clip_mins[0] = -16;
+               hull->clip_mins[1] = -16;
+               hull->clip_mins[2] = -18;
+               hull->clip_maxs[0] = 16;
+               hull->clip_maxs[1] = 16;
+               hull->clip_maxs[2] = 18;
+       }
+       else
+       {
+               hull = &loadmodel->hulls[1];
+               hull->clipnodes = out;
+               hull->firstclipnode = 0;
+               hull->lastclipnode = count-1;
+               hull->planes = loadmodel->planes;
+               hull->clip_mins[0] = -16;
+               hull->clip_mins[1] = -16;
+               hull->clip_mins[2] = -24;
+               hull->clip_maxs[0] = 16;
+               hull->clip_maxs[1] = 16;
+               hull->clip_maxs[2] = 32;
+
+               hull = &loadmodel->hulls[2];
+               hull->clipnodes = out;
+               hull->firstclipnode = 0;
+               hull->lastclipnode = count-1;
+               hull->planes = loadmodel->planes;
+               hull->clip_mins[0] = -32;
+               hull->clip_mins[1] = -32;
+               hull->clip_mins[2] = -24;
+               hull->clip_maxs[0] = 32;
+               hull->clip_maxs[1] = 32;
+               hull->clip_maxs[2] = 64;
+       }
 
        for (i=0 ; i<count ; i++, out++, in++)
        {
                out->planenum = LittleLong(in->planenum);
                out->children[0] = LittleShort(in->children[0]);
                out->children[1] = LittleShort(in->children[1]);
+               if (out->children[0] >= count || out->children[1] >= count)
+                       Host_Error("Corrupt clipping hull (out of range child)\n");
        }
 }
 
@@ -1074,7 +1083,7 @@ void Mod_LoadBrushModel (model_t *mod, void *buffer)
        header = (dheader_t *)buffer;
 
        i = LittleLong (header->version);
-       if (i != BSPVERSION & i != 30)
+       if (i != BSPVERSION && i != 30)
                Host_Error ("Mod_LoadBrushModel: %s has wrong version number (%i should be %i or 30 (HalfLife))", mod->name, i, BSPVERSION);
        hlbsp = i == 30;
        halflifebsp.value = hlbsp;