This repository has been archived on 2022-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
Xash3DArchive/engine/common/mod_studio.c

952 lines
26 KiB
C
Raw Normal View History

2011-05-09 22:00:00 +02:00
/*
sv_studio.c - server studio utilities
Copyright (C) 2010 Uncle Mike
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 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. See the
GNU General Public License for more details.
*/
2010-08-25 22:00:00 +02:00
#include "common.h"
#include "server.h"
2010-09-10 22:00:00 +02:00
#include "studio.h"
2010-11-15 22:00:00 +01:00
#include "r_studioint.h"
2011-03-08 22:00:00 +01:00
#include "library.h"
2010-08-25 22:00:00 +02:00
2012-02-08 21:00:00 +01:00
typedef int (*STUDIOAPI)( int, sv_blending_interface_t**, server_studio_api_t*, float (*transform)[3][4], float (*bones)[MAXSTUDIOBONES][3][4] );
2010-11-15 22:00:00 +01:00
2012-02-11 21:00:00 +01:00
typedef struct mstudiocache_s
{
float frame;
int sequence;
vec3_t angles;
vec3_t origin;
vec3_t size;
2017-03-08 22:00:00 +01:00
byte controller[4];
2012-02-11 21:00:00 +01:00
byte blending[2];
model_t *model;
uint current_hull;
uint current_plane;
uint numhitboxes;
} mstudiocache_t;
#define STUDIO_CACHESIZE 16
#define STUDIO_CACHEMASK (STUDIO_CACHESIZE - 1)
// trace global variables
2012-12-22 21:00:00 +01:00
static sv_blending_interface_t *pBlendAPI = NULL;
2012-02-11 21:00:00 +01:00
static studiohdr_t *mod_studiohdr;
static matrix3x4 studio_transform;
static hull_t cache_hull[MAXSTUDIOBONES];
static hull_t studio_hull[MAXSTUDIOBONES];
static matrix3x4 studio_bones[MAXSTUDIOBONES];
static uint studio_hull_hitgroup[MAXSTUDIOBONES];
static uint cache_hull_hitgroup[MAXSTUDIOBONES];
static mstudiocache_t cache_studio[STUDIO_CACHESIZE];
2017-07-30 23:00:00 +02:00
static mclipnode_t studio_clipnodes[6];
2012-02-11 21:00:00 +01:00
static mplane_t studio_planes[768];
static mplane_t cache_planes[768];
// current cache state
static int cache_current;
static int cache_current_hull;
static int cache_current_plane;
2010-08-25 22:00:00 +02:00
/*
====================
2012-02-11 21:00:00 +01:00
Mod_InitStudioHull
2010-08-25 22:00:00 +02:00
====================
*/
2012-02-11 21:00:00 +01:00
void Mod_InitStudioHull( void )
2010-08-25 22:00:00 +02:00
{
int i, side;
2012-02-11 21:00:00 +01:00
if( studio_hull[0].planes != NULL )
return; // already initailized
2010-08-25 22:00:00 +02:00
for( i = 0; i < 6; i++ )
{
2012-02-11 21:00:00 +01:00
studio_clipnodes[i].planenum = i;
2010-08-25 22:00:00 +02:00
side = i & 1;
2012-02-11 21:00:00 +01:00
studio_clipnodes[i].children[side] = CONTENTS_EMPTY;
if( i != 5 ) studio_clipnodes[i].children[side^1] = i + 1;
else studio_clipnodes[i].children[side^1] = CONTENTS_SOLID;
}
2010-08-25 22:00:00 +02:00
2012-02-11 21:00:00 +01:00
for( i = 0; i < MAXSTUDIOBONES; i++ )
{
studio_hull[i].clipnodes = studio_clipnodes;
studio_hull[i].planes = &studio_planes[i*6];
studio_hull[i].firstclipnode = 0;
studio_hull[i].lastclipnode = 5;
}
}
/*
===============================================================================
STUDIO MODELS CACHE
===============================================================================
*/
/*
====================
ClearStudioCache
====================
*/
void Mod_ClearStudioCache( void )
{
2016-11-17 22:00:00 +01:00
memset( cache_studio, 0, sizeof( cache_studio ));
2012-02-11 21:00:00 +01:00
cache_current_hull = cache_current_plane = 0;
cache_current = 0;
}
/*
====================
AddToStudioCache
====================
*/
void Mod_AddToStudioCache( float frame, int sequence, vec3_t angles, vec3_t origin, vec3_t size, byte *pcontroller, byte *pblending, model_t *model, hull_t *hull, int numhitboxes )
{
mstudiocache_t *pCache;
if( numhitboxes + cache_current_hull >= MAXSTUDIOBONES )
Mod_ClearStudioCache();
cache_current++;
pCache = &cache_studio[cache_current & STUDIO_CACHEMASK];
pCache->frame = frame;
pCache->sequence = sequence;
VectorCopy( angles, pCache->angles );
VectorCopy( origin, pCache->origin );
VectorCopy( size, pCache->size );
2017-03-08 22:00:00 +01:00
memcpy( pCache->controller, pcontroller, 4 );
2016-11-17 22:00:00 +01:00
memcpy( pCache->blending, pblending, 2 );
2012-02-11 21:00:00 +01:00
pCache->model = model;
pCache->current_hull = cache_current_hull;
pCache->current_plane = cache_current_plane;
2016-11-17 22:00:00 +01:00
memcpy( &cache_hull[cache_current_hull], hull, numhitboxes * sizeof( hull_t ));
memcpy( &cache_planes[cache_current_plane], studio_planes, numhitboxes * sizeof( mplane_t ) * 6 );
memcpy( &cache_hull_hitgroup[cache_current_hull], studio_hull_hitgroup, numhitboxes * sizeof( uint ));
2012-02-11 21:00:00 +01:00
cache_current_hull += numhitboxes;
cache_current_plane += numhitboxes * 6;
pCache->numhitboxes = numhitboxes;
}
/*
====================
CheckStudioCache
====================
*/
2017-03-08 22:00:00 +01:00
mstudiocache_t *Mod_CheckStudioCache( model_t *model, float frame, int sequence, vec3_t angles, vec3_t origin, vec3_t size, byte *controller, byte *blending )
2012-02-11 21:00:00 +01:00
{
2017-03-08 22:00:00 +01:00
mstudiocache_t *pCached;
2012-02-11 21:00:00 +01:00
int i;
for( i = 0; i < STUDIO_CACHESIZE; i++ )
{
2017-03-08 22:00:00 +01:00
pCached = &cache_studio[(cache_current - i) & STUDIO_CACHEMASK];
2012-02-11 21:00:00 +01:00
2017-03-08 22:00:00 +01:00
if( pCached->model != model )
continue;
if( pCached->frame != frame )
continue;
if( pCached->sequence != sequence )
continue;
if( !VectorCompare( pCached->angles, angles ))
continue;
if( !VectorCompare( pCached->origin, origin ))
continue;
if( !VectorCompare( pCached->size, size ))
continue;
if( memcmp( pCached->controller, controller, 4 ) != 0 )
continue;
if( memcmp( pCached->blending, blending, 2 ) != 0 )
continue;
return pCached;
2012-02-11 21:00:00 +01:00
}
2017-03-08 22:00:00 +01:00
2012-02-11 21:00:00 +01:00
return NULL;
2010-08-25 22:00:00 +02:00
}
2012-02-11 21:00:00 +01:00
/*
===============================================================================
STUDIO MODELS TRACING
===============================================================================
*/
2010-08-25 22:00:00 +02:00
/*
====================
2012-02-11 21:00:00 +01:00
SetStudioHullPlane
2010-08-25 22:00:00 +02:00
====================
*/
2017-03-08 22:00:00 +01:00
void Mod_SetStudioHullPlane( int planenum, int bone, int axis, float offset, const vec3_t size )
2010-08-25 22:00:00 +02:00
{
2017-03-08 22:00:00 +01:00
mplane_t *pl = &studio_planes[planenum];
2012-02-11 21:00:00 +01:00
pl->type = 5;
pl->normal[0] = studio_bones[bone][0][axis];
pl->normal[1] = studio_bones[bone][1][axis];
pl->normal[2] = studio_bones[bone][2][axis];
pl->dist = (pl->normal[0] * studio_bones[bone][0][3]) + (pl->normal[1] * studio_bones[bone][1][3]) + (pl->normal[2] * studio_bones[bone][2][3]) + offset;
2017-03-08 22:00:00 +01:00
if( planenum & 1 ) pl->dist -= DotProductFabs( pl->normal, size );
else pl->dist += DotProductFabs( pl->normal, size );
2012-02-11 21:00:00 +01:00
}
/*
====================
HullForStudio
2012-12-22 21:00:00 +01:00
NOTE: pEdict may be NULL
2012-02-11 21:00:00 +01:00
====================
*/
hull_t *Mod_HullForStudio( model_t *model, float frame, int sequence, vec3_t angles, vec3_t origin, vec3_t size, byte *pcontroller, byte *pblending, int *numhitboxes, edict_t *pEdict )
{
vec3_t angles2;
mstudiocache_t *bonecache;
mstudiobbox_t *phitbox;
2017-01-14 22:00:00 +01:00
qboolean bSkipShield;
2012-02-11 21:00:00 +01:00
int i, j;
2017-01-14 22:00:00 +01:00
bSkipShield = false;
2014-05-17 22:00:00 +02:00
*numhitboxes = 0; // assume error
2017-02-12 22:00:00 +01:00
if( mod_studiocache->value )
2012-02-11 21:00:00 +01:00
{
bonecache = Mod_CheckStudioCache( model, frame, sequence, angles, origin, size, pcontroller, pblending );
if( bonecache != NULL )
{
2016-11-17 22:00:00 +01:00
memcpy( studio_planes, &cache_planes[bonecache->current_plane], bonecache->numhitboxes * sizeof( mplane_t ) * 6 );
memcpy( studio_hull_hitgroup, &cache_hull_hitgroup[bonecache->current_hull], bonecache->numhitboxes * sizeof( uint ));
memcpy( studio_hull, &cache_hull[bonecache->current_hull], bonecache->numhitboxes * sizeof( hull_t ));
2012-02-11 21:00:00 +01:00
*numhitboxes = bonecache->numhitboxes;
return studio_hull;
}
}
2017-07-02 23:00:00 +02:00
mod_studiohdr = Mod_StudioExtradata( model );
2012-02-11 21:00:00 +01:00
if( !mod_studiohdr ) return NULL; // probably not a studiomodel
VectorCopy( angles, angles2 );
2013-09-03 22:00:00 +02:00
2017-03-08 22:00:00 +01:00
if( !FBitSet( host.features, ENGINE_COMPENSATE_QUAKE_BUG ))
2013-09-03 22:00:00 +02:00
angles2[PITCH] = -angles2[PITCH]; // stupid quake bug
2012-02-11 21:00:00 +01:00
2014-05-08 22:00:00 +02:00
pBlendAPI->SV_StudioSetupBones( model, frame, sequence, angles2, origin, pcontroller, pblending, -1, pEdict );
2012-02-11 21:00:00 +01:00
phitbox = (mstudiobbox_t *)((byte *)mod_studiohdr + mod_studiohdr->hitboxindex);
2017-01-14 22:00:00 +01:00
if( SV_IsValidEdict( pEdict ) && pEdict->v.gamestate == 1 )
bSkipShield = 1;
2017-03-08 22:00:00 +01:00
for( i = j = 0; i < mod_studiohdr->numhitboxes; i++, j += 6 )
2012-02-11 21:00:00 +01:00
{
2017-03-08 22:00:00 +01:00
if( bSkipShield && i == 21 )
continue; // CS stuff
2017-01-14 22:00:00 +01:00
2012-02-11 21:00:00 +01:00
studio_hull_hitgroup[i] = phitbox[i].group;
2017-03-08 22:00:00 +01:00
Mod_SetStudioHullPlane( j + 0, phitbox[i].bone, 0, phitbox[i].bbmax[0], size );
Mod_SetStudioHullPlane( j + 1, phitbox[i].bone, 0, phitbox[i].bbmin[0], size );
Mod_SetStudioHullPlane( j + 2, phitbox[i].bone, 1, phitbox[i].bbmax[1], size );
Mod_SetStudioHullPlane( j + 3, phitbox[i].bone, 1, phitbox[i].bbmin[1], size );
Mod_SetStudioHullPlane( j + 4, phitbox[i].bone, 2, phitbox[i].bbmax[2], size );
Mod_SetStudioHullPlane( j + 5, phitbox[i].bone, 2, phitbox[i].bbmin[2], size );
2012-02-11 21:00:00 +01:00
}
2012-12-21 21:00:00 +01:00
// tell trace code about hitbox count
2017-01-14 22:00:00 +01:00
*numhitboxes = (bSkipShield) ? (mod_studiohdr->numhitboxes - 1) : (mod_studiohdr->numhitboxes);
2012-02-11 21:00:00 +01:00
2017-02-12 22:00:00 +01:00
if( mod_studiocache->value )
2012-02-11 21:00:00 +01:00
Mod_AddToStudioCache( frame, sequence, angles, origin, size, pcontroller, pblending, model, studio_hull, *numhitboxes );
return studio_hull;
2010-08-25 22:00:00 +02:00
}
/*
===============================================================================
2012-02-11 21:00:00 +01:00
STUDIO MODELS SETUP BONES
2010-08-25 22:00:00 +02:00
===============================================================================
*/
/*
====================
StudioCalcBoneAdj
====================
*/
2012-02-11 21:00:00 +01:00
static void Mod_StudioCalcBoneAdj( float *adj, const byte *pcontroller )
2010-08-25 22:00:00 +02:00
{
int i, j;
float value;
mstudiobonecontroller_t *pbonecontroller;
2012-02-11 21:00:00 +01:00
pbonecontroller = (mstudiobonecontroller_t *)((byte *)mod_studiohdr + mod_studiohdr->bonecontrollerindex);
2010-08-25 22:00:00 +02:00
2012-02-11 21:00:00 +01:00
for( j = 0; j < mod_studiohdr->numbonecontrollers; j++ )
2010-08-25 22:00:00 +02:00
{
i = pbonecontroller[j].index;
2012-12-21 21:00:00 +01:00
if( i == STUDIO_MOUTH )
continue; // ignore mouth
2010-08-25 22:00:00 +02:00
if( i <= MAXSTUDIOCONTROLLERS )
{
// check for 360% wrapping
if( pbonecontroller[j].type & STUDIO_RLOOP )
{
2011-03-01 22:00:00 +01:00
value = pcontroller[i] * (360.0f / 256.0f) + pbonecontroller[j].start;
2010-08-25 22:00:00 +02:00
}
else
{
2011-02-20 22:00:00 +01:00
value = pcontroller[i] / 255.0f;
2012-02-11 21:00:00 +01:00
value = bound( 0.0f, value, 1.0f );
2010-08-25 22:00:00 +02:00
value = (1.0f - value) * pbonecontroller[j].start + value * pbonecontroller[j].end;
}
}
switch( pbonecontroller[j].type & STUDIO_TYPES )
{
case STUDIO_XR:
case STUDIO_YR:
case STUDIO_ZR:
adj[j] = value * (M_PI / 180.0f);
break;
case STUDIO_X:
case STUDIO_Y:
case STUDIO_Z:
adj[j] = value;
break;
}
}
}
/*
====================
StudioCalcRotations
====================
*/
2012-02-11 21:00:00 +01:00
static void Mod_StudioCalcRotations( int boneused[], int numbones, const byte *pcontroller, float pos[][3], vec4_t *q, mstudioseqdesc_t *pseqdesc, mstudioanim_t *panim, float f )
2010-08-25 22:00:00 +02:00
{
2010-11-15 22:00:00 +01:00
int i, j, frame;
2010-08-25 22:00:00 +02:00
mstudiobone_t *pbone;
float adj[MAXSTUDIOCONTROLLERS];
2010-11-15 22:00:00 +01:00
float s;
2010-08-25 22:00:00 +02:00
2017-03-08 22:00:00 +01:00
// bah, fix this bug with changing sequences too fast
2010-08-25 22:00:00 +02:00
if( f > pseqdesc->numframes - 1 )
2017-03-08 22:00:00 +01:00
{
2012-12-21 21:00:00 +01:00
f = 0.0f;
2017-03-08 22:00:00 +01:00
}
2010-08-25 22:00:00 +02:00
else if( f < -0.01f )
2017-03-08 22:00:00 +01:00
{
// BUG ( somewhere else ) but this code should validate this data.
// This could cause a crash if the frame # is negative, so we'll go ahead
// and clamp it here
2010-08-25 22:00:00 +02:00
f = -0.01f;
2017-03-08 22:00:00 +01:00
}
2010-08-25 22:00:00 +02:00
frame = (int)f;
s = (f - frame);
// add in programtic controllers
2012-02-11 21:00:00 +01:00
pbone = (mstudiobone_t *)((byte *)mod_studiohdr + mod_studiohdr->boneindex);
2010-08-25 22:00:00 +02:00
2012-02-11 21:00:00 +01:00
Mod_StudioCalcBoneAdj( adj, pcontroller );
2010-08-25 22:00:00 +02:00
2010-11-15 22:00:00 +01:00
for( j = numbones - 1; j >= 0; j-- )
2010-08-25 22:00:00 +02:00
{
2010-11-15 22:00:00 +01:00
i = boneused[j];
2017-03-06 22:00:00 +01:00
R_StudioCalcBoneQuaternion( frame, s, &pbone[i], &panim[i], adj, q[i] );
R_StudioCalcBonePosition( frame, s, &pbone[i], &panim[i], adj, pos[i] );
2010-08-25 22:00:00 +02:00
}
if( pseqdesc->motiontype & STUDIO_X ) pos[pseqdesc->motionbone][0] = 0.0f;
if( pseqdesc->motiontype & STUDIO_Y ) pos[pseqdesc->motionbone][1] = 0.0f;
if( pseqdesc->motiontype & STUDIO_Z ) pos[pseqdesc->motionbone][2] = 0.0f;
}
/*
====================
StudioSetupBones
2010-11-15 22:00:00 +01:00
NOTE: pEdict is unused
2010-08-25 22:00:00 +02:00
====================
*/
2010-11-15 22:00:00 +01:00
static void SV_StudioSetupBones( model_t *pModel, float frame, int sequence, const vec3_t angles, const vec3_t origin,
2014-05-08 22:00:00 +02:00
const byte *pcontroller, const byte *pblending, int iBone, const edict_t *pEdict )
2010-08-25 22:00:00 +02:00
{
2011-10-01 22:00:00 +02:00
int i, j, numbones = 0;
2010-11-15 22:00:00 +01:00
int boneused[MAXSTUDIOBONES];
2017-03-08 22:00:00 +01:00
float f = 0.0;
2010-08-25 22:00:00 +02:00
mstudiobone_t *pbones;
mstudioseqdesc_t *pseqdesc;
mstudioanim_t *panim;
static float pos[MAXSTUDIOBONES][3];
static vec4_t q[MAXSTUDIOBONES];
2010-12-22 22:00:00 +01:00
matrix3x4 bonematrix;
2010-08-25 22:00:00 +02:00
static float pos2[MAXSTUDIOBONES][3];
static vec4_t q2[MAXSTUDIOBONES];
static float pos3[MAXSTUDIOBONES][3];
static vec4_t q3[MAXSTUDIOBONES];
static float pos4[MAXSTUDIOBONES][3];
static vec4_t q4[MAXSTUDIOBONES];
2012-02-11 21:00:00 +01:00
if( sequence < 0 || sequence >= mod_studiohdr->numseq )
2011-10-01 22:00:00 +02:00
{
2018-02-15 22:00:00 +01:00
// only show warn if sequence that out of range was specified intentionally
if( sequence > mod_studiohdr->numseq )
2018-09-08 23:00:00 +02:00
Con_Reportf( S_WARN "SV_StudioSetupBones: sequence %i/%i out of range for model %s\n", sequence, mod_studiohdr->numseq, pModel->name );
2011-10-01 22:00:00 +02:00
sequence = 0;
}
2012-02-11 21:00:00 +01:00
pseqdesc = (mstudioseqdesc_t *)((byte *)mod_studiohdr + mod_studiohdr->seqindex) + sequence;
pbones = (mstudiobone_t *)((byte *)mod_studiohdr + mod_studiohdr->boneindex);
2017-03-08 22:00:00 +01:00
panim = R_StudioGetAnim( mod_studiohdr, pModel, pseqdesc );
2010-08-25 22:00:00 +02:00
2012-02-11 21:00:00 +01:00
if( iBone < -1 || iBone >= mod_studiohdr->numbones )
2010-11-15 22:00:00 +01:00
iBone = 0;
2010-08-25 22:00:00 +02:00
2010-11-15 22:00:00 +01:00
if( iBone == -1 )
{
2012-02-11 21:00:00 +01:00
numbones = mod_studiohdr->numbones;
for( i = 0; i < mod_studiohdr->numbones; i++ )
2010-11-15 22:00:00 +01:00
boneused[(numbones - i) - 1] = i;
}
else
{
2011-10-01 22:00:00 +02:00
// only the parent bones
2010-11-15 22:00:00 +01:00
for( i = iBone; i != -1; i = pbones[i].parent )
2011-10-01 22:00:00 +02:00
boneused[numbones++] = i;
2010-11-15 22:00:00 +01:00
}
2017-03-08 22:00:00 +01:00
if( pseqdesc->numframes > 1 )
f = ( frame * ( pseqdesc->numframes - 1 )) / 256.0f;
2012-02-11 21:00:00 +01:00
Mod_StudioCalcRotations( boneused, numbones, pcontroller, pos, q, pseqdesc, panim, f );
2010-08-25 22:00:00 +02:00
if( pseqdesc->numblends > 1 )
{
float s;
2012-02-11 21:00:00 +01:00
panim += mod_studiohdr->numbones;
Mod_StudioCalcRotations( boneused, numbones, pcontroller, pos2, q2, pseqdesc, panim, f );
2010-08-25 22:00:00 +02:00
2010-11-15 22:00:00 +01:00
s = (float)pblending[0] / 255.0f;
2010-08-25 22:00:00 +02:00
2017-03-08 22:00:00 +01:00
R_StudioSlerpBones( mod_studiohdr->numbones, q, pos, q2, pos2, s );
2010-08-25 22:00:00 +02:00
if( pseqdesc->numblends == 4 )
{
2012-02-11 21:00:00 +01:00
panim += mod_studiohdr->numbones;
Mod_StudioCalcRotations( boneused, numbones, pcontroller, pos3, q3, pseqdesc, panim, f );
2010-08-25 22:00:00 +02:00
2012-02-11 21:00:00 +01:00
panim += mod_studiohdr->numbones;
Mod_StudioCalcRotations( boneused, numbones, pcontroller, pos4, q4, pseqdesc, panim, f );
2010-08-25 22:00:00 +02:00
2010-11-15 22:00:00 +01:00
s = (float)pblending[0] / 255.0f;
2017-03-08 22:00:00 +01:00
R_StudioSlerpBones( mod_studiohdr->numbones, q3, pos3, q4, pos4, s );
2010-08-25 22:00:00 +02:00
2010-11-15 22:00:00 +01:00
s = (float)pblending[1] / 255.0f;
2017-03-08 22:00:00 +01:00
R_StudioSlerpBones( mod_studiohdr->numbones, q, pos, q3, pos3, s );
2010-08-25 22:00:00 +02:00
}
}
2012-02-11 21:00:00 +01:00
Matrix3x4_CreateFromEntity( studio_transform, angles, origin, 1.0f );
2011-04-05 22:00:00 +02:00
2010-11-15 22:00:00 +01:00
for( j = numbones - 1; j >= 0; j-- )
2010-08-25 22:00:00 +02:00
{
2010-11-15 22:00:00 +01:00
i = boneused[j];
2011-04-05 22:00:00 +02:00
2010-12-22 22:00:00 +01:00
Matrix3x4_FromOriginQuat( bonematrix, q[i], pos[i] );
2010-08-25 22:00:00 +02:00
if( pbones[i].parent == -1 )
2012-02-11 21:00:00 +01:00
Matrix3x4_ConcatTransforms( studio_bones[i], studio_transform, bonematrix );
else Matrix3x4_ConcatTransforms( studio_bones[i], studio_bones[pbones[i].parent], bonematrix );
2010-08-25 22:00:00 +02:00
}
}
2012-02-11 21:00:00 +01:00
/*
====================
StudioGetAttachment
====================
*/
2017-03-08 22:00:00 +01:00
void Mod_StudioGetAttachment( const edict_t *e, int iAtt, float *origin, float *angles )
2010-08-25 22:00:00 +02:00
{
2010-11-15 22:00:00 +01:00
mstudioattachment_t *pAtt;
2012-02-12 21:00:00 +01:00
vec3_t angles2;
2019-02-21 22:00:00 +01:00
matrix3x4 localPose;
matrix3x4 worldPose;
2012-02-12 21:00:00 +01:00
model_t *mod;
2010-11-15 22:00:00 +01:00
2018-02-25 22:00:00 +01:00
mod = SV_ModelHandle( e->v.modelindex );
2017-07-02 23:00:00 +02:00
mod_studiohdr = (studiohdr_t *)Mod_StudioExtradata( mod );
2012-02-12 21:00:00 +01:00
if( !mod_studiohdr ) return;
2010-11-15 22:00:00 +01:00
2012-02-11 21:00:00 +01:00
if( mod_studiohdr->numattachments <= 0 )
2017-08-18 23:00:00 +02:00
{
if( origin ) VectorCopy( e->v.origin, origin );
if( FBitSet( host.features, ENGINE_COMPUTE_STUDIO_LERP ) && angles )
VectorCopy( e->v.angles, angles );
2010-08-25 22:00:00 +02:00
return;
2017-08-18 23:00:00 +02:00
}
2010-11-15 22:00:00 +01:00
2017-06-23 23:00:00 +02:00
iAtt = bound( 0, iAtt, mod_studiohdr->numattachments - 1 );
2010-11-15 22:00:00 +01:00
// calculate attachment origin and angles
2017-06-23 23:00:00 +02:00
pAtt = (mstudioattachment_t *)((byte *)mod_studiohdr + mod_studiohdr->attachmentindex) + iAtt;
2010-11-15 22:00:00 +01:00
2012-02-12 21:00:00 +01:00
VectorCopy( e->v.angles, angles2 );
2013-09-03 22:00:00 +02:00
2017-03-08 22:00:00 +01:00
if( !FBitSet( host.features, ENGINE_COMPENSATE_QUAKE_BUG ))
2013-09-03 22:00:00 +02:00
angles2[PITCH] = -angles2[PITCH];
2012-02-12 21:00:00 +01:00
2017-06-23 23:00:00 +02:00
pBlendAPI->SV_StudioSetupBones( mod, e->v.frame, e->v.sequence, angles2, e->v.origin, e->v.controller, e->v.blending, pAtt->bone, e );
2010-11-15 22:00:00 +01:00
2019-02-21 22:00:00 +01:00
Matrix3x4_LoadIdentity( localPose );
Matrix3x4_SetOrigin( localPose, pAtt->org[0], pAtt->org[1], pAtt->org[2] );
Matrix3x4_ConcatTransforms( worldPose, studio_bones[pAtt->bone], localPose );
2011-02-05 22:00:00 +01:00
2019-02-21 22:00:00 +01:00
if( origin != NULL ) // origin is used always
Matrix3x4_OriginFromMatrix( worldPose, origin );
2012-02-12 21:00:00 +01:00
2019-02-21 22:00:00 +01:00
if( FBitSet( host.features, ENGINE_COMPUTE_STUDIO_LERP ) && angles != NULL )
Matrix3x4_AnglesFromMatrix( worldPose, angles );
2010-08-25 22:00:00 +02:00
}
2012-02-11 21:00:00 +01:00
/*
====================
GetBonePosition
====================
*/
2012-02-12 21:00:00 +01:00
void Mod_GetBonePosition( const edict_t *e, int iBone, float *origin, float *angles )
2012-02-11 21:00:00 +01:00
{
2012-02-12 21:00:00 +01:00
model_t *mod;
2018-02-25 22:00:00 +01:00
mod = SV_ModelHandle( e->v.modelindex );
2017-07-02 23:00:00 +02:00
mod_studiohdr = (studiohdr_t *)Mod_StudioExtradata( mod );
2012-02-12 21:00:00 +01:00
if( !mod_studiohdr ) return;
2012-02-11 21:00:00 +01:00
2017-03-08 22:00:00 +01:00
pBlendAPI->SV_StudioSetupBones( mod, e->v.frame, e->v.sequence, e->v.angles, e->v.origin, e->v.controller, e->v.blending, iBone, e );
2012-02-11 21:00:00 +01:00
2012-02-12 21:00:00 +01:00
if( origin ) Matrix3x4_OriginFromMatrix( studio_bones[iBone], origin );
2019-02-21 22:00:00 +01:00
if( angles ) Matrix3x4_AnglesFromMatrix( studio_bones[iBone], angles );
2012-02-11 21:00:00 +01:00
}
/*
====================
HitgroupForStudioHull
====================
*/
int Mod_HitgroupForStudioHull( int index )
{
return studio_hull_hitgroup[index];
}
/*
====================
StudioBoundVertex
====================
*/
2017-03-08 22:00:00 +01:00
void Mod_StudioBoundVertex( vec3_t mins, vec3_t maxs, int *numverts, const vec3_t vertex )
2012-02-11 21:00:00 +01:00
{
2017-03-08 22:00:00 +01:00
if((*numverts) == 0 )
ClearBounds( mins, maxs );
2012-02-11 21:00:00 +01:00
2017-03-08 22:00:00 +01:00
AddPointToBounds( vertex, mins, maxs );
(*numverts)++;
2012-02-11 21:00:00 +01:00
}
/*
====================
StudioAccumulateBoneVerts
====================
*/
2017-03-08 22:00:00 +01:00
void Mod_StudioAccumulateBoneVerts( vec3_t mins, vec3_t maxs, int *numverts, vec3_t bone_mins, vec3_t bone_maxs, int *numbones )
2010-08-25 22:00:00 +02:00
{
2017-03-08 22:00:00 +01:00
vec3_t delta;
vec3_t point;
2012-02-11 21:00:00 +01:00
2017-03-08 22:00:00 +01:00
if( *numbones <= 0 )
2010-08-25 22:00:00 +02:00
return;
2012-02-11 21:00:00 +01:00
// calculate the midpoint of the second vertex,
2017-03-08 22:00:00 +01:00
VectorSubtract( bone_maxs, bone_mins, delta );
2012-02-11 21:00:00 +01:00
2017-03-08 22:00:00 +01:00
VectorScale( delta, 0.5f, point );
Mod_StudioBoundVertex( mins, maxs, numverts, point );
2011-10-06 22:00:00 +02:00
2017-03-08 22:00:00 +01:00
VectorClear( bone_mins );
VectorClear( bone_maxs );
*numbones = 0;
2012-02-11 21:00:00 +01:00
}
/*
====================
StudioComputeBounds
====================
*/
2017-03-08 22:00:00 +01:00
void Mod_StudioComputeBounds( void *buffer, vec3_t mins, vec3_t maxs, qboolean ignore_sequences )
2012-02-11 21:00:00 +01:00
{
2017-03-08 22:00:00 +01:00
int i, j, k, numseq;
2012-02-11 21:00:00 +01:00
studiohdr_t *pstudiohdr;
mstudiobodyparts_t *pbodypart;
mstudiomodel_t *m_pSubModel;
2017-03-06 22:00:00 +01:00
mstudioseqgroup_t *pseqgroup;
2012-02-11 21:00:00 +01:00
mstudioseqdesc_t *pseqdesc;
mstudiobone_t *pbones;
2017-03-06 22:00:00 +01:00
mstudioanim_t *panim;
2017-03-08 22:00:00 +01:00
vec3_t bone_mins, bone_maxs;
vec3_t vert_mins, vert_maxs;
int vert_count, bone_count;
2012-02-11 21:00:00 +01:00
int bodyCount = 0;
2017-03-08 22:00:00 +01:00
vec3_t pos, *pverts;
2012-02-11 21:00:00 +01:00
2017-03-08 22:00:00 +01:00
vert_count = bone_count = 0;
VectorClear( bone_mins );
VectorClear( bone_maxs );
VectorClear( vert_mins );
VectorClear( vert_maxs );
2012-02-11 21:00:00 +01:00
// Get the body part portion of the model
pstudiohdr = (studiohdr_t *)buffer;
pbodypart = (mstudiobodyparts_t *)((byte *)pstudiohdr + pstudiohdr->bodypartindex);
// each body part has nummodels variations so there are as many total variations as there
// are in a matrix of each part by each other part
for( i = 0; i < pstudiohdr->numbodyparts; i++ )
bodyCount += pbodypart[i].nummodels;
2017-03-08 22:00:00 +01:00
// The studio models we want are vec3_t mins, vec3_t maxsight after the bodyparts (still need to
2012-02-11 21:00:00 +01:00
// find a detailed breakdown of the mdl format). Move pointer there.
m_pSubModel = (mstudiomodel_t *)(&pbodypart[pstudiohdr->numbodyparts]);
for( i = 0; i < bodyCount; i++ )
{
2017-03-08 22:00:00 +01:00
pverts = (vec3_t *)((byte *)pstudiohdr + m_pSubModel[i].vertindex);
2012-02-11 21:00:00 +01:00
for( j = 0; j < m_pSubModel[i].numverts; j++ )
2017-03-08 22:00:00 +01:00
Mod_StudioBoundVertex( bone_mins, bone_maxs, &vert_count, pverts[j] );
2012-02-11 21:00:00 +01:00
}
pbones = (mstudiobone_t *)((byte *)pstudiohdr + pstudiohdr->boneindex);
2017-03-08 22:00:00 +01:00
numseq = (ignore_sequences) ? 1 : pstudiohdr->numseq;
2012-02-11 21:00:00 +01:00
2017-03-08 22:00:00 +01:00
for( i = 0; i < numseq; i++ )
2012-02-11 21:00:00 +01:00
{
2017-03-06 22:00:00 +01:00
pseqdesc = (mstudioseqdesc_t *)((byte *)pstudiohdr + pstudiohdr->seqindex) + i;
pseqgroup = (mstudioseqgroup_t *)((byte *)pstudiohdr + pstudiohdr->seqgroupindex) + pseqdesc->seqgroup;
if( pseqdesc->seqgroup == 0 )
panim = (mstudioanim_t *)((byte *)pstudiohdr + pseqgroup->data + pseqdesc->animindex);
else continue;
2012-02-11 21:00:00 +01:00
for( j = 0; j < pstudiohdr->numbones; j++ )
{
2017-03-06 22:00:00 +01:00
for( k = 0; k < pseqdesc->numframes; k++ )
2012-02-11 21:00:00 +01:00
{
2017-03-06 22:00:00 +01:00
R_StudioCalcBonePosition( k, 0, &pbones[j], panim, NULL, pos );
2017-03-08 22:00:00 +01:00
Mod_StudioBoundVertex( vert_mins, vert_maxs, &bone_count, pos );
2012-02-11 21:00:00 +01:00
}
}
2017-03-08 22:00:00 +01:00
Mod_StudioAccumulateBoneVerts( bone_mins, bone_maxs, &vert_count, vert_mins, vert_maxs, &bone_count );
2012-02-11 21:00:00 +01:00
}
2017-03-08 22:00:00 +01:00
VectorCopy( bone_mins, mins );
VectorCopy( bone_maxs, maxs );
2012-02-11 21:00:00 +01:00
}
/*
====================
Mod_GetStudioBounds
====================
*/
qboolean Mod_GetStudioBounds( const char *name, vec3_t mins, vec3_t maxs )
{
int result = false;
byte *f;
if( !Q_strstr( name, "models" ) || !Q_strstr( name, ".mdl" ))
return false;
f = FS_LoadFile( name, NULL, false );
if( !f ) return false;
if( *(uint *)f == IDSTUDIOHEADER )
{
VectorClear( mins );
VectorClear( maxs );
2017-03-08 22:00:00 +01:00
Mod_StudioComputeBounds( f, mins, maxs, false );
2012-02-11 21:00:00 +01:00
result = true;
}
Mem_Free( f );
return result;
2010-11-15 22:00:00 +01:00
}
2018-02-28 22:00:00 +01:00
/*
===============
Mod_StudioTexName
extract texture filename from modelname
===============
*/
2018-03-08 22:00:00 +01:00
const char *Mod_StudioTexName( const char *modname )
2018-02-28 22:00:00 +01:00
{
static char texname[MAX_QPATH];
2018-03-08 22:00:00 +01:00
Q_strncpy( texname, modname, sizeof( texname ));
2018-02-28 22:00:00 +01:00
COM_StripExtension( texname );
Q_strncat( texname, "T.mdl", sizeof( texname ));
return texname;
}
/*
================
Mod_StudioBodyVariations
calc studio body variations
================
*/
static int Mod_StudioBodyVariations( model_t *mod )
{
studiohdr_t *pstudiohdr;
mstudiobodyparts_t *pbodypart;
int i, count = 1;
pstudiohdr = (studiohdr_t *)Mod_StudioExtradata( mod );
if( !pstudiohdr ) return 0;
pbodypart = (mstudiobodyparts_t *)((byte *)pstudiohdr + pstudiohdr->bodypartindex);
// each body part has nummodels variations so there are as many total variations as there
// are in a matrix of each part by each other part
for( i = 0; i < pstudiohdr->numbodyparts; i++ )
count = count * pbodypart[i].nummodels;
return count;
}
/*
=================
R_StudioLoadHeader
=================
*/
studiohdr_t *R_StudioLoadHeader( model_t *mod, const void *buffer )
{
byte *pin;
studiohdr_t *phdr;
int i;
if( !buffer ) return NULL;
pin = (byte *)buffer;
phdr = (studiohdr_t *)pin;
i = phdr->version;
if( i != STUDIO_VERSION )
{
2018-09-10 23:00:00 +02:00
Con_Printf( S_ERROR "%s has wrong version number (%i should be %i)\n", mod->name, i, STUDIO_VERSION );
2018-02-28 22:00:00 +01:00
return NULL;
}
return (studiohdr_t *)buffer;
}
/*
=================
Mod_LoadStudioModel
=================
*/
void Mod_LoadStudioModel( model_t *mod, const void *buffer, qboolean *loaded )
{
studiohdr_t *phdr;
if( loaded ) *loaded = false;
loadmodel->mempool = Mem_AllocPool( va( "^2%s^7", loadmodel->name ));
loadmodel->type = mod_studio;
phdr = R_StudioLoadHeader( mod, buffer );
if( !phdr ) return; // bad model
if( phdr->numtextures == 0 )
{
studiohdr_t *thdr;
byte *in, *out;
void *buffer2 = NULL;
size_t size1, size2;
2018-03-08 22:00:00 +01:00
buffer2 = FS_LoadFile( Mod_StudioTexName( mod->name ), NULL, false );
2018-02-28 22:00:00 +01:00
thdr = R_StudioLoadHeader( mod, buffer2 );
if( !thdr )
{
2018-09-10 23:00:00 +02:00
Con_Printf( S_WARN "Mod_LoadStudioModel: %s missing textures file\n", mod->name );
2018-02-28 22:00:00 +01:00
if( buffer2 ) Mem_Free( buffer2 );
}
else
{
Mod_StudioLoadTextures( mod, thdr );
// give space for textures and skinrefs
size1 = thdr->numtextures * sizeof( mstudiotexture_t );
size2 = thdr->numskinfamilies * thdr->numskinref * sizeof( short );
2018-05-26 23:00:00 +02:00
mod->cache.data = Mem_Calloc( loadmodel->mempool, phdr->length + size1 + size2 );
2018-02-28 22:00:00 +01:00
memcpy( loadmodel->cache.data, buffer, phdr->length ); // copy main mdl buffer
phdr = (studiohdr_t *)loadmodel->cache.data; // get the new pointer on studiohdr
phdr->numskinfamilies = thdr->numskinfamilies;
phdr->numtextures = thdr->numtextures;
phdr->numskinref = thdr->numskinref;
phdr->textureindex = phdr->length;
phdr->skinindex = phdr->textureindex + size1;
in = (byte *)thdr + thdr->textureindex;
out = (byte *)phdr + phdr->textureindex;
memcpy( out, in, size1 + size2 ); // copy textures + skinrefs
phdr->length += size1 + size2;
Mem_Free( buffer2 ); // release T.mdl
}
}
else
{
// NOTE: don't modify source buffer because it's used for CRC computing
2018-05-26 23:00:00 +02:00
loadmodel->cache.data = Mem_Calloc( loadmodel->mempool, phdr->length );
2018-02-28 22:00:00 +01:00
memcpy( loadmodel->cache.data, buffer, phdr->length );
phdr = (studiohdr_t *)loadmodel->cache.data; // get the new pointer on studiohdr
Mod_StudioLoadTextures( mod, phdr );
// NOTE: we wan't keep raw textures in memory. just cutoff model pointer above texture base
loadmodel->cache.data = Mem_Realloc( loadmodel->mempool, loadmodel->cache.data, phdr->texturedataindex );
phdr = (studiohdr_t *)loadmodel->cache.data; // get the new pointer on studiohdr
phdr->length = phdr->texturedataindex; // update model size
}
// setup bounding box
if( !VectorCompare( vec3_origin, phdr->bbmin ))
{
// clipping bounding box
VectorCopy( phdr->bbmin, loadmodel->mins );
VectorCopy( phdr->bbmax, loadmodel->maxs );
}
else if( !VectorCompare( vec3_origin, phdr->min ))
{
// movement bounding box
VectorCopy( phdr->min, loadmodel->mins );
VectorCopy( phdr->max, loadmodel->maxs );
}
else
{
// well compute bounds from vertices and round to nearest even values
Mod_StudioComputeBounds( phdr, loadmodel->mins, loadmodel->maxs, true );
RoundUpHullSize( loadmodel->mins );
RoundUpHullSize( loadmodel->maxs );
}
loadmodel->numframes = Mod_StudioBodyVariations( loadmodel );
loadmodel->radius = RadiusFromBounds( loadmodel->mins, loadmodel->maxs );
loadmodel->flags = phdr->flags; // copy header flags
if( loaded ) *loaded = true;
}
/*
=================
Mod_UnloadStudioModel
=================
*/
void Mod_UnloadStudioModel( model_t *mod )
{
Assert( mod != NULL );
if( mod->type != mod_studio )
return; // not a studio
Mod_StudioUnloadTextures( mod->cache.data );
Mem_FreePool( &mod->mempool );
memset( mod, 0, sizeof( *mod ));
}
2010-11-15 22:00:00 +01:00
static sv_blending_interface_t gBlendAPI =
{
SV_BLENDING_INTERFACE_VERSION,
SV_StudioSetupBones,
};
static server_studio_api_t gStudioAPI =
{
Mod_Calloc,
Mod_CacheCheck,
Mod_LoadCacheFile,
2017-07-02 23:00:00 +02:00
Mod_StudioExtradata,
2010-11-15 22:00:00 +01:00
};
/*
===============
2012-02-11 21:00:00 +01:00
Mod_InitStudioAPI
2010-11-15 22:00:00 +01:00
Initialize server studio (blending interface)
===============
*/
2012-02-11 21:00:00 +01:00
void Mod_InitStudioAPI( void )
2010-11-15 22:00:00 +01:00
{
static STUDIOAPI pBlendIface;
pBlendAPI = &gBlendAPI;
2018-02-28 22:00:00 +01:00
pBlendIface = (STUDIOAPI)COM_GetProcAddress( svgame.hInstance, "Server_GetBlendingInterface" );
2012-02-11 21:00:00 +01:00
if( pBlendIface && pBlendIface( SV_BLENDING_INTERFACE_VERSION, &pBlendAPI, &gStudioAPI, &studio_transform, &studio_bones ))
2012-02-08 21:00:00 +01:00
{
2018-09-10 23:00:00 +02:00
Con_Reportf( "SV_LoadProgs: ^2initailized Server Blending interface ^7ver. %i\n", SV_BLENDING_INTERFACE_VERSION );
2012-02-11 21:00:00 +01:00
return;
2012-02-08 21:00:00 +01:00
}
2010-11-15 22:00:00 +01:00
// just restore pointer to builtin function
pBlendAPI = &gBlendAPI;
2012-12-22 21:00:00 +01:00
}
/*
===============
Mod_ResetStudioAPI
Returns to default callbacks
===============
*/
void Mod_ResetStudioAPI( void )
{
pBlendAPI = &gBlendAPI;
2010-08-25 22:00:00 +02:00
}