]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - model_shared.c
damn, I can't convert hex/dec...
[xonotic/darkplaces.git] / model_shared.c
index de52950347f08dc0007da78251ba4ce95a358343..9e610206e2d823999c27219d8283d0824289e324 100644 (file)
@@ -26,7 +26,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "image.h"
 #include "r_shadow.h"
 
-cvar_t r_mipskins = {CVAR_SAVE, "r_mipskins", "0", "mipmaps skins (so they become blurrier in the distance), disabled by default because it tends to blur with strange border colors from the skin"};
+cvar_t r_mipskins = {CVAR_SAVE, "r_mipskins", "0", "mipmaps model skins so they render faster in the distance and do not display noise artifacts, can cause discoloration of skins if they contain undesirable border colors"};
 
 model_t *loadmodel;
 
@@ -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,144 +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, int loadpantsandshirt, int loadglowtexture)
-{
-       imageskin_t s;
-       memset(skinframe, 0, sizeof(*skinframe));
-       skinframe->base = r_texture_notexture;
-       if (cls.state == ca_dedicated)
-               return false;
-       if (!image_loadskin(&s, basename))
-               return false;
-       skinframe->base = R_LoadTexture2D (loadmodel->texturepool, basename, s.basepixels_width, s.basepixels_height, s.basepixels, TEXTYPE_RGBA, textureflags, NULL);
-       if (s.nmappixels != NULL)
-               skinframe->nmap = R_LoadTexture2D (loadmodel->texturepool, va("%s_nmap", basename), s.nmappixels_width, s.nmappixels_height, s.nmappixels, TEXTYPE_RGBA, textureflags, NULL);
-       if (s.glosspixels != NULL)
-               skinframe->gloss = R_LoadTexture2D (loadmodel->texturepool, va("%s_gloss", basename), s.glosspixels_width, s.glosspixels_height, s.glosspixels, TEXTYPE_RGBA, textureflags, NULL);
-       if (s.glowpixels != NULL && loadglowtexture)
-               skinframe->glow = R_LoadTexture2D (loadmodel->texturepool, va("%s_glow", basename), s.glowpixels_width, s.glowpixels_height, s.glowpixels, TEXTYPE_RGBA, textureflags, NULL);
-       if (s.maskpixels != NULL)
-               skinframe->fog = R_LoadTexture2D (loadmodel->texturepool, va("%s_mask", basename), s.maskpixels_width, s.maskpixels_height, s.maskpixels, TEXTYPE_RGBA, textureflags, NULL);
-       if (loadpantsandshirt)
-       {
-               if (s.pantspixels != NULL)
-                       skinframe->pants = R_LoadTexture2D (loadmodel->texturepool, va("%s_pants", basename), s.pantspixels_width, s.pantspixels_height, s.pantspixels, TEXTYPE_RGBA, textureflags, NULL);
-               if (s.shirtpixels != NULL)
-                       skinframe->shirt = R_LoadTexture2D (loadmodel->texturepool, va("%s_shirt", basename), s.shirtpixels_width, s.shirtpixels_height, s.shirtpixels, TEXTYPE_RGBA, textureflags, NULL);
-       }
-       if (!skinframe->base)
-               skinframe->base = r_texture_notexture;
-       if (!skinframe->nmap)
-               skinframe->nmap = r_texture_blanknormalmap;
-       image_freeskin(&s);
-       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];
@@ -1436,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
        //
@@ -1463,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);
        }
 }