-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimatedMesh.cpp
More file actions
155 lines (119 loc) · 4.72 KB
/
AnimatedMesh.cpp
File metadata and controls
155 lines (119 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "precomp.h"
#include "AnimatedMesh.h"
#include "MyShader.h"
#include "Animation.h"
#include "Animator.h"
#include "Material.h"
#include "Camera.h"
#include "AssetManager.h"
Framework::AnimatedMesh::AnimatedMesh(const std::string& filePath) :
Mesh()
{
glGenBuffers(1, &mBoneIdsBuffer);
glGenBuffers(1, &mBoneWeightsBuffer);
SetShader(AssetManager::Inst().GetAsset<MyShader>("shaders/animated.vert,shaders/standard.frag"));
Assimp::Importer importer{};
LoadFrom(filePath, importer.ReadFile(filePath, sReadFileFlags));
}
Framework::AnimatedMesh::~AnimatedMesh()
{
glDeleteBuffers(1, &mBoneIdsBuffer);
glDeleteBuffers(1, &mBoneWeightsBuffer);
}
void Framework::AnimatedMesh::Draw(const Camera& camera, const glm::mat4 modelMatrix, const Animator* animator) const
{
const std::vector<glm::mat4>& transforms = animator->GetFinalBoneMatrices();
const MyShader* shader = GetShader();
shader->Bind();
for (size_t i = 1; i < transforms.size(); i++)
{
shader->SetInputMatrix(std::string{ "finalBonesMatrices[" + std::to_string(i) + "]" }.c_str(), transforms[i]);
}
shader->SetInputTexture(0, "sampler", *GetMaterial()->GetDiffuse());
shader->SetInputMatrix("viewProjection", camera.GetViewProjection());
shader->SetInputMatrix("model", modelMatrix);
SimpleDraw();
shader->Unbind();
}
void Framework::AnimatedMesh::SetBoneIds(std::vector<glm::ivec4> ids)
{
mBoneIds = std::move(ids);
glBindVertexArray(GetVertexArrayObject());
glBindBuffer(GL_ARRAY_BUFFER, mBoneIdsBuffer);
glBufferData(GL_ARRAY_BUFFER, mBoneIds.size() * sizeof(glm::ivec4), &mBoneIds[0], GL_STATIC_DRAW);
glVertexAttribIPointer(3, sMaxNumOfBonesPerVertex, GL_INT, sizeof(glm::ivec4), (void*)0);
glEnableVertexAttribArray(3);
glBindVertexArray(0);
CheckGL();
}
void Framework::AnimatedMesh::SetBoneWeights(std::vector<glm::vec4> weights)
{
mBoneWeights = std::move(weights);
glBindVertexArray(GetVertexArrayObject());
glBindBuffer(GL_ARRAY_BUFFER, mBoneWeightsBuffer);
glBufferData(GL_ARRAY_BUFFER, mBoneWeights.size() * sizeof(glm::vec4), &mBoneWeights[0], GL_STATIC_DRAW);
glVertexAttribPointer(4, sMaxNumOfBonesPerVertex, GL_FLOAT, GL_FALSE, sizeof(glm::vec4), (void*)0);
glEnableVertexAttribArray(4);
glBindVertexArray(0);
CheckGL();
}
void Framework::AnimatedMesh::LoadFrom(const std::string& filePath, const aiScene* scene)
{
Mesh::LoadFrom(filePath, scene);
size_t numOfVertices = GetNumOfVertices();
std::vector<glm::ivec4> tmpIds(numOfVertices);
for (glm::ivec4& boneIndices : tmpIds)
{
for (glm::length_t i = 0; i < boneIndices.length(); i++)
{
boneIndices[i] = sNullBoneIndex;
}
}
std::vector<glm::vec4> tmpWeights(numOfVertices);
assert(scene != nullptr
&& scene->mNumMeshes == 1);
const aiMesh* mesh = scene->mMeshes[0];
for (uint aiBoneIndex = 0; aiBoneIndex < mesh->mNumBones; aiBoneIndex++)
{
const aiBone* aiBone = mesh->mBones[aiBoneIndex];
const std::string boneName = aiBone->mName.C_Str();
auto boneIt = mBoneLookUp.find(boneName);
if (boneIt == mBoneLookUp.end())
{
BoneData newBoneInfo{};
newBoneInfo.mIndex = static_cast<int>(mBoneLookUp.size()) + 1;
newBoneInfo.mOffset = Math::ToGLM(aiBone->mOffsetMatrix);
boneIt = mBoneLookUp.insert({ boneName, newBoneInfo }).first;
}
aiVertexWeight* weights = aiBone->mWeights;
uint numWeights = aiBone->mNumWeights;
for (uint weightIndex = 0; weightIndex < numWeights; ++weightIndex)
{
uint vertexId = weights[weightIndex].mVertexId;
float weight = weights[weightIndex].mWeight;
assert(vertexId <= GetNumOfVertices());
glm::ivec4& boneIndex = tmpIds[vertexId];
glm::vec4& boneWeights = tmpWeights[vertexId];
for (uint i = 0; i < sMaxNumOfBonesPerVertex; i++)
{
if (boneIndex[i] == sNullBoneIndex)
{
boneIndex[i] = boneIt->second.mIndex;
boneWeights[i] = weight;
break;
}
}
}
}
SetBoneIds(std::move(tmpIds));
SetBoneWeights(std::move(tmpWeights));
for (uint aiAnimIndex = 0; aiAnimIndex < scene->mNumAnimations; aiAnimIndex++)
{
const aiAnimation* aiAnim = scene->mAnimations[aiAnimIndex];
std::unique_ptr<Animation> animation = std::make_unique<Animation>(aiAnim, scene, *this);
const std::string name = aiAnim->mName.C_Str();
assert(mAnimationLookUp.find(name) == mAnimationLookUp.end());
mAnimationLookUp[name] = animation.get();
mAnimations.push_back(std::move(animation));
}
}