395 lines
11 KiB
C
395 lines
11 KiB
C
//=======================================================================
|
|
// Copyright XashXT Group 2007 ©
|
|
// r_sprite.c - render sprite models
|
|
//=======================================================================
|
|
|
|
#include "gl_local.h"
|
|
|
|
/*
|
|
=============================================================
|
|
|
|
SPRITE MODELS
|
|
|
|
=============================================================
|
|
*/
|
|
|
|
/*
|
|
====================
|
|
Sprite model loader
|
|
====================
|
|
*/
|
|
void *R_SpriteLoadFrame (model_t *mod, void *pin, mspriteframe_t **ppframe, int framenum, byte *pal )
|
|
{
|
|
dspriteframe_t *pinframe;
|
|
mspriteframe_t *pspriteframe;
|
|
int width, height, size, origin[2];
|
|
char name[64];
|
|
rgbdata_t r_frame;
|
|
image_t *image;
|
|
|
|
pinframe = (dspriteframe_t *)pin;
|
|
|
|
width = LittleLong (pinframe->width);
|
|
height = LittleLong (pinframe->height);
|
|
size = width * height;
|
|
|
|
pspriteframe = Mem_Alloc(mod->mempool, sizeof (mspriteframe_t));
|
|
memset (pspriteframe, 0, sizeof (mspriteframe_t));
|
|
|
|
*ppframe = pspriteframe;
|
|
|
|
pspriteframe->width = r_frame.width = width;
|
|
pspriteframe->height = r_frame.height = height;
|
|
origin[0] = LittleLong (pinframe->origin[0]);
|
|
origin[1] = LittleLong (pinframe->origin[1]);
|
|
|
|
pspriteframe->up = origin[1];
|
|
pspriteframe->down = origin[1] - height;
|
|
pspriteframe->left = origin[0];
|
|
pspriteframe->right = width + origin[0];
|
|
pspriteframe->texnum = 0;
|
|
r_frame.type = PF_INDEXED_32;
|
|
r_frame.numMips = 1;
|
|
r_frame.flags = IMAGE_HAS_ALPHA;
|
|
|
|
// extract sprite name from path
|
|
FS_FileBase( mod->name, name );
|
|
strcat(name, va("_%i", framenum));
|
|
r_frame.palette = pal;
|
|
r_frame.size = width * height * 4; // for bounds checking
|
|
r_frame.buffer = (byte *)(pinframe + 1);
|
|
|
|
image = R_LoadImage( name, &r_frame, it_sprite );
|
|
|
|
if(image)
|
|
{
|
|
pspriteframe->texnum = image->texnum[0];
|
|
mod->skins[framenum] = image;
|
|
}
|
|
else MsgWarn("%s has null frame %d\n", image->name, framenum );
|
|
|
|
return (void *)((byte *)(pinframe+1) + size);
|
|
}
|
|
|
|
void R_SpriteLoadModel( model_t *mod, void *buffer )
|
|
{
|
|
int i, size, version, numframes;
|
|
dsprite_t *pin;
|
|
short *numi;
|
|
msprite_t *psprite;
|
|
frametype_t *pframetype;
|
|
byte pal[256][4];
|
|
vec4_t rgbacolor;
|
|
float framerate;
|
|
|
|
pin = (dsprite_t *)buffer;
|
|
version = LittleLong (pin->version);
|
|
|
|
switch( version )
|
|
{
|
|
case SPRITE_VERSION_HALF:
|
|
ResetRGBA( rgbacolor );
|
|
framerate = 0.0f;
|
|
break;
|
|
case SPRITE_VERSION_XASH:
|
|
//unpack rgba
|
|
rgbacolor[0] = LittleLong((pin->rgbacolor & 0x000000FF) >> 0);
|
|
rgbacolor[1] = LittleLong((pin->rgbacolor & 0x0000FF00) >> 8);
|
|
rgbacolor[2] = LittleLong((pin->rgbacolor & 0x00FF0000) >> 16);
|
|
rgbacolor[3] = LittleLong((pin->rgbacolor & 0xFF000000) >> 24);
|
|
|
|
TransformRGBA(rgbacolor, rgbacolor ); //convert into float
|
|
framerate = LittleFloat (pin->framerate);
|
|
break;
|
|
default:
|
|
Msg("Warning: %s has wrong version number (%i should be %i or %i)", mod->name, version, SPRITE_VERSION_HALF, SPRITE_VERSION_XASH );
|
|
return;
|
|
}
|
|
|
|
numframes = LittleLong (pin->numframes);
|
|
size = sizeof (msprite_t) + (numframes - 1) * sizeof (psprite->frames);
|
|
|
|
psprite = Mem_Alloc(mod->mempool, size );
|
|
mod->extradata = psprite; //make link to extradata
|
|
|
|
psprite->type = LittleLong(pin->type);
|
|
psprite->maxwidth = LittleLong (pin->width);
|
|
psprite->maxheight = LittleLong (pin->height);
|
|
psprite->rendermode = LittleLong (pin->texFormat);
|
|
psprite->framerate = framerate;
|
|
psprite->numframes = numframes;
|
|
Mem_Copy(psprite->rgba, rgbacolor, sizeof(psprite->rgba));
|
|
numi = (short *)(pin + 1);
|
|
|
|
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;
|
|
|
|
if (LittleShort(*numi) == 256)
|
|
{
|
|
byte *src = (byte *)(numi+1);
|
|
|
|
switch( psprite->rendermode )
|
|
{
|
|
case SPR_NORMAL:
|
|
for (i = 0; i < 256; i++)
|
|
{
|
|
pal[i][0] = *src++;
|
|
pal[i][1] = *src++;
|
|
pal[i][2] = *src++;
|
|
pal[i][3] = 0;
|
|
}
|
|
break;
|
|
case SPR_ADDGLOW:
|
|
case SPR_ADDITIVE:
|
|
for (i = 0; i < 256; i++)
|
|
{
|
|
pal[i][0] = *src++;
|
|
pal[i][1] = *src++;
|
|
pal[i][2] = *src++;
|
|
pal[i][3] = 255;
|
|
}
|
|
break;
|
|
case SPR_INDEXALPHA:
|
|
for (i = 0; i < 256; i++)
|
|
{
|
|
pal[i][0] = *src++;
|
|
pal[i][1] = *src++;
|
|
pal[i][2] = *src++;
|
|
pal[i][3] = i;
|
|
}
|
|
break;
|
|
case SPR_ALPHTEST:
|
|
for (i = 0; i < 256; i++)
|
|
{
|
|
pal[i][0] = *src++;
|
|
pal[i][1] = *src++;
|
|
pal[i][2] = *src++;
|
|
pal[i][3] = 255;
|
|
}
|
|
pal[255][0] = pal[255][1] = pal[255][2] = pal[255][3] = 0;
|
|
break;
|
|
default:
|
|
for (i = 0; i < 256; i++)
|
|
{
|
|
pal[i][0] = *src++;
|
|
pal[i][1] = *src++;
|
|
pal[i][2] = *src++;
|
|
pal[i][3] = 0;
|
|
}
|
|
Msg("Warning: %s has unknown texFormat (%i, should be in range 0-4 )\n", mod->name, psprite->rendermode );
|
|
break;
|
|
}
|
|
pframetype = (frametype_t *)(src);
|
|
}
|
|
else
|
|
{
|
|
MsgWarn("%s has wrong number of palette colors %i (should be 256)\n", mod->name, numi);
|
|
return;
|
|
}
|
|
if(numframes < 1)
|
|
{
|
|
MsgWarn("%s has invalid # of frames: %d\n", mod->name, numframes );
|
|
return;
|
|
}
|
|
|
|
mod->numframes = mod->numtexinfo = numframes;
|
|
mod->registration_sequence = registration_sequence;
|
|
|
|
for (i = 0; i < numframes; i++ )
|
|
{
|
|
int frametype;
|
|
|
|
frametype = LittleLong (pframetype->type);
|
|
psprite->frames[i].frametype = frametype;
|
|
|
|
if(frametype == 0)//SPR_SINGLE
|
|
{
|
|
pframetype = (frametype_t *)R_SpriteLoadFrame(mod, pframetype + 1, &psprite->frames[i].frameptr, i, (byte *)(&pal[0][0]));
|
|
}
|
|
else Sys_Error("R_SpriteLoadModel: group frames are not supported\n");
|
|
if(pframetype == NULL) break;
|
|
}
|
|
}
|
|
|
|
/*
|
|
================
|
|
R_GetSpriteFrame
|
|
================
|
|
*/
|
|
mspriteframe_t *R_GetSpriteFrame (entity_t *currententity)
|
|
{
|
|
msprite_t *psprite;
|
|
mspriteframe_t *pspriteframe;
|
|
int frame;
|
|
|
|
psprite = currententity->model->extradata;
|
|
frame = currententity->frame;
|
|
|
|
if ((frame >= psprite->numframes) || (frame < 0))
|
|
{
|
|
MsgDev(D_WARN, "R_GetSpriteFrame: no such frame %d (%s)\n", frame, currententity->model->name);
|
|
frame = 0;
|
|
}
|
|
|
|
if (psprite->frames[frame].frametype == 0) // SPR_SINGLE
|
|
{
|
|
pspriteframe = psprite->frames[frame].frameptr;
|
|
}
|
|
else Sys_Error("R_GetSpriteFrame: group frames are not supported\n");
|
|
|
|
return pspriteframe;
|
|
}
|
|
|
|
bool R_AcceptSpritePass( entity_t *e, int pass )
|
|
{
|
|
msprite_t *psprite = (msprite_t *)currentmodel->extradata;
|
|
|
|
if(pass == RENDERPASS_SOLID)
|
|
{
|
|
// pass for solid ents
|
|
if(psprite->rendermode == SPR_NORMAL) return true; // solid sprite
|
|
if(psprite->rendermode == SPR_ADDGLOW) return false; // draw it at second pass
|
|
if(psprite->rendermode == SPR_ADDITIVE) return false; // must be draw first always
|
|
if(psprite->rendermode == SPR_ALPHTEST) return true; // already blended by alphatest
|
|
if(psprite->rendermode == SPR_INDEXALPHA) return true; // already blended by alphatest
|
|
}
|
|
if(pass == RENDERPASS_ALPHA)
|
|
{
|
|
// pass for blended ents
|
|
if(e->flags & RF_TRANSLUCENT) return true; // solid sprite with custom blend
|
|
if(psprite->rendermode == SPR_NORMAL) return false; // solid sprite
|
|
if(psprite->rendermode == SPR_ADDGLOW) return true; // can draw
|
|
if(psprite->rendermode == SPR_ADDITIVE) return true; // can draw
|
|
if(psprite->rendermode == SPR_ALPHTEST) return false; // already drawed
|
|
if(psprite->rendermode == SPR_INDEXALPHA) return false; // already drawed
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void R_DrawSpriteModel( int passnum )
|
|
{
|
|
mspriteframe_t *frame;
|
|
vec3_t point, forward, right, up;
|
|
msprite_t *psprite;
|
|
entity_t *e = currententity;
|
|
model_t *mod = currentmodel;
|
|
float realtime = r_newrefdef.time;
|
|
|
|
if(!R_AcceptSpritePass( e, passnum )) return;
|
|
|
|
// don't even bother culling, because it's just a single
|
|
// polygon without a surface cache
|
|
|
|
psprite = (msprite_t *)mod->extradata;
|
|
|
|
// xash auto-animate
|
|
if(psprite->framerate > 0 && psprite->numframes > 1)
|
|
{
|
|
float frametime;
|
|
|
|
mod->prevanimtime = mod->animtime;
|
|
mod->animtime = realtime;
|
|
frametime = mod->animtime - mod->prevanimtime;
|
|
if(frametime == 0 ) return;//first call
|
|
|
|
mod->frame += psprite->framerate * frametime;
|
|
while (mod->frame > psprite->numframes) mod->frame -= psprite->numframes;
|
|
while (mod->frame < 0) mod->frame += psprite->numframes;
|
|
e->frame = mod->frame; // set properly frame
|
|
}
|
|
else e->frame = fmod(e->frame, psprite->numframes);
|
|
frame = R_GetSpriteFrame(e);
|
|
|
|
// merge alpha value
|
|
if( e->flags & RF_TRANSLUCENT ) psprite->rgba[3] = e->alpha;
|
|
if( e->scale == 0 ) e->scale = 1.0f; // merge scale
|
|
|
|
// setup oriented
|
|
switch( psprite->type )
|
|
{
|
|
case SPR_ORIENTED:
|
|
AngleVectors(e->angles, forward, right, up);
|
|
VectorScale(forward, 0.01, forward );// offset for decals
|
|
VectorSubtract(e->origin, forward, e->origin );
|
|
break;
|
|
case SPR_FACING_UPRIGHT:
|
|
up[0] = up[1] = 0;
|
|
up[2] = 1;
|
|
right[0] = e->origin[1] - r_origin[1];
|
|
right[1] = -(e->origin[0] - r_origin[0]);
|
|
right[2] = 0;
|
|
VectorNormalize (right);
|
|
VectorCopy (vforward, forward);
|
|
break;
|
|
case SPR_VP_PARALLEL_UPRIGHT:
|
|
up[0] = up[1] = 0;
|
|
up[2] = 1;
|
|
VectorCopy (vup, right);
|
|
VectorCopy (vforward, forward);
|
|
break;
|
|
default:
|
|
// normal sprite
|
|
VectorCopy (vup, up);
|
|
VectorCopy (vright, right);
|
|
VectorCopy (vforward, forward);
|
|
break;
|
|
}
|
|
|
|
// setup rendermode
|
|
switch( psprite->rendermode )
|
|
{
|
|
case SPR_NORMAL:
|
|
// solid sprite
|
|
break;
|
|
case SPR_ADDGLOW:
|
|
GL_DisableDepthTest();
|
|
// intentional falltrough
|
|
case SPR_ADDITIVE:
|
|
GL_EnableBlend();
|
|
qglBlendFunc(GL_SRC_ALPHA, GL_ONE);
|
|
break;
|
|
case SPR_ALPHTEST:
|
|
case SPR_INDEXALPHA:
|
|
GL_EnableAlphaTest();
|
|
break;
|
|
}
|
|
|
|
GL_Bind(currentmodel->skins[(int)e->frame]->texnum[0]);
|
|
GL_TexEnv( GL_MODULATE );
|
|
qglColor4fv( psprite->rgba );
|
|
|
|
qglDisable(GL_CULL_FACE);
|
|
qglBegin (GL_QUADS);
|
|
qglTexCoord2f (0, 0);
|
|
VectorMA (e->origin, frame->up * e->scale, up, point);
|
|
VectorMA (point, frame->left * e->scale, right, point);
|
|
qglVertex3fv (point);
|
|
|
|
qglTexCoord2f (1, 0);
|
|
VectorMA (e->origin, frame->up * e->scale, up, point);
|
|
VectorMA (point, frame->right * e->scale, right, point);
|
|
qglVertex3fv (point);
|
|
|
|
qglTexCoord2f (1, 1);
|
|
VectorMA (e->origin, frame->down * e->scale, up, point);
|
|
VectorMA (point, frame->right * e->scale, right, point);
|
|
qglVertex3fv (point);
|
|
|
|
qglTexCoord2f (0, 1);
|
|
VectorMA (e->origin, frame->down * e->scale, up, point);
|
|
VectorMA (point, frame->left * e->scale, right, point);
|
|
qglVertex3fv (point);
|
|
qglEnd();
|
|
|
|
// restore states
|
|
GL_DisableBlend();
|
|
GL_DisableAlphaTest();
|
|
GL_EnableDepthTest();
|
|
GL_TexEnv(GL_REPLACE);
|
|
qglEnable(GL_CULL_FACE);
|
|
qglColor4f( 1, 1, 1, 1 );
|
|
}
|
|
|