CSEngine
Loading...
Searching...
No Matches
DrawableSkinnedMeshComponent.cpp
1#include "../Util/GLProgramHandle.h"
2#include "DrawableSkinnedMeshComponent.h"
3#include "../OGLDef.h"
4
5#define MeshComponent DrawableSkinnedMeshComponent
6
7using namespace CSE;
8
9MeshComponent::MeshComponent(SGameObject* l_gameObject) : DrawableStaticMeshComponent(l_gameObject) {
10 m_classType = "DrawableSkinnedMeshComponent";
11}
12
13MeshComponent::~MeshComponent() = default;
14
15void MeshComponent::SetRootJoint(SGameObject* joint_object, int joint_size) {
16 m_jointRoot = joint_object->GetComponent<JointComponent>();
17 m_jointSize = joint_size;
18
19 m_jointRoot->calcInverseBindTransform(mat4::Identity());
20 // SetJointSize(joint_object);
21}
22
23JointComponent* DrawableSkinnedMeshComponent::GetRootJoint() {
24 return m_jointRoot;
25}
26
27
28std::vector<mat4> DrawableSkinnedMeshComponent::GetJointMatrix() const {
29 std::vector<mat4> jointMatrices;
30
31 if (m_jointRoot == nullptr) return jointMatrices;
32
33 jointMatrices.resize(m_jointSize);
34 addJointsToVector(m_jointRoot, jointMatrices);
35 return jointMatrices;
36}
37
38
39void DrawableSkinnedMeshComponent::addJointsToVector(JointComponent* headJoint, std::vector<mat4>& matrix) const {
40 int index = headJoint->GetID();
41 matrix[index] = headJoint->GetAnimationMatrix();
42
43 const auto& children = headJoint->GetGameObject()->GetChildren();
44 for (const auto& childJoint : children) {
45 const auto& joint = childJoint->GetComponent<JointComponent>();
46
47 if (joint == nullptr) continue;
48 addJointsToVector(joint, matrix);
49 }
50}
51
52void DrawableSkinnedMeshComponent::SetJointSize(SGameObject* joint_object) {
53 if (joint_object->GetComponent<JointComponent>() == nullptr) return;
54 m_jointSize++;
55 auto children = joint_object->GetChildren();
56 for (const auto& childJoint : children) {
57 SetJointSize(childJoint);
58 }
59}
60
61bool DrawableSkinnedMeshComponent::SetMesh(const SISurface& meshSurface) {
62 return DrawableStaticMeshComponent::SetMesh(meshSurface);
63}
64
65SComponent* DrawableSkinnedMeshComponent::Clone(SGameObject* object) {
66 INIT_COMPONENT_CLONE(DrawableSkinnedMeshComponent, clone);
67
68 clone->m_meshId = m_meshId;
69 clone->m_jointSize = m_jointSize;
70
71 return clone;
72}
73
74void DrawableSkinnedMeshComponent::CopyReference(SComponent* src, std::map<SGameObject*, SGameObject*> lists_obj,
75 std::map<SComponent*, SComponent*> lists_comp) {
76 if (src == nullptr) return;
77 auto* convert = static_cast<DrawableSkinnedMeshComponent*>(src);
78
79 //Copy Components
80 FIND_COMP_REFERENCE(m_jointRoot, convert, JointComponent);
81}