Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

483 lines
14 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #ifndef INCLUDED_STUDIOMODEL
  9. #define INCLUDED_STUDIOMODEL
  10. #include "mathlib/mathlib.h"
  11. #include "studio.h"
  12. #include "mouthinfo.h"
  13. #include "UtlLinkedList.h"
  14. #include "utlsymbol.h"
  15. #include "bone_setup.h"
  16. #include "datacache/imdlcache.h"
  17. #include "viewersettings.h"
  18. #include "tier1/utlstring.h"
  19. #define DEFAULT_BLEND_TIME 0.2
  20. //-----------------------------------------------------------------------------
  21. // Forward declarations
  22. //-----------------------------------------------------------------------------
  23. typedef struct IFACE_TAG IFACE;
  24. typedef struct IMESH_TAG IMESH;
  25. typedef struct ResolutionUpdateTag ResolutionUpdate;
  26. typedef struct FaceUpdateTag FaceUpdate;
  27. class IMaterial;
  28. class IDataCache;
  29. class IStudioPhysics;
  30. class IMaterialSystem;
  31. class IMDLCache;
  32. class CPhysmesh;
  33. struct hlmvsolid_t;
  34. struct constraint_ragdollparams_t;
  35. class IStudioRender;
  36. class IPhysicsSurfaceProps;
  37. class IPhysicsCollision;
  38. class IStudioDataCache;
  39. class IDataCache;
  40. class IFileSystem;
  41. class IMaterialSystemHardwareConfig;
  42. class CJiggleBones;
  43. //-----------------------------------------------------------------------------
  44. // Singleton interfaces
  45. //-----------------------------------------------------------------------------
  46. extern IStudioRender *g_pStudioRender;
  47. extern IMDLCache *g_pMDLCache;
  48. extern IPhysicsSurfaceProps *physprop;
  49. extern IPhysicsCollision *physcollision;
  50. extern IStudioDataCache *g_pStudioDataCache;
  51. extern IDataCache *g_pDataCache;
  52. extern IFileSystem *g_pFileSystem;
  53. extern IMaterialSystem *g_pMaterialSystem;
  54. extern IMaterialSystemHardwareConfig *g_pMaterialSystemHardwareConfig;
  55. class AnimationLayer
  56. {
  57. public:
  58. float m_cycle; // 0 to 1 animation playback index
  59. int m_sequence; // sequence index
  60. float m_weight;
  61. float m_playbackrate;
  62. int m_priority; // lower priorities get layered first
  63. };
  64. struct StudioLookTarget
  65. {
  66. float m_flWeight;
  67. Vector m_vecPosition;
  68. bool m_bSelf;
  69. };
  70. struct HitboxInfo_t
  71. {
  72. CUtlString m_Name;
  73. mstudiobbox_t m_BBox;
  74. };
  75. // I'm saving this as internal data because we may add or remove hitboxes
  76. // I'm using a utllinkedlist so hitbox IDs remain constant on add + remove
  77. typedef CUtlLinkedList< HitboxInfo_t, unsigned short > HitboxList_t;
  78. struct HitboxSet_t
  79. {
  80. CUtlString m_Name;
  81. HitboxList_t m_Hitboxes;
  82. };
  83. class StudioModel
  84. {
  85. public:
  86. StudioModel();
  87. // memory handling, uses calloc so members are zero'd out on instantiation
  88. static void *operator new( size_t nSize );
  89. static void* operator new( size_t size, int nBlockUse, const char *pFileName, int nLine );
  90. static void operator delete( void *pData );
  91. static void operator delete( void* p, int nBlockUse, const char *pFileName, int nLine );
  92. static void Init( void );
  93. static void Shutdown( void ); // garymcthack - need to call this.
  94. static void UpdateViewState( const Vector& viewOrigin,
  95. const Vector& viewRight,
  96. const Vector& viewUp,
  97. const Vector& viewPlaneNormal );
  98. static void ReleaseStudioModel( void );
  99. static void RestoreStudioModel( void );
  100. static void UnloadGroupFiles();
  101. char const *GetFileName( void );
  102. IStudioRender *GetStudioRender();
  103. static void UpdateStudioRenderConfig( bool bWireframe, bool bZBufferWireframe, bool bNormals, bool bTangentFrame );
  104. studiohdr_t *getAnimHeader (int i) const;
  105. virtual void ModelInit( void ) { }
  106. bool IsModelLoaded() const;
  107. void FreeModel( bool bReleasing );
  108. bool LoadModel( const char *modelname );
  109. virtual bool PostLoadModel ( const char *modelname );
  110. bool HasModel();
  111. virtual int DrawModel( bool mergeBones = false );
  112. virtual void AdvanceFrame( float dt );
  113. float GetInterval( void );
  114. float GetCycle( void );
  115. float GetFrame( void );
  116. int GetMaxFrame( void );
  117. int SetFrame( int frame );
  118. float GetCycle( int iLayer );
  119. float GetFrame( int iLayer );
  120. int GetMaxFrame( int iLayer );
  121. int SetFrame( int iLayer, int frame );
  122. void ExtractBbox( Vector &mins, Vector &maxs );
  123. void SetBlendTime( float blendtime );
  124. int LookupSequence( const char *szSequence );
  125. int LookupActivity( const char *szActivity );
  126. int SetSequence( int iSequence );
  127. int SetSequence( const char *szSequence );
  128. const char* GetSequenceName( int iSequence );
  129. void ClearOverlaysSequences( void );
  130. void ClearAnimationLayers( void );
  131. int GetNewAnimationLayer( int iPriority = 0 );
  132. int SetOverlaySequence( int iLayer, int iSequence, float flWeight );
  133. float SetOverlayRate( int iLayer, float flCycle, float flFrameRate );
  134. int GetOverlaySequence( int iLayer );
  135. float GetOverlaySequenceWeight( int iLayer );
  136. void StartBlending( void );
  137. float GetTransitionAmount( void );
  138. int GetSequence( void );
  139. void GetSequenceInfo( int iSequence, float *pflFrameRate, float *pflGroundSpeed );
  140. void GetSequenceInfo( float *pflFrameRate, float *pflGroundSpeed );
  141. float GetFPS( int iSequence );
  142. float GetFPS( );
  143. float GetDuration( int iSequence );
  144. float GetDuration( );
  145. int GetNumFrames( int iSequence );
  146. bool GetSequenceLoops( int iSequence );
  147. void GetMovement( float prevCycle[5], Vector &vecPos, QAngle &vecAngles );
  148. void GetMovement( int iSequence, float prevCycle, float currCycle, Vector &vecPos, QAngle &vecAngles );
  149. void GetSeqAnims( int iSequence, mstudioanimdesc_t *panim[4], float *pweights );
  150. void GetSeqAnims( mstudioanimdesc_t *panim[4], float *pweights );
  151. float GetGroundSpeed( int iSequence );
  152. float GetGroundSpeed( void );
  153. float GetCurrentVelocity( void );
  154. bool IsHidden( int iSequence );
  155. float SetController( int iController, float flValue );
  156. int LookupPoseParameter( char const *szName );
  157. float SetPoseParameter( int iParameter, float flValue );
  158. float SetPoseParameter( char const *szName, float flValue );
  159. float GetPoseParameter( char const *szName );
  160. float GetPoseParameter( int iParameter );
  161. bool GetPoseParameterRange( int iParameter, float *pflMin, float *pflMax );
  162. float* GetPoseParameters();
  163. int LookupAttachment( char const *szName );
  164. int SetBodygroup( int iGroup, int iValue = -1 );
  165. int SetSkin( int iValue );
  166. int FindBone( const char *pName );
  167. LocalFlexController_t LookupFlexController( char *szName );
  168. void SetFlexController( char *szName, float flValue );
  169. void SetFlexController( LocalFlexController_t iFlex, float flValue );
  170. float GetFlexController( char *szName );
  171. float GetFlexController( LocalFlexController_t iFlex );
  172. void SetFlexControllerRaw( LocalFlexController_t iFlex, float flValue );
  173. float GetFlexControllerRaw( LocalFlexController_t iFlex );
  174. // void CalcBoneTransform( int iBone, Vector pos[], Quaternion q[], matrix3x4_t& bonematrix );
  175. void UpdateBoneChain( Vector pos[], Quaternion q[], int iBone, matrix3x4_t *pBoneToWorld );
  176. void SetViewTarget( void ); // ???
  177. void GetBodyPoseParametersFromFlex( void );
  178. void CalcHeadRotation( Vector pos[], Quaternion q[] );
  179. float SetHeadPosition( matrix3x4_t& attToWorld, Vector const &vTargetPos, float dt );
  180. int GetNumLODs() const;
  181. float GetLODSwitchValue( int lod ) const;
  182. void SetLODSwitchValue( int lod, float switchValue );
  183. void scaleMeshes( float scale );
  184. void scaleBones( float scale );
  185. // Physics
  186. void OverrideBones( bool *override );
  187. int Physics_GetBoneCount( void );
  188. const char * Physics_GetBoneName( int index );
  189. int Physics_GetBoneIndex( const char *pName );
  190. void Physics_GetData( int boneIndex, hlmvsolid_t *psolid, constraint_ragdollparams_t *pConstraint ) const;
  191. void Physics_SetData( int boneIndex, const hlmvsolid_t *psolid, constraint_ragdollparams_t const *pConstraint );
  192. void Physics_SetPreview( int previewBone, int axis, float t );
  193. float Physics_GetMass( void );
  194. void Physics_SetMass( float mass );
  195. char *Physics_DumpQC( void );
  196. float GetSequenceTime() const { return m_sequencetime; }
  197. float GetTimeDelta() const { return m_dt; }
  198. CStudioHdr *m_pStudioHdr;
  199. CStudioHdr *GetStudioHdr() const;
  200. studiohdr_t *GetStudioRenderHdr() const;
  201. studiohwdata_t *GetHardwareData( void ) const;
  202. // Get and set the model transform (i.e. what m_origin and m_angles are used to generate).
  203. void GetModelTransform( matrix3x4_t &mat );
  204. void SetModelTransform( const matrix3x4_t &mat );
  205. public:
  206. // entity settings
  207. QAngle m_angles; // rot
  208. Vector m_origin; // trans
  209. protected:
  210. int m_bodynum; // bodypart selection
  211. int m_skinnum; // skin group selection
  212. float m_controller[4]; // bone controllers
  213. public:
  214. CMouthInfo m_mouth;
  215. protected:
  216. char *m_pModelName; // model file name
  217. // bool m_owntexmodel; // do we have a modelT.mdl ?
  218. // Previouse sequence data
  219. float m_blendtime;
  220. float m_sequencetime;
  221. int m_prevsequence;
  222. float m_prevcycle;
  223. float m_dt;
  224. // Blending info
  225. // Gesture,Sequence layering state
  226. #define MAXSTUDIOANIMLAYERS 8
  227. AnimationLayer m_Layer[MAXSTUDIOANIMLAYERS];
  228. int m_iActiveLayers;
  229. public:
  230. float m_cycle; // 0 to 1 animation playback index
  231. protected:
  232. int m_sequence; // sequence index
  233. float m_poseparameter[MAXSTUDIOPOSEPARAM]; // intra-sequence blending
  234. float m_weight;
  235. // internal data
  236. MDLHandle_t m_MDLHandle;
  237. mstudiomodel_t *m_pmodel;
  238. public:
  239. CUtlVector< HitboxSet_t > m_HitboxSets;
  240. CUtlVector< CUtlSymbol > m_SurfaceProps;
  241. protected:
  242. // class data
  243. static Vector *m_AmbientLightColors;
  244. // Added data
  245. // IMESH *m_pimesh;
  246. // VertexUpdate *m_pvertupdate;
  247. // FaceUpdate *m_pfaceupdate;
  248. IFACE *m_pface;
  249. // studiohdr_t *m_ptexturehdr;
  250. Vector4D m_adj; // FIX: non persistant, make static
  251. public:
  252. IStudioPhysics *m_pPhysics;
  253. private:
  254. int m_physPreviewBone;
  255. int m_physPreviewAxis;
  256. float m_physPreviewParam;
  257. float m_physMass;
  258. public:
  259. mstudioseqdesc_t &GetSeqDesc( int seq );
  260. const matrix3x4_t* BoneToWorld( int nBoneIndex ) const;
  261. private:
  262. mstudioanimdesc_t &GetAnimDesc( int anim );
  263. mstudioanim_t *GetAnim( int anim );
  264. void DrawPhysmesh( CPhysmesh *pMesh, int boneIndex, IMaterial *pMaterial, float *color );
  265. void DrawPhysConvex( CPhysmesh *pMesh, IMaterial *pMaterial );
  266. void SetupLighting( void );
  267. virtual void SetupModel( int bodypart );
  268. private:
  269. float m_flexweight[MAXSTUDIOFLEXCTRL];
  270. matrix3x4_t m_pBoneToWorld[MAXSTUDIOBONES];
  271. public:
  272. virtual void RunFlexRules( void );
  273. virtual int BoneMask( void );
  274. virtual void SetUpBones( bool mergeBones );
  275. int GetLodUsed( void );
  276. float GetLodMetric( void );
  277. const char *GetKeyValueText( int iSequence );
  278. private:
  279. // Drawing helper methods
  280. void DrawBones( );
  281. void DrawAttachments( );
  282. void DrawEditAttachment();
  283. void DrawHitboxes();
  284. void DrawPhysicsModel( );
  285. void DrawIllumPosition( );
  286. void DrawOriginAxis( );
  287. public:
  288. // generic interface to rendering?
  289. void drawBox (Vector const *v, float const * color );
  290. void drawWireframeBox (Vector const *v, float const* color );
  291. void drawTransform( matrix3x4_t& m, float flLength = 4 );
  292. void drawLine( Vector const &p1, Vector const &p2, int r = 0, int g = 0, int b = 255 );
  293. void drawTransparentBox( Vector const &bbmin, Vector const &bbmax, const matrix3x4_t& m, float const *color, float const *wirecolor );
  294. private:
  295. int m_LodUsed;
  296. float m_LodMetric;
  297. public:
  298. void SetSolveHeadTurn( int solve );
  299. int GetSolveHeadTurn() const;
  300. void ClearLookTargets( void );
  301. void AddLookTarget( const Vector& vecPosition, float flWeight );
  302. void AddLookTargetSelf( float flWeight );
  303. void SetModelYaw( float yaw );
  304. float GetModelYaw( void ) const;
  305. void SetBodyYaw( float yaw );
  306. float GetBodyYaw( void ) const;
  307. void SetSpineYaw( float yaw );
  308. float GetSpineYaw( void ) const;
  309. private:
  310. // 0 == no, 1 == based on dt, 2 == completely.
  311. int m_nSolveHeadTurn;
  312. CUtlVector < StudioLookTarget > m_vecHeadTargets;
  313. float m_flModelYaw;
  314. float m_flBodyYaw;
  315. float m_flSpineYaw;
  316. public:
  317. bool m_bIsTransparent;
  318. bool m_bHasProxy;
  319. // necessary for accessing correct vertexes
  320. void SetCurrentModel();
  321. public:
  322. CIKContext m_ik;
  323. float m_prevGroundCycles[5];
  324. float m_prevIKCycles[5];
  325. public:
  326. void IncrementFramecounter( void ) { m_iFramecounter++; };
  327. private:
  328. int m_iFramecounter;
  329. private:
  330. CJiggleBones *m_pJiggleBones;
  331. };
  332. //-----------------------------------------------------------------------------
  333. // Inline methods
  334. //-----------------------------------------------------------------------------
  335. inline CStudioHdr *StudioModel::GetStudioHdr( void ) const
  336. {
  337. if (!m_pStudioHdr || m_pStudioHdr->IsReadyForAccess())
  338. return m_pStudioHdr;
  339. studiohdr_t *hdr = g_pMDLCache->GetStudioHdr( m_MDLHandle );
  340. m_pStudioHdr->Init( hdr );
  341. if (m_pStudioHdr->IsReadyForAccess())
  342. return m_pStudioHdr;
  343. return NULL;
  344. }
  345. inline studiohdr_t *StudioModel::GetStudioRenderHdr( void ) const
  346. {
  347. return g_pMDLCache->GetStudioHdr( m_MDLHandle );
  348. }
  349. inline studiohwdata_t *StudioModel::GetHardwareData( void ) const
  350. {
  351. return g_pMDLCache->GetHardwareData( m_MDLHandle );
  352. }
  353. inline studiohdr_t *StudioModel::getAnimHeader( int i ) const
  354. {
  355. // return g_pMDLCache->GetStudioHdr( m_AnimHandle[i] );
  356. // return m_panimhdr[i];
  357. }
  358. inline char const *StudioModel::GetFileName( void )
  359. {
  360. return m_pModelName;
  361. }
  362. inline IStudioRender *StudioModel::GetStudioRender()
  363. {
  364. return g_pStudioRender;
  365. }
  366. inline bool StudioModel::IsModelLoaded() const
  367. {
  368. return m_MDLHandle != MDLHANDLE_INVALID;
  369. }
  370. inline const matrix3x4_t* StudioModel::BoneToWorld( int nBoneIndex ) const
  371. {
  372. return &m_pBoneToWorld[nBoneIndex];
  373. }
  374. //-----------------------------------------------------------------------------
  375. // Globals
  376. //-----------------------------------------------------------------------------
  377. extern Vector g_vright; // needs to be set to viewer's right in order for chrome to work
  378. extern StudioModel *g_pStudioModel;
  379. extern StudioModel *g_pStudioExtraModel[HLMV_MAX_MERGED_MODELS];
  380. #endif // INCLUDED_STUDIOMODEL