X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=model_sprite.c;h=13b0798e05f84cd5ca6cda2856b82f1a87f4ef6e;hb=7977ef74dc79b00067952417dd4252ed3ebcbadb;hp=4db67bca50b0eb800e967a02f7bfb6b74f5e6ad9;hpb=400e4a34bbef54082e7d39758fa5aa7ab1b654c7;p=xonotic%2Fdarkplaces.git diff --git a/model_sprite.c b/model_sprite.c index 4db67bca..13b0798e 100644 --- a/model_sprite.c +++ b/model_sprite.c @@ -8,7 +8,7 @@ of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. @@ -23,6 +23,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // on the same machine. #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"}; /* =============== @@ -31,237 +34,366 @@ Mod_SpriteInit */ void Mod_SpriteInit (void) { + Cvar_RegisterVariable(&r_mipsprites); } -void Mod_Sprite_StripExtension(char *in, char *out) -{ - char *end; - end = in + strlen(in); - if ((end - in) >= 6) - if (strcmp(end - 6, ".spr32") == 0) - end -= 6; - if ((end - in) >= 4) - if (strcmp(end - 4, ".spr") == 0) - end -= 4; - while (in < end) - *out++ = *in++; - *out++ = 0; -} - -/* -================= -Mod_LoadSpriteFrame -================= -*/ -void * Mod_LoadSpriteFrame (void * pin, mspriteframe_t **ppframe, int framenum, int bytesperpixel) +static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version, const unsigned int *palette, const unsigned int *alphapalette) { + int i, j, groupframes, realframes, x, y, origin[2], width, height; + dspriteframetype_t *pinframetype; dspriteframe_t *pinframe; - mspriteframe_t *pspriteframe; - int i, width, height, size, origin[2]; - char name[256], tempname[256]; - byte *pixbuf, *pixel, *inpixel; + dspritegroup_t *pingroup; + dspriteinterval_t *pinintervals; + float modelradius, interval; + char name[MAX_QPATH], fogname[MAX_QPATH]; + const void *startframes; + modelradius = 0; - pinframe = (dspriteframe_t *)pin; + if (loadmodel->numframes < 1) + Host_Error ("Mod_Sprite_SharedSetup: Invalid # of frames: %d", loadmodel->numframes); - width = LittleLong (pinframe->width); - height = LittleLong (pinframe->height); - size = width * height * bytesperpixel; + // LordHavoc: hack to allow sprites to be non-fullbright + for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++) + { + if (loadmodel->name[i] == '!') + { + loadmodel->flags2 &= ~EF_FULLBRIGHT; + break; + } + } - pspriteframe = Hunk_AllocName (sizeof (mspriteframe_t), va("%s frames", loadname)); +// +// load the frames +// + startframes = datapointer; + realframes = 0; + for (i = 0;i < loadmodel->numframes;i++) + { + pinframetype = (dspriteframetype_t *)datapointer; + datapointer += sizeof(dspriteframetype_t); - memset (pspriteframe, 0, sizeof (mspriteframe_t)); + if (LittleLong (pinframetype->type) == SPR_SINGLE) + groupframes = 1; + else + { + pingroup = (dspritegroup_t *)datapointer; + datapointer += sizeof(dspritegroup_t); - *ppframe = pspriteframe; + groupframes = LittleLong(pingroup->numframes); - pspriteframe->width = width; - pspriteframe->height = height; - origin[0] = LittleLong (pinframe->origin[0]); - origin[1] = LittleLong (pinframe->origin[1]); + datapointer += sizeof(dspriteinterval_t) * groupframes; + } - pspriteframe->up = origin[1]; - pspriteframe->down = origin[1] - height; - pspriteframe->left = origin[0]; - pspriteframe->right = width + origin[0]; + for (j = 0;j < groupframes;j++) + { + pinframe = (dspriteframe_t *)datapointer; + if (version == SPRITE32_VERSION) + datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height) * 4; + else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION) + datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height); + } + realframes += groupframes; + } - Mod_Sprite_StripExtension(loadmodel->name, tempname); - sprintf (name, "%s_%i", tempname, framenum); - pspriteframe->gl_texturenum = loadtextureimagewithmask(name, 0, 0, false, true); - pspriteframe->gl_fogtexturenum = image_masktexnum; - if (pspriteframe->gl_texturenum == 0) + 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); + + datapointer = (unsigned char *)startframes; + realframes = 0; + for (i = 0;i < loadmodel->numframes;i++) { - pspriteframe->gl_texturenum = GL_LoadTexture (name, width, height, (byte *)(pinframe + 1), true, true, bytesperpixel); - // make fog version (just alpha) - pixbuf = pixel = qmalloc(width*height*4); - inpixel = (byte *)(pinframe + 1); - if (bytesperpixel == 1) + pinframetype = (dspriteframetype_t *)datapointer; + datapointer += sizeof(dspriteframetype_t); + + if (LittleLong (pinframetype->type) == SPR_SINGLE) { - for (i = 0;i < width*height;i++) - { - *pixel++ = 255; - *pixel++ = 255; - *pixel++ = 255; - if (*inpixel++ != 255) - *pixel++ = 255; - else - *pixel++ = 0; - } + groupframes = 1; + interval = 0.1f; } else { - inpixel+=3; - for (i = 0;i < width*height;i++) + pingroup = (dspritegroup_t *)datapointer; + datapointer += sizeof(dspritegroup_t); + + groupframes = LittleLong(pingroup->numframes); + + pinintervals = (dspriteinterval_t *)datapointer; + datapointer += sizeof(dspriteinterval_t) * groupframes; + + interval = LittleFloat(pinintervals[0].interval); + if (interval < 0.01f) + Host_Error("Mod_Sprite_SharedSetup: invalid interval"); + } + + sprintf(loadmodel->animscenes[i].name, "frame %i", i); + loadmodel->animscenes[i].firstframe = realframes; + loadmodel->animscenes[i].framecount = groupframes; + loadmodel->animscenes[i].framerate = 1.0f / interval; + loadmodel->animscenes[i].loop = true; + + for (j = 0;j < groupframes;j++) + { + pinframe = (dspriteframe_t *)datapointer; + datapointer += sizeof(dspriteframe_t); + + origin[0] = LittleLong (pinframe->origin[0]); + origin[1] = LittleLong (pinframe->origin[1]); + width = LittleLong (pinframe->width); + height = LittleLong (pinframe->height); + + loadmodel->sprite.sprdata_frames[realframes].left = origin[0]; + loadmodel->sprite.sprdata_frames[realframes].right = origin[0] + width; + loadmodel->sprite.sprdata_frames[realframes].up = origin[1]; + loadmodel->sprite.sprdata_frames[realframes].down = origin[1] - height; + + x = max(loadmodel->sprite.sprdata_frames[realframes].left * loadmodel->sprite.sprdata_frames[realframes].left, loadmodel->sprite.sprdata_frames[realframes].right * loadmodel->sprite.sprdata_frames[realframes].right); + y = max(loadmodel->sprite.sprdata_frames[realframes].up * loadmodel->sprite.sprdata_frames[realframes].up, loadmodel->sprite.sprdata_frames[realframes].down * loadmodel->sprite.sprdata_frames[realframes].down); + if (modelradius < x + y) + modelradius = x + y; + + if (width > 0 && height > 0 && cls.state != ca_dedicated) { - *pixel++ = 255; - *pixel++ = 255; - *pixel++ = 255; - *pixel++ = *inpixel; - inpixel+=4; + if (groupframes > 1) + sprintf (name, "%s_%i_%i", loadmodel->name, i, j); + else + sprintf (name, "%s_%i", loadmodel->name, i); + Mod_LoadSkinFrame(&loadmodel->sprite.sprdata_frames[realframes].skin, name, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, false, false); + + if (!loadmodel->sprite.sprdata_frames[realframes].skin.base) + { + if (groupframes > 1) + sprintf (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].skin, 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].skin, name, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, false, false, datapointer, width, height, 8, palette, alphapalette); + } } + + if (version == SPRITE32_VERSION) + datapointer += width * height * 4; + else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION) + datapointer += width * height; + realframes++; } - sprintf (name, "%s_%ifog", loadmodel->name, framenum); - pspriteframe->gl_fogtexturenum = GL_LoadTexture (name, width, height, pixbuf, true, true, 4); - qfree(pixbuf); } - return (void *)((byte *)pinframe + sizeof (dspriteframe_t) + size); + modelradius = sqrt(modelradius); + for (i = 0;i < 3;i++) + { + loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius; + loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius; + } + loadmodel->radius = modelradius; + loadmodel->radius2 = modelradius * modelradius; } - -/* -================= -Mod_LoadSpriteGroup -================= -*/ -void * Mod_LoadSpriteGroup (void * pin, mspriteframe_t **ppframe, int framenum, int bytesperpixel) +extern void R_Model_Sprite_Draw(entity_render_t *ent); +void Mod_IDSP_Load(model_t *mod, void *buffer, void *bufferend) { - dspritegroup_t *pingroup; - mspritegroup_t *pspritegroup; - int i, numframes; - dspriteinterval_t *pin_intervals; - float *poutintervals; - void *ptemp; - - pingroup = (dspritegroup_t *)pin; + int version; + const unsigned char *datapointer; - numframes = LittleLong (pingroup->numframes); + datapointer = (unsigned char *)buffer; - pspritegroup = Hunk_AllocName (sizeof (mspritegroup_t) + (numframes - 1) * sizeof (pspritegroup->frames[0]), va("%s frames", loadname)); + loadmodel->type = mod_sprite; + loadmodel->flags2 = EF_FULLBRIGHT; - pspritegroup->numframes = numframes; + loadmodel->DrawSky = NULL; + loadmodel->Draw = R_Model_Sprite_Draw; + loadmodel->CompileShadowVolume = NULL; + loadmodel->DrawShadowVolume = NULL; + loadmodel->DrawLight = NULL; - *ppframe = (mspriteframe_t *)pspritegroup; - - pin_intervals = (dspriteinterval_t *)(pingroup + 1); + version = LittleLong(((dsprite_t *)buffer)->version); + if (version == SPRITE_VERSION || version == SPRITE32_VERSION) + { + dsprite_t *pinqsprite; - poutintervals = Hunk_AllocName (numframes * sizeof (float), va("%s frames", loadname)); + pinqsprite = (dsprite_t *)datapointer; + datapointer += sizeof(dsprite_t); - pspritegroup->intervals = poutintervals; + loadmodel->numframes = LittleLong (pinqsprite->numframes); + loadmodel->sprite.sprnum_type = LittleLong (pinqsprite->type); + loadmodel->synctype = (synctype_t)LittleLong (pinqsprite->synctype); - for (i=0 ; iversion), NULL, NULL); + } + else if (version == SPRITEHL_VERSION) { - *poutintervals = LittleFloat (pin_intervals->interval); - if (*poutintervals <= 0.0) - Host_Error ("Mod_LoadSpriteGroup: interval<=0"); + int i, rendermode; + unsigned char palette[256][4], alphapalette[256][4]; + const unsigned char *in; + dspritehl_t *pinhlsprite; + + pinhlsprite = (dspritehl_t *)datapointer; + datapointer += sizeof(dspritehl_t); + + loadmodel->numframes = LittleLong (pinhlsprite->numframes); + loadmodel->sprite.sprnum_type = LittleLong (pinhlsprite->type); + loadmodel->synctype = (synctype_t)LittleLong (pinhlsprite->synctype); + rendermode = pinhlsprite->rendermode; + + in = datapointer; + datapointer += 2; + i = in[0] + in[1] * 256; + if (i != 256) + Host_Error ("Mod_IDSP_Load: unexpected number of palette colors %i (should be 256)", i); + in = datapointer; + datapointer += 768; + switch(rendermode) + { + case SPRHL_OPAQUE: + for (i = 0;i < 256;i++) + { + palette[i][0] = *in++; + palette[i][1] = *in++; + palette[i][2] = *in++; + 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][3] = 255; + } + loadmodel->flags2 |= EF_ADDITIVE; + break; + case SPRHL_INDEXALPHA: + for (i = 0;i < 256;i++) + { + palette[i][0] = in[765]; + palette[i][1] = in[766]; + palette[i][2] = in[767]; + palette[i][3] = i; + in += 3; + } + break; + case SPRHL_ALPHATEST: + for (i = 0;i < 256;i++) + { + palette[i][0] = *in++; + palette[i][1] = *in++; + palette[i][2] = *in++; + palette[i][3] = 255; + } + palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0; + break; + default: + Host_Error("Mod_IDSP_Load: unknown texFormat (%i, should be 0, 1, 2, or 3)", i); + 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]; + } - poutintervals++; - pin_intervals++; + Mod_Sprite_SharedSetup(datapointer, LittleLong (pinhlsprite->version), (unsigned int *)(&palette[0][0]), (unsigned int *)(&alphapalette[0][0])); } + 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); +} - ptemp = (void *)pin_intervals; - for (i=0 ; iframes[i], framenum * 100 + i, bytesperpixel); +void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend) +{ + int i, version; + const dsprite2_t *pinqsprite; + float modelradius; - return ptemp; -} + loadmodel->type = mod_sprite; + loadmodel->flags2 = EF_FULLBRIGHT; + loadmodel->DrawSky = NULL; + loadmodel->Draw = R_Model_Sprite_Draw; + loadmodel->CompileShadowVolume = NULL; + loadmodel->DrawShadowVolume = NULL; + loadmodel->DrawLight = NULL; -/* -================= -Mod_LoadSpriteModel -================= -*/ -void Mod_LoadSpriteModel (model_t *mod, void *buffer) -{ - int i; - int version; - dsprite_t *pin; - msprite_t *psprite; - int numframes; - int size; - dspriteframetype_t *pframetype; - // LordHavoc: 32bit textures - int bytesperpixel; - - mod->flags = EF_FULLBRIGHT; - // LordHavoc: hack to allow sprites to be non-fullbright - for (i = 0;i < MAX_QPATH && mod->name[i];i++) + pinqsprite = (dsprite2_t *)buffer; + + version = LittleLong(pinqsprite->version); + if (version != SPRITE2_VERSION) + Host_Error("Mod_IDS2_Load: %s has wrong version number (%i should be 2 (quake 2)", loadmodel->name, version); + + loadmodel->numframes = LittleLong (pinqsprite->numframes); + if (loadmodel->numframes < 1) + Host_Error ("Mod_IDS2_Load: Invalid # of frames: %d", loadmodel->numframes); + loadmodel->sprite.sprnum_type = SPR_VP_PARALLEL; + loadmodel->synctype = ST_SYNC; + + // Hack to allow sprites to be non-fullbright + for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++) { - if (mod->name[i] == '!') + if (loadmodel->name[i] == '!') { - mod->flags &= ~EF_FULLBRIGHT; + loadmodel->flags2 &= ~EF_FULLBRIGHT; break; } } - pin = (dsprite_t *)buffer; + 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); - version = LittleLong (pin->version); - if (version == 2) + modelradius = 0; + for (i = 0;i < loadmodel->numframes;i++) { - version = 32; - Con_Printf("warning: %s is a version 2 sprite (RGBA), supported for now, please hex edit to version 32 incase HalfLife sprites might be supported at some point.\n", mod->name); + int origin[2], x, y, width, height; + const dsprite2frame_t *pinframe; + mspriteframe_t *sprframe; + + dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "frame %i", i); + loadmodel->animscenes[i].firstframe = i; + loadmodel->animscenes[i].framecount = 1; + loadmodel->animscenes[i].framerate = 10; + loadmodel->animscenes[i].loop = true; + + pinframe = &pinqsprite->frames[i]; + + origin[0] = LittleLong (pinframe->origin_x); + origin[1] = LittleLong (pinframe->origin_y); + width = LittleLong (pinframe->width); + height = LittleLong (pinframe->height); + + sprframe = &loadmodel->sprite.sprdata_frames[i]; + + // note that sp2 origin[0] is positive, where as it is negative in + // spr/spr32/hlspr + sprframe->left = -origin[0]; + sprframe->right = -origin[0] + width; + sprframe->up = origin[1]; + sprframe->down = origin[1] - height; + + x = max(sprframe->left * sprframe->left, sprframe->right * sprframe->right); + y = 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) + { + Mod_LoadSkinFrame(&sprframe->skin, pinframe->name, (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, false, false); + // TODO: use a default texture if we can't load it? + if (sprframe->skin.base == NULL) + Host_Error("Mod_IDS2_Load: failed to load %s", pinframe->name); + } } - // LordHavoc: 32bit textures - if (version != SPRITE_VERSION && version != SPRITE32_VERSION) - Host_Error ("%s has wrong version number " - "(%i should be %i or %i)", mod->name, version, SPRITE_VERSION, SPRITE32_VERSION); - bytesperpixel = 1; - if (version == SPRITE32_VERSION) - bytesperpixel = 4; - - numframes = LittleLong (pin->numframes); - - size = sizeof (msprite_t) + (numframes - 1) * sizeof (psprite->frames); - - psprite = Hunk_AllocName (size, va("%s info", loadname)); - - mod->cache.data = psprite; - - psprite->type = LittleLong (pin->type); - psprite->maxwidth = LittleLong (pin->width); - psprite->maxheight = LittleLong (pin->height); - psprite->beamlength = LittleFloat (pin->beamlength); - mod->synctype = LittleLong (pin->synctype); - psprite->numframes = numframes; - - mod->mins[0] = mod->mins[1] = -psprite->maxwidth/2; - mod->maxs[0] = mod->maxs[1] = psprite->maxwidth/2; - mod->mins[2] = -psprite->maxheight/2; - mod->maxs[2] = psprite->maxheight/2; - -// -// load the frames -// - if (numframes < 1) - Host_Error ("Mod_LoadSpriteModel: Invalid # of frames: %d\n", numframes); - - mod->numframes = numframes; - pframetype = (dspriteframetype_t *)(pin + 1); - - for (i=0 ; itype); - psprite->frames[i].type = frametype; - - if (frametype == SPR_SINGLE) - pframetype = (dspriteframetype_t *) Mod_LoadSpriteFrame (pframetype + 1, &psprite->frames[i].frameptr, i, bytesperpixel); - else - pframetype = (dspriteframetype_t *) Mod_LoadSpriteGroup (pframetype + 1, &psprite->frames[i].frameptr, i, bytesperpixel); + loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius; + loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius; } - - mod->type = mod_sprite; + loadmodel->radius = modelradius; + loadmodel->radius2 = modelradius * modelradius; }