Refactoring
This commit is contained in:
parent
24851a89e8
commit
83cceecccd
@ -757,13 +757,6 @@ namespace ZL
|
|||||||
}
|
}
|
||||||
|
|
||||||
prepared = true;
|
prepared = true;
|
||||||
|
|
||||||
/*
|
|
||||||
if (startBones.size() > MAX_GPU_BONES)
|
|
||||||
{
|
|
||||||
std::cout << "Warning: model has " << startBones.size()
|
|
||||||
<< " bones, exceeding GPU skinning limit of " << MAX_GPU_BONES << std::endl;
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BoneSystem::ComputeSkinningMatrices(int frame, std::vector<Matrix4f>& outMatrices) const
|
void BoneSystem::ComputeSkinningMatrices(int frame, std::vector<Matrix4f>& outMatrices) const
|
||||||
@ -897,13 +890,6 @@ namespace ZL
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*std::cout << "m=" << skinningMatrixForEachBone[5] << std::endl;
|
|
||||||
|
|
||||||
std::cout << "Start Mesh data " << std::endl;
|
|
||||||
std::cout << startMesh.PositionData[0] << std::endl;
|
|
||||||
std::cout << startMesh.PositionData[10] << std::endl;
|
|
||||||
std::cout << startMesh.PositionData[100] << std::endl;
|
|
||||||
*/
|
|
||||||
for (int i = 0; i < mesh.PositionData.size(); i++)
|
for (int i = 0; i < mesh.PositionData.size(); i++)
|
||||||
{
|
{
|
||||||
Vector4f originalPos = {
|
Vector4f originalPos = {
|
||||||
@ -926,30 +912,10 @@ namespace ZL
|
|||||||
}
|
}
|
||||||
vMoved = true;
|
vMoved = true;
|
||||||
finalPos = finalPos + (skinningMatrixForEachBone[verticesBoneWeight[i][j].boneIndex] * originalPos) * verticesBoneWeight[i][j].weight;
|
finalPos = finalPos + (skinningMatrixForEachBone[verticesBoneWeight[i][j].boneIndex] * originalPos) * verticesBoneWeight[i][j].weight;
|
||||||
|
|
||||||
/*if (i == 100)
|
|
||||||
{
|
|
||||||
std::cout << "bone index=" << verticesBoneWeight[i][j].boneIndex << std::endl;
|
|
||||||
std::cout << "weight=" << verticesBoneWeight[i][j].weight << std::endl;
|
|
||||||
std::cout << "skinning matrix=" << skinningMatrixForEachBone[verticesBoneWeight[i][j].boneIndex] << std::endl;
|
|
||||||
std::cout << "original pos=" << originalPos.transpose() << std::endl;
|
|
||||||
std::cout << "final pos=" << finalPos.transpose() << std::endl;
|
|
||||||
std::cout << "-----------------" << std::endl;
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*if (i == 100)
|
|
||||||
{
|
|
||||||
std::cout << originalPos << std::endl;
|
|
||||||
std::cout << finalPos << std::endl;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
/*if (abs(finalPos(0) - originalPos(0)) > 1 || abs(finalPos(1) - originalPos(1)) > 1 || abs(finalPos(2) - originalPos(2)) > 1)
|
|
||||||
{
|
|
||||||
|
|
||||||
}*/
|
|
||||||
|
|
||||||
if (!vMoved)
|
if (!vMoved)
|
||||||
{
|
{
|
||||||
finalPos = originalPos;
|
finalPos = originalPos;
|
||||||
@ -962,5 +928,41 @@ namespace ZL
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BoneAnimationData::prepareGpuSkinningVBOs() {
|
||||||
|
if (gpuSkinningPrepared)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
model.gpuBoneData.PrepareGpuSkinningData(model.verticesBoneWeight);
|
||||||
|
|
||||||
|
// Upload bind-pose mesh (static, done once)
|
||||||
|
bindPoseMutable.AssignFrom(model.startMesh);
|
||||||
|
|
||||||
|
auto& gpu = model.gpuBoneData;
|
||||||
|
|
||||||
|
boneIndices0VBO = std::make_shared<VBOHolder>();
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, boneIndices0VBO->getBuffer());
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, gpu.boneIndices0.size() * sizeof(Eigen::Vector4f),
|
||||||
|
gpu.boneIndices0.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
boneIndices1VBO = std::make_shared<VBOHolder>();
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, boneIndices1VBO->getBuffer());
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, gpu.boneIndices1.size() * sizeof(Eigen::Vector2f),
|
||||||
|
gpu.boneIndices1.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
boneWeights0VBO = std::make_shared<VBOHolder>();
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, boneWeights0VBO->getBuffer());
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, gpu.boneWeights0.size() * sizeof(Eigen::Vector4f),
|
||||||
|
gpu.boneWeights0.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
boneWeights1VBO = std::make_shared<VBOHolder>();
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, boneWeights1VBO->getBuffer());
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, gpu.boneWeights1.size() * sizeof(Eigen::Vector2f),
|
||||||
|
gpu.boneWeights1.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
gpuSkinningPrepared = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -84,6 +84,8 @@ namespace ZL
|
|||||||
std::shared_ptr<VBOHolder> boneWeights1VBO;
|
std::shared_ptr<VBOHolder> boneWeights1VBO;
|
||||||
bool gpuSkinningPrepared = false;
|
bool gpuSkinningPrepared = false;
|
||||||
std::vector<Eigen::Matrix4f> skinningMatrices;
|
std::vector<Eigen::Matrix4f> skinningMatrices;
|
||||||
|
|
||||||
|
void prepareGpuSkinningVBOs();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -301,43 +301,6 @@ void Character::draw(Renderer& renderer) {
|
|||||||
renderer.shaderManager.PopShader();
|
renderer.shaderManager.PopShader();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Character::prepareGpuSkinningVBOs(BoneAnimationData& anim) {
|
|
||||||
if (anim.gpuSkinningPrepared)
|
|
||||||
{
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
anim.model.gpuBoneData.PrepareGpuSkinningData(anim.model.verticesBoneWeight);
|
|
||||||
|
|
||||||
// Upload bind-pose mesh (static, done once)
|
|
||||||
anim.bindPoseMutable.AssignFrom(anim.model.startMesh);
|
|
||||||
|
|
||||||
auto& gpu = anim.model.gpuBoneData;
|
|
||||||
|
|
||||||
anim.boneIndices0VBO = std::make_shared<VBOHolder>();
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, anim.boneIndices0VBO->getBuffer());
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, gpu.boneIndices0.size() * sizeof(Eigen::Vector4f),
|
|
||||||
gpu.boneIndices0.data(), GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
anim.boneIndices1VBO = std::make_shared<VBOHolder>();
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, anim.boneIndices1VBO->getBuffer());
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, gpu.boneIndices1.size() * sizeof(Eigen::Vector2f),
|
|
||||||
gpu.boneIndices1.data(), GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
anim.boneWeights0VBO = std::make_shared<VBOHolder>();
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, anim.boneWeights0VBO->getBuffer());
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, gpu.boneWeights0.size() * sizeof(Eigen::Vector4f),
|
|
||||||
gpu.boneWeights0.data(), GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
anim.boneWeights1VBO = std::make_shared<VBOHolder>();
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, anim.boneWeights1VBO->getBuffer());
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, gpu.boneWeights1.size() * sizeof(Eigen::Vector2f),
|
|
||||||
gpu.boneWeights1.data(), GL_STATIC_DRAW);
|
|
||||||
|
|
||||||
anim.gpuSkinningPrepared = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Character::drawGpuSkinning(Renderer& renderer) {
|
void Character::drawGpuSkinning(Renderer& renderer) {
|
||||||
AnimationState drawState = resolveActiveState();
|
AnimationState drawState = resolveActiveState();
|
||||||
auto it = animations.find(drawState);
|
auto it = animations.find(drawState);
|
||||||
@ -347,7 +310,7 @@ void Character::drawGpuSkinning(Renderer& renderer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto& anim = it->second;
|
auto& anim = it->second;
|
||||||
prepareGpuSkinningVBOs(anim);
|
anim.prepareGpuSkinningVBOs();
|
||||||
|
|
||||||
if (anim.skinningMatrices.empty())
|
if (anim.skinningMatrices.empty())
|
||||||
{
|
{
|
||||||
@ -456,7 +419,7 @@ void Character::drawShadowDepthGpuSkinning(Renderer& renderer) {
|
|||||||
if (it == animations.end()) return;
|
if (it == animations.end()) return;
|
||||||
|
|
||||||
auto& anim = it->second;
|
auto& anim = it->second;
|
||||||
prepareGpuSkinningVBOs(anim);
|
anim.prepareGpuSkinningVBOs();
|
||||||
if (anim.skinningMatrices.empty()) {
|
if (anim.skinningMatrices.empty()) {
|
||||||
if (anim.model.animations.empty() || anim.model.animations[0].keyFrames.empty()) return;
|
if (anim.model.animations.empty() || anim.model.animations[0].keyFrames.empty()) return;
|
||||||
anim.model.ComputeSkinningMatrices(
|
anim.model.ComputeSkinningMatrices(
|
||||||
@ -566,7 +529,8 @@ void Character::drawGpuSkinningWithShadow(Renderer& renderer, const Eigen::Matri
|
|||||||
if (it == animations.end() || !texture) return;
|
if (it == animations.end() || !texture) return;
|
||||||
|
|
||||||
auto& anim = it->second;
|
auto& anim = it->second;
|
||||||
prepareGpuSkinningVBOs(anim);
|
anim.prepareGpuSkinningVBOs();
|
||||||
|
|
||||||
if (anim.skinningMatrices.empty()) {
|
if (anim.skinningMatrices.empty()) {
|
||||||
if (anim.model.animations.empty() || anim.model.animations[0].keyFrames.empty()) return;
|
if (anim.model.animations.empty() || anim.model.animations[0].keyFrames.empty()) return;
|
||||||
anim.model.ComputeSkinningMatrices(
|
anim.model.ComputeSkinningMatrices(
|
||||||
|
|||||||
@ -97,8 +97,6 @@ private:
|
|||||||
// if the requested state has no loaded animation.
|
// if the requested state has no loaded animation.
|
||||||
AnimationState resolveActiveState() const;
|
AnimationState resolveActiveState() const;
|
||||||
|
|
||||||
// GPU skinning: prepare per-animation VBOs (called lazily on first draw)
|
|
||||||
void prepareGpuSkinningVBOs(BoneAnimationData& anim);
|
|
||||||
// GPU skinning: draw using shader-based skinning
|
// GPU skinning: draw using shader-based skinning
|
||||||
void drawGpuSkinning(Renderer& renderer);
|
void drawGpuSkinning(Renderer& renderer);
|
||||||
// Shadow: draw into depth map (no texture, no projection push)
|
// Shadow: draw into depth map (no texture, no projection push)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user