]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_shared.c
fixed bug where animated mdl skins (such as Tomaz's health box and
[xonotic/darkplaces.git] / model_shared.c
index 090c93d16ec70fb6d89cea238329ede09035b6fd..9c63e458bea43b4251615a07248c3faef00edf61 100644 (file)
@@ -67,7 +67,23 @@ static void mod_shutdown(void)
 static void mod_newmap(void)
 {
        msurface_t *surface;
-       int i, surfacenum, ssize, tsize;
+       int i, j, k, surfacenum, ssize, tsize;
+
+       R_SkinFrame_PrepareForPurge();
+       for (i = 0;i < mod_numknown;i++)
+       {
+               if (mod_known[i].mempool && mod_known[i].data_textures)
+               {
+                       for (j = 0;j < mod_known[i].num_textures;j++)
+                       {
+                               for (k = 0;k < mod_known[i].data_textures[j].numskinframes;k++)
+                                       R_SkinFrame_MarkUsed(mod_known[i].data_textures[j].skinframes[k]);
+                               for (k = 0;k < mod_known[i].data_textures[j].backgroundnumskinframes;k++)
+                                       R_SkinFrame_MarkUsed(mod_known[i].data_textures[j].backgroundskinframes[k]);
+                       }
+               }
+       }
+       R_SkinFrame_Purge();
 
        if (!cl_stainmaps_clearonload.integer)
                return;
@@ -123,9 +139,9 @@ void Mod_UnloadModel (model_t *mod)
        isworldmodel = mod->isworldmodel;
        used = mod->used;
        if (mod->surfmesh.ebo)
-               R_Mesh_DestroyEBO(mod->surfmesh.ebo);
+               R_Mesh_DestroyBufferObject(mod->surfmesh.ebo);
        if (mod->surfmesh.vbo)
-               R_Mesh_DestroyVBO(mod->surfmesh.vbo);
+               R_Mesh_DestroyBufferObject(mod->surfmesh.vbo);
        // free textures/memory attached to the model
        R_FreeTexturePool(&mod->texturepool);
        Mem_FreePool(&mod->mempool);
@@ -218,9 +234,6 @@ model_t *Mod_LoadModel(model_t *mod, qboolean crash, qboolean checkdisk, qboolea
 
                // all models use memory, so allocate a memory pool
                mod->mempool = Mem_AllocPool(mod->name, 0, NULL);
-               // all models load textures, so allocate a texture pool
-               if (cls.state != ca_dedicated)
-                       mod->texturepool = R_AllocTexturePool();
 
                num = LittleLong(*((int *)buf));
                // call the apropriate loader
@@ -920,7 +933,7 @@ static void Mod_ShadowMesh_CreateVBOs(shadowmesh_t *mesh)
 
        // element buffer is easy because it's just one array
        if (mesh->numtriangles)
-               mesh->ebo = R_Mesh_CreateStaticEBO(mesh->element3i, mesh->numtriangles * sizeof(int[3]));
+               mesh->ebo = R_Mesh_CreateStaticBufferObject(GL_ELEMENT_ARRAY_BUFFER_ARB, mesh->element3i, mesh->numtriangles * sizeof(int[3]), "shadowmesh");
 
        // vertex buffer is several arrays and we put them in the same buffer
        //
@@ -943,7 +956,7 @@ static void Mod_ShadowMesh_CreateVBOs(shadowmesh_t *mesh)
                if (mesh->tvector3f         ) memcpy(mem + mesh->vbooffset_tvector3f         , mesh->tvector3f         , mesh->numverts * sizeof(float[3]));
                if (mesh->normal3f          ) memcpy(mem + mesh->vbooffset_normal3f          , mesh->normal3f          , mesh->numverts * sizeof(float[3]));
                if (mesh->texcoord2f        ) memcpy(mem + mesh->vbooffset_texcoord2f        , mesh->texcoord2f        , mesh->numverts * sizeof(float[2]));
-               mesh->vbo = R_Mesh_CreateStaticVBO(mem, size);
+               mesh->vbo = R_Mesh_CreateStaticBufferObject(GL_ARRAY_BUFFER_ARB, mem, size, "shadowmesh");
                Mem_Free(mem);
        }
 }
@@ -1023,197 +1036,14 @@ void Mod_ShadowMesh_Free(shadowmesh_t *mesh)
        for (;mesh;mesh = nextmesh)
        {
                if (mesh->ebo)
-                       R_Mesh_DestroyEBO(mesh->ebo);
+                       R_Mesh_DestroyBufferObject(mesh->ebo);
                if (mesh->vbo)
-                       R_Mesh_DestroyVBO(mesh->vbo);
+                       R_Mesh_DestroyBufferObject(mesh->vbo);
                nextmesh = mesh->next;
                Mem_Free(mesh);
        }
 }
 
-static rtexture_t *GL_TextureForSkinLayer(const unsigned char *in, int width, int height, const char *name, const unsigned int *palette, int textureflags, qboolean force)
-{
-       int i;
-       if (!force)
-       {
-               for (i = 0;i < width*height;i++)
-                       if (((unsigned char *)&palette[in[i]])[3] > 0)
-                               break;
-               if (i == width*height)
-                       return NULL;
-       }
-       return R_LoadTexture2D (loadmodel->texturepool, name, width, height, in, TEXTYPE_PALETTE, textureflags, palette);
-}
-
-int Mod_LoadSkinFrame(skinframe_t *skinframe, const char *basename, int textureflags, qboolean loadpantsandshirt, qboolean loadglowtexture)
-{
-       // FIXME: it should be possible to disable loading gloss and normalmap using cvars, to prevent wasted loading time and memory usage
-       qboolean loadnormalmap = true;
-       qboolean loadgloss = true;
-       int j;
-       unsigned char *pixels;
-       unsigned char *bumppixels;
-       unsigned char *basepixels;
-       int basepixels_width;
-       int basepixels_height;
-       char name[MAX_QPATH];
-       memset(skinframe, 0, sizeof(*skinframe));
-       Image_StripImageExtension(basename, name, sizeof(name));
-       skinframe->base = r_texture_notexture;
-       if (cls.state == ca_dedicated)
-               return false;
-
-       basepixels = loadimagepixels(name, false, 0, 0);
-       if (basepixels == NULL)
-               return false;
-       basepixels_width = image_width;
-       basepixels_height = image_height;
-       skinframe->base = R_LoadTexture2D (loadmodel->texturepool, basename, basepixels_width, basepixels_height, basepixels, TEXTYPE_RGBA, textureflags, NULL);
-
-       for (j = 3;j < basepixels_width * basepixels_height * 4;j += 4)
-               if (basepixels[j] < 255)
-                       break;
-       if (j < basepixels_width * basepixels_height * 4)
-       {
-               // has transparent pixels
-               pixels = (unsigned char *)Mem_Alloc(loadmodel->mempool, image_width * image_height * 4);
-               for (j = 0;j < image_width * image_height * 4;j += 4)
-               {
-                       pixels[j+0] = 255;
-                       pixels[j+1] = 255;
-                       pixels[j+2] = 255;
-                       pixels[j+3] = basepixels[j+3];
-               }
-               skinframe->fog = R_LoadTexture2D (loadmodel->texturepool, va("%s_mask", basename), image_width, image_height, pixels, TEXTYPE_RGBA, textureflags, NULL);
-               Mem_Free(pixels);
-       }
-
-       // _luma is supported for tenebrae compatibility
-       // (I think it's a very stupid name, but oh well)
-       if (loadglowtexture && ((pixels = loadimagepixels(va("%s_glow", name), false, 0, 0)) != NULL || (pixels = loadimagepixels(va("%s_luma", name), false, 0, 0)) != NULL)) {skinframe->glow = R_LoadTexture2D (loadmodel->texturepool, va("%s_glow", basename), image_width, image_height, pixels, TEXTYPE_RGBA, textureflags, NULL);Mem_Free(pixels);pixels = NULL;}
-       // _norm is the name used by tenebrae and has been adopted as standard
-       if (loadnormalmap)
-       {
-               if ((pixels = loadimagepixels(va("%s_norm", name), false, 0, 0)) != NULL)
-               {
-                       skinframe->nmap = R_LoadTexture2D (loadmodel->texturepool, va("%s_nmap", basename), image_width, image_height, pixels, TEXTYPE_RGBA, textureflags, NULL);
-                       Mem_Free(pixels);
-                       pixels = NULL;
-               }
-               else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixels(va("%s_bump", name), false, 0, 0)) != NULL)
-               {
-                       pixels = (unsigned char *)Mem_Alloc(loadmodel->mempool, image_width * image_height * 4);
-                       Image_HeightmapToNormalmap(bumppixels, pixels, image_width, image_height, false, r_shadow_bumpscale_bumpmap.value);
-                       skinframe->nmap = R_LoadTexture2D (loadmodel->texturepool, va("%s_nmap", basename), image_width, image_height, pixels, TEXTYPE_RGBA, textureflags, NULL);
-                       Mem_Free(pixels);
-                       Mem_Free(bumppixels);
-               }
-               else if (r_shadow_bumpscale_basetexture.value > 0)
-               {
-                       pixels = (unsigned char *)Mem_Alloc(loadmodel->mempool, basepixels_width * basepixels_height * 4);
-                       Image_HeightmapToNormalmap(basepixels, pixels, basepixels_width, basepixels_height, false, r_shadow_bumpscale_basetexture.value);
-                       skinframe->nmap = R_LoadTexture2D (loadmodel->texturepool, va("%s_nmap", basename), basepixels_width, basepixels_height, pixels, TEXTYPE_RGBA, textureflags, NULL);
-                       Mem_Free(pixels);
-               }
-       }
-       if (loadgloss         && (pixels = loadimagepixels(va("%s_gloss", name), false, 0, 0)) != NULL) {skinframe->gloss = R_LoadTexture2D (loadmodel->texturepool, va("%s_gloss", basename), image_width, image_height, pixels, TEXTYPE_RGBA, textureflags, NULL);Mem_Free(pixels);pixels = NULL;}
-       if (loadpantsandshirt && (pixels = loadimagepixels(va("%s_pants", name), false, 0, 0)) != NULL) {skinframe->pants = R_LoadTexture2D (loadmodel->texturepool, va("%s_pants", basename), image_width, image_height, pixels, TEXTYPE_RGBA, textureflags, NULL);Mem_Free(pixels);pixels = NULL;}
-       if (loadpantsandshirt && (pixels = loadimagepixels(va("%s_shirt", name), false, 0, 0)) != NULL) {skinframe->shirt = R_LoadTexture2D (loadmodel->texturepool, va("%s_shirt", basename), image_width, image_height, pixels, TEXTYPE_RGBA, textureflags, NULL);Mem_Free(pixels);pixels = NULL;}
-
-       if (!skinframe->base)
-               skinframe->base = r_texture_notexture;
-       if (!skinframe->nmap)
-               skinframe->nmap = r_texture_blanknormalmap;
-
-       if (basepixels)
-               Mem_Free(basepixels);
-
-       return true;
-}
-
-int Mod_LoadSkinFrame_Internal(skinframe_t *skinframe, const char *basename, int textureflags, int loadpantsandshirt, int loadglowtexture, const unsigned char *skindata, int width, int height, int bitsperpixel, const unsigned int *palette, const unsigned int *alphapalette)
-{
-       int i;
-       unsigned char *temp1, *temp2;
-       memset(skinframe, 0, sizeof(*skinframe));
-       if (cls.state == ca_dedicated)
-               return false;
-       if (!skindata)
-               return false;
-       if (bitsperpixel == 32)
-       {
-               if (r_shadow_bumpscale_basetexture.value > 0)
-               {
-                       temp1 = (unsigned char *)Mem_Alloc(loadmodel->mempool, width * height * 8);
-                       temp2 = temp1 + width * height * 4;
-                       Image_HeightmapToNormalmap(skindata, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
-                       skinframe->nmap = R_LoadTexture2D(loadmodel->texturepool, va("%s_nmap", basename), width, height, temp2, TEXTYPE_RGBA, textureflags | TEXF_ALPHA, NULL);
-                       Mem_Free(temp1);
-               }
-               skinframe->base = skinframe->merged = R_LoadTexture2D(loadmodel->texturepool, basename, width, height, skindata, TEXTYPE_RGBA, textureflags, NULL);
-               if (textureflags & TEXF_ALPHA)
-               {
-                       for (i = 3;i < width * height * 4;i += 4)
-                               if (skindata[i] < 255)
-                                       break;
-                       if (i < width * height * 4)
-                       {
-                               unsigned char *fogpixels = (unsigned char *)Mem_Alloc(loadmodel->mempool, width * height * 4);
-                               memcpy(fogpixels, skindata, width * height * 4);
-                               for (i = 0;i < width * height * 4;i += 4)
-                                       fogpixels[i] = fogpixels[i+1] = fogpixels[i+2] = 255;
-                               skinframe->fog = R_LoadTexture2D(loadmodel->texturepool, va("%s_fog", basename), width, height, fogpixels, TEXTYPE_RGBA, textureflags, NULL);
-                               Mem_Free(fogpixels);
-                       }
-               }
-       }
-       else if (bitsperpixel == 8)
-       {
-               if (r_shadow_bumpscale_basetexture.value > 0)
-               {
-                       temp1 = (unsigned char *)Mem_Alloc(loadmodel->mempool, width * height * 8);
-                       temp2 = temp1 + width * height * 4;
-                       if (bitsperpixel == 32)
-                               Image_HeightmapToNormalmap(skindata, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
-                       else
-                       {
-                               // use either a custom palette or the quake palette
-                               Image_Copy8bitRGBA(skindata, temp1, width * height, palette ? palette : palette_complete);
-                               Image_HeightmapToNormalmap(temp1, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
-                       }
-                       skinframe->nmap = R_LoadTexture2D(loadmodel->texturepool, va("%s_nmap", basename), width, height, temp2, TEXTYPE_RGBA, textureflags | TEXF_ALPHA, NULL);
-                       Mem_Free(temp1);
-               }
-               // use either a custom palette, or the quake palette
-               skinframe->base = skinframe->merged = GL_TextureForSkinLayer(skindata, width, height, va("%s_merged", basename), palette ? palette : (loadglowtexture ? palette_nofullbrights : ((textureflags & TEXF_ALPHA) ? palette_transparent : palette_complete)), textureflags, true); // all
-               if (!palette && loadglowtexture)
-                       skinframe->glow = GL_TextureForSkinLayer(skindata, width, height, va("%s_glow", basename), palette_onlyfullbrights, textureflags, false); // glow
-               if (!palette && loadpantsandshirt)
-               {
-                       skinframe->pants = GL_TextureForSkinLayer(skindata, width, height, va("%s_pants", basename), palette_pantsaswhite, textureflags, false); // pants
-                       skinframe->shirt = GL_TextureForSkinLayer(skindata, width, height, va("%s_shirt", basename), palette_shirtaswhite, textureflags, false); // shirt
-               }
-               if (skinframe->pants || skinframe->shirt)
-                       skinframe->base = GL_TextureForSkinLayer(skindata, width, height, va("%s_nospecial", basename),loadglowtexture ? palette_nocolormapnofullbrights : palette_nocolormap, textureflags, false); // no special colors
-               if (textureflags & TEXF_ALPHA)
-               {
-                       // if not using a custom alphapalette, use the quake one
-                       if (!alphapalette)
-                               alphapalette = palette_alpha;
-                       for (i = 0;i < width * height;i++)
-                               if (((unsigned char *)alphapalette)[skindata[i]*4+3] < 255)
-                                       break;
-                       if (i < width * height)
-                               skinframe->fog = GL_TextureForSkinLayer(skindata, width, height, va("%s_fog", basename), alphapalette, textureflags, true); // fog mask
-               }
-       }
-       else
-               return false;
-       if (!skinframe->nmap)
-               skinframe->nmap = r_texture_blanknormalmap;
-       return true;
-}
-
 void Mod_GetTerrainVertex3fTexCoord2fFromRGBA(const unsigned char *imagepixels, int imagewidth, int imageheight, int ix, int iy, float *vertex3f, float *texcoord2f, matrix4x4_t *pixelstepmatrix, matrix4x4_t *pixeltexturestepmatrix)
 {
        float v[3], tc[3];
@@ -1489,7 +1319,7 @@ static void Mod_BuildVBOs(void)
 
        // element buffer is easy because it's just one array
        if (loadmodel->surfmesh.num_triangles)
-               loadmodel->surfmesh.ebo = R_Mesh_CreateStaticEBO(loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles * sizeof(int[3]));
+               loadmodel->surfmesh.ebo = R_Mesh_CreateStaticBufferObject(GL_ELEMENT_ARRAY_BUFFER_ARB, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles * sizeof(int[3]), loadmodel->name);
 
        // vertex buffer is several arrays and we put them in the same buffer
        //
@@ -1516,7 +1346,7 @@ static void Mod_BuildVBOs(void)
                if (loadmodel->surfmesh.data_texcoordtexture2f ) memcpy(mem + loadmodel->surfmesh.vbooffset_texcoordtexture2f , loadmodel->surfmesh.data_texcoordtexture2f , loadmodel->surfmesh.num_vertices * sizeof(float[2]));
                if (loadmodel->surfmesh.data_texcoordlightmap2f) memcpy(mem + loadmodel->surfmesh.vbooffset_texcoordlightmap2f, loadmodel->surfmesh.data_texcoordlightmap2f, loadmodel->surfmesh.num_vertices * sizeof(float[2]));
                if (loadmodel->surfmesh.data_lightmapcolor4f   ) memcpy(mem + loadmodel->surfmesh.vbooffset_lightmapcolor4f   , loadmodel->surfmesh.data_lightmapcolor4f   , loadmodel->surfmesh.num_vertices * sizeof(float[4]));
-               loadmodel->surfmesh.vbo = R_Mesh_CreateStaticVBO(mem, size);
+               loadmodel->surfmesh.vbo = R_Mesh_CreateStaticBufferObject(GL_ARRAY_BUFFER_ARB, mem, size, loadmodel->name);
                Mem_Free(mem);
        }
 }