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.

151 lines
4.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Dme version of a joint of a skeletal model (gets compiled into a MDL)
  4. //
  5. //=============================================================================
  6. #include "movieobjects/dmejoint.h"
  7. #include "datamodel/dmelementfactoryhelper.h"
  8. #include "materialsystem/imaterialsystem.h"
  9. #include "materialsystem/imesh.h"
  10. #include "tier1/KeyValues.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( DmeJoint, CDmeJoint );
  17. //-----------------------------------------------------------------------------
  18. // Should I draw joints?
  19. //-----------------------------------------------------------------------------
  20. bool CDmeJoint::s_bDrawJoints = false;
  21. //-----------------------------------------------------------------------------
  22. // Purpose:
  23. //-----------------------------------------------------------------------------
  24. void CDmeJoint::OnConstruction()
  25. {
  26. if ( !g_pMaterialSystem )
  27. return;
  28. KeyValues *pVMTKeyValues = new KeyValues( "wireframe" );
  29. pVMTKeyValues->SetInt( "$vertexcolor", 1 );
  30. pVMTKeyValues->SetInt( "$ignorez", 1 );
  31. m_JointMaterial.Init( "__DmeJointMaterial", pVMTKeyValues );
  32. }
  33. void CDmeJoint::OnDestruction()
  34. {
  35. if ( !g_pMaterialSystem )
  36. return;
  37. m_JointMaterial.Shutdown();
  38. }
  39. //-----------------------------------------------------------------------------
  40. // Activate, deactivate joint drawing
  41. //-----------------------------------------------------------------------------
  42. void CDmeJoint::DrawJointHierarchy( bool bDrawJoints )
  43. {
  44. s_bDrawJoints = bDrawJoints;
  45. }
  46. //-----------------------------------------------------------------------------
  47. // For rendering joints
  48. //-----------------------------------------------------------------------------
  49. #define AXIS_SIZE 3.0f
  50. void CDmeJoint::DrawJoints( )
  51. {
  52. if ( !g_pMaterialSystem )
  53. return;
  54. int cn = GetChildCount();
  55. // Draw the joint hierarchy
  56. PushDagTransform();
  57. matrix3x4_t shapeToWorld;
  58. GetShapeToWorldTransform( shapeToWorld );
  59. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  60. pRenderContext->MatrixMode( MATERIAL_MODEL );
  61. pRenderContext->LoadMatrix( shapeToWorld );
  62. pRenderContext->Bind( m_JointMaterial );
  63. IMesh *pMesh = pRenderContext->GetDynamicMesh( );
  64. CMeshBuilder meshBuilder;
  65. meshBuilder.Begin( pMesh, MATERIAL_LINES, 3 + cn );
  66. for ( int ci = 0; ci < cn; ++ci )
  67. {
  68. CDmeJoint *pJoint = CastElement<CDmeJoint>( GetChild( ci ) );
  69. if ( !pJoint )
  70. continue;
  71. Vector vecChildPosition = pJoint->GetTransform()->GetPosition();
  72. meshBuilder.Position3f( 0.0f, 0.0f, 0.0f );
  73. meshBuilder.Color4ub( 128, 128, 128, 255 );
  74. meshBuilder.AdvanceVertex();
  75. meshBuilder.Position3fv( vecChildPosition.Base() );
  76. meshBuilder.Color4ub( 128, 128, 128, 255 );
  77. meshBuilder.AdvanceVertex();
  78. }
  79. meshBuilder.Position3f( 0.0f, 0.0f, 0.0f );
  80. meshBuilder.Color4ub( 255, 0, 0, 255 );
  81. meshBuilder.AdvanceVertex();
  82. meshBuilder.Position3f( AXIS_SIZE, 0.0f, 0.0f );
  83. meshBuilder.Color4ub( 255, 0, 0, 255 );
  84. meshBuilder.AdvanceVertex();
  85. meshBuilder.Position3f( 0.0f, 0.0f, 0.0f );
  86. meshBuilder.Color4ub( 0, 255, 0, 255 );
  87. meshBuilder.AdvanceVertex();
  88. meshBuilder.Position3f( 0.0f, AXIS_SIZE, 0.0f );
  89. meshBuilder.Color4ub( 0, 255, 0, 255 );
  90. meshBuilder.AdvanceVertex();
  91. meshBuilder.Position3f( 0.0f, 0.0f, 0.0f );
  92. meshBuilder.Color4ub( 0, 0, 255, 255 );
  93. meshBuilder.AdvanceVertex();
  94. meshBuilder.Position3f( 0.0f, 0.0f, AXIS_SIZE );
  95. meshBuilder.Color4ub( 0, 0, 255, 255 );
  96. meshBuilder.AdvanceVertex();
  97. meshBuilder.End();
  98. pMesh->Draw();
  99. pRenderContext->MatrixMode( MATERIAL_MODEL );
  100. pRenderContext->LoadIdentity();
  101. PopDagTransform();
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Rendering method for the dag
  105. //-----------------------------------------------------------------------------
  106. void CDmeJoint::Draw( CDmeDrawSettings *pDrawSettings /* = NULL */ )
  107. {
  108. if ( s_bDrawJoints && IsVisible() )
  109. {
  110. DrawJoints();
  111. }
  112. BaseClass::Draw( pDrawSettings );
  113. }