]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_brush.c
renamed q3mface_t to q3msurface_t
[xonotic/darkplaces.git] / model_brush.c
index e3919a63ff8f587002a65158c286b88780956f10..8f090d1e2135489e0939aea8cc814b93084b474b 100644 (file)
@@ -788,15 +788,13 @@ loc0:
                        surf = cl.worldmodel->brushq1.surfaces + node->firstsurface;
                        for (i = 0;i < node->numsurfaces;i++, surf++)
                        {
-                               if (!(surf->flags & SURF_LIGHTMAP))
+                               if (!(surf->flags & SURF_LIGHTMAP) || !surf->samples)
                                        continue;       // no lightmaps
 
-                               ds = (int) (x * surf->texinfo->vecs[0][0] + y * surf->texinfo->vecs[0][1] + mid * surf->texinfo->vecs[0][2] + surf->texinfo->vecs[0][3])- surf->texturemins[0];
+                               ds = (int) (x * surf->texinfo->vecs[0][0] + y * surf->texinfo->vecs[0][1] + mid * surf->texinfo->vecs[0][2] + surf->texinfo->vecs[0][3]) - surf->texturemins[0];
                                dt = (int) (x * surf->texinfo->vecs[1][0] + y * surf->texinfo->vecs[1][1] + mid * surf->texinfo->vecs[1][2] + surf->texinfo->vecs[1][3]) - surf->texturemins[1];
-                               ds = bound(0, ds, surf->extents[0]);
-                               dt = bound(0, dt, surf->extents[1]);
 
-                               if (surf->samples)
+                               if (ds >= 0 && ds < surf->extents[0] && dt >= 0 && dt < surf->extents[1])
                                {
                                        qbyte *lightmap;
                                        int lmwidth, lmheight, maps, line3, size3, dsfrac = ds & 15, dtfrac = dt & 15, scale = 0, r00 = 0, g00 = 0, b00 = 0, r01 = 0, g01 = 0, b01 = 0, r10 = 0, g10 = 0, b10 = 0, r11 = 0, g11 = 0, b11 = 0;
@@ -853,8 +851,8 @@ middle sample (the one which was requested)
                                        ambientcolor[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 32768.0f);
                                        ambientcolor[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 32768.0f);
                                        ambientcolor[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 32768.0f);
+                                       return true; // success
                                }
-                               return true; // success
                        }
                }
 
@@ -1211,7 +1209,7 @@ static void Mod_Q1BSP_LoadLighting(lump_t *l)
                strlcpy (litfilename, loadmodel->name, sizeof (litfilename));
                FS_StripExtension (litfilename, litfilename, sizeof (litfilename));
                strlcat (litfilename, ".lit", sizeof (litfilename));
-               data = (qbyte*) FS_LoadFile(litfilename, false);
+               data = (qbyte*) FS_LoadFile(litfilename, tempmempool, false);
                if (data)
                {
                        if (fs_filesize > 8 && data[0] == 'Q' && data[1] == 'L' && data[2] == 'I' && data[3] == 'T')
@@ -1266,7 +1264,7 @@ static void Mod_Q1BSP_LoadLightList(void)
        strlcpy (lightsfilename, loadmodel->name, sizeof (lightsfilename));
        FS_StripExtension (lightsfilename, lightsfilename, sizeof(lightsfilename));
        strlcat (lightsfilename, ".lights", sizeof (lightsfilename));
-       s = lightsstring = (char *) FS_LoadFile(lightsfilename, false);
+       s = lightsstring = (char *) FS_LoadFile(lightsfilename, tempmempool, false);
        if (s)
        {
                numlights = 0;
@@ -2640,9 +2638,9 @@ static void Mod_Q1BSP_BuildLightmapUpdateChains(mempool_t *mempool, model_t *mod
        int i, j, stylecounts[256], totalcount, remapstyles[256];
        msurface_t *surf;
        memset(stylecounts, 0, sizeof(stylecounts));
-       for (i = 0;i < model->brushq1.nummodelsurfaces;i++)
+       for (i = 0;i < model->nummodelsurfaces;i++)
        {
-               surf = model->brushq1.surfaces + model->brushq1.firstmodelsurface + i;
+               surf = model->brushq1.surfaces + model->firstmodelsurface + i;
                for (j = 0;j < MAXLIGHTMAPS;j++)
                        stylecounts[surf->styles[j]]++;
        }
@@ -2672,9 +2670,9 @@ static void Mod_Q1BSP_BuildLightmapUpdateChains(mempool_t *mempool, model_t *mod
                model->brushq1.light_styleupdatechains[i] = model->brushq1.light_styleupdatechainsbuffer + j;
                j += stylecounts[model->brushq1.light_style[i]] + 1;
        }
-       for (i = 0;i < model->brushq1.nummodelsurfaces;i++)
+       for (i = 0;i < model->nummodelsurfaces;i++)
        {
-               surf = model->brushq1.surfaces + model->brushq1.firstmodelsurface + i;
+               surf = model->brushq1.surfaces + model->firstmodelsurface + i;
                for (j = 0;j < MAXLIGHTMAPS;j++)
                        if (surf->styles[j] != 255)
                                *model->brushq1.light_styleupdatechains[remapstyles[surf->styles[j]]]++ = surf;
@@ -2693,7 +2691,7 @@ static void Mod_Q1BSP_BuildPVSTextureChains(model_t *model)
        int i, j;
        for (i = 0;i < model->brushq1.numtextures;i++)
                model->brushq1.pvstexturechainslength[i] = 0;
-       for (i = 0, j = model->brushq1.firstmodelsurface;i < model->brushq1.nummodelsurfaces;i++, j++)
+       for (i = 0, j = model->firstmodelsurface;i < model->nummodelsurfaces;i++, j++)
        {
                if (model->brushq1.surfacepvsframes[j] == model->brushq1.pvsframecount)
                {
@@ -2711,7 +2709,7 @@ static void Mod_Q1BSP_BuildPVSTextureChains(model_t *model)
                else
                        model->brushq1.pvstexturechains[i] = NULL;
        }
-       for (i = 0, j = model->brushq1.firstmodelsurface;i < model->brushq1.nummodelsurfaces;i++, j++)
+       for (i = 0, j = model->firstmodelsurface;i < model->nummodelsurfaces;i++, j++)
                if (model->brushq1.surfacepvsframes[j] == model->brushq1.pvsframecount)
                        *model->brushq1.pvstexturechains[model->brushq1.surfaces[j].texinfo->texture->number]++ = model->brushq1.surfaces + j;
        for (i = 0;i < model->brushq1.numtextures;i++)
@@ -2873,8 +2871,6 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer)
        dheader_t *header;
        dmodel_t *bm;
        mempool_t *mainmempool;
-       char *loadname;
-       model_t *originalloadmodel;
        float dist, modelyawradius, modelradius, *vec;
        msurface_t *surf;
        int numshadowmeshtriangles;
@@ -2948,10 +2944,9 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer)
        mod->numframes = 2;             // regular and alternate animation
 
        mainmempool = mod->mempool;
-       loadname = mod->name;
 
        Mod_Q1BSP_LoadLightList();
-       originalloadmodel = loadmodel;
+       loadmodel = loadmodel;
 
        // make a single combined shadow mesh to allow optimized shadow volume creation
        numshadowmeshtriangles = 0;
@@ -2966,11 +2961,43 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer)
        loadmodel->brush.shadowmesh = Mod_ShadowMesh_Finish(loadmodel->mempool, loadmodel->brush.shadowmesh, false, true);
        Mod_BuildTriangleNeighbors(loadmodel->brush.shadowmesh->neighbor3i, loadmodel->brush.shadowmesh->element3i, loadmodel->brush.shadowmesh->numtriangles);
        
-//
-// set up the submodels(FIXME: this is confusing)
-//
+       // LordHavoc: to clear the fog around the original quake submodel code, I
+       // will explain:
+       // first of all, some background info on the submodels:
+       // model 0 is the map model (the world, named maps/e1m1.bsp for example)
+       // model 1 and higher are submodels (doors and the like, named *1, *2, etc)
+       // now the weird for loop itself:
+       // the loop functions in an odd way, on each iteration it sets up the
+       // current 'mod' model (which despite the confusing code IS the model of
+       // the number i), at the end of the loop it duplicates the model to become
+       // the next submodel, and loops back to set up the new submodel.
+
+       // LordHavoc: now the explanation of my sane way (which works identically):
+       // set up the world model, then on each submodel copy from the world model
+       // and set up the submodel with the respective model info.
        for (i = 0;i < mod->brush.numsubmodels;i++)
        {
+               // LordHavoc: this code was originally at the end of this loop, but
+               // has been transformed to something more readable at the start here.
+
+               // LordHavoc: only register submodels if it is the world
+               // (prevents external bsp models from replacing world submodels with
+               //  their own)
+               if (loadmodel->isworldmodel && i)
+               {
+                       char name[10];
+                       // duplicate the basic information
+                       sprintf(name, "*%i", i);
+                       mod = Mod_FindName(name);
+                       // copy the base model to this one
+                       *mod = *loadmodel;
+                       // rename the clone back to its proper name
+                       strcpy(mod->name, name);
+                       // textures and memory belong to the main model
+                       mod->texturepool = NULL;
+                       mod->mempool = NULL;
+               }
+
                bm = &mod->brushq1.submodels[i];
 
                mod->brushq1.hulls[0].firstclipnode = bm->headnode[0];
@@ -2980,14 +3007,13 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer)
                        mod->brushq1.hulls[j].lastclipnode = mod->brushq1.numclipnodes - 1;
                }
 
-               mod->brushq1.firstmodelsurface = bm->firstface;
-               mod->brushq1.nummodelsurfaces = bm->numfaces;
+               mod->firstmodelsurface = bm->firstface;
+               mod->nummodelsurfaces = bm->numfaces;
 
                // make the model surface list (used by shadowing/lighting)
-               mod->numsurfaces = mod->brushq1.nummodelsurfaces;
-               mod->surfacelist = Mem_Alloc(originalloadmodel->mempool, mod->numsurfaces * sizeof(*mod->surfacelist));
-               for (j = 0;j < mod->numsurfaces;j++)
-                       mod->surfacelist[j] = mod->brushq1.firstmodelsurface + j;
+               mod->surfacelist = Mem_Alloc(loadmodel->mempool, mod->nummodelsurfaces * sizeof(*mod->surfacelist));
+               for (j = 0;j < mod->nummodelsurfaces;j++)
+                       mod->surfacelist[j] = mod->firstmodelsurface + j;
 
                // this gets altered below if sky is used
                mod->DrawSky = NULL;
@@ -3003,19 +3029,19 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer)
                        mod->brush.LightPoint = NULL;
                        mod->brush.AmbientSoundLevelsForPoint = NULL;
                }
-               mod->brushq1.pvstexturechains = Mem_Alloc(originalloadmodel->mempool, mod->brushq1.numtextures * sizeof(msurface_t **));
-               mod->brushq1.pvstexturechainsbuffer = Mem_Alloc(originalloadmodel->mempool,(mod->brushq1.nummodelsurfaces + mod->brushq1.numtextures) * sizeof(msurface_t *));
-               mod->brushq1.pvstexturechainslength = Mem_Alloc(originalloadmodel->mempool, mod->brushq1.numtextures * sizeof(int));
+               mod->brushq1.pvstexturechains = Mem_Alloc(loadmodel->mempool, mod->brushq1.numtextures * sizeof(msurface_t **));
+               mod->brushq1.pvstexturechainsbuffer = Mem_Alloc(loadmodel->mempool,(mod->nummodelsurfaces + mod->brushq1.numtextures) * sizeof(msurface_t *));
+               mod->brushq1.pvstexturechainslength = Mem_Alloc(loadmodel->mempool, mod->brushq1.numtextures * sizeof(int));
                Mod_Q1BSP_BuildPVSTextureChains(mod);
-               Mod_Q1BSP_BuildLightmapUpdateChains(originalloadmodel->mempool, mod);
-               if (mod->brushq1.nummodelsurfaces)
+               Mod_Q1BSP_BuildLightmapUpdateChains(loadmodel->mempool, mod);
+               if (mod->nummodelsurfaces)
                {
                        // LordHavoc: calculate bmodel bounding box rather than trusting what it says
                        mod->normalmins[0] = mod->normalmins[1] = mod->normalmins[2] = 1000000000.0f;
                        mod->normalmaxs[0] = mod->normalmaxs[1] = mod->normalmaxs[2] = -1000000000.0f;
                        modelyawradius = 0;
                        modelradius = 0;
-                       for (j = 0, surf = &mod->brushq1.surfaces[mod->brushq1.firstmodelsurface];j < mod->brushq1.nummodelsurfaces;j++, surf++)
+                       for (j = 0, surf = &mod->brushq1.surfaces[mod->firstmodelsurface];j < mod->nummodelsurfaces;j++, surf++)
                        {
                                // we only need to have a drawsky function if it is used(usually only on world model)
                                if (surf->texinfo->texture->flags & SURF_DRAWSKY)
@@ -3053,30 +3079,13 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer)
                else
                {
                        // LordHavoc: empty submodel(lacrima.bsp has such a glitch)
-                       Con_Printf("warning: empty submodel *%i in %s\n", i+1, loadname);
+                       Con_Printf("warning: empty submodel *%i in %s\n", i+1, loadmodel->name);
                }
-               Mod_Q1BSP_BuildSurfaceNeighbors(mod->brushq1.surfaces + mod->brushq1.firstmodelsurface, mod->brushq1.nummodelsurfaces, originalloadmodel->mempool);
+               Mod_Q1BSP_BuildSurfaceNeighbors(mod->brushq1.surfaces + mod->firstmodelsurface, mod->nummodelsurfaces, loadmodel->mempool);
 
                mod->brushq1.num_visleafs = bm->visleafs;
-
-               // LordHavoc: only register submodels if it is the world
-               // (prevents bsp models from replacing world submodels)
-               if (loadmodel->isworldmodel && i < (mod->brush.numsubmodels - 1))
-               {
-                       char    name[10];
-                       // duplicate the basic information
-                       sprintf(name, "*%i", i+1);
-                       loadmodel = Mod_FindName(name);
-                       *loadmodel = *mod;
-                       strcpy(loadmodel->name, name);
-                       // textures and memory belong to the main model
-                       loadmodel->texturepool = NULL;
-                       loadmodel->mempool = NULL;
-                       mod = loadmodel;
-               }
        }
 
-       loadmodel = originalloadmodel;
        //Mod_Q1BSP_ProcessLightList();
 
        if (developer.integer)
@@ -3594,12 +3603,12 @@ static void Mod_Q3BSP_LoadTextures(lump_t *l)
        {
                for (i = 0;i < search->numfilenames;i++)
                {
-                       if ((f = FS_LoadFile(search->filenames[i], false)))
+                       if ((f = FS_LoadFile(search->filenames[i], tempmempool, false)))
                        {
                                text = f;
                                while (COM_ParseToken(&text, false))
                                {
-                                       strncpy(shadername, com_token, sizeof(shadername));
+                                       strlcpy (shadername, com_token, sizeof (shadername));
                                        flags = 0;
                                        sky[0] = 0;
                                        if (COM_ParseToken(&text, false) && !strcasecmp(com_token, "{"))
@@ -3987,7 +3996,7 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l)
 static void Mod_Q3BSP_LoadFaces(lump_t *l)
 {
        q3dface_t *in;
-       q3mface_t *out;
+       q3msurface_t *out;
        int i, j, n, count, invalidelements, patchsize[2], finalwidth, finalheight, xlevel, ylevel, row0, row1, x, y, *e, finalvertices, finaltriangles;
        //int *originalelement3i;
        //int *originalneighbor3i;
@@ -4458,7 +4467,7 @@ static void Mod_Q3BSP_LoadLeafBrushes(lump_t *l)
 static void Mod_Q3BSP_LoadLeafFaces(lump_t *l)
 {
        int *in;
-       q3mface_t **out;
+       q3msurface_t **out;
        int i, n, count;
 
        in = (void *)(mod_base + l->fileofs);
@@ -4771,7 +4780,7 @@ static void Mod_Q3BSP_TraceLine_RecursiveBSPNode(trace_t *trace, q3mnode_t *node
        int i, startside, endside;
        float dist1, dist2, midfrac, mid[3], nodesegmentmins[3], nodesegmentmaxs[3];
        q3mleaf_t *leaf;
-       q3mface_t *face;
+       q3msurface_t *face;
        colbrushf_t *brush;
        if (startfrac > trace->realfraction)
                return;
@@ -4855,7 +4864,7 @@ static void Mod_Q3BSP_TraceBrush_RecursiveBSPNode(trace_t *trace, q3mnode_t *nod
        float nodesegmentmins[3], nodesegmentmaxs[3];
        q3mleaf_t *leaf;
        colbrushf_t *brush;
-       q3mface_t *face;
+       q3msurface_t *face;
        /*
                // find which nodes the line is in and recurse for them
                while (node->plane)
@@ -5233,7 +5242,7 @@ static void Mod_Q3BSP_TraceBox(model_t *model, int frame, trace_t *trace, const
        colbrushf_t *thisbrush_start, *thisbrush_end;
        matrix4x4_t startmatrix, endmatrix;
        static int markframe = 0;
-       q3mface_t *face;
+       q3msurface_t *face;
        memset(trace, 0, sizeof(*trace));
        trace->fraction = 1;
        trace->realfraction = 1;
@@ -5312,7 +5321,7 @@ static int Mod_Q3BSP_BoxTouchingPVS(model_t *model, const qbyte *pvs, const vec3
        int clusterindex, side, nodestackindex = 0;
        q3mnode_t *node, *nodestack[1024];
        node = model->brushq3.data_nodes;
-       if (!loadmodel->brush.num_pvsclusters)
+       if (!model->brush.num_pvsclusters)
                return true;
        for (;;)
        {
@@ -5337,9 +5346,9 @@ static int Mod_Q3BSP_BoxTouchingPVS(model_t *model, const qbyte *pvs, const vec3
                        // leaf - check cluster bit
                        clusterindex = ((q3mleaf_t *)node)->clusterindex;
 #if 0
-                       if (clusterindex >= loadmodel->brush.num_pvsclusters)
+                       if (clusterindex >= model->brush.num_pvsclusters)
                        {
-                               Con_Printf("%i >= %i\n", clusterindex, loadmodel->brush.num_pvsclusters);
+                               Con_Printf("%i >= %i\n", clusterindex, model->brush.num_pvsclusters);
                                return true;
                        }
 #endif
@@ -5408,7 +5417,7 @@ static int Mod_Q3BSP_FatPVS(model_t *model, const vec3_t org, vec_t radius, qbyt
 {
        int bytes = model->brush.num_pvsclusterbytes;
        bytes = min(bytes, pvsbufferlength);
-       if (r_novis.integer || !loadmodel->brush.num_pvsclusters || !Mod_Q3BSP_GetPVS(model, org))
+       if (r_novis.integer || !model->brush.num_pvsclusters || !Mod_Q3BSP_GetPVS(model, org))
        {
                memset(pvsbuffer, 0xFF, bytes);
                return bytes;
@@ -5464,7 +5473,7 @@ void Mod_Q3BSP_RecursiveGetVisible(q3mnode_t *node, model_t *model, const vec3_t
        if ((pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
        {
                int marksurfacenum;
-               q3mface_t *surf;
+               q3msurface_t *surf;
                if (maxleafs && *numleafs < maxleafs)
                        leaflist[(*numleaf)++] = leaf;
                if (maxsurfaces)
@@ -5520,7 +5529,7 @@ void Mod_Q3BSP_Load(model_t *mod, void *buffer)
        int i, j, numshadowmeshtriangles;
        q3dheader_t *header;
        float corner[3], yawradius, modelradius;
-       q3mface_t *face;
+       q3msurface_t *face;
 
        mod->type = mod_brushq3;
        mod->numframes = 1;
@@ -5531,7 +5540,7 @@ void Mod_Q3BSP_Load(model_t *mod, void *buffer)
        i = LittleLong(header->version);
        if (i != Q3BSPVERSION)
                Host_Error("Mod_Q3BSP_Load: %s has wrong version number (%i, should be %i)", mod->name, i, Q3BSPVERSION);
-       if (loadmodel->isworldmodel)
+       if (mod->isworldmodel)
        {
                Cvar_SetValue("halflifebsp", false);
                // until we get a texture for it...
@@ -5620,9 +5629,9 @@ void Mod_Q3BSP_Load(model_t *mod, void *buffer)
                mod->brushq3.submodel = i;
 
                // make the model surface list (used by shadowing/lighting)
-               mod->numsurfaces = mod->brushq3.data_thismodel->numfaces;
-               mod->surfacelist = Mem_Alloc(loadmodel->mempool, mod->numsurfaces * sizeof(*mod->surfacelist));
-               for (j = 0;j < mod->numsurfaces;j++)
+               mod->nummodelsurfaces = mod->brushq3.data_thismodel->numfaces;
+               mod->surfacelist = Mem_Alloc(loadmodel->mempool, mod->nummodelsurfaces * sizeof(*mod->surfacelist));
+               for (j = 0;j < mod->nummodelsurfaces;j++)
                        mod->surfacelist[j] = (mod->brushq3.data_thismodel->firstface - mod->brushq3.data_faces) + j;
 
                VectorCopy(mod->brushq3.data_thismodel->mins, mod->normalmins);