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.

171 lines
4.5 KiB

  1. //========= Copyright 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 );
  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 )
  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. Vector vecAmbient[6];
  106. Vector4D vecAmbient4D[6];
  107. LightDesc_t desc[2];
  108. int nLightCount = enginetools->GetLightingConditions( vecCenter, vecAmbient, 2, desc );
  109. int nMaxLights = g_pMaterialSystemHardwareConfig->MaxNumLights();
  110. if( nLightCount > nMaxLights )
  111. {
  112. nLightCount = nMaxLights;
  113. }
  114. int i;
  115. for( i = 0; i < 6; i++ )
  116. {
  117. VectorCopy( vecAmbient[i], vecAmbient4D[i].AsVector3D() );
  118. vecAmbient4D[i][3] = 1.0f;
  119. }
  120. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  121. pRenderContext->SetAmbientLightCube( vecAmbient4D );
  122. for( i = 0; i < nLightCount; i++ )
  123. {
  124. LightDesc_t *pLight = &desc[i];
  125. pLight->m_Flags = 0;
  126. if( pLight->m_Attenuation0 != 0.0f )
  127. {
  128. pLight->m_Flags |= LIGHTTYPE_OPTIMIZATIONFLAGS_HAS_ATTENUATION0;
  129. }
  130. if( pLight->m_Attenuation1 != 0.0f )
  131. {
  132. pLight->m_Flags |= LIGHTTYPE_OPTIMIZATIONFLAGS_HAS_ATTENUATION1;
  133. }
  134. if( pLight->m_Attenuation2 != 0.0f )
  135. {
  136. pLight->m_Flags |= LIGHTTYPE_OPTIMIZATIONFLAGS_HAS_ATTENUATION2;
  137. }
  138. pRenderContext->SetLight( i, desc[i] );
  139. }
  140. for( ; i < nMaxLights; i++ )
  141. {
  142. LightDesc_t disableDesc;
  143. disableDesc.m_Type = MATERIAL_LIGHT_DISABLE;
  144. pRenderContext->SetLight( i, disableDesc );
  145. }
  146. }
  147. #endif // DMEMDLRENDERABLE_H