Skip to content

Commit

Permalink
Merge branch 'LCS-139-141' of https://github.com/LastCoffeeStudio/LCS…
Browse files Browse the repository at this point in the history
…Engine into LCS-139-141
  • Loading branch information
Guriva committed Mar 15, 2018
2 parents 2ccf154 + e3f8c94 commit 21d4368
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 32 deletions.
62 changes: 62 additions & 0 deletions LCSEngine/AnimationComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
{
Expand Down Expand Up @@ -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<GameObject*> nodes;

for (vector<GameObject*>::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<GameObject*>::iterator it = node->children.begin(); it != node->children.end(); ++it)
{
nodes.push(*it);
}
}
}

void AnimationComponent::fillJoints()
{
queue<GameObject*> gameObjects;
for (vector<GameObject*>::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<GameObject*>::iterator it = node->children.begin(); it != node->children.end(); ++it)
{
gameObjects.push(*it);
}
}
}
3 changes: 3 additions & 0 deletions LCSEngine/AnimationComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class AnimationComponent : public Component
std::map<std::string, GameObject*> joints;
std::string animationName = "";
std::string currentAnimationName = "";
char rootNodeBones[128] = "";
unsigned int idAnim = 0;
float blendTime = 0.f;

Expand All @@ -30,6 +31,8 @@ class AnimationComponent : public Component
void playAnimation();
void stopAnimation();
void blendAnimation();
void setRootNodeBones();
void fillJoints();
};

#endif //__ANIMATIONCOMPONENT_H__
39 changes: 32 additions & 7 deletions LCSEngine/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ void GameObject::draw()
}
}*/

renderData data;
/*renderData data;
data.id = id;
data.idVertVBO = idVertVBO;
data.sizeVertVBO = sizeVertVBO;
Expand All @@ -324,7 +324,7 @@ void GameObject::draw()
data.hasTexture = hasTexture;
data.textureCoordsID = texCoordsID;
data.mode = GL_TRIANGLES;
App->renderer->renderQueue.insert(std::pair<GLuint,renderData>(program,data));
App->renderer->renderQueue.insert(std::pair<GLuint,renderData>(program,data));*/

//drawAABB();
//drawOBB();
Expand Down Expand Up @@ -563,7 +563,7 @@ Component* GameObject::getComponent(TypeComponent type)

void GameObject::updateBones(const AnimationComponent* anim)
{
GameObject* root = anim->gameObject;
GameObject* root = anim->rootBone;

queue<GameObject*> gameObjects;
gameObjects.push(root);
Expand All @@ -590,6 +590,31 @@ void GameObject::updateBones(const AnimationComponent* anim)
}
}

void GameObject::updateTransformBones(const AnimationComponent* anim)
{
GameObject* rootBone = anim->rootBone;

queue<GameObject*> gameObjects;

for (vector<GameObject*>::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<GameObject*>::iterator it = node->children.begin(); it != node->children.end(); ++it)
{
gameObjects.push(*it);
}
}
}

void GameObject::updateVertices(const AnimationComponent* anim)
{
queue<GameObject*> gameObjects;
Expand All @@ -613,8 +638,7 @@ void GameObject::updateVertices(const AnimationComponent* anim)
{
for (vector<Weight*>::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);
}
Expand All @@ -640,8 +664,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:
Expand Down
4 changes: 3 additions & 1 deletion LCSEngine/GameObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ class GameObject
vector<GameObject*> children;
GameObject* parent = nullptr;
int nameNumber = 0;
float4x4 id;
float4x4 id = float4x4::identity;
float4x4 idBone = float4x4::identity;
AABB aabb;
OBB obb;
bool visible = true;

private:
string getFinalName(string name);
void updateBones(const AnimationComponent* anim);
void updateTransformBones(const AnimationComponent* anim);
void updateVertices(const AnimationComponent* anim);
void updateComponents();
void updateElements();
Expand Down
25 changes: 1 addition & 24 deletions LCSEngine/SceneManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -137,25 +136,3 @@ GameObject* SceneManager::createObject(GameObject* parent, aiNode* node)

return gameObject;
}

void SceneManager::fillJoints(const GameObject* root, std::map<std::string, GameObject*>& joints)
{
queue<GameObject*> gameObjects;
for (vector<GameObject*>::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<GameObject*>::iterator it = node->children.begin(); it != node->children.end(); ++it)
{
gameObjects.push(*it);
}
}
}

0 comments on commit 21d4368

Please sign in to comment.