Counter Strike : Global Offensive Source Code
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.

538 lines
16 KiB

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