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