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.

264 lines
7.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "datamodel/dmelementfactoryhelper.h"
  7. #include "tier1/KeyValues.h"
  8. #include "tier1/utlrbtree.h"
  9. #include "movieobjects/dmedag.h"
  10. #include "movieobjects/dmedrawsettings.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. //-----------------------------------------------------------------------------
  14. // Expose this class to the scene database
  15. //-----------------------------------------------------------------------------
  16. IMPLEMENT_ELEMENT_FACTORY( DmeDrawSettings, CDmeDrawSettings );
  17. //-----------------------------------------------------------------------------
  18. // Statics
  19. //-----------------------------------------------------------------------------
  20. bool CDmeDrawSettings::s_bWireframeMaterialInitialized( false );
  21. CMaterialReference CDmeDrawSettings::s_WireframeMaterial;
  22. bool CDmeDrawSettings::s_bWireframeOnShadedMaterialInitialized( false );
  23. CMaterialReference CDmeDrawSettings::s_WireframeOnShadedMaterial;
  24. bool CDmeDrawSettings::s_bFlatGrayMaterial( false );
  25. CMaterialReference CDmeDrawSettings::s_FlatGrayMaterial;
  26. bool CDmeDrawSettings::s_bUnlitGrayMaterial( false );
  27. CMaterialReference CDmeDrawSettings::s_UnlitGrayMaterial;
  28. CUtlRBTree< CUtlSymbol > CDmeDrawSettings::s_KnownDrawableTypes;
  29. //-----------------------------------------------------------------------------
  30. //
  31. //-----------------------------------------------------------------------------
  32. void CDmeDrawSettings::OnConstruction()
  33. {
  34. if ( s_KnownDrawableTypes.Count() == 0 )
  35. {
  36. BuildKnownDrawableTypes();
  37. }
  38. SetDefLessFunc< CUtlRBTree< CUtlSymbol > >( m_NotDrawable );
  39. m_NotDrawable.RemoveAll();
  40. m_DrawType.InitAndSet( this, "drawType", static_cast< int >( DRAW_SMOOTH ) );
  41. m_bBackfaceCulling.InitAndSet( this, "backfaceCulling", true );
  42. m_bWireframeOnShaded.InitAndSet( this, "wireframeOnShaded", false );
  43. m_bXRay.InitAndSet( this, "xray", false );
  44. m_bGrayShade.InitAndSet( this, "grayShade", false );
  45. m_bNormals.InitAndSet( this, "normals", false );
  46. m_NormalLength.InitAndSet( this, "normalLength", 1.0 );
  47. m_Color.InitAndSet( this, "color", Color( 0, 0, 0, 1 ) );
  48. m_bDeltaHighlight.InitAndSet( this, "highlightDeltas", false );
  49. m_flHighlightSize.InitAndSet( this, "highlightSize", 1.5f );
  50. m_cHighlightColor.InitAndSet( this, "highlightColor", Color( 0xff, 0x14, 0x93, 0xff ) ); // Deep Pink
  51. m_IsAMaterialBound = false;
  52. }
  53. //-----------------------------------------------------------------------------
  54. //
  55. //-----------------------------------------------------------------------------
  56. void CDmeDrawSettings::OnDestruction()
  57. {
  58. }
  59. //-----------------------------------------------------------------------------
  60. //
  61. //-----------------------------------------------------------------------------
  62. void CDmeDrawSettings::Resolve()
  63. {
  64. }
  65. //-----------------------------------------------------------------------------
  66. // Wireframe
  67. //-----------------------------------------------------------------------------
  68. void CDmeDrawSettings::BindWireframe()
  69. {
  70. if ( !s_bWireframeMaterialInitialized )
  71. {
  72. s_bWireframeMaterialInitialized = true;
  73. KeyValues *pKeyValues = new KeyValues( "wireframe" );
  74. pKeyValues->SetInt( "$decal", 0 );
  75. pKeyValues->SetInt( "$vertexcolor", 1 );
  76. pKeyValues->SetInt( "$ignorez", 1 );
  77. s_WireframeMaterial.Init( "__DmeWireframe", pKeyValues );
  78. }
  79. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  80. pRenderContext->Bind( s_WireframeMaterial );
  81. m_IsAMaterialBound = true;
  82. }
  83. //-----------------------------------------------------------------------------
  84. // Wireframe as a decal
  85. //-----------------------------------------------------------------------------
  86. void CDmeDrawSettings::BindWireframeOnShaded()
  87. {
  88. if ( !s_bWireframeOnShadedMaterialInitialized )
  89. {
  90. s_bWireframeOnShadedMaterialInitialized = true;
  91. KeyValues *pKeyValues = new KeyValues( "wireframe" );
  92. pKeyValues->SetInt( "$decal", 0 );
  93. pKeyValues->SetInt( "$vertexcolor", 1 );
  94. pKeyValues->SetInt( "$ignorez", 0 );
  95. s_WireframeOnShadedMaterial.Init( "__DmeWireframeOnShaded", pKeyValues );
  96. }
  97. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  98. pRenderContext->Bind( s_WireframeOnShadedMaterial );
  99. m_IsAMaterialBound = true;
  100. }
  101. //-----------------------------------------------------------------------------
  102. // Flat Gray Shaded
  103. //-----------------------------------------------------------------------------
  104. void CDmeDrawSettings::BindGray()
  105. {
  106. if ( !s_bFlatGrayMaterial )
  107. {
  108. s_bFlatGrayMaterial = true;
  109. KeyValues *pKeyValues = new KeyValues( "VertexLitGeneric" );
  110. pKeyValues->SetInt( "$model", 1 );
  111. s_FlatGrayMaterial.Init( "__DmeFlatGray", pKeyValues );
  112. }
  113. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  114. pRenderContext->Bind( s_FlatGrayMaterial );
  115. m_IsAMaterialBound = true;
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Flat Gray Shaded
  119. //-----------------------------------------------------------------------------
  120. void CDmeDrawSettings::BindUnlitGray()
  121. {
  122. if ( !s_bUnlitGrayMaterial )
  123. {
  124. s_bUnlitGrayMaterial = true;
  125. KeyValues *pKeyValues = new KeyValues( "UnlitGeneric" );
  126. pKeyValues->SetInt( "$model", 1 );
  127. pKeyValues->SetInt( "$vertexcolor", 1 );
  128. s_UnlitGrayMaterial.Init( "__DmeUnlitGray", pKeyValues );
  129. }
  130. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  131. pRenderContext->Bind( s_UnlitGrayMaterial );
  132. m_IsAMaterialBound = true;
  133. }
  134. //-----------------------------------------------------------------------------
  135. //
  136. //-----------------------------------------------------------------------------
  137. bool CDmeDrawSettings::Drawable( CDmElement *pElement )
  138. {
  139. if ( m_NotDrawable.IsValidIndex( m_NotDrawable.Find( pElement->GetType() ) ) )
  140. return false;
  141. return true;
  142. }
  143. //-----------------------------------------------------------------------------
  144. //
  145. //-----------------------------------------------------------------------------
  146. void CDmeDrawSettings::BuildKnownDrawableTypes()
  147. {
  148. SetDefLessFunc< CUtlRBTree< CUtlSymbol > >( s_KnownDrawableTypes );
  149. s_KnownDrawableTypes.RemoveAll();
  150. s_KnownDrawableTypes.InsertIfNotFound( g_pDataModel->GetSymbol( "DmeMesh" ) );
  151. s_KnownDrawableTypes.InsertIfNotFound( g_pDataModel->GetSymbol( "DmeJoint" ) );
  152. s_KnownDrawableTypes.InsertIfNotFound( g_pDataModel->GetSymbol( "DmeModel" ) );
  153. s_KnownDrawableTypes.InsertIfNotFound( g_pDataModel->GetSymbol( "DmeAttachment" ) );
  154. m_NotDrawable.RemoveAll();
  155. }
  156. //-----------------------------------------------------------------------------
  157. //
  158. //-----------------------------------------------------------------------------
  159. void CDmeDrawSettings::DrawDag( CDmeDag *pDag )
  160. {
  161. if ( !pDag )
  162. return;
  163. m_vHighlightPoints.RemoveAll();
  164. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  165. m_IsAMaterialBound = false;
  166. if ( GetDeltaHighlight() )
  167. {
  168. BindUnlitGray();
  169. }
  170. else
  171. {
  172. if ( !Shaded() )
  173. {
  174. BindWireframe();
  175. }
  176. else if ( GetGrayShade() )
  177. {
  178. BindGray();
  179. }
  180. }
  181. pDag->Draw( this );
  182. m_IsAMaterialBound = false;
  183. if ( GetDeltaHighlight() || ( GetWireframeOnShaded() && Shaded() ) )
  184. {
  185. VMatrix m;
  186. pRenderContext->GetMatrix( MATERIAL_PROJECTION, &m );
  187. /* Extract the near and far clipping plane values from projection matrix
  188. float c = m[ 2 ][ 2 ];
  189. float d = m[ 2 ][ 3 ];
  190. const float near = d / ( c - 1.0f );
  191. const float far = d / ( c + 1.0f );
  192. */
  193. const float zBias = 0.00025;
  194. m[ 2 ][ 2 ] += zBias;
  195. m[ 2 ][ 3 ] += zBias;
  196. pRenderContext->MatrixMode( MATERIAL_PROJECTION );
  197. pRenderContext->PushMatrix();
  198. pRenderContext->LoadMatrix( m );
  199. BindWireframeOnShaded();
  200. PushDrawType();
  201. SetDrawType( CDmeDrawSettings::DRAW_WIREFRAME );
  202. pDag->Draw( this );
  203. PopDrawType();
  204. pRenderContext->MatrixMode( MATERIAL_PROJECTION );
  205. pRenderContext->PopMatrix();
  206. }
  207. }