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.

132 lines
3.7 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =====//
  2. //
  3. // Purpose: Decorator class to make a DME renderable as a MDL
  4. //
  5. //===========================================================================//
  6. #ifndef DMEMDLRENDERABLE_H
  7. #define DMEMDLRENDERABLE_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "toolutils/dmerenderable.h"
  12. #include "movieobjects/dmemdl.h"
  13. #include "movieobjects/dmetransform.h"
  14. #include "datacache/imdlcache.h"
  15. #include "mathlib/mathlib.h"
  16. #include "datamodel/dmehandle.h"
  17. #include "toolutils/enginetools_int.h"
  18. #include "materialsystem/imaterialsystemhardwareconfig.h"
  19. #include "tier3/tier3.h"
  20. //-----------------------------------------------------------------------------
  21. // Deals with the base implementation for turning a Dme into a renderable
  22. //-----------------------------------------------------------------------------
  23. template < class T >
  24. class CDmeMdlRenderable : public CDmeRenderable< T >
  25. {
  26. DEFINE_UNINSTANCEABLE_ELEMENT( CDmeMdlRenderable, CDmeRenderable< T > );
  27. // IClientUnknown implementation.
  28. public:
  29. virtual int GetBody();
  30. virtual int GetSkin();
  31. virtual int DrawModel( int flags, const RenderableInstance_t &instance );
  32. virtual void GetRenderBounds( Vector& mins, Vector& maxs );
  33. void SetModelName( const char *pMDLName );
  34. protected:
  35. CDmeMDL *GetMDL() { return m_hMDL; }
  36. private:
  37. void SetUpLighting( const Vector &vecCenter );
  38. CDmeHandle<CDmeMDL> m_hMDL;
  39. CDmeHandle<CDmeTransform> m_hTransform;
  40. };
  41. //-----------------------------------------------------------------------------
  42. // Construction, destruction
  43. //-----------------------------------------------------------------------------
  44. template < class T >
  45. void CDmeMdlRenderable<T>::OnConstruction()
  46. {
  47. m_hMDL = CreateElement<CDmeMDL>( "MDLRenderable", GetFileId() );
  48. m_hTransform = CreateElement<CDmeTransform>( "MDLTransform", GetFileId() );
  49. }
  50. template < class T >
  51. void CDmeMdlRenderable<T>::OnDestruction()
  52. {
  53. g_pDataModel->DestroyElement( m_hMDL );
  54. g_pDataModel->DestroyElement( m_hTransform );
  55. }
  56. template < class T >
  57. int CDmeMdlRenderable<T>::GetBody()
  58. {
  59. return m_hMDL->m_nBody;
  60. }
  61. template < class T >
  62. int CDmeMdlRenderable<T>::GetSkin()
  63. {
  64. return m_hMDL->m_nSkin;
  65. }
  66. template < class T >
  67. int CDmeMdlRenderable<T>::DrawModel( int flags, const RenderableInstance_t &instance )
  68. {
  69. matrix3x4_t mat;
  70. AngleMatrix( GetRenderAngles(), GetRenderOrigin(), mat );
  71. m_hTransform->SetTransform( mat );
  72. m_hMDL->m_flTime = Plat_FloatTime();
  73. SetUpLighting( GetRenderOrigin() );
  74. bool bIsDrawingInEngine = m_hMDL->IsDrawingInEngine();
  75. m_hMDL->DrawInEngine( true );
  76. m_hMDL->Draw( mat );
  77. m_hMDL->DrawInEngine( bIsDrawingInEngine );
  78. return 1;
  79. }
  80. template < class T >
  81. void CDmeMdlRenderable<T>::GetRenderBounds( Vector& mins, Vector& maxs )
  82. {
  83. m_hMDL->GetBoundingBox( &mins, &maxs );
  84. }
  85. template < class T >
  86. void CDmeMdlRenderable<T>::SetModelName( const char *pMDLRelativePath )
  87. {
  88. if ( pMDLRelativePath )
  89. {
  90. MDLHandle_t hMdl = g_pMDLCache->FindMDL( pMDLRelativePath );
  91. m_hMDL->SetMDL( hMdl );
  92. }
  93. else
  94. {
  95. m_hMDL->SetMDL( MDLHANDLE_INVALID );
  96. }
  97. }
  98. //-----------------------------------------------------------------------------
  99. // Set up lighting conditions
  100. //-----------------------------------------------------------------------------
  101. template < class T >
  102. void CDmeMdlRenderable<T>::SetUpLighting( const Vector &vecCenter )
  103. {
  104. // Set up lighting conditions
  105. MaterialLightingState_t state;
  106. state.m_vecLightingOrigin = vecCenter;
  107. state.m_nLocalLightCount = enginetools->GetLightingConditions( vecCenter, state.m_vecAmbientCube, MATERIAL_MAX_LIGHT_COUNT, state.m_pLocalLightDesc );
  108. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  109. pRenderContext->SetLightingState( state );
  110. }
  111. #endif // DMEMDLRENDERABLE_H