Skip to content

Commit

Permalink
fix: Show MMD model when no motions are given (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
mityu authored Feb 1, 2025
1 parent c6e66c4 commit 9ea9e2f
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,12 +613,13 @@ void Routine::Update() {
const auto size{Context::getWindowSize()};
const auto model = mmd_.GetModel();
const size_t vertCount = model->GetVertexCount();
const double vmdFrame = stm_sec(stm_since(timeBeginAnimation_)) * Constant::VmdFPS;
const double elapsedTime = stm_sec(stm_since(timeLastFrame_));

auto& animations = mmd_.GetAnimations();

if (!animations.empty()) {
const double elapsedTime = stm_sec(stm_since(timeLastFrame_));
const double vmdFrame = stm_sec(stm_since(timeBeginAnimation_)) * Constant::VmdFPS;

// Update camera animation.
auto& [vmdAnim, cameraAnim] = animations[motionID_];
if (cameraAnim) {
Expand Down Expand Up @@ -655,34 +656,54 @@ void Routine::Update() {
model->UpdateAllAnimation(vmdAnim.get(), vmdFrame, elapsedTime);
}
model->EndAnimation();
}
model->Update();

sg_update_buffer(
posVB_, sg_range{
.ptr = model->GetUpdatePositions(),
.size = vertCount * sizeof(glm::vec3),
});
sg_update_buffer(
normVB_, sg_range{
.ptr = model->GetUpdateNormals(),
.size = vertCount * sizeof(glm::vec3),
});
sg_update_buffer(
uvVB_, sg_range{
.ptr = model->GetUpdateUVs(),
.size = vertCount * sizeof(glm::vec2),
});

if (!animations.empty()) {
const auto& vmdAnim = animations[motionID_].first;
model->Update();

sg_update_buffer(
posVB_, sg_range{
.ptr = model->GetUpdatePositions(),
.size = vertCount * sizeof(glm::vec3),
});
sg_update_buffer(
normVB_, sg_range{
.ptr = model->GetUpdateNormals(),
.size = vertCount * sizeof(glm::vec3),
});
sg_update_buffer(
uvVB_, sg_range{
.ptr = model->GetUpdateUVs(),
.size = vertCount * sizeof(glm::vec2),
});

timeLastFrame_ = stm_now();
if (vmdFrame > vmdAnim->GetMaxKeyTime()) {
model->SaveBaseAnimation();
timeBeginAnimation_ = timeLastFrame_;
selectNextMotion();
needBridgeMotions_ = true;
}
} else {
viewMatrix_ =
userView_.GetWorldViewMatrix() *
glm::lookAt(defaultCamera_.eye, defaultCamera_.center, glm::vec3(0, 1, 0));
projectionMatrix_ = glm::perspectiveFovRH(
glm::radians(30.0f), static_cast<float>(size.x), static_cast<float>(size.y), 1.0f,
10000.0f);
sg_update_buffer(
posVB_, sg_range{
.ptr = model->GetPositions(),
.size = vertCount * sizeof(glm::vec3),
});
sg_update_buffer(
normVB_, sg_range{
.ptr = model->GetNormals(),
.size = vertCount * sizeof(glm::vec3),
});
sg_update_buffer(
uvVB_, sg_range{
.ptr = model->GetUVs(),
.size = vertCount * sizeof(glm::vec2),
});
}
}

Expand Down

0 comments on commit 9ea9e2f

Please sign in to comment.