Skip to content

Commit

Permalink
feat: #40 Update calc bone matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
ducph committed Jan 6, 2020
1 parent 81dc00e commit e17950f
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Projects/Irrlicht/Include/matrix4.h
Original file line number Diff line number Diff line change
Expand Up @@ -1321,8 +1321,8 @@ namespace core
}
#endif
//const CMatrix4<T> &m = *this;
#define m(x,y) M[ (x << 2) + y]
#define out(x,y) out.M[ (x << 2) + y]
#define m(x,y) M[(x << 2) + y]
#define out(x,y) out.M[(x << 2) + y]

f32 d = (m(0, 0) * m(1, 1) - m(0, 1) * m(1, 0)) * (m(2, 2) * m(3, 3) - m(2, 3) * m(3, 2)) -
(m(0, 0) * m(1, 2) - m(0, 2) * m(1, 0)) * (m(2, 1) * m(3, 3) - m(2, 3) * m(3, 1)) +
Expand Down
2 changes: 2 additions & 0 deletions Projects/Skylicht/Engine/Source/Entity/CEntityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This file is part of the "Skylicht Engine".

#include "Transform/CTransformComponentSystem.h"
#include "Transform/CWorldTransformSystem.h"
#include "Transform/CWorldInvTransformSystem.h"
#include "RenderMesh/CJointSystem.h"
#include "RenderMesh/CRenderMeshSystem.h"
#include "RenderMesh/CJointAnimationSystem.h"
Expand All @@ -42,6 +43,7 @@ namespace Skylicht
addSystem<CComponentTransformSystem>();
addSystem<CJointSystem>();
addSystem<CWorldTransformSystem>();
addSystem<CWorldInvTransformSystem>();
addSystem<CJointAnimationSystem>();
addSystem<CSkinnedMeshSystem>();
addSystem<CSoftwareSkinningSystem>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ This file is part of the "Skylicht Engine".

#include "pch.h"
#include "CJointAnimationSystem.h"
#include "Entity/CEntityManager.h"

namespace Skylicht
{
Expand All @@ -41,18 +42,22 @@ namespace Skylicht
{
m_joints.set_used(0);
m_transforms.set_used(0);
m_rootInvTransform.set_used(0);
}

void CJointAnimationSystem::onQuery(CEntityManager *entityManager, CEntity *entity)
{
CJointData *joint = entity->getData<CJointData>();
if (joint != NULL)
if (joint != NULL && joint->RootIndex != 0)
{
CWorldTransformData *transform = entity->getData<CWorldTransformData>();
if (transform != NULL)
CWorldInvTransformData *rootInvTransform = entityManager->getEntity(joint->RootIndex)->getData<CWorldInvTransformData>();

if (transform != NULL && rootInvTransform != NULL)
{
m_joints.push_back(joint);
m_transforms.push_back(transform);
m_rootInvTransform.push_back(rootInvTransform);
}
}
}
Expand All @@ -64,14 +69,14 @@ namespace Skylicht

void CJointAnimationSystem::update(CEntityManager *entityManager)
{
CJointData** joints = m_joints.pointer();
CWorldTransformData** transforms = m_transforms.pointer();
CJointData **joints = m_joints.pointer();
CWorldTransformData **transforms = m_transforms.pointer();
CWorldInvTransformData **rootInvTransform = m_rootInvTransform.pointer();

// calc animation matrix for CSkinnedMeshSystem
for (u32 i = 0, n = m_joints.size(); i < n; i++)
{
// CJointData *joint = joints[i];
// joint->AnimationMatrix = do later (see CColladaLoader::constructEntityPrefab)
joints[i]->AnimationMatrix.setbyproduct_nocheck(rootInvTransform[i]->WorldInverse, transforms[i]->World);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ This file is part of the "Skylicht Engine".

#include "Entity/IEntitySystem.h"
#include "Transform/CWorldTransformData.h"
#include "Transform/CWorldInvTransformData.h"
#include "CJointData.h"

namespace Skylicht
Expand All @@ -35,6 +36,7 @@ namespace Skylicht
protected:
core::array<CJointData*> m_joints;
core::array<CWorldTransformData*> m_transforms;
core::array<CWorldInvTransformData*> m_rootInvTransform;

public:
CJointAnimationSystem();
Expand Down
4 changes: 3 additions & 1 deletion Projects/Skylicht/Engine/Source/RenderMesh/CJointData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ This file is part of the "Skylicht Engine".
namespace Skylicht
{
CJointData::CJointData() :
BoneRoot(false)
BoneRoot(false),
RootIndex(-1),
BoneIndex(-1)
{

}
Expand Down
3 changes: 3 additions & 0 deletions Projects/Skylicht/Engine/Source/RenderMesh/CJointData.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ namespace Skylicht
bool BoneRoot;
int Depth;

int RootIndex;
int BoneIndex;

std::string SID;
std::string BoneName;

Expand Down
16 changes: 12 additions & 4 deletions Projects/Skylicht/Engine/Source/RenderMesh/CRenderMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This file is part of the "Skylicht Engine".
#include "Entity/CEntityManager.h"

#include "Transform/CWorldTransformData.h"
#include "Transform/CWorldInvTransformSystem.h"
#include "RenderMesh/CRenderMeshData.h"
#include "RenderMesh/CJointData.h"
#include "Culling/CCullingData.h"
Expand Down Expand Up @@ -95,13 +96,9 @@ namespace Skylicht
spawnTransform->Depth = rootTransform->Depth + srcTransform->Depth;

if (srcTransform->ParentIndex == -1)
{
spawnTransform->ParentIndex = rootEntity->getIndex();
}
else
{
spawnTransform->ParentIndex = entityIndex[srcTransform->ParentIndex];
}
}

// copy render data
Expand Down Expand Up @@ -141,9 +138,14 @@ namespace Skylicht
spawnJoint->AnimationMatrix = srcJointData->AnimationMatrix;
spawnJoint->DefaultAnimationMatrix = srcJointData->DefaultAnimationMatrix;
spawnJoint->DefaultRelativeMatrix = srcJointData->DefaultRelativeMatrix;
spawnJoint->RelativeAnimationMatrix = srcJointData->RelativeAnimationMatrix;
spawnJoint->BoneIndex = srcJointData->BoneIndex;
spawnJoint->RootIndex = rootEntity->getIndex();
}
}

bool addInvData = false;

// re-map joint with new entity in CEntityManager
for (CRenderMeshData *&r :renderers)
{
Expand All @@ -165,6 +167,12 @@ namespace Skylicht
joint.JointData = entityManager->getEntity(joint.EntityIndex)->getData<CJointData>();
}
}

if (addInvData == false)
{
rootEntity->addData<CWorldInvTransformData>();
addInvData = true;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
!@
MIT License
Copyright (c) 2019 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#include "pch.h"
#include "CWorldInvTransformData.h"

namespace Skylicht
{
CWorldInvTransformData::CWorldInvTransformData()
{
}

CWorldInvTransformData::~CWorldInvTransformData()
{

}
}
41 changes: 41 additions & 0 deletions Projects/Skylicht/Engine/Source/Transform/CWorldInvTransformData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
!@
MIT License
Copyright (c) 2019 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#pragma once

#include "Entity/IEntityData.h"

namespace Skylicht
{
class CWorldInvTransformData : public IEntityData
{
public:
core::matrix4 WorldInverse;

public:
CWorldInvTransformData();

virtual ~CWorldInvTransformData();
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
!@
MIT License
Copyright (c) 2019 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#include "pch.h"
#include "CWorldInvTransformSystem.h"
#include "Entity/CEntityManager.h"
#include "Transform/CTransform.h"

namespace Skylicht
{
CWorldInvTransformSystem::CWorldInvTransformSystem()
{
}

CWorldInvTransformSystem::~CWorldInvTransformSystem()
{
}

void CWorldInvTransformSystem::beginQuery()
{
m_world.set_used(0);
m_worldInv.set_used(0);
}

void CWorldInvTransformSystem::onQuery(CEntityManager *entityManager, CEntity *entity)
{
CWorldInvTransformData *worldInv = entity->getData<CWorldInvTransformData>();
if (worldInv != NULL)
{
CWorldTransformData *world = entity->getData<CWorldTransformData>();
if (world != NULL)
{
m_world.push_back(world);
m_worldInv.push_back(worldInv);
}
}
}

void CWorldInvTransformSystem::init(CEntityManager *entityManager)
{

}

void CWorldInvTransformSystem::update(CEntityManager *entityManager)
{
CWorldTransformData **worlds = m_world.pointer();
CWorldInvTransformData **worldInvs = m_worldInv.pointer();

for (u32 i = 0, n = m_world.size(); i < n; i++)
{
// Get inverse matrix of world
worlds[i]->World.getInverse(worldInvs[i]->WorldInverse);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
!@
MIT License
Copyright (c) 2019 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#pragma once

#include "CWorldTransformData.h"
#include "CWorldInvTransformData.h"
#include "Entity/IEntitySystem.h"

namespace Skylicht
{
class CWorldInvTransformSystem : public IEntitySystem
{
protected:
core::array<CWorldTransformData*> m_world;
core::array<CWorldInvTransformData*> m_worldInv;

public:
CWorldInvTransformSystem();

virtual ~CWorldInvTransformSystem();

virtual void beginQuery();

virtual void onQuery(CEntityManager *entityManager, CEntity *entity);

virtual void init(CEntityManager *entityManager);

virtual void update(CEntityManager *entityManager);
};
}

0 comments on commit e17950f

Please sign in to comment.