]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_brush.c
changed cl_stainmaps to be a loadtime cvar which decides whether to
[xonotic/darkplaces.git] / model_brush.c
index 1b0cc330d4ecf813e5f97d7303494ddbceb578b1..dbf98b029a3e69ac1d86f05b9333176a4f62708d 100644 (file)
@@ -885,7 +885,7 @@ static void Mod_Q1BSP_TraceBox(struct model_s *model, int frame, trace_t *trace,
 
 static int Mod_Q1BSP_PointSuperContents(struct model_s *model, int frame, const vec3_t point)
 {
-       int num = 0;
+       int num = model->brushq1.hulls[0].firstclipnode;
        mplane_t *plane;
        mclipnode_t *nodes = model->brushq1.hulls[0].clipnodes;
        mplane_t *planes = model->brushq1.hulls[0].planes;
@@ -1444,7 +1444,7 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l)
                        if (name[j] >= 'A' && name[j] <= 'Z')
                                name[j] += 'a' - 'A';
 
-               if (dmiptex->name[0] && Mod_LoadTextureFromQ3Shader(loadmodel->data_textures + i, name, true, false, false))
+               if (dmiptex->name[0] && Mod_LoadTextureFromQ3Shader(loadmodel->data_textures + i, name, false, false, 0))
                        continue;
 
                tx = loadmodel->data_textures + i;
@@ -1702,7 +1702,8 @@ static void Mod_Q1BSP_LoadLighting(lump_t *l)
                                i = LittleLong(((int *)data)[1]);
                                if (i == 1)
                                {
-                                       Con_DPrintf("loaded %s\n", litfilename);
+                                       if (developer_loading.integer)
+                                               Con_Printf("loaded %s\n", litfilename);
                                        loadmodel->brushq1.lightdata = (unsigned char *)Mem_Alloc(loadmodel->mempool, filesize - 8);
                                        memcpy(loadmodel->brushq1.lightdata, data + 8, filesize - 8);
                                        Mem_Free(data);
@@ -1714,7 +1715,8 @@ static void Mod_Q1BSP_LoadLighting(lump_t *l)
                                                        i = LittleLong(((int *)data)[1]);
                                                        if (i == 1)
                                                        {
-                                                               Con_DPrintf("loaded %s\n", dlitfilename);
+                                                               if (developer_loading.integer)
+                                                                       Con_Printf("loaded %s\n", dlitfilename);
                                                                loadmodel->brushq1.nmaplightdata = (unsigned char *)Mem_Alloc(loadmodel->mempool, filesize - 8);
                                                                memcpy(loadmodel->brushq1.nmaplightdata, data + 8, filesize - 8);
                                                                loadmodel->brushq3.deluxemapping_modelspace = false;
@@ -2167,7 +2169,7 @@ struct alloc_lm_state
 static void init_alloc_lm_state (struct alloc_lm_state* state)
 {
        int r;
-       
+
        state->currentY = 0;
        for (r = 0; r < MAX_SINGLE_LM_SIZE; r++)
        {
@@ -2402,16 +2404,11 @@ static void Mod_Q1BSP_LoadFaces(lump_t *l)
                {
                        int i, iu, iv, lightmapx, lightmapy;
                        float u, v, ubase, vbase, uscale, vscale;
-                       
+
                        smax = surface->lightmapinfo->extents[0] >> 4;
                        tmax = surface->lightmapinfo->extents[1] >> 4;
                        ssize = (surface->lightmapinfo->extents[0] >> 4) + 1;
                        tsize = (surface->lightmapinfo->extents[1] >> 4) + 1;
-                       
-                       // stainmap for permanent marks on walls
-                       surface->lightmapinfo->stainsamples = (unsigned char *)Mem_Alloc(loadmodel->mempool, ssize * tsize * 3);
-                       // clear to white
-                       memset(surface->lightmapinfo->stainsamples, 255, ssize * tsize * 3);
 
                        if (!lightmaptexture || !Mod_Q1BSP_AllocLightmapBlock(&allocState, lightmapsize, lightmapsize, ssize, tsize, &lightmapx, &lightmapy))
                        {
@@ -2449,7 +2446,36 @@ static void Mod_Q1BSP_LoadFaces(lump_t *l)
                        }
 
                }
+
+               if (cl_stainmaps.integer)
+               {
+                       // allocate stainmaps for permanent marks on walls
+                       int stainmapsize = 0;
+                       unsigned char *stainsamples = NULL;
+                       for (surfacenum = 0, surface = loadmodel->data_surfaces;surfacenum < count;surfacenum++, surface++)
+                       {
+                               ssize = (surface->lightmapinfo->extents[0] >> 4) + 1;
+                               tsize = (surface->lightmapinfo->extents[1] >> 4) + 1;
+                               stainmapsize += ssize * tsize * 3;
+                       }
+                       // allocate and clear to white
+                       stainsamples = (unsigned char *)Mem_Alloc(loadmodel->mempool, stainmapsize);
+                       memset(stainsamples, 255, stainmapsize);
+                       // assign pointers
+                       for (surfacenum = 0, surface = loadmodel->data_surfaces;surfacenum < count;surfacenum++, surface++)
+                       {
+                               ssize = (surface->lightmapinfo->extents[0] >> 4) + 1;
+                               tsize = (surface->lightmapinfo->extents[1] >> 4) + 1;
+                               surface->lightmapinfo->stainsamples = stainsamples;
+                               stainsamples += ssize * tsize * 3;
+                       }
+               }
        }
+
+       // generate ushort elements array if possible
+       if (loadmodel->surfmesh.data_element3s)
+               for (i = 0;i < loadmodel->surfmesh.num_triangles*3;i++)
+                       loadmodel->surfmesh.data_element3s[i] = loadmodel->surfmesh.data_element3i[i];
 }
 
 static void Mod_Q1BSP_LoadNodes_RecursiveSetParent(mnode_t *node, mnode_t *parent)
@@ -2622,11 +2648,14 @@ qboolean Mod_Q1BSP_CheckWaterAlphaSupport(void)
        int i, j;
        mleaf_t *leaf;
        const unsigned char *pvs;
+       // if there's no vis data, assume supported (because everything is visible all the time)
+       if (!loadmodel->brush.data_pvsclusters)
+               return true;
        // check all liquid leafs to see if they can see into empty leafs, if any
        // can we can assume this map supports r_wateralpha
        for (i = 0, leaf = loadmodel->brush.data_leafs;i < loadmodel->brush.num_leafs;i++, leaf++)
        {
-               if ((leaf->contents == CONTENTS_WATER || leaf->contents == CONTENTS_SLIME) && (leaf->clusterindex >= 0 && loadmodel->brush.data_pvsclusters))
+               if ((leaf->contents == CONTENTS_WATER || leaf->contents == CONTENTS_SLIME) && leaf->clusterindex >= 0)
                {
                        pvs = loadmodel->brush.data_pvsclusters + leaf->clusterindex * loadmodel->brush.num_pvsclusterbytes;
                        for (j = 0;j < loadmodel->brush.num_leafs;j++)
@@ -3586,11 +3615,15 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer, void *bufferend)
                        mod->brush.LightPoint = NULL;
                        mod->brush.AmbientSoundLevelsForPoint = NULL;
                }
+               // copy the submodel bounds, then enlarge the yaw and rotated bounds according to radius
+               VectorCopy(bm->mins, mod->normalmins);
+               VectorCopy(bm->maxs, mod->normalmaxs);
+               VectorCopy(bm->mins, mod->yawmins);
+               VectorCopy(bm->maxs, mod->yawmaxs);
+               VectorCopy(bm->mins, mod->rotatedmins);
+               VectorCopy(bm->maxs, mod->rotatedmaxs);
                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, surface = &mod->data_surfaces[mod->firstmodelsurface];j < mod->nummodelsurfaces;j++, surface++)
@@ -3603,12 +3636,6 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer, void *bufferend)
                                // calculate bounding shapes
                                for (k = 0, vec = (loadmodel->surfmesh.data_vertex3f + 3 * surface->num_firstvertex);k < surface->num_vertices;k++, vec += 3)
                                {
-                                       if (mod->normalmins[0] > vec[0]) mod->normalmins[0] = vec[0];
-                                       if (mod->normalmins[1] > vec[1]) mod->normalmins[1] = vec[1];
-                                       if (mod->normalmins[2] > vec[2]) mod->normalmins[2] = vec[2];
-                                       if (mod->normalmaxs[0] < vec[0]) mod->normalmaxs[0] = vec[0];
-                                       if (mod->normalmaxs[1] < vec[1]) mod->normalmaxs[1] = vec[1];
-                                       if (mod->normalmaxs[2] < vec[2]) mod->normalmaxs[2] = vec[2];
                                        dist = vec[0]*vec[0]+vec[1]*vec[1];
                                        if (modelyawradius < dist)
                                                modelyawradius = dist;
@@ -3619,11 +3646,16 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer, void *bufferend)
                        }
                        modelyawradius = sqrt(modelyawradius);
                        modelradius = sqrt(modelradius);
-                       mod->yawmins[0] = mod->yawmins[1] = - (mod->yawmaxs[0] = mod->yawmaxs[1] = modelyawradius);
-                       mod->yawmins[2] = mod->normalmins[2];
-                       mod->yawmaxs[2] = mod->normalmaxs[2];
-                       mod->rotatedmins[0] = mod->rotatedmins[1] = mod->rotatedmins[2] = -modelradius;
-                       mod->rotatedmaxs[0] = mod->rotatedmaxs[1] = mod->rotatedmaxs[2] = modelradius;
+                       mod->yawmins[0] = min(mod->yawmins[0], -modelyawradius);
+                       mod->yawmaxs[0] = min(mod->yawmaxs[0], -modelyawradius);
+                       mod->yawmins[1] = min(mod->yawmins[1],  modelyawradius);
+                       mod->yawmaxs[1] = min(mod->yawmaxs[1],  modelyawradius);
+                       mod->rotatedmins[0] = min(mod->rotatedmins[0], -modelradius);
+                       mod->rotatedmaxs[0] = min(mod->rotatedmaxs[0],  modelradius);
+                       mod->rotatedmins[1] = min(mod->rotatedmins[1], -modelradius);
+                       mod->rotatedmaxs[1] = min(mod->rotatedmaxs[1],  modelradius);
+                       mod->rotatedmins[2] = min(mod->rotatedmins[2], -modelradius);
+                       mod->rotatedmaxs[2] = min(mod->rotatedmaxs[2],  modelradius);
                        mod->radius = modelradius;
                        mod->radius2 = modelradius * modelradius;
 
@@ -4165,7 +4197,7 @@ static void Mod_Q3BSP_LoadTextures(lump_t *l)
 {
        q3dtexture_t *in;
        texture_t *out;
-       int i, count, c;
+       int i, count;
 
        in = (q3dtexture_t *)(mod_base + l->fileofs);
        if (l->filelen % sizeof(*in))
@@ -4187,12 +4219,8 @@ static void Mod_Q3BSP_LoadTextures(lump_t *l)
        if (cls.state == ca_dedicated)
                return;
 
-       c = 0;
        for (i = 0;i < count;i++, in++, out++)
-               if (!Mod_LoadTextureFromQ3Shader(out, out->name, false, true, false))
-                       c++;
-       if (c)
-               Con_DPrintf("%s: %i textures missing shaders\n", loadmodel->name, c);
+               Mod_LoadTextureFromQ3Shader(out, out->name, true, true, TEXF_MIPMAP | TEXF_ALPHA | TEXF_PRECACHE | (r_picmipworld.integer ? TEXF_PICMIP : 0) | TEXF_COMPRESS);
 }
 
 static void Mod_Q3BSP_LoadPlanes(lump_t *l)
@@ -4507,7 +4535,8 @@ static void Mod_Q3BSP_LoadLightmaps(lump_t *l, lump_t *faceslump)
                                        ;
                                for (mergeheight = 1;mergewidth*mergeheight < j && mergeheight < (1 << power);mergeheight *= 2)
                                        ;
-                               Con_DPrintf("lightmap merge texture #%i is %ix%i (%i of %i used)\n", lightmapindex, mergewidth*128, mergeheight*128, min(j, mergewidth*mergeheight), mergewidth*mergeheight);
+                               if (developer_loading.integer)
+                                       Con_Printf("lightmap merge texture #%i is %ix%i (%i of %i used)\n", lightmapindex, mergewidth*128, mergeheight*128, min(j, mergewidth*mergeheight), mergewidth*mergeheight);
                                loadmodel->brushq3.data_lightmaps[lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va("lightmap%04i", lightmapindex), mergewidth * 128, mergeheight * 128, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_PRECACHE | (gl_texturecompression_q3bsplightmaps.integer ? TEXF_COMPRESS : 0), NULL);
                                if (loadmodel->brushq3.data_deluxemaps)
                                        loadmodel->brushq3.data_deluxemaps[lightmapindex] = R_LoadTexture2D(loadmodel->texturepool, va("deluxemap%04i", lightmapindex), mergewidth * 128, mergeheight * 128, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_PRECACHE | (gl_texturecompression_q3bspdeluxemaps.integer ? TEXF_COMPRESS : 0), NULL);
@@ -4567,7 +4596,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
        {
                // check face type first
                type = LittleLong(in->type);
-               if (type != Q3FACETYPE_POLYGON
+               if (type != Q3FACETYPE_FLAT
                 && type != Q3FACETYPE_PATCH
                 && type != Q3FACETYPE_MESH
                 && type != Q3FACETYPE_FLARE)
@@ -4636,7 +4665,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                }
                switch(type)
                {
-               case Q3FACETYPE_POLYGON:
+               case Q3FACETYPE_FLAT:
                case Q3FACETYPE_MESH:
                        // no processing necessary
                        break;
@@ -4701,7 +4730,7 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
                out->num_firsttriangle = meshtriangles;
                switch(type)
                {
-               case Q3FACETYPE_POLYGON:
+               case Q3FACETYPE_FLAT:
                case Q3FACETYPE_MESH:
                        // no processing necessary, except for lightmap merging
                        for (j = 0;j < out->num_vertices;j++)
@@ -4879,6 +4908,11 @@ static void Mod_Q3BSP_LoadFaces(lump_t *l)
        // for per pixel lighting
        Mod_BuildTextureVectorsFromNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_texcoordtexture2f, loadmodel->surfmesh.data_normal3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_svector3f, loadmodel->surfmesh.data_tvector3f, true);
 
+       // generate ushort elements array if possible
+       if (loadmodel->surfmesh.data_element3s)
+               for (i = 0;i < loadmodel->surfmesh.num_triangles*3;i++)
+                       loadmodel->surfmesh.data_element3s[i] = loadmodel->surfmesh.data_element3i[i];
+
        // free the no longer needed vertex data
        loadmodel->brushq3.num_vertices = 0;
        if (loadmodel->brushq3.data_vertex3f)
@@ -5736,6 +5770,30 @@ void Mod_Q3BSP_Load(model_t *mod, void *buffer, void *bufferend)
 
                VectorCopy(mod->brushq3.data_models[i].mins, mod->normalmins);
                VectorCopy(mod->brushq3.data_models[i].maxs, mod->normalmaxs);
+               // enlarge the bounding box to enclose all geometry of this model,
+               // because q3map2 sometimes lies (mostly to affect the lightgrid),
+               // which can in turn mess up the farclip (as well as culling when
+               // outside the level - an unimportant concern)
+
+               //printf("Editing model %d... BEFORE re-bounding: %f %f %f - %f %f %f\n", i, mod->normalmins[0], mod->normalmins[1], mod->normalmins[2], mod->normalmaxs[0], mod->normalmaxs[1], mod->normalmaxs[2]);
+               for (j = 0;j < mod->nummodelsurfaces;j++)
+               {
+                       const msurface_t *surface = mod->data_surfaces + j + mod->firstmodelsurface;
+                       const float *v = mod->surfmesh.data_vertex3f + 3 * surface->num_firstvertex;
+                       int k;
+                       if (!surface->num_vertices)
+                               continue;
+                       for (k = 0;k < surface->num_vertices;k++, v += 3)
+                       {
+                               mod->normalmins[0] = min(mod->normalmins[0], v[0]);
+                               mod->normalmins[1] = min(mod->normalmins[1], v[1]);
+                               mod->normalmins[2] = min(mod->normalmins[2], v[2]);
+                               mod->normalmaxs[0] = max(mod->normalmaxs[0], v[0]);
+                               mod->normalmaxs[1] = max(mod->normalmaxs[1], v[1]);
+                               mod->normalmaxs[2] = max(mod->normalmaxs[2], v[2]);
+                       }
+               }
+               //printf("Editing model %d... AFTER re-bounding: %f %f %f - %f %f %f\n", i, mod->normalmins[0], mod->normalmins[1], mod->normalmins[2], mod->normalmaxs[0], mod->normalmaxs[1], mod->normalmaxs[2]);
                corner[0] = max(fabs(mod->normalmins[0]), fabs(mod->normalmaxs[0]));
                corner[1] = max(fabs(mod->normalmins[1]), fabs(mod->normalmaxs[1]));
                corner[2] = max(fabs(mod->normalmins[2]), fabs(mod->normalmaxs[2]));