11#include " inexor/vulkan-renderer/gltf2/gltf_loader.hpp"
2+
3+ #include " inexor/vulkan-renderer/wrapper/descriptor.hpp"
4+ #include " inexor/vulkan-renderer/wrapper/descriptor_builder.hpp"
5+
26#include < glm/gtc/type_ptr.hpp>
37
48namespace inexor ::vulkan_renderer::gltf2 {
59
6- Model::Model (const wrapper::Device &device, std::string &file_name, std::string &model_name)
7- : m_device(device), m_name(model_name), m_file_name(file_name) {
10+ Model::Model (const wrapper::Device &device, std::uint32_t swapchain_image_count, std::string &file_name,
11+ std::string &model_name)
12+ : m_device(device), m_swapchain_image_count(swapchain_image_count), m_model_name(model_name),
13+ m_file_name (file_name) {
814 tinygltf::TinyGLTF gltf_context;
915
1016 std::string gltf_loader_error = " " ;
@@ -25,10 +31,9 @@ Model::Model(const wrapper::Device &device, std::string &file_name, std::string
2531 load_node (m_gltf_model.nodes [scene.nodes [i]], m_gltf_model, nullptr , m_index_data, m_vertex_data);
2632 }
2733
28- // TODO: Implement!
2934 setup_descriptor_sets ();
3035
31- // TODO: Create mesh buffer!
36+ create_mesh_buffer ();
3237}
3338
3439Model::~Model () {}
@@ -96,7 +101,6 @@ void Model::load_materials() {
96101
97102void Model::load_node (const tinygltf::Node &inputNode, const tinygltf::Model &input, std::shared_ptr<ModelNode> parent,
98103 std::vector<uint32_t > &indexBuffer, std::vector<ModelVertex> &vertexBuffer) {
99-
100104 std::shared_ptr<ModelNode> node = std::make_shared<ModelNode>();
101105 node->matrix = glm::mat4 (1 .0f );
102106
@@ -245,6 +249,37 @@ void Model::load_node(const tinygltf::Node &inputNode, const tinygltf::Model &in
245249 }
246250}
247251
252+ void Model::setup_descriptor_sets () {
253+ spdlog::debug (" Setting up descriptor sets." );
254+
255+ m_uniform_buffers.emplace_back (m_device, " model matrices uniform buffer" , sizeof (ModelMatrixUBO));
256+
257+ wrapper::DescriptorBuilder builder (m_device, m_swapchain_image_count);
258+
259+ std::string model_descriptor_name = " Descriptor for glTF model " + m_model_name;
260+
261+ const auto number_of_textures = m_textures.size ();
262+
263+ // Gather parameters for descriptor builder.
264+ // It's important to pass the number of textures to the constructors to pre-allocate the required memory.
265+ std::vector<VkSampler> texture_images_sampler (number_of_textures);
266+ std::vector<VkImageView> texture_image_views (number_of_textures);
267+
268+ for (const auto &texture : m_textures) {
269+ texture_images_sampler.emplace_back (texture.sampler ());
270+ texture_image_views.emplace_back (texture.image_view ());
271+ }
272+
273+ const std::vector<std::uint32_t > texture_bindings (number_of_textures, 0 );
274+ const std::vector<VkShaderStageFlagBits> texture_stage_flags (number_of_textures, VK_SHADER_STAGE_FRAGMENT_BIT);
275+
276+ m_descriptors.emplace_back (
277+ builder.add_uniform_buffer <ModelMatrixUBO>(m_uniform_buffers[0 ].buffer (), 0 , VK_SHADER_STAGE_VERTEX_BIT)
278+ .add_combined_image_sampler_array (texture_images_sampler, texture_image_views, texture_bindings,
279+ texture_stage_flags)
280+ .build (model_descriptor_name));
281+ }
282+
248283void Model::draw_node (VkCommandBuffer cmd_buffer, VkPipelineLayout layout, std::shared_ptr<ModelNode> node) {
249284 if (node->mesh .primitives .size () > 0 ) {
250285 // Pass the node's matrix via push constants
0 commit comments