change default snd_soundradius back to 1000, matching Quake
[divverent/darkplaces.git] / model_sprite.c
index f5d9e4e..7c80ef4 100644 (file)
@@ -25,7 +25,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "quakedef.h"
 #include "image.h"
 
-cvar_t r_mipsprites = {CVAR_SAVE, "r_mipsprites", "1", "mipmaps skins (so they become blurrier in the distance), unlike skins the sprites do not have strange border colors"};
+cvar_t r_mipsprites = {CVAR_SAVE, "r_mipsprites", "1", "mipmaps sprites so they render faster in the distance and do not display noise artifacts"};
+cvar_t r_labelsprites_scale = {CVAR_SAVE, "r_labelsprites_scale", "1", "global scale to apply to label sprites before conversion to HUD coordinates"};
+cvar_t r_labelsprites_roundtopixels = {CVAR_SAVE, "r_labelsprites_roundtopixels", "1", "try to make label sprites sharper by rounding their size to 0.5x or 1x and by rounding their position to whole pixels if possible"};
 
 /*
 ===============
@@ -35,32 +37,47 @@ Mod_SpriteInit
 void Mod_SpriteInit (void)
 {
        Cvar_RegisterVariable(&r_mipsprites);
+       Cvar_RegisterVariable(&r_labelsprites_scale);
+       Cvar_RegisterVariable(&r_labelsprites_roundtopixels);
 }
 
-static void Mod_SpriteSetupTexture(mspriteframe_t *frame, qboolean fullbright, qboolean additive)
+static void Mod_SpriteSetupTexture(texture_t *texture, skinframe_t *skinframe, qboolean fullbright, qboolean additive)
 {
-       texture_t *texture = &frame->texture;
+       if (!skinframe)
+               skinframe = R_SkinFrame_LoadMissing();
+       texture->offsetmapping = OFFSETMAPPING_OFF;
+       texture->offsetscale = 1;
+       texture->specularscalemod = 1;
+       texture->specularpowermod = 1;
        texture->basematerialflags = MATERIALFLAG_WALL;
        if (fullbright)
                texture->basematerialflags |= MATERIALFLAG_FULLBRIGHT;
        if (additive)
-               texture->basematerialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_NOSHADOW;
-       else if (texture->skinframes[0].fog)
-               texture->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_NOSHADOW;
+               texture->basematerialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
+       else if (skinframe->hasalpha)
+               texture->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
        texture->currentmaterialflags = texture->basematerialflags;
-       texture->currentskinframe = texture->skinframes + 0;
+       texture->numskinframes = 1;
+       texture->currentskinframe = texture->skinframes[0] = skinframe;
+       texture->surfaceflags = 0;
+       texture->supercontents = SUPERCONTENTS_SOLID;
+       if (!(texture->basematerialflags & MATERIALFLAG_BLENDED))
+               texture->supercontents |= SUPERCONTENTS_OPAQUE;
 }
 
-static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version, const unsigned int *palette, const unsigned int *alphapalette, qboolean additive)
+static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version, const unsigned int *palette, qboolean additive)
 {
-       int                                     i, j, groupframes, realframes, x, y, origin[2], width, height, fullbright;
+       int                                     i, j, groupframes, realframes, x, y, origin[2], width, height;
+       qboolean                        fullbright;
        dspriteframetype_t      *pinframetype;
        dspriteframe_t          *pinframe;
        dspritegroup_t          *pingroup;
        dspriteinterval_t       *pinintervals;
+       skinframe_t                     *skinframe;
        float                           modelradius, interval;
        char                            name[MAX_QPATH], fogname[MAX_QPATH];
        const void                      *startframes;
+       int                 texflags = (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ISSPRITE | TEXF_PICMIP | TEXF_ALPHA | TEXF_CLAMP;
        modelradius = 0;
 
        if (loadmodel->numframes < 1)
@@ -107,6 +124,9 @@ static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version
 
        loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
        loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * realframes);
+       loadmodel->num_textures = realframes;
+       loadmodel->num_texturesperskin = 1;
+       loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures);
 
        datapointer = (unsigned char *)startframes;
        realframes = 0;
@@ -135,7 +155,7 @@ static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version
                                Host_Error("Mod_Sprite_SharedSetup: invalid interval");
                }
 
-               sprintf(loadmodel->animscenes[i].name, "frame %i", i);
+               dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i);
                loadmodel->animscenes[i].firstframe = realframes;
                loadmodel->animscenes[i].framecount = groupframes;
                loadmodel->animscenes[i].framerate = 1.0f / interval;
@@ -161,27 +181,46 @@ static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version
                        if (modelradius < x + y)
                                modelradius = x + y;
 
-                       if (width > 0 && height > 0 && cls.state != ca_dedicated)
+                       if (cls.state != ca_dedicated)
                        {
-                               if (groupframes > 1)
-                                       sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
-                               else
-                                       sprintf (name, "%s_%i", loadmodel->name, i);
-                               if (!Mod_LoadSkinFrame(&loadmodel->sprite.sprdata_frames[realframes].texture.skinframes[0], name, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, false, false))
+                               skinframe = NULL;
+                               // note: Nehahra's null.spr has width == 0 and height == 0
+                               if (width > 0 && height > 0)
                                {
                                        if (groupframes > 1)
-                                               sprintf (fogname, "%s_%i_%ifog", loadmodel->name, i, j);
+                                       {
+                                               dpsnprintf (name, sizeof(name), "%s_%i_%i", loadmodel->name, i, j);
+                                               dpsnprintf (fogname, sizeof(fogname), "%s_%i_%ifog", loadmodel->name, i, j);
+                                       }
                                        else
-                                               sprintf (fogname, "%s_%ifog", loadmodel->name, i);
-                                       if (version == SPRITE32_VERSION)
-                                               Mod_LoadSkinFrame_Internal(&loadmodel->sprite.sprdata_frames[realframes].texture.skinframes[0], name, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, false, false, datapointer, width, height, 32, NULL, NULL);
-                                       else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
-                                               Mod_LoadSkinFrame_Internal(&loadmodel->sprite.sprdata_frames[realframes].texture.skinframes[0], name, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, false, false, datapointer, width, height, 8, palette, alphapalette);
+                                       {
+                                               dpsnprintf (name, sizeof(name), "%s_%i", loadmodel->name, i);
+                                               dpsnprintf (fogname, sizeof(fogname), "%s_%ifog", loadmodel->name, i);
+                                       }
+                                       if (!(skinframe = R_SkinFrame_LoadExternal(name, texflags | TEXF_COMPRESS, false)))
+                                       {
+                                               unsigned char *pixels = (unsigned char *) Mem_Alloc(loadmodel->mempool, width*height*4);
+                                               if (version == SPRITE32_VERSION)
+                                               {
+                                                       for (x = 0;x < width*height;x++)
+                                                       {
+                                                               pixels[x*4+2] = datapointer[x*4+0];
+                                                               pixels[x*4+1] = datapointer[x*4+1];
+                                                               pixels[x*4+0] = datapointer[x*4+2];
+                                                               pixels[x*4+3] = datapointer[x*4+3];
+                                                       }
+                                               }
+                                               else //if (version == SPRITEHL_VERSION || version == SPRITE_VERSION)
+                                                       Image_Copy8bitBGRA(datapointer, pixels, width*height, palette ? palette : palette_bgra_transparent);
+                                               skinframe = R_SkinFrame_LoadInternalBGRA(name, texflags, pixels, width, height);
+                                               Mem_Free(pixels);
+                                       }
                                }
+                               if (skinframe == NULL)
+                                       skinframe = R_SkinFrame_LoadMissing();
+                               Mod_SpriteSetupTexture(&loadmodel->data_textures[realframes], skinframe, fullbright, additive);
                        }
 
-                       Mod_SpriteSetupTexture(&loadmodel->sprite.sprdata_frames[realframes], fullbright, additive);
-
                        if (version == SPRITE32_VERSION)
                                datapointer += width * height * 4;
                        else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
@@ -201,21 +240,24 @@ static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version
 }
 
 extern void R_Model_Sprite_Draw(entity_render_t *ent);
-void Mod_IDSP_Load(model_t *mod, void *buffer, void *bufferend)
+void Mod_IDSP_Load(dp_model_t *mod, void *buffer, void *bufferend)
 {
        int version;
        const unsigned char *datapointer;
 
        datapointer = (unsigned char *)buffer;
 
+       loadmodel->modeldatatypestring = "SPR1";
+
        loadmodel->type = mod_sprite;
-       loadmodel->flags2 = EF_FULLBRIGHT;
 
        loadmodel->DrawSky = NULL;
        loadmodel->Draw = R_Model_Sprite_Draw;
+       loadmodel->DrawDepth = NULL;
        loadmodel->CompileShadowVolume = NULL;
        loadmodel->DrawShadowVolume = NULL;
        loadmodel->DrawLight = NULL;
+       loadmodel->DrawAddWaterPlanes = NULL;
 
        version = LittleLong(((dsprite_t *)buffer)->version);
        if (version == SPRITE_VERSION || version == SPRITE32_VERSION)
@@ -229,12 +271,12 @@ void Mod_IDSP_Load(model_t *mod, void *buffer, void *bufferend)
                loadmodel->sprite.sprnum_type = LittleLong (pinqsprite->type);
                loadmodel->synctype = (synctype_t)LittleLong (pinqsprite->synctype);
 
-               Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), NULL, NULL, false);
+               Mod_Sprite_SharedSetup(datapointer, LittleLong (pinqsprite->version), NULL, false);
        }
        else if (version == SPRITEHL_VERSION)
        {
                int i, rendermode;
-               unsigned char palette[256][4], alphapalette[256][4];
+               unsigned char palette[256][4];
                const unsigned char *in;
                dspritehl_t *pinhlsprite;
 
@@ -258,18 +300,18 @@ void Mod_IDSP_Load(model_t *mod, void *buffer, void *bufferend)
                case SPRHL_OPAQUE:
                        for (i = 0;i < 256;i++)
                        {
-                               palette[i][0] = *in++;
-                               palette[i][1] = *in++;
-                               palette[i][2] = *in++;
+                               palette[i][2] = in[i*3+0];
+                               palette[i][1] = in[i*3+1];
+                               palette[i][0] = in[i*3+2];
                                palette[i][3] = 255;
                        }
                        break;
                case SPRHL_ADDITIVE:
                        for (i = 0;i < 256;i++)
                        {
-                               palette[i][0] = *in++;
-                               palette[i][1] = *in++;
-                               palette[i][2] = *in++;
+                               palette[i][2] = in[i*3+0];
+                               palette[i][1] = in[i*3+1];
+                               palette[i][0] = in[i*3+2];
                                palette[i][3] = 255;
                        }
                        // also passes additive == true to Mod_Sprite_SharedSetup
@@ -277,9 +319,9 @@ void Mod_IDSP_Load(model_t *mod, void *buffer, void *bufferend)
                case SPRHL_INDEXALPHA:
                        for (i = 0;i < 256;i++)
                        {
-                               palette[i][0] = in[765];
+                               palette[i][2] = in[765];
                                palette[i][1] = in[766];
-                               palette[i][2] = in[767];
+                               palette[i][0] = in[767];
                                palette[i][3] = i;
                                in += 3;
                        }
@@ -287,9 +329,9 @@ void Mod_IDSP_Load(model_t *mod, void *buffer, void *bufferend)
                case SPRHL_ALPHATEST:
                        for (i = 0;i < 256;i++)
                        {
-                               palette[i][0] = *in++;
-                               palette[i][1] = *in++;
-                               palette[i][2] = *in++;
+                               palette[i][2] = in[i*3+0];
+                               palette[i][1] = in[i*3+1];
+                               palette[i][0] = in[i*3+2];
                                palette[i][3] = 255;
                        }
                        palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0;
@@ -300,35 +342,36 @@ void Mod_IDSP_Load(model_t *mod, void *buffer, void *bufferend)
                        return;
                }
 
-               for (i = 0;i < 256;i++)
-               {
-                       alphapalette[i][0] = 255;
-                       alphapalette[i][1] = 255;
-                       alphapalette[i][2] = 255;
-                       alphapalette[i][3] = palette[i][3];
-               }
-
-               Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (unsigned int *)(&palette[0][0]), (unsigned int *)(&alphapalette[0][0]), rendermode == SPRHL_ADDITIVE);
+               Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (unsigned int *)(&palette[0][0]), rendermode == SPRHL_ADDITIVE);
        }
        else
                Host_Error("Mod_IDSP_Load: %s has wrong version number (%i). Only %i (quake), %i (HalfLife), and %i (sprite32) supported",
                                        loadmodel->name, version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
+
+       loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;
 }
 
 
-void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend)
+void Mod_IDS2_Load(dp_model_t *mod, void *buffer, void *bufferend)
 {
-       int i, version, fullbright;
+       int i, version;
+       qboolean fullbright;
        const dsprite2_t *pinqsprite;
+       skinframe_t *skinframe;
        float modelradius;
+       int texflags = (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ISSPRITE | TEXF_PICMIP | TEXF_COMPRESS | TEXF_ALPHA | TEXF_CLAMP;
+
+       loadmodel->modeldatatypestring = "SPR2";
 
        loadmodel->type = mod_sprite;
 
        loadmodel->DrawSky = NULL;
        loadmodel->Draw = R_Model_Sprite_Draw;
+       loadmodel->DrawDepth = NULL;
        loadmodel->CompileShadowVolume = NULL;
        loadmodel->DrawShadowVolume = NULL;
        loadmodel->DrawLight = NULL;
+       loadmodel->DrawAddWaterPlanes = NULL;
 
        pinqsprite = (dsprite2_t *)buffer;
 
@@ -350,6 +393,9 @@ void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend)
 
        loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
        loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * loadmodel->numframes);
+       loadmodel->num_textures = loadmodel->numframes;
+       loadmodel->num_texturesperskin = 1;
+       loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures);
 
        modelradius = 0;
        for (i = 0;i < loadmodel->numframes;i++)
@@ -384,12 +430,21 @@ void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend)
                y = (int)max(sprframe->up * sprframe->up, sprframe->down * sprframe->down);
                if (modelradius < x + y)
                        modelradius = x + y;
+       }
 
-               if (width > 0 && height > 0 && cls.state != ca_dedicated)
-                       if (!Mod_LoadSkinFrame(&sprframe->texture.skinframes[0], pinframe->name, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, false, false))
-                               Host_Error("Mod_IDS2_Load: failed to load %s", pinframe->name);
-
-               Mod_SpriteSetupTexture(sprframe, fullbright, false);
+       if (cls.state != ca_dedicated)
+       {
+               for (i = 0;i < loadmodel->numframes;i++)
+               {
+                       const dsprite2frame_t *pinframe;
+                       pinframe = &pinqsprite->frames[i];
+                       if (!(skinframe = R_SkinFrame_LoadExternal(pinframe->name, texflags, false)))
+                       {
+                               Con_Printf("Mod_IDS2_Load: failed to load %s", pinframe->name);
+                               skinframe = R_SkinFrame_LoadMissing();
+                       }
+                       Mod_SpriteSetupTexture(&loadmodel->data_textures[i], skinframe, fullbright, false);
+               }
        }
 
        modelradius = sqrt(modelradius);
@@ -400,4 +455,6 @@ void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend)
        }
        loadmodel->radius = modelradius;
        loadmodel->radius2 = modelradius * modelradius;
+
+       loadmodel->surfmesh.isanimated = loadmodel->numframes > 1 || loadmodel->animscenes[0].framecount > 1;
 }