-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGltfModel.cpp
More file actions
162 lines (135 loc) · 4.38 KB
/
GltfModel.cpp
File metadata and controls
162 lines (135 loc) · 4.38 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
156
157
158
159
160
161
162
#include"GltfModel.h"
GltfModel::~GltfModel()
{
nodes.clear();
skins.clear();
}
int GltfModel::findNode(const int& index)
{
for (int i = 0; i < nodes.size(); i++)
{
if (nodes[i].index == index)
{
return i;
}
}
return -1;
}
int GltfModel::nodeFromIndex(const int& index)
{
return findNode(index);
}
//アニメーションの各ノードの更新処理
void GltfModel::updateAllNodes(NodeTransform& nodeTransform, std::vector<std::array<glm::mat4, 128>>& jointMatrices, size_t& updatedIndex)
{
for (int i = 0; i < nodes.size(); i++)
{
if (nodes[i].mesh.vertices.size() != 0 && nodes[i].skinIndex > -1)
{
nodes[i].update(nodeTransform, jointMatrices[nodes[i].globalHasSkinNodeIndex], updatedIndex);
}
}
}
//アニメーション長さを取得
float GltfModel::animationDuration(std::string animationName)
{
Animation& animation = animations[animationName];
return animation.end - animation.start;
}
//指定したアニメーションの行列を取得
void GltfModel::updateAnimation(double animationTime, std::string animationName
, NodeTransform& nodeTransform, std::vector<std::array<glm::mat4, 128>>& jointMatrices)
{
if (animations.empty()) {
std::cout << ".glTF does not contain animation." << std::endl;
return;
}
Animation& animation = animations[animationName];
//アニメーション行列の初期化
nodeTransform.resetMat();
bool updated = false;
for (auto& channel : animation.channels) {
AnimationSampler& sampler = animation.samplers[channel.samplerIndex];
if (sampler.inputs.size() > sampler.outputsVec4.size()) {
continue;
}
for (size_t i = 0; i < sampler.inputs.size() - 1; i++) {
if ((animationTime >= sampler.inputs[i]) && (animationTime <= sampler.inputs[i + 1])) {
float u = static_cast<float>(std::max(0.0, animationTime - sampler.inputs[i]) / (sampler.inputs[i + 1] - sampler.inputs[i]));
if (u <= 1.0f) {
//GltfNodeの配列上のノードの番号を求める
size_t nodeIndex = channel.nodeOffset;
switch (channel.path) {
case AnimationChannel::PathType::TRANSLATION://平行移動
nodeTransform.translation[nodeIndex] = sampler.translate(i, animationTime);
break;
case AnimationChannel::PathType::SCALE://スケール
nodeTransform.scale[nodeIndex] = sampler.scale(i, animationTime);
break;
case AnimationChannel::PathType::ROTATION://回転
nodeTransform.rotation[nodeIndex] = sampler.rotate(i, animationTime);
break;
}
updated = true;
}
}
}
}
if (updated) {
size_t updatedIndex = 0;
for (int i = 0; i < nodes.size(); i++)
{
nodes[i].setLocalMatrix(nodeTransform);
}
updateAllNodes(nodeTransform, jointMatrices, updatedIndex);
}
}
void GltfModel::setPointBufferNum()
{
vertBuffer.resize(meshCount);
indeBuffer.resize(meshCount);
descriptorSet.resize(meshCount);
createBuffer();
}
void GltfModel::createBuffer()
{
for (int i = 0; i < nodes.size(); i++)
{
if (nodes[i].mesh.vertices.size() != 0)
{
vertBuffer[nodes[i].mesh.meshIndex] = bufferFactory->Create(sizeof(Vertex) * nodes[i].mesh.vertices.size()
, nodes[i].mesh.vertices.data(), BufferUsage::VERTEX, BufferTransferType::DST);
indeBuffer[nodes[i].mesh.meshIndex] = bufferFactory->Create(sizeof(uint32_t) * nodes[i].mesh.indices.size()
, nodes[i].mesh.indices.data(), BufferUsage::INDEX, BufferTransferType::DST);
}
}
}
void GltfModel::createDescriptorSet(std::vector<std::shared_ptr<DescriptorSet>>& descriptorSet)
{
for (int i = 0; i < nodes.size(); i++)
{
if (nodes[i].mesh.vertices.size() != 0)
{
const std::shared_ptr<DescriptorSetLayout> layout =
layoutFactory->Create(LayoutPattern::RAYCAST);
const DescriptorSetProperty descProperty = descriptorSetFactory->getBuilder()
->initProperty()
->withBindingBuffer(0)
->withDescriptorSetCount(1)
->withTypeBuffer(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)
->withBuffer(vertBuffer[nodes[i].mesh.meshIndex])
->withRange(sizeof(Vertex) * static_cast<uint32_t>(nodes[i].mesh.vertices.size()))
->addBufferInfo()
->withBindingBuffer(1)
->withDescriptorSetCount(1)
->withTypeBuffer(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)
->addBufferInfo()
->withDescriptorSetLayout(layout)
->withBuffer(indeBuffer[nodes[i].mesh.meshIndex])
->withRange(sizeof(uint32_t) * static_cast<uint32_t>(nodes[i].mesh.indices.size()))
->addBufferInfo()
->Build();
descriptorSet[nodes[i].mesh.meshIndex] = descriptorSetFactory->Create(descProperty);
}
}
}