Skip to content

Commit

Permalink
[LCS-112] Added GetTransform in ModuleAnimation
Browse files Browse the repository at this point in the history
  • Loading branch information
Guriva committed Mar 7, 2018
1 parent 199ee27 commit b927a56
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
21 changes: 12 additions & 9 deletions LCSEngine/GameObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@
#include "CameraComponent.h"
#include "TransformComponent.h"
#include "AnimationComponent.h"
#include "AudioListenerComponent.h"
#include "AudioSourceComponent.h"
#include "ComponentFactory.h"
#include "ModuleSceneMain.h"
#include "ModuleCamera.h"
#include "ModuleRender.h"
#include "ModuleTextures.h"
#include "ModuleAnimation.h"
#include "ModuleAudio.h"
#include "ModuleGUI.h"
#include "Shader.h"
#include "QuadTree.h"
#include "Model.h"
#include "AssetTexture.h"
#include "Imgui/imgui.h"
#include "SDL/include/SDL_assert.h"
#include "Glew/include/glew.h"
#include "DevIL/include/IL/il.h"
#include <queue>
#include "Model.h"
#include "AssetTexture.h"
#include "AudioListenerComponent.h"
#include "ModuleAudio.h"
#include "AudioSourceComponent.h"

GameObject::GameObject() {}

Expand Down Expand Up @@ -61,7 +62,6 @@ void GameObject::preUpdate()
switch ((*it)->typeComponent)
{
case TRANSFORM:
//Check if the element is static, in that case the transform is the identity
if (staticFlag)
{
id = ((TransformComponent*)(*it))->transform.Transposed()*id;
Expand All @@ -81,8 +81,6 @@ void GameObject::preUpdate()
texCoordsID = ((MeshComponent*)(*it))->idTexCoords;
colorID = ((MeshComponent*)(*it))->idColors;

//TODO: set textureID and texCoordsID for renderer

break;
case MATERIAL:
program = ((MaterialComponent*)(*it))->program;
Expand Down Expand Up @@ -145,7 +143,12 @@ void GameObject::preUpdate()
break;
case AUDIOSOURCE:
App->audio->updatePositionAudioSource(((AudioSourceComponent*)(*it))->idAudioGameObj, id);

break;
case ANIMATION:

break;
default:
break;
}

}
Expand Down
38 changes: 31 additions & 7 deletions LCSEngine/ModuleAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ unsigned int ModuleAnimation::play(const char* name)

void ModuleAnimation::stop(unsigned int id)
{
if (id != 0 && id <= instances.size() && instances[id-1] != nullptr)
if (id != 0 && id <= instances.size() && instances[id - 1] != nullptr)
{
instances[id-1]->animation = nullptr;
instances[id-1] = nullptr;
Expand All @@ -112,23 +112,47 @@ bool ModuleAnimation::getTransform(unsigned int id, const char* boneName, float3
{
if (anim->bones[i]->name == boneName)
{
//position = calculatePosition();
//rotation = calculateRotation();
float pos = (float)(instances[id - 1]->localTime * anim->bones[i]->positions.size() - 1) / (float)anim->duration;
float rot = (float)(instances[id - 1]->localTime * anim->bones[i]->rotations.size() - 1) / (float)anim->duration;

unsigned int posEndIndex, rotEndIndex;
(pos == anim->bones[i]->positions.size() - 1) ? posEndIndex = 0 : posEndIndex = (unsigned int)(pos + 1);
(rot == anim->bones[i]->rotations.size() - 1) ? rotEndIndex = 0 : rotEndIndex = (unsigned int)(rot + 1);

float3 iniPos = anim->bones[i]->positions[(unsigned int)pos];
float3 endPos = anim->bones[i]->positions[posEndIndex];
Quat iniRot = anim->bones[i]->rotations[(unsigned int)rot];
Quat endRot = anim->bones[i]->rotations[rotEndIndex];

position = linearInterpolationPosition(iniPos, endPos, pos-floor(pos));
rotation = linearInterpolationRotation(iniRot, endRot, rot-floor(rot));
success = true;
}
}
}
return success;
}

float3 ModuleAnimation::calculatePosition(const float3& iniPos, const float3& endPos, float time) const
float3 ModuleAnimation::linearInterpolationPosition(const float3& iniPos, const float3& endPos, float time) const
{
float3 position = { 0.f, 0.f, 0.f };
return position;
return iniPos*(1.f-time)+endPos*time;
}

Quat ModuleAnimation::calculateRotation(const Quat& iniRot, const Quat& endRot, float time) const
Quat ModuleAnimation::linearInterpolationRotation(const Quat& iniRot, const Quat& endRot, float time) const
{
Quat rotation = { 0.f, 0.f, 0.f, 0.f };
float dot = iniRot.Dot(endRot);
float scalar = time;

if (dot < 0.f)
{
scalar = -scalar;
}

rotation.x = iniRot.x*(1.f - time) + endRot.x*scalar;
rotation.y = iniRot.y*(1.f - time) + endRot.y*scalar;
rotation.z = iniRot.z*(1.f - time) + endRot.z*scalar;
rotation.w = iniRot.w*(1.f - time) + endRot.w*scalar;

return rotation;
}
8 changes: 4 additions & 4 deletions LCSEngine/ModuleAnimation.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ struct Bone

struct Animation
{
double duration = 0;
unsigned int duration = 0;
std::vector<Bone*> bones;
};

struct AnimationInstance
{
Animation* animation = nullptr;
double localTime = 0; //ms
unsigned int localTime = 0; //ms
bool loop = true;
};

Expand All @@ -52,8 +52,8 @@ class ModuleAnimation : public Module
const aiScene* scene = 0;

private:
float3 calculatePosition(const float3& iniPos, const float3& endPos, float time) const;
Quat calculateRotation(const Quat& iniRot, const Quat& endRot, float time) const;
float3 linearInterpolationPosition(const float3& iniPos, const float3& endPos, float time) const;
Quat linearInterpolationRotation(const Quat& iniRot, const Quat& endRot, float time) const;
};

#endif //__MODULEANIMATION_H__
11 changes: 6 additions & 5 deletions LCSEngine/ModuleSceneMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ void ModuleSceneMain::drawGrid()
GLint projectLoc = glGetUniformLocation(program, "projection");
glUniformMatrix4fv(projectLoc, 1, GL_FALSE, App->camera->getProjectMatrix());

glUniform1i(glGetUniformLocation(program, "useText"), false);

float3 cameraPos = App->camera->currentCamera->frustum.pos;

glBegin(GL_LINES);
Expand All @@ -334,31 +336,30 @@ void ModuleSceneMain::drawGrid()
if (Distance(float2(cameraPos.x, cameraPos.z), float2(0.0f, 0.0f)) < sqrt(pow(POS_LINES_GRID, 2) + pow(POS_LINES_GRID, 2)))
{
// draw line for x axis
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
// draw line for y axis
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
// draw line for Z axis
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 1.0);
}

for (unsigned int i = 0; i <= COUNT_LINES_GRID; i += DIST_BTW_LINES_GRID)
{
glColor3f(1.0f, 1.0f, 1.0f);
//Linies en X
glVertex3f(-POS_LINES_GRID + floor(cameraPos.x), 0.0f, (float)i - POS_LINES_GRID + floor(cameraPos.z));
glVertex3f(POS_LINES_GRID + floor(cameraPos.x), 0.0f, (float)i - POS_LINES_GRID + floor(cameraPos.z));
glColor3f(1.0f, 1.0f, 1.0f);
//Linies en Y
glVertex3f((float)i - POS_LINES_GRID + floor(cameraPos.x), 0.0f, -POS_LINES_GRID + floor(cameraPos.z));
glVertex3f((float)i - POS_LINES_GRID + floor(cameraPos.x), 0.0f, POS_LINES_GRID + floor(cameraPos.z));
}
glEnd();


//glBindBuffer(GL_ARRAY_BUFFER, idVertVBO);
//glBufferData(GL_ARRAY_BUFFER, sizeof(float) * verticesVBO.size() * 3, verticesVBO[0].ptr(), GL_STATIC_DRAW);
}

void ModuleSceneMain::swapDefaultShader()
Expand Down

0 comments on commit b927a56

Please sign in to comment.