Skip to content

Commit

Permalink
large physics-update
Browse files Browse the repository at this point in the history
  • Loading branch information
crocdialer committed Nov 12, 2024
1 parent ace7599 commit e6a55ce
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 13 deletions.
6 changes: 6 additions & 0 deletions projects/pbr_viewer/collision_cereal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ void serialize(Archive &archive, vierkant::collision::capsule_t &s)
archive(cereal::make_nvp("radius", s.radius), cereal::make_nvp("height", s.height));
}

template<class Archive>
void serialize(Archive &archive, vierkant::collision::mesh_t &s)
{
archive(cereal::make_nvp("mesh_id", s.mesh_id), cereal::make_nvp("convex_hull", s.convex_hull));
}

}// namespace vierkant::collision

namespace vierkant
Expand Down
32 changes: 30 additions & 2 deletions projects/pbr_viewer/pbr_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ PBRViewer::PBRViewer(const crocore::Application::create_info_t &create_info) : c
this->loop_throttling = !m_settings.window_info.vsync;
this->target_loop_frequency = m_settings.target_fps;

m_scene->physics_context().mesh_provider = [&mesh_map = m_mesh_map](const auto &mesh_id) {
return mesh_map[mesh_id];
};
#ifndef NDEBUG
m_settings.use_validation = true;
#endif
Expand Down Expand Up @@ -393,11 +396,36 @@ vierkant::window_delegate_t::draw_result_t PBRViewer::draw(const vierkant::Windo

if(m_settings.draw_physics)
{
if(auto geom = m_scene->physics_context().debug_render())
auto physics_debug_result = m_scene->physics_context().debug_render();

if(physics_debug_result.lines)
{
m_draw_context.draw_lines(m_renderer_overlay, geom->positions, geom->colors, m_camera->view_transform(),
m_draw_context.draw_lines(m_renderer_overlay, physics_debug_result.lines->positions,
physics_debug_result.lines->colors, m_camera->view_transform(),
m_camera->projection_matrix());
}

for(uint32_t i = 0; i < physics_debug_result.aabbs.size(); ++i)
{
const auto &aabb = physics_debug_result.aabbs[i];
m_draw_context.draw_boundingbox(m_renderer_overlay, aabb, m_camera->view_transform(),
m_camera->projection_matrix());

const auto &[transform, geom] = physics_debug_result.triangle_meshes[i];
auto &mesh = m_physics_meshes[geom];
if(!mesh)
{
vierkant::Mesh::create_info_t mesh_create_info = {};
mesh_create_info.mesh_buffer_params.use_vertex_colors = true;
mesh = vierkant::Mesh::create_from_geometry(m_device, geom, mesh_create_info);
mesh->materials.front()->m.blend_mode = vierkant::BlendMode::Blend;
}
auto color = physics_debug_result.colors[i];
color.w = 0.6f;
m_draw_context.draw_mesh(m_renderer_overlay, mesh, m_camera->view_transform() * transform,
m_camera->projection_matrix(), vierkant::ShaderType::UNLIT_COLOR, color, false,
false);
}
}

for(const auto &obj: selected_objects)
Expand Down
3 changes: 2 additions & 1 deletion projects/pbr_viewer/pbr_viewer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class PBRViewer : public crocore::Application

//! optional mesh-index and set of enabled entries.
std::optional<size_t> mesh_index;
std::optional<std::set<uint32_t>> entry_indices = {};
std::optional<std::unordered_set<uint32_t>> entry_indices = {};

//! optional animation-state
std::optional<vierkant::animation_component_t> animation_state = {};
Expand Down Expand Up @@ -232,6 +232,7 @@ class PBRViewer : public crocore::Application

// init a scene with physics-support on application-threadpool
std::shared_ptr<vierkant::PhysicsScene> m_scene = vierkant::PhysicsScene::create();
std::unordered_map<vierkant::GeometryConstPtr, vierkant::MeshPtr> m_physics_meshes;

// selection of scene-renderers
vierkant::PBRDeferredPtr m_pbr_renderer;
Expand Down
1 change: 1 addition & 0 deletions projects/pbr_viewer/pbr_viewer_load_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,7 @@ vierkant::MeshPtr PBRViewer::load_mesh(const std::filesystem::path &path)
{
auto lock = std::unique_lock(*m_queue_model_loading->mutex);
mesh = vierkant::model::load_mesh(load_params, *model_assets);
mesh->id = mesh_id;
}

m_num_loading--;
Expand Down
22 changes: 13 additions & 9 deletions projects/pbr_viewer/pbr_viewer_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <crocore/filesystem.hpp>
#include <vierkant/imgui/imgui_util.h>

#include <glm/gtc/random.hpp>
#include "pbr_viewer.hpp"

bool DEMO_GUI = false;
Expand Down Expand Up @@ -330,16 +331,19 @@ void PBRViewer::create_ui()

ImGui::Separator();
ImGui::Spacing();
if(ImGui::Button("add object"))
if(ImGui::Button("add boxes (25)"))
{
auto new_obj = m_scene->create_mesh_object({m_box_mesh});
new_obj->transform.translation.y = 10.f;

vierkant::object_component auto &cmp = new_obj->add_component<vierkant::physics_component_t>();
vierkant::collision::box_t box = {m_box_mesh->entries.front().bounding_box.half_extents()};
cmp.shape = box;
cmp.mass = 1.f;
m_scene->add_object(new_obj);
for(uint32_t i = 0; i < 25; ++i)
{
auto new_obj = m_scene->create_mesh_object({m_box_mesh});
new_obj->transform.translation.y = 10.f;
new_obj->transform.translation += glm::ballRand(1.f);
vierkant::object_component auto &cmp = new_obj->add_component<vierkant::physics_component_t>();
vierkant::collision::box_t box = {m_box_mesh->entries.front().bounding_box.half_extents()};
cmp.shape = box;
cmp.mass = 1.f;
m_scene->add_object(new_obj);
}
}

ImGui::Separator();
Expand Down
1 change: 1 addition & 0 deletions projects/pbr_viewer/serialization.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cereal/types/memory.hpp>
#include <cereal/types/vector.hpp>
#include <cereal/types/set.hpp>
#include <cereal/types/unordered_set.hpp>
#include <cereal/types/unordered_map.hpp>
#include <cereal/types/optional.hpp>
#include <cereal/types/variant.hpp>
Expand Down

0 comments on commit e6a55ce

Please sign in to comment.