OpenClonk
StdMeshUpdate Class Reference

#include <StdMeshUpdate.h>

Public Member Functions

 StdMeshUpdate (const StdMesh &old_mesh)
 
void Update (StdMeshInstance *instance, const StdMesh &new_mesh) const
 
const StdMeshGetOldMesh () const
 

Detailed Description

Definition at line 48 of file StdMeshUpdate.h.

Constructor & Destructor Documentation

◆ StdMeshUpdate()

StdMeshUpdate::StdMeshUpdate ( const StdMesh old_mesh)

Definition at line 84 of file StdMeshUpdate.cpp.

84  :
85  OldMesh(&old_mesh), BoneNamesByIndex(OldMesh->GetSkeleton().GetNumBones())
86 {
87  for (unsigned int i = 0; i < OldMesh->GetSkeleton().GetNumBones(); ++i)
88  BoneNamesByIndex[i] = OldMesh->GetSkeleton().GetBone(i).Name;
89 }
StdCopyStrBuf Name
Definition: StdMesh.h:34
const StdMeshSkeleton & GetSkeleton() const
Definition: StdMesh.h:203
const StdMeshBone & GetBone(size_t i) const
Definition: StdMesh.h:116
size_t GetNumBones() const
Definition: StdMesh.h:117

References StdMeshSkeleton::GetBone(), StdMesh::GetSkeleton(), and StdMeshBone::Name.

Here is the call graph for this function:

Member Function Documentation

◆ GetOldMesh()

const StdMesh& StdMeshUpdate::GetOldMesh ( ) const
inline

Definition at line 56 of file StdMeshUpdate.h.

56 { return *OldMesh; }

Referenced by C4DefGraphicsPtrBackupEntry::AssignRemoval(), and C4DefGraphicsPtrBackupEntry::AssignUpdate().

Here is the caller graph for this function:

◆ Update()

void StdMeshUpdate::Update ( StdMeshInstance instance,
const StdMesh new_mesh 
) const

Definition at line 91 of file StdMeshUpdate.cpp.

92 {
93  assert(&instance->GetMesh() == OldMesh);
94 
95  // Update instance to represent new mesh
96  instance->Mesh = &new_mesh;
97  instance->BoneTransforms = std::vector<StdMeshMatrix>(new_mesh.GetSkeleton().GetNumBones(), StdMeshMatrix::Identity());
98  instance->BoneTransformsDirty = true;
99 
100  for (auto & SubMeshInstance : instance->SubMeshInstances)
101  delete SubMeshInstance;
102  instance->SubMeshInstances.resize(new_mesh.GetNumSubMeshes());
103  for (unsigned int i = 0; i < instance->SubMeshInstances.size(); ++i)
104  {
105  instance->SubMeshInstances[i] = new StdSubMeshInstance(*instance, new_mesh.GetSubMesh(i), instance->GetCompletion());
106 
107  // Submeshes are reset to their default material,
108  // so the submesh order is unaltered
109  instance->SubMeshInstancesOrdered = instance->SubMeshInstances;
110 
111  // TODO: We might try to restore the previous mesh material here, using global material manager, maybe iff the number of submesh instances is unchanged.
112  }
113 
114  // Update child bone of attach parent. If the bone does not exist anymore
115  // in the updated mesh, then detach the mesh from its parent
116  if(instance->AttachParent)
117  {
118  if(!instance->AttachParent->SetChildBone(BoneNamesByIndex[instance->AttachParent->ChildBone]))
119  {
120  bool OwnChild = instance->AttachParent->OwnChild;
121  instance->AttachParent->Parent->DetachMesh(instance->AttachParent->Number);
122 
123  // If the attachment owned the child instance then detach procedure
124  // deleted the child instance. In that case we do not want to proceed
125  // with the mesh update procedure.
126  if(OwnChild) return;
127  }
128  }
129 
130  // Update parent bones of attach children. If a bone does not exist in the
131  // updated mesh then detach the mesh from its parent.
132  std::vector<unsigned int> Removal;
133  for(StdMeshInstance::AttachedMeshIter iter = instance->AttachedMeshesBegin(); iter != instance->AttachedMeshesEnd(); ++iter)
134  {
135  if(!(*iter)->SetParentBone(BoneNamesByIndex[(*iter)->ParentBone]))
136  {
137  // Do not detach the mesh right here so we can finish iterating over
138  // all attached meshes first.
139  Removal.push_back((*iter)->Number);
140  }
141  }
142 
143  for(unsigned int i : Removal)
144  instance->DetachMesh(i);
145 
146  // Update custom nodes in the animation tree. Leaf nodes which refer to an animation that
147  // does not exist anymore are removed.
148  for (unsigned int i = instance->AnimationStack.size(); i > 0; --i)
149  if(!UpdateAnimationNode(instance, instance->AnimationStack[i-1]))
150  instance->StopAnimation(instance->AnimationStack[i-1]);
151 }
const StdSubMesh & GetSubMesh(size_t i) const
Definition: StdMesh.h:198
size_t GetNumSubMeshes() const
Definition: StdMesh.h:199
bool SetChildBone(const StdStrBuf &bone)
Definition: StdMesh.cpp:1001
StdMeshInstance * Parent
Definition: StdMesh.h:510
float GetCompletion() const
Definition: StdMesh.h:554
std::vector< StdSubMeshInstance * > SubMeshInstances
Definition: StdMesh.h:655
std::vector< StdSubMeshInstance * > SubMeshInstancesOrdered
Definition: StdMesh.h:656
AnimationNodeList AnimationStack
Definition: StdMesh.h:652
bool DetachMesh(unsigned int number)
Definition: StdMesh.cpp:1395
AttachedMeshIter AttachedMeshesBegin() const
Definition: StdMesh.h:584
void StopAnimation(AnimationNode *node)
Definition: StdMesh.cpp:1190
const StdMesh & GetMesh() const
Definition: StdMesh.h:622
std::vector< StdMeshMatrix > BoneTransforms
Definition: StdMesh.h:653
AttachedMeshIter AttachedMeshesEnd() const
Definition: StdMesh.h:585
bool BoneTransformsDirty
Definition: StdMesh.h:663
AttachedMesh * AttachParent
Definition: StdMesh.h:661
AttachedMeshList::const_iterator AttachedMeshIter
Definition: StdMesh.h:544
const StdMesh * Mesh
Definition: StdMesh.h:647
static StdMeshMatrix Identity()

References StdMeshInstance::AnimationStack, StdMeshInstance::AttachedMeshesBegin(), StdMeshInstance::AttachedMeshesEnd(), StdMeshInstance::AttachParent, StdMeshInstance::BoneTransforms, StdMeshInstance::BoneTransformsDirty, StdMeshInstance::DetachMesh(), StdMeshInstance::GetCompletion(), StdMeshInstance::GetMesh(), StdMeshSkeleton::GetNumBones(), StdMesh::GetNumSubMeshes(), StdMesh::GetSkeleton(), StdMesh::GetSubMesh(), StdMeshMatrix::Identity(), StdMeshInstance::Mesh, StdMeshInstance::AttachedMesh::Number, StdMeshInstance::AttachedMesh::OwnChild, StdMeshInstance::AttachedMesh::Parent, StdMeshInstance::AttachedMesh::SetChildBone(), StdMeshInstance::StopAnimation(), StdMeshInstance::SubMeshInstances, and StdMeshInstance::SubMeshInstancesOrdered.

Referenced by C4DefGraphicsPtrBackupEntry::AssignUpdate().

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: