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.

157 lines
4.6 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "movieobjects/dmemorphoperator.h"
  7. #include "movieobjects/dmevertexdata.h"
  8. #include "movieobjects/dmemesh.h"
  9. #include "movieobjects_interfaces.h"
  10. #include "datamodel/dmelementfactoryhelper.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( DmeMorphOperator, CDmeMorphOperator );
  17. //-----------------------------------------------------------------------------
  18. // Purpose:
  19. //-----------------------------------------------------------------------------
  20. void CDmeMorphOperator::OnConstruction()
  21. {
  22. m_mesh.Init( this, "mesh", FATTRIB_HAS_CALLBACK );
  23. m_deltaStateWeights.Init( this, "deltaStateWeights", FATTRIB_MUSTCOPY );
  24. m_baseStateName.Init( this, "baseStateName", FATTRIB_TOPOLOGICAL );
  25. }
  26. void CDmeMorphOperator::OnDestruction()
  27. {
  28. }
  29. //-----------------------------------------------------------------------------
  30. // accessors
  31. //-----------------------------------------------------------------------------
  32. uint CDmeMorphOperator::NumDeltaStateWeights()
  33. {
  34. return m_deltaStateWeights.Count();
  35. }
  36. CDmElement *CDmeMorphOperator::GetDeltaStateWeight( uint i )
  37. {
  38. return m_deltaStateWeights[ i ];
  39. }
  40. CDmeMesh *CDmeMorphOperator::GetMesh()
  41. {
  42. return m_mesh.GetElement();
  43. }
  44. //-----------------------------------------------------------------------------
  45. // This function gets called whenever an attribute changes
  46. //-----------------------------------------------------------------------------
  47. void CDmeMorphOperator::OnAttributeChanged( CDmAttribute *pAttribute )
  48. {
  49. if ( pAttribute == m_mesh.GetAttribute() )
  50. {
  51. CDmeMesh *pMesh = GetMesh();
  52. if ( pMesh )
  53. {
  54. #if 0 // right now, the file already contains these weights, and re-creating them breaks the channel connections
  55. m_deltaStateWeights.RemoveAll();
  56. uint dn = pMesh->NumDeltaStates();
  57. for ( uint di = 0; di < dn; ++di )
  58. {
  59. CDmElement *pDeltaState = pMesh->GetDeltaState( di );
  60. const char *name = pDeltaState->GetName();
  61. CDmElement *pDeltaWeight = CreateElement< CDmElement >( name, GetFileId() );
  62. pDeltaWeight->SetAttributeValue( "weight", 0.0f );
  63. m_deltaStateWeights.AddToTail( pDeltaWeight->GetHandle() );
  64. }
  65. #endif
  66. }
  67. }
  68. }
  69. //-----------------------------------------------------------------------------
  70. // Purpose:
  71. //-----------------------------------------------------------------------------
  72. void CDmeMorphOperator::Operate()
  73. {
  74. CDmeMesh *mesh = GetMesh();
  75. uint mn = NumDeltaStateWeights();
  76. for ( uint mi = 0; mi < mn; ++mi )
  77. {
  78. CDmElement *pDeltaState = GetDeltaStateWeight( mi );
  79. const char *deltaName = pDeltaState->GetName();
  80. float deltaWeight = pDeltaState->GetValue< float >( "weight" );
  81. int di = mesh->FindDeltaStateIndex( deltaName );
  82. if ( di != -1 )
  83. {
  84. mesh->SetDeltaStateWeight( di, MESH_DELTA_WEIGHT_NORMAL, deltaWeight );
  85. }
  86. else
  87. {
  88. Msg( "MorphOperator::Operate: invalid delta state name: %s\n", deltaName );
  89. }
  90. }
  91. }
  92. // hack to avoid MSVC complaining about multiply defined symbols
  93. namespace MorphOp
  94. {
  95. void AddAttr( CUtlVector< CDmAttribute * > &attrs, CDmAttribute *pAttr )
  96. {
  97. if ( pAttr == NULL )
  98. return;
  99. attrs.AddToTail( pAttr );
  100. }
  101. void AddVertexAttributes( CUtlVector< CDmAttribute * > &attrs, CDmElement *pObject )
  102. {
  103. AddAttr( attrs, pObject->GetAttribute( "coordinates" ) );
  104. AddAttr( attrs, pObject->GetAttribute( "normals" ) );
  105. AddAttr( attrs, pObject->GetAttribute( "textureCoordinates" ) );
  106. // TODO - add colors, occlusionFactors, boneIndices*, boneWeights*, tangents
  107. }
  108. };
  109. using namespace MorphOp;
  110. void CDmeMorphOperator::GetInputAttributes( CUtlVector< CDmAttribute * > &attrs )
  111. {
  112. uint nWeights = NumDeltaStateWeights();
  113. for ( uint wi = 0; wi < nWeights; ++wi )
  114. {
  115. CDmElement *pDelta = GetDeltaStateWeight( wi );
  116. AddAttr( attrs, pDelta->GetAttribute( "weight" ) );
  117. }
  118. CDmeMesh *pMesh = GetMesh();
  119. CDmeVertexData *pBaseState = pMesh->FindBaseState( m_baseStateName.Get() );
  120. AddVertexAttributes( attrs, pBaseState );
  121. uint nDeltas = pMesh->DeltaStateCount();
  122. for ( uint di = 0; di < nDeltas; ++di )
  123. {
  124. CDmElement *pDeltaState = pMesh->GetDeltaState( di );
  125. AddAttr( attrs, pDeltaState->GetAttribute( "indices" ) );
  126. AddVertexAttributes( attrs, pDeltaState );
  127. }
  128. }
  129. void CDmeMorphOperator::GetOutputAttributes( CUtlVector< CDmAttribute * > &attrs )
  130. {
  131. AddVertexAttributes( attrs, GetMesh() );
  132. }