diff --git a/LCSEngine/AnimationComponent.cpp b/LCSEngine/AnimationComponent.cpp index 0b9e41e..81454b8 100644 --- a/LCSEngine/AnimationComponent.cpp +++ b/LCSEngine/AnimationComponent.cpp @@ -20,6 +20,18 @@ void AnimationComponent::drawGUI() if (ImGui::CollapsingHeader("Animation")) { ImGui::Checkbox("Active", &isEnable); + + ImGui::PushID("RootNode"); + ImGui::InputText("", &rootNodeBones[0], IM_ARRAYSIZE(rootNodeBones)); + ImGui::PopID(); + ImGui::PushID("Button RootNode"); + if (ImGui::Button("Set Root Node Bones")) + { + setRootNodeBones(); + fillJoints(); + } + ImGui::PopID(); + ImGui::Text("Animation: "); ImGui::SameLine(0); if (ImGui::BeginMenu(animationName.c_str())) { @@ -117,4 +129,54 @@ void AnimationComponent::blendAnimation() App->animations->blendTo(idAnim, animationName.c_str(), unsigned int(blendTime*1000)); currentAnimationName = animationName; } +} + +void AnimationComponent::setRootNodeBones() +{ + rootBone = nullptr; + queue nodes; + + for (vector::iterator it = gameObject->children.begin(); it != gameObject->children.end(); ++it) + { + nodes.push(*it); + } + + while (!nodes.empty()) + { + GameObject* node = nodes.front(); + nodes.pop(); + + if (node->name == rootNodeBones) + { + rootBone = node; + return; + } + + for (vector::iterator it = node->children.begin(); it != node->children.end(); ++it) + { + nodes.push(*it); + } + } +} + +void AnimationComponent::fillJoints() +{ + queue gameObjects; + for (vector::const_iterator it = rootBone->children.begin(); it != rootBone->children.end(); ++it) + { + gameObjects.push(*it); + } + + while (!gameObjects.empty()) + { + GameObject* node = gameObjects.front(); + gameObjects.pop(); + + joints[node->name] = node; + + for (vector::iterator it = node->children.begin(); it != node->children.end(); ++it) + { + gameObjects.push(*it); + } + } } \ No newline at end of file diff --git a/LCSEngine/AnimationComponent.h b/LCSEngine/AnimationComponent.h index 1f21916..b8bc6d8 100644 --- a/LCSEngine/AnimationComponent.h +++ b/LCSEngine/AnimationComponent.h @@ -22,6 +22,7 @@ class AnimationComponent : public Component std::map joints; std::string animationName = ""; std::string currentAnimationName = ""; + char rootNodeBones[128] = ""; unsigned int idAnim = 0; float blendTime = 0.f; @@ -30,6 +31,8 @@ class AnimationComponent : public Component void playAnimation(); void stopAnimation(); void blendAnimation(); + void setRootNodeBones(); + void fillJoints(); }; #endif //__ANIMATIONCOMPONENT_H__ diff --git a/LCSEngine/GameObject.cpp b/LCSEngine/GameObject.cpp index 7393aa1..b772e3a 100644 --- a/LCSEngine/GameObject.cpp +++ b/LCSEngine/GameObject.cpp @@ -309,7 +309,7 @@ void GameObject::draw() } }*/ - renderData data; + /*renderData data; data.id = id; data.idVertVBO = idVertVBO; data.sizeVertVBO = sizeVertVBO; @@ -323,7 +323,7 @@ void GameObject::draw() data.hasTexture = hasTexture; data.textureCoordsID = texCoordsID; data.mode = GL_TRIANGLES; - App->renderer->renderQueue.insert(std::pair(program,data)); + App->renderer->renderQueue.insert(std::pair(program,data));*/ //drawAABB(); //drawOBB(); @@ -562,7 +562,7 @@ Component* GameObject::getComponent(TypeComponent type) void GameObject::updateBones(const AnimationComponent* anim) { - GameObject* root = anim->gameObject; + GameObject* root = anim->rootBone; queue gameObjects; gameObjects.push(root); @@ -589,6 +589,31 @@ void GameObject::updateBones(const AnimationComponent* anim) } } +void GameObject::updateTransformBones(const AnimationComponent* anim) +{ + GameObject* rootBone = anim->rootBone; + + queue gameObjects; + + for (vector::iterator it = rootBone->children.begin(); it != rootBone->children.end(); ++it) + { + gameObjects.push(*it); + } + + while (!gameObjects.empty()) + { + GameObject* node = gameObjects.front(); + gameObjects.pop(); + + node->idBone = ((TransformComponent*)node->getComponent(TRANSFORM))->transform*node->parent->idBone; + + for (vector::iterator it = node->children.begin(); it != node->children.end(); ++it) + { + gameObjects.push(*it); + } + } +} + void GameObject::updateVertices(const AnimationComponent* anim) { queue gameObjects; @@ -612,8 +637,7 @@ void GameObject::updateVertices(const AnimationComponent* anim) { for (vector::iterator itW = (*it)->weights.begin(); itW != (*it)->weights.end(); ++itW) { - GameObject* object = anim->joints.find((*it)->name)->second; - float4x4 transform = ((TransformComponent*)object->getComponent(TRANSFORM))->transform; + float4x4 transform = anim->joints.find((*it)->name)->second->idBone; float4 finalPos = (*itW)->weight * (transform * (*it)->bind * float4(mesh->originalVertices[(*itW)->vertex], 1.f)); mesh->verticesVBO[(*itW)->vertex] += float3(finalPos.x,finalPos.y,finalPos.z); } @@ -639,8 +663,9 @@ void GameObject::updateComponents() case ANIMATION: if (((AnimationComponent*)(*it))->idAnim != 0) { - updateBones(((AnimationComponent*)(*it))); - updateVertices(((AnimationComponent*)(*it))); + updateBones((AnimationComponent*)(*it)); + updateTransformBones((AnimationComponent*)(*it)); + updateVertices((AnimationComponent*)(*it)); } break; default: diff --git a/LCSEngine/GameObject.h b/LCSEngine/GameObject.h index d088402..67d2c39 100644 --- a/LCSEngine/GameObject.h +++ b/LCSEngine/GameObject.h @@ -49,7 +49,8 @@ class GameObject vector children; GameObject* parent = nullptr; int nameNumber = 0; - float4x4 id; + float4x4 id = float4x4::identity; + float4x4 idBone = float4x4::identity; AABB aabb; OBB obb; bool visible = true; @@ -57,6 +58,7 @@ class GameObject private: string getFinalName(string name); void updateBones(const AnimationComponent* anim); + void updateTransformBones(const AnimationComponent* anim); void updateVertices(const AnimationComponent* anim); void updateComponents(); void updateElements(); diff --git a/LCSEngine/SceneManager.cpp b/LCSEngine/SceneManager.cpp index f254f6c..58dbdfd 100644 --- a/LCSEngine/SceneManager.cpp +++ b/LCSEngine/SceneManager.cpp @@ -34,8 +34,7 @@ void SceneManager::load(const char* file) { ComponentFactory* factory = ComponentFactory::getInstance(); AnimationComponent* anim = (AnimationComponent*)(factory->getComponent(ANIMATION, root)); - anim->rootBone = root; - fillJoints(root, anim->joints); + anim->rootBone = nullptr; root->addComponent(anim); } } @@ -137,25 +136,3 @@ GameObject* SceneManager::createObject(GameObject* parent, aiNode* node) return gameObject; } - -void SceneManager::fillJoints(const GameObject* root, std::map& joints) -{ - queue gameObjects; - for (vector::const_iterator it = root->children.begin(); it != root->children.end(); ++it) - { - gameObjects.push(*it); - } - - while (!gameObjects.empty()) - { - GameObject* node = gameObjects.front(); - gameObjects.pop(); - - joints[node->name] = node; - - for (vector::iterator it = node->children.begin(); it != node->children.end(); ++it) - { - gameObjects.push(*it); - } - } -} \ No newline at end of file