Skip to content

Commit

Permalink
[animation_renderer] fixed various bugs where object_extras and the a…
Browse files Browse the repository at this point in the history
…ctual object datas could go out-of-sync, namely when recycling old objects
  • Loading branch information
harrand committed Sep 18, 2023
1 parent cae9c03 commit 5e4d6dc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
26 changes: 21 additions & 5 deletions src/tz/ren/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ namespace tz::ren
animation_renderer::object_handle animation_renderer::add_object(object_init_data init)
{
auto handle = mesh_renderer::add_object(init);
tz::assert(static_cast<std::size_t>(static_cast<tz::hanval>(handle)) == this->object_extras.size());
this->object_extras.push_back
({
auto hanval = static_cast<std::size_t>(static_cast<tz::hanval>(handle));
this->object_extras.push_back({});
this->object_extras[hanval] =
{
.base_transform = init.trs
});
};
tz::assert(this->object_extras.size() == mesh_renderer::draw_count(), "Object extra and draw count mismatch (%zu and %zu)", this->object_extras.size(), mesh_renderer::draw_count());
//tz::assert(static_cast<std::size_t>(static_cast<tz::hanval>(handle)) == this->object_extras.size());
return handle;
}

Expand Down Expand Up @@ -128,6 +131,11 @@ namespace tz::ren
tz::report("Re-use GLTF spot %zu", hanval);
this->gltf_free_list.erase(this->gltf_free_list.begin());
this_gltf = &this->gltfs[hanval];
*this_gltf =
{
.data = gltf,
.object_offset = static_cast<unsigned int>(mesh_renderer::draw_count() - this->gltf_free_list.size())
};
this_gltf->assets.gltfh = static_cast<tz::hanval>(hanval);
}
else
Expand Down Expand Up @@ -351,14 +359,14 @@ namespace tz::ren
parent = gltf.node_object_map.at(parent_node_id.value());
}
}
std::size_t this_extra_id = this->object_extras.size();
this_object = this->add_object
({
.trs = node.transform,
.mesh = {},
.bound_textures = {},
.parent = parent
});
std::size_t this_extra_id = static_cast<std::size_t>(static_cast<tz::hanval>(this_object));
// new object belongs to this asset package.
gltf.assets.objects.push_back(this_object);
// node id also maps to this object.
Expand Down Expand Up @@ -818,6 +826,14 @@ namespace tz::ren
if(mesh_renderer::draw_count() > 0 && ImGui::CollapsingHeader("Objects", ImGuiTreeNodeFlags_DefaultOpen))
{
constexpr float slider_height = 160.0f;
if(ImGui::Button("+") && std::cmp_less(object_id, mesh_renderer::draw_count() - 2))
{
object_id++;
}
if(ImGui::Button("-") && object_id >= 1)
{
object_id--;
}
ImGui::VSliderInt("##objectid", ImVec2{18.0f, slider_height}, &object_id, 0, mesh_renderer::draw_count() - 1);
std::string objname = "Object " + std::to_string(object_id);

Expand Down
5 changes: 4 additions & 1 deletion src/tz/ren/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ namespace tz::ren
void mesh_renderer::remove_object(object_handle oh, transform_hierarchy::remove_strategy strategy)
{
auto hanval = static_cast<std::size_t>(static_cast<tz::hanval>(oh));
this->free_list.push_back(oh);
if(std::find(this->free_list.begin(), this->free_list.end(), oh) == this->free_list.end())
{
this->free_list.push_back(oh);
}
// set to front mesh (which needs to be a null locator);
this->compute_pass.get_draw_list_meshes()[hanval] = {};

Expand Down

0 comments on commit 5e4d6dc

Please sign in to comment.