Allows creation of instances of meshes and drawing all of them with single draw call. Instances can have their own transformation data (location, rotation and scale).
76 lines
1.8 KiB
C++
76 lines
1.8 KiB
C++
#include "mesh_controller.hpp"
|
|
|
|
#include <map>
|
|
#include <queue>
|
|
#include <memory>
|
|
|
|
namespace rse::graphics::mesh
|
|
{
|
|
|
|
std::map<uint16_t, struct Mesh> g_meshes;
|
|
std::queue<uint16_t> free_ids;
|
|
|
|
std::map<uint16_t, std::queue<uint16_t>> instance_free_ids;
|
|
|
|
uint16_t create_mesh(std::vector<rse::graphics::vulkanbase::Vertex> vertices, std::vector<uint16_t> indices)
|
|
{
|
|
static uint16_t last_id = 0U;
|
|
uint16_t assignable_id = 0U;
|
|
|
|
if (free_ids.empty()) {
|
|
assignable_id = last_id++;
|
|
} else {
|
|
assignable_id = free_ids.front();
|
|
free_ids.pop();
|
|
}
|
|
|
|
g_meshes.emplace(assignable_id, Mesh (
|
|
assignable_id,
|
|
vertices,
|
|
indices
|
|
));
|
|
|
|
return assignable_id;
|
|
}
|
|
|
|
void create_mesh_instance(uint16_t mesh_id, InstanceData instance_data)
|
|
{
|
|
uint16_t instance_id = 0U;
|
|
auto& instances = g_meshes[mesh_id].instances;
|
|
auto& free_instance_ids = instance_free_ids[mesh_id];
|
|
|
|
if (free_instance_ids.empty()) {
|
|
instance_id = instances.size();
|
|
} else {
|
|
instance_id = free_instance_ids.front();
|
|
free_instance_ids.pop();
|
|
}
|
|
|
|
instances[instance_id] = std::make_unique<struct MeshInstance>(
|
|
instance_id,
|
|
instance_data
|
|
);
|
|
}
|
|
|
|
//TODO: Replace with something else. It will take to much time to do this each time we draw frame for all the objects
|
|
std::vector<InstanceData> get_instance_data_for_mesh(uint16_t mesh_id) {
|
|
std::vector<InstanceData> data;
|
|
|
|
for(const auto& [id, instance] : g_meshes[mesh_id].instances) {
|
|
data.push_back(instance.get()->instance_data);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
|
|
std::vector<rse::graphics::vulkanbase::Vertex> get_mesh_vertices(uint16_t mesh_id) {
|
|
return g_meshes[mesh_id].vertices;
|
|
}
|
|
|
|
std::vector<uint16_t> get_mesh_indices(uint16_t mesh_id) {
|
|
return g_meshes[mesh_id].indices;
|
|
}
|
|
|
|
} // namespace rse::graphics::mesh
|