]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_shared.c
Added Quake 2 sprite support (.sp2 files). Added the corresponding QC extension ...
[xonotic/darkplaces.git] / model_shared.c
index 366a03f4088e04b3311e5a49c6dc2054065a09f7..5064ac4b24189821c84c5cc2d886a50069e3db2e 100644 (file)
@@ -35,7 +35,7 @@ model_t *loadmodel;
 static model_t mod_known[MAX_MOD_KNOWN];
 
 rtexturepool_t *mod_shared_texturepool;
-rtexture_t *r_notexture;
+rtexture_t *r_texture_notexture;
 rtexture_t *mod_shared_detailtextures[NUM_DETAILTEXTURES];
 rtexture_t *mod_shared_distorttexture[64];
 
@@ -136,36 +136,8 @@ void Mod_BuildDistortTexture (void)
        return;
 }
 
-texture_t r_surf_notexture;
-
 void Mod_SetupNoTexture(void)
 {
-       int x, y;
-       qbyte pix[16][16][4];
-
-       // this makes a light grey/dark grey checkerboard texture
-       for (y = 0;y < 16;y++)
-       {
-               for (x = 0;x < 16;x++)
-               {
-                       if ((y < 8) ^ (x < 8))
-                       {
-                               pix[y][x][0] = 128;
-                               pix[y][x][1] = 128;
-                               pix[y][x][2] = 128;
-                               pix[y][x][3] = 255;
-                       }
-                       else
-                       {
-                               pix[y][x][0] = 64;
-                               pix[y][x][1] = 64;
-                               pix[y][x][2] = 64;
-                               pix[y][x][3] = 255;
-                       }
-               }
-       }
-
-       r_notexture = R_LoadTexture2D(mod_shared_texturepool, "notexture", 16, 16, &pix[0][0][0], TEXTYPE_RGBA, TEXF_MIPMAP, NULL);
 }
 
 static void mod_start(void)
@@ -194,30 +166,24 @@ static void mod_shutdown(void)
 
 static void mod_newmap(void)
 {
-       msurface_t *surf;
-       int i, surfnum, ssize, tsize;
+       msurface_t *surface;
+       int i, surfacenum, ssize, tsize;
 
-       if (!cl_stainmapsclearonload.integer)
+       if (!cl_stainmaps_clearonload.integer)
                return;
 
-       for (i=0; i<MAX_MOD_KNOWN; i++)
+       for (i = 0;i < MAX_MOD_KNOWN;i++)
        {
-               if (mod_known[i].name[0] && mod_known[i].type == mod_brushq1)
+               if (mod_known[i].name[0])
                {
-                       for (surfnum=0, surf=mod_known[i].brushq1.surfaces; surfnum<mod_known[i].brushq1.numsurfaces;surfnum++, surf++)
+                       for (surfacenum = 0, surface = mod_known[i].brush.data_surfaces;surfacenum < mod_known[i].brush.num_surfaces;surfacenum++, surface++)
                        {
-                               if (surf->texinfo->texture->flags & SURF_LIGHTMAP)
+                               if (surface->lightmapinfo && surface->lightmapinfo->stainsamples)
                                {
-                                       ssize = (surf->extents[0] >> 4) + 1;
-                                       tsize = (surf->extents[1] >> 4) + 1;
-                                       
-                                       if (ssize > 256 || tsize > 256)
-                                               Host_Error("Bad surface extents");
-
-                                       if (surf->stainsamples)
-                                               memset(surf->stainsamples, 255, ssize * tsize * 3);
-
-                                       surf->cached_dlight = true;
+                                       ssize = (surface->lightmapinfo->extents[0] >> 4) + 1;
+                                       tsize = (surface->lightmapinfo->extents[1] >> 4) + 1;
+                                       memset(surface->lightmapinfo->stainsamples, 255, ssize * tsize * 3);
+                                       surface->cached_dlight = true;
                                }
                        }
                }
@@ -265,7 +231,7 @@ void Mod_UnloadModel (model_t *mod)
        Mod_FreeModel(mod);
        strcpy(mod->name, name);
        mod->isworldmodel = isworldmodel;
-       mod->needload = true;
+       mod->loaded = false;
 }
 
 /*
@@ -289,31 +255,32 @@ static model_t *Mod_LoadModel(model_t *mod, qboolean crash, qboolean checkdisk,
        crc = 0;
        buf = NULL;
        if (mod->isworldmodel != isworldmodel)
-               mod->needload = true;
-       if (mod->needload || checkdisk)
+               mod->loaded = false;
+       if (!mod->loaded || checkdisk)
        {
-               if (checkdisk && !mod->needload)
+               if (checkdisk && mod->loaded)
                        Con_DPrintf("checking model %s\n", mod->name);
                buf = FS_LoadFile (mod->name, tempmempool, false);
                if (buf)
                {
                        crc = CRC_Block(buf, fs_filesize);
                        if (mod->crc != crc)
-                               mod->needload = true;
+                               mod->loaded = false;
                }
        }
-       if (!mod->needload)
+       if (mod->loaded)
                return mod; // already loaded
 
        Con_DPrintf("loading model %s\n", mod->name);
        // LordHavoc: unload the existing model in this slot (if there is one)
        Mod_UnloadModel(mod);
+
        // load the model
        mod->isworldmodel = isworldmodel;
        mod->used = true;
        mod->crc = crc;
-       // errors can prevent the corresponding mod->needload = false;
-       mod->needload = true;
+       // errors can prevent the corresponding mod->loaded = true;
+       mod->loaded = false;
 
        // default model radius and bounding box (mainly for missing models)
        mod->radius = 16;
@@ -339,6 +306,7 @@ static model_t *Mod_LoadModel(model_t *mod, qboolean crash, qboolean checkdisk,
                else if (!memcmp(buf, "IDP2", 4)) Mod_IDP2_Load(mod, buf);
                else if (!memcmp(buf, "IDP3", 4)) Mod_IDP3_Load(mod, buf);
                else if (!memcmp(buf, "IDSP", 4)) Mod_IDSP_Load(mod, buf);
+               else if (!memcmp(buf, "IDS2", 4)) Mod_IDS2_Load(mod, buf);
                else if (!memcmp(buf, "IBSP", 4)) Mod_IBSP_Load(mod, buf);
                else if (!memcmp(buf, "ZYMOTICMODEL", 12)) Mod_ZYMOTICMODEL_Load(mod, buf);
                else if (strlen(mod->name) >= 4 && !strcmp(mod->name - 4, ".map")) Mod_MAP_Load(mod, buf);
@@ -353,7 +321,7 @@ static model_t *Mod_LoadModel(model_t *mod, qboolean crash, qboolean checkdisk,
        }
 
        // no errors occurred
-       mod->needload = false;
+       mod->loaded = true;
        return mod;
 }
 
@@ -361,7 +329,7 @@ void Mod_CheckLoaded(model_t *mod)
 {
        if (mod)
        {
-               if (mod->needload)
+               if (!mod->loaded)
                        Mod_LoadModel(mod, true, true, mod->isworldmodel);
                else
                {
@@ -403,6 +371,15 @@ void Mod_PurgeUnused(void)
                                Mod_FreeModel(mod);
 }
 
+// only used during loading!
+void Mod_RemoveStaleWorldModels(model_t *skip)
+{
+       int i;
+       for (i = 0;i < MAX_MOD_KNOWN;i++)
+               if (mod_known[i].isworldmodel && skip != &mod_known[i])
+                       Mod_UnloadModel(mod_known + i);
+}
+
 void Mod_LoadModels(void)
 {
        int i;
@@ -448,7 +425,7 @@ model_t *Mod_FindName(const char *name)
        {
                mod = freemod;
                strcpy (mod->name, name);
-               mod->needload = true;
+               mod->loaded = false;
                mod->used = true;
                return mod;
        }
@@ -457,20 +434,6 @@ model_t *Mod_FindName(const char *name)
        return NULL;
 }
 
-/*
-==================
-Mod_TouchModel
-
-==================
-*/
-void Mod_TouchModel(const char *name)
-{
-       model_t *mod;
-
-       mod = Mod_FindName(name);
-       mod->used = true;
-}
-
 /*
 ==================
 Mod_ForName
@@ -790,6 +753,38 @@ void Mod_BuildTextureVectorsAndNormals(int numverts, int numtriangles, const flo
                        VectorNormalize(v);
 }
 
+surfmesh_t *Mod_AllocSurfMesh(mempool_t *mempool, int numvertices, int numtriangles, qboolean detailtexcoords, qboolean lightmapoffsets, qboolean vertexcolors, qboolean neighbors)
+{
+       surfmesh_t *mesh;
+       qbyte *data;
+       mesh = Mem_Alloc(mempool, sizeof(surfmesh_t) + numvertices * (3 + 3 + 3 + 3 + 2 + 2 + (detailtexcoords ? 2 : 0) + (vertexcolors ? 4 : 0)) * sizeof(float) + numvertices * (lightmapoffsets ? 1 : 0) * sizeof(int) + numtriangles * (3 + (neighbors ? 3 : 0)) * sizeof(int));
+       mesh->num_vertices = numvertices;
+       mesh->num_triangles = numtriangles;
+       data = (qbyte *)(mesh + 1);
+       if (mesh->num_vertices)
+       {
+               mesh->data_vertex3f = (float *)data, data += sizeof(float[3]) * mesh->num_vertices;
+               mesh->data_svector3f = (float *)data, data += sizeof(float[3]) * mesh->num_vertices;
+               mesh->data_tvector3f = (float *)data, data += sizeof(float[3]) * mesh->num_vertices;
+               mesh->data_normal3f = (float *)data, data += sizeof(float[3]) * mesh->num_vertices;
+               mesh->data_texcoordtexture2f = (float *)data, data += sizeof(float[2]) * mesh->num_vertices;
+               mesh->data_texcoordlightmap2f = (float *)data, data += sizeof(float[2]) * mesh->num_vertices;
+               if (detailtexcoords)
+                       mesh->data_texcoorddetail2f = (float *)data, data += sizeof(float[2]) * mesh->num_vertices;
+               if (vertexcolors)
+                       mesh->data_lightmapcolor4f = (float *)data, data += sizeof(float[4]) * mesh->num_vertices;
+               if (lightmapoffsets)
+                       mesh->data_lightmapoffsets = (int *)data, data += sizeof(int) * mesh->num_vertices;
+       }
+       if (mesh->num_triangles)
+       {
+               mesh->data_element3i = (int *)data, data += sizeof(int[3]) * mesh->num_triangles;
+               if (neighbors)
+                       mesh->data_neighbor3i = (int *)data, data += sizeof(int[3]) * mesh->num_triangles;
+       }
+       return mesh;
+}
+
 shadowmesh_t *Mod_ShadowMesh_Alloc(mempool_t *mempool, int maxverts, int maxtriangles, rtexture_t *map_diffuse, rtexture_t *map_specular, rtexture_t *map_normal, int light, int neighbors, int expandable)
 {
        shadowmesh_t *newmesh;
@@ -1322,6 +1317,14 @@ int Mod_CountSkinFiles(skinfile_t *skinfile)
        return i;
 }
 
+void Mod_SnapVertices(int numcomponents, int numvertices, float *vertices, float snap)
+{
+       int i;
+       double isnap = 1.0 / snap;
+       for (i = 0;i < numvertices*numcomponents;i++)
+               vertices[i] = floor(vertices[i]*isnap)*snap;
+}
+
 int Mod_RemoveDegenerateTriangles(int numtriangles, const int *inelement3i, int *outelement3i, const float *vertex3f)
 {
        int i, outtriangles;