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.

165 lines
5.0 KiB

  1. //====== Copyright � 1996-2004, 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. // Statics
  19. //-----------------------------------------------------------------------------
  20. bool CDmeJoint::sm_bDrawJoints = false;
  21. IMaterial *CDmeJoint::sm_pMatJoint = NULL;
  22. //-----------------------------------------------------------------------------
  23. // Purpose:
  24. //-----------------------------------------------------------------------------
  25. void CDmeJoint::OnConstruction()
  26. {
  27. if ( !g_pMaterialSystem )
  28. return;
  29. if ( !sm_pMatJoint )
  30. {
  31. KeyValues *pVMTKeyValues = new KeyValues( "wireframe" );
  32. pVMTKeyValues->SetInt( "$vertexcolor", 1 );
  33. pVMTKeyValues->SetInt( "$ignorez", 1 );
  34. sm_pMatJoint = g_pMaterialSystem->CreateMaterial( "__DmeJoint", pVMTKeyValues );
  35. if ( sm_pMatJoint )
  36. {
  37. m_MatRefJoint.Init( sm_pMatJoint );
  38. sm_pMatJoint->DecrementReferenceCount(); // CreateMaterial adds a ref, just want the CMaterialReference's
  39. // Cache material now to avoid an unwanted implicit Ref that occurs on first use which is never cleared
  40. g_pMaterialSystem->CacheUsedMaterials();
  41. }
  42. }
  43. else
  44. {
  45. m_MatRefJoint.Init( sm_pMatJoint );
  46. }
  47. }
  48. //-----------------------------------------------------------------------------
  49. //
  50. //-----------------------------------------------------------------------------
  51. void CDmeJoint::OnDestruction()
  52. {
  53. }
  54. //-----------------------------------------------------------------------------
  55. // Activate, deactivate joint drawing
  56. //-----------------------------------------------------------------------------
  57. void CDmeJoint::DrawJointHierarchy( bool bDrawJoints )
  58. {
  59. sm_bDrawJoints = bDrawJoints;
  60. }
  61. //-----------------------------------------------------------------------------
  62. // For rendering joints
  63. //-----------------------------------------------------------------------------
  64. #define AXIS_SIZE 3.0f
  65. void CDmeJoint::DrawJoints( )
  66. {
  67. if ( !g_pMaterialSystem )
  68. return;
  69. int cn = GetChildCount();
  70. // Draw the joint hierarchy
  71. PushDagTransform();
  72. matrix3x4_t shapeToWorld;
  73. GetShapeToWorldTransform( shapeToWorld );
  74. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  75. pRenderContext->MatrixMode( MATERIAL_MODEL );
  76. pRenderContext->LoadMatrix( shapeToWorld );
  77. pRenderContext->Bind( m_MatRefJoint );
  78. IMesh *pMesh = pRenderContext->GetDynamicMesh( );
  79. CMeshBuilder meshBuilder;
  80. meshBuilder.Begin( pMesh, MATERIAL_LINES, 3 + cn );
  81. for ( int ci = 0; ci < cn; ++ci )
  82. {
  83. CDmeJoint *pJoint = CastElement<CDmeJoint>( GetChild( ci ) );
  84. if ( !pJoint )
  85. continue;
  86. Vector vecChildPosition = pJoint->GetTransform()->GetPosition();
  87. meshBuilder.Position3f( 0.0f, 0.0f, 0.0f );
  88. meshBuilder.Color4ub( 128, 128, 128, 255 );
  89. meshBuilder.AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();
  90. meshBuilder.Position3fv( vecChildPosition.Base() );
  91. meshBuilder.Color4ub( 128, 128, 128, 255 );
  92. meshBuilder.AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();
  93. }
  94. meshBuilder.Position3f( 0.0f, 0.0f, 0.0f );
  95. meshBuilder.Color4ub( 255, 0, 0, 255 );
  96. meshBuilder.AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();
  97. meshBuilder.Position3f( AXIS_SIZE, 0.0f, 0.0f );
  98. meshBuilder.Color4ub( 255, 0, 0, 255 );
  99. meshBuilder.AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();
  100. meshBuilder.Position3f( 0.0f, 0.0f, 0.0f );
  101. meshBuilder.Color4ub( 0, 255, 0, 255 );
  102. meshBuilder.AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();
  103. meshBuilder.Position3f( 0.0f, AXIS_SIZE, 0.0f );
  104. meshBuilder.Color4ub( 0, 255, 0, 255 );
  105. meshBuilder.AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();
  106. meshBuilder.Position3f( 0.0f, 0.0f, 0.0f );
  107. meshBuilder.Color4ub( 0, 0, 255, 255 );
  108. meshBuilder.AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();
  109. meshBuilder.Position3f( 0.0f, 0.0f, AXIS_SIZE );
  110. meshBuilder.Color4ub( 0, 0, 255, 255 );
  111. meshBuilder.AdvanceVertexF<VTX_HAVEPOS | VTX_HAVECOLOR, 0>();
  112. meshBuilder.End();
  113. pMesh->Draw();
  114. pRenderContext->MatrixMode( MATERIAL_MODEL );
  115. pRenderContext->LoadIdentity();
  116. PopDagTransform();
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Rendering method for the dag
  120. //-----------------------------------------------------------------------------
  121. void CDmeJoint::Draw( CDmeDrawSettings *pDrawSettings /* = NULL */ )
  122. {
  123. if ( sm_bDrawJoints && IsVisible() )
  124. {
  125. DrawJoints();
  126. }
  127. BaseClass::Draw( pDrawSettings );
  128. }