Skip to content

Commit

Permalink
Removing node-to-animation references because animations already cont…
Browse files Browse the repository at this point in the history
…ain a reference to nodes.
  • Loading branch information
hamsham committed Feb 9, 2022
1 parent 4ece103 commit d219d89
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 69 deletions.
2 changes: 1 addition & 1 deletion softlight/include/softlight/SL_Animation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ class SL_Animation
* An unsigned integer, containing the index of the AnimationChannel in
* the input node's std::vector<AnimationChannel> to use for animation.
*/
void add_channel(const SL_SceneNode& node, const size_t nodeTrackId) noexcept;
void add_channel(const size_t sceneChannelId, const size_t nodeTrackId, const size_t nodeId) noexcept;

/**
* Remove a single Animation channel from *this.
Expand Down
2 changes: 1 addition & 1 deletion softlight/include/softlight/SL_SceneFileLoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ class SL_SceneFileLoader
* @return The ID of a node who's track was successfully imported,
* UINT_MAX if not.
*/
unsigned import_animation_track(
size_t import_animation_track(
const aiNodeAnim* const pInAnim,
SL_AnimationChannel& outAnim,
const SL_AnimPrecision animDuration
Expand Down
18 changes: 14 additions & 4 deletions softlight/include/softlight/SL_SceneGraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,8 @@ class SL_SceneGraph
*
* @param nodeId
* The array index of a node being deleted.
*
* @param animId
* The array index of the animation being deleted.
*/
void delete_node_animation_data(const size_t nodeId, const size_t animId) noexcept;
void delete_node_animation_data(const size_t nodeId) noexcept;

public: // member functions
/**
Expand Down Expand Up @@ -444,6 +441,19 @@ class SL_SceneGraph
* succeeded, SCENE_NODE_ROOT_ID if not.
*/
size_t import(SL_SceneGraph& inGraph) noexcept;



#if 0
size_t insert_mesh(const SL_Mesh& m, const SL_BoundingBox& meshBounds) noexcept;

size_t insert_mesh_node(
size_t parentId,
const char* name,
const size_t numSubMeshes,
const size_t* subMeshIds,
const SL_Transform& transform) noexcept;
#endif
};


Expand Down
7 changes: 0 additions & 7 deletions softlight/include/softlight/SL_SceneNode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,6 @@ struct alignas(sizeof(size_t)) SL_SceneNode final
* Bone nodes can use this to access the "mBones" member of a scene graph.
*/
size_t dataId;

/**
* This member represents an index into the parent SceneGraph's "nodeAnims"
* member. Use this to retrieve a single animation track related to the
* current node.
*/
size_t animListId;
};


Expand Down
6 changes: 3 additions & 3 deletions softlight/src/SL_Animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ size_t SL_Animation::size() const noexcept
/*-------------------------------------
* Add a node's animation track to *this
-------------------------------------*/
void SL_Animation::add_channel(const SL_SceneNode& node, const size_t nodeTrackId) noexcept
void SL_Animation::add_channel(const size_t sceneChannelId, const size_t nodeTrackId, const size_t nodeId) noexcept
{
mChannelIds.push_back(node.animListId);
mChannelIds.push_back(sceneChannelId);
mTrackIds.push_back(nodeTrackId);
mTransformIds.push_back(node.nodeId);
mTransformIds.push_back(nodeId);
}


Expand Down
29 changes: 14 additions & 15 deletions softlight/src/SL_SceneFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,8 @@ bool SL_SceneFileLoader::import_animations(const aiScene* const pScene) noexcept

SL_AlignedVector<SL_AlignedVector<SL_AnimationChannel>>& allChannels = graph.mNodeAnims;

std::unordered_map<size_t, size_t> nodesToAnimChannels;

for (unsigned i = 0; i < totalAnimations; ++i)
{
const aiAnimation* const pInAnim = pAnimations[i];
Expand All @@ -1570,32 +1572,29 @@ bool SL_SceneFileLoader::import_animations(const aiScene* const pScene) noexcept
{
SL_AnimationChannel track;
const aiNodeAnim* const pInTrack = pInAnim->mChannels[c];
const unsigned nodeId = import_animation_track(pInTrack, track, anim.duration());
const size_t nodeId = import_animation_track(pInTrack, track, anim.duration());

if (nodeId == UINT_MAX)
if (nodeId == SCENE_NODE_ROOT_ID)
{
// failing to load an Animation track is not an error.
ret = false;
continue;
}

// Grab the scene node which contains the data ID to map with a
// transformation and animation channel
SL_SceneNode& node = graph.mNodes[nodeId];

// If a list of animation tracks doesn't exist for the current node
// then be sure to add it.
if (node.animListId == SCENE_NODE_ROOT_ID)
// transformation and animation channel. If a list of animation
// tracks doesn't exist for the current node then be sure to add it
if (!nodesToAnimChannels.count(nodeId))
{
node.animListId = allChannels.size();
nodesToAnimChannels[nodeId] = allChannels.size();
allChannels.emplace_back(SL_AlignedVector<SL_AnimationChannel>{});
}

SL_AlignedVector<SL_AnimationChannel>& nodeChannels = allChannels[node.animListId];
SL_AlignedVector<SL_AnimationChannel>& nodeChannels = allChannels[nodesToAnimChannels[nodeId]];
nodeChannels.emplace_back(std::move(track));

// Add the node's imported track to the current animation
anim.add_channel(node, nodeChannels.size() - 1);
anim.add_channel(nodesToAnimChannels[nodeId], nodeChannels.size() - 1, nodeId);
}

std::cout
Expand Down Expand Up @@ -1623,7 +1622,7 @@ bool SL_SceneFileLoader::import_animations(const aiScene* const pScene) noexcept
/*-------------------------------------
* Import a single Animation track
-------------------------------------*/
unsigned SL_SceneFileLoader::import_animation_track(
size_t SL_SceneFileLoader::import_animation_track(
const aiNodeAnim* const pInAnim,
SL_AnimationChannel& outAnim,
const SL_AnimPrecision animDuration
Expand All @@ -1633,7 +1632,7 @@ unsigned SL_SceneFileLoader::import_animation_track(
const unsigned rotFrames = pInAnim->mNumRotationKeys;
const SL_AlignedVector<std::string>& nodeNames = mPreloader.mSceneData.mNodeNames;
const char* const pInName = pInAnim->mNodeName.C_Str();
unsigned nodeId = 0;
size_t nodeId = 0;

// Locate the node associated with the current animation track.
for (; nodeId < nodeNames.size(); ++nodeId)
Expand All @@ -1648,13 +1647,13 @@ unsigned SL_SceneFileLoader::import_animation_track(
{
std::cerr << "\tError: Unable to locate the animation track for a scene node: " << pInAnim << std::endl;
outAnim.clear();
return UINT_MAX;
return SCENE_NODE_ROOT_ID;
}

if (!outAnim.size(posFrames, sclFrames, rotFrames))
{
std::cout << "Unable to import the Animation \"" << pInAnim->mNodeName.C_Str() << "\"." << std::endl;
return UINT_MAX;
return SCENE_NODE_ROOT_ID;
}

SL_AnimationKeyListVec3& outPosFrames = outAnim.mPosFrames;
Expand Down
98 changes: 62 additions & 36 deletions softlight/src/SL_SceneGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,10 @@ void SL_SceneGraph::delete_camera_node_data(const size_t nodeDataId) noexcept
/*-------------------------------------
* SL_Animation Deletion
-------------------------------------*/
void SL_SceneGraph::delete_node_animation_data(const size_t nodeId, const size_t animId) noexcept
void SL_SceneGraph::delete_node_animation_data(const size_t nodeId) noexcept
{
// Remove all animation channels associated with the current node.
for (size_t i = mAnimations.size(); i-- > 0;)
for (size_t i = mAnimations.size(); i--;)
{

// Animations contain transformation IDs which may need to be
Expand All @@ -343,26 +343,14 @@ void SL_SceneGraph::delete_node_animation_data(const size_t nodeId, const size_t
SL_Animation& currentAnim = mAnimations[i];

// friendly class member manipulation
std::vector<size_t>& currentAnimIds = currentAnim.mChannelIds;
std::vector<size_t>& currentTransIds = currentAnim.mTransformIds;

// Reduce the animation ID of all animations in *this.
for (size_t j = currentAnimIds.size(); j-- > 0;)
for (size_t j = currentTransIds.size(); j--;)
{
if (currentTransIds[j] == nodeId)
{
currentAnim.erase(j);
continue;
}

if (currentTransIds[j] > nodeId)
{
--currentTransIds[j];
}

if (animId != SL_SceneNodeProp::SCENE_NODE_ROOT_ID && currentAnimIds[j] > animId)
{
--currentAnimIds[j];
}
}

Expand All @@ -372,12 +360,6 @@ void SL_SceneGraph::delete_node_animation_data(const size_t nodeId, const size_t
mAnimations.erase(mAnimations.begin() + i);
}
}

// SL_Animation Channels
if (animId != SL_SceneNodeProp::SCENE_NODE_ROOT_ID)
{
mNodeAnims.erase(mNodeAnims.begin() + animId);
}
}

/*-------------------------------------
Expand Down Expand Up @@ -432,7 +414,6 @@ size_t SL_SceneGraph::delete_node(const size_t nodeIndex) noexcept
const SL_SceneNode& n = mNodes[nodeIndex];
const SL_SceneNodeType typeId = n.type;
const size_t dataId = n.dataId;
const size_t animId = n.animListId;

LS_DEBUG_ASSERT(nodeIndex == n.nodeId);

Expand Down Expand Up @@ -464,7 +445,7 @@ size_t SL_SceneGraph::delete_node(const size_t nodeIndex) noexcept
mNodeNames.erase(mNodeNames.begin() + nodeIndex);

// early exit in case there are no animations tied to the current node.
delete_node_animation_data(nodeIndex, animId);
delete_node_animation_data(nodeIndex);

// Decrement all node ID and data ID indices that are greater than those in
// the current node. Also deal with the last bit of transformation data in
Expand All @@ -474,7 +455,6 @@ size_t SL_SceneGraph::delete_node(const size_t nodeIndex) noexcept
SL_SceneNode& nextNode = mNodes[i];
const SL_SceneNodeType nextType = nextNode.type;
size_t& nextDataId = nextNode.dataId;
size_t& nextAnimId = nextNode.animListId;
const size_t nextParentId = mNodeParentIds[i];

// Placing assertion here because nodeIds must never equate to the
Expand All @@ -498,13 +478,6 @@ size_t SL_SceneGraph::delete_node(const size_t nodeIndex) noexcept
{
--nextDataId;
}

// decrement the animation ID from all nodes with a value greater than
// the current node's
if (nextAnimId > animId && nextAnimId != SL_SceneNodeProp::SCENE_NODE_ROOT_ID)
{
--nextAnimId;
}
}

return numDeleted;
Expand Down Expand Up @@ -721,9 +694,6 @@ bool SL_SceneGraph::copy_node(const size_t nodeIndex) noexcept
mNodeParentIds[dupe] += displacement;
}

// we're copying nodes, not animations
mNodes[dupe].animListId = SL_SceneNodeProp::SCENE_NODE_ROOT_ID;

LS_LOG_MSG("Copying node parent from ", mNodeParentIds[orig], " to ", mNodeParentIds[dupe]);

// Enumerate how much node data is getting moved too
Expand Down Expand Up @@ -996,8 +966,6 @@ size_t SL_SceneGraph::import(SL_SceneGraph& inGraph) noexcept

n.dataId += mNumNodeMeshes.size();
}

n.animListId += mNodeAnims.size();
}
std::move(inGraph.mNodes.begin(), inGraph.mNodes.end(), std::back_inserter(mNodes));
inGraph.mNodes.clear();
Expand Down Expand Up @@ -1067,3 +1035,61 @@ size_t SL_SceneGraph::import(SL_SceneGraph& inGraph) noexcept

return baseNodeId;
}



/*-------------------------------------
*
-------------------------------------*/
#if 0
size_t SL_SceneGraph::insert_mesh(const SL_Mesh& m, const SL_BoundingBox& meshBounds) noexcept
{
LS_DEBUG_ASSERT(mMeshes.size() == mMeshBounds.size());

mMeshes.push_back(m);
mMeshBounds.push_back(meshBounds);

return mMeshes.size()-1;
}



/*-------------------------------------
*
-------------------------------------*/
size_t SL_SceneGraph::insert_mesh_node(
size_t parentId,
const char* name,
const size_t numSubMeshes,
const size_t* subMeshIds,
const SL_Transform& transform) noexcept
{
LS_ASSERT(parentId == SCENE_NODE_ROOT_ID || parentId < mNodes.size());
LS_ASSERT(name != nullptr && name[0] != '\0');
LS_ASSERT(numSubMeshes != 0);

mNodeMeshes.emplace_back(new(std::nothrow) size_t[numSubMeshes]);
LS_ASSERT(mNodeMeshes.back().get() != nullptr);

size_t* const pSubMeshIds = mNodeMeshes.back().get();
for (size_t i = 0; i < numSubMeshes; ++i)
{
pSubMeshIds[i] = subMeshIds[i];
}

mNumNodeMeshes.push_back(numSubMeshes);

SL_SceneNode node;
node.nodeId = mNodes.size();
node.dataId = mNodeMeshes.size();
node.type = SL_SceneNodeType::NODE_TYPE_MESH;

mNodes.push_back(node);
mNodeNames.emplace_back(name);
mBaseTransforms.push_back(transform.transform());
mCurrentTransforms.push_back(transform);
mModelMatrices.push_back(transform.transform());

return mNodes.size()-1;
}
#endif
1 change: 0 additions & 1 deletion softlight/src/SL_SceneNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ void sl_reset(SL_SceneNode& n) noexcept
n.type = SL_SceneNodeType::NODE_TYPE_EMPTY;
n.nodeId = SL_SceneNodeProp::SCENE_NODE_ROOT_ID;
n.dataId = SL_SceneNodeProp::SCENE_NODE_ROOT_ID;
n.animListId = SL_SceneNodeProp::SCENE_NODE_ROOT_ID;
}
3 changes: 2 additions & 1 deletion softlight/src/SL_TextMeshLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,8 @@ unsigned SL_TextMeshLoader::allocate_cpu_data(
}

{
sceneData.mNodes.push_back(SL_SceneNode{SL_SceneNodeType::NODE_TYPE_MESH, 0, 0, SCENE_NODE_ROOT_ID});
sceneData.mNodes.push_back(SL_SceneNode{SL_SceneNodeType::NODE_TYPE_MESH, 0, 0});
sceneData.mNodeParentIds.push_back(SCENE_NODE_ROOT_ID);
sceneData.mBaseTransforms.emplace_back(math::mat4{1.f});
sceneData.mCurrentTransforms.emplace_back(SL_Transform{math::mat4{1.f}, SL_TRANSFORM_TYPE_MODEL});
sceneData.mModelMatrices.emplace_back(math::mat4{1.f});
Expand Down

0 comments on commit d219d89

Please sign in to comment.