diff options
author | Frederico Linhares <fred@linhares.blue> | 2024-03-23 19:05:26 -0300 |
---|---|---|
committer | Frederico Linhares <fred@linhares.blue> | 2024-03-23 19:12:24 -0300 |
commit | 8acb4a0aad7f4d1b2438010d487489a9641e30cb (patch) | |
tree | 50a0fc3094e1d5e29a1fbcc8d7f94ffa2561fa84 /src/vk | |
parent | bf240d7eeaa89657462b705849fde56e54e237db (diff) |
fixt Make skeletal mesh work properly
Diffstat (limited to 'src/vk')
-rw-r--r-- | src/vk/device.cpp | 13 | ||||
-rw-r--r-- | src/vk/device.hpp | 1 | ||||
-rw-r--r-- | src/vk/graphics_pipeline_3d_skeletal.cpp | 91 | ||||
-rw-r--r-- | src/vk/renderer.cpp | 5 |
4 files changed, 65 insertions, 45 deletions
diff --git a/src/vk/device.cpp b/src/vk/device.cpp index d0690d7..78baa53 100644 --- a/src/vk/device.cpp +++ b/src/vk/device.cpp @@ -203,6 +203,12 @@ Device::Device(VkPhysicalDevice vk_physical_device, bool with_swapchain) this->vert3d_shader_module = create_shader_module( this->device, vert3d_shader_module.c_str()); + std::string vert3d_skeletal_shader_module{data_dir}; + vert3d_skeletal_shader_module.append( + "\\glsl\\shader_3d_skeletal.vert.spv"); + this->vert3d_skeletal_shader_module = create_shader_module( + this->device, vert3d_skeletal_shader_module.c_str()); + std::string frag3d_shader_module{data_dir}; frag3d_shader_module.append("\\glsl\\shader_3d.frag.spv"); this->frag3d_shader_module = create_shader_module( @@ -245,6 +251,11 @@ Device::Device(VkPhysicalDevice vk_physical_device, bool with_swapchain) this->vert3d_shader_module = create_shader_module( this->device, vert3d_shader_module.c_str()); + std::string vert3d_skeletal_shader_module{data_dir}; + vert3d_skeletal_shader_module.append("/glsl/shader_3d_skeletal.vert.spv"); + this->vert3d_skeletal_shader_module = create_shader_module( + this->device, vert3d_skeletal_shader_module.c_str()); + std::string frag3d_shader_module{data_dir}; frag3d_shader_module.append("/glsl/shader_3d.frag.spv"); this->frag3d_shader_module = create_shader_module( @@ -308,6 +319,8 @@ Device::~Device() vkDestroyShaderModule( this->device, this->frag_sprite_3d_shader_module, nullptr); vkDestroyShaderModule(this->device, this->vert3d_shader_module, nullptr); + vkDestroyShaderModule( + this->device, this->vert3d_skeletal_shader_module, nullptr); vkDestroyShaderModule(this->device, this->frag3d_shader_module, nullptr); vkDestroyShaderModule( this->device, this->vert2d_solid_shader_module, nullptr); diff --git a/src/vk/device.hpp b/src/vk/device.hpp index 36fc08d..e52a143 100644 --- a/src/vk/device.hpp +++ b/src/vk/device.hpp @@ -40,6 +40,7 @@ public: VkShaderModule vert_sprite_3d_shader_module; VkShaderModule frag_sprite_3d_shader_module; VkShaderModule vert3d_shader_module; + VkShaderModule vert3d_skeletal_shader_module; VkShaderModule frag3d_shader_module; VkShaderModule vert2d_solid_shader_module; VkShaderModule frag2d_solid_shader_module; diff --git a/src/vk/graphics_pipeline_3d_skeletal.cpp b/src/vk/graphics_pipeline_3d_skeletal.cpp index 364b502..b000d89 100644 --- a/src/vk/graphics_pipeline_3d_skeletal.cpp +++ b/src/vk/graphics_pipeline_3d_skeletal.cpp @@ -39,7 +39,7 @@ load_pipeline(void *obj) vert_shader_stage_info.flags = 0; vert_shader_stage_info.stage = VK_SHADER_STAGE_VERTEX_BIT; vert_shader_stage_info.module = - cg_core.vk_device_with_swapchain->vert3d_shader_module; + cg_core.vk_device_with_swapchain->vert3d_skeletal_shader_module; vert_shader_stage_info.pName = "main"; vert_shader_stage_info.pSpecializationInfo = nullptr; @@ -287,51 +287,52 @@ GraphicsPipeline3DSkeletal::draw( vkCmdSetScissor(draw_command_buffer, 0, 1, &vk_scissor); } - // Draw models - for(auto& [skeletal_mesh, instances]: + vkCmdBindPipeline( + draw_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, + this->graphic_pipeline); + + // Draw models + for(auto& [skeletal_mesh, instances]: cg_core.vk_renderer->skeletal_models_to_draw[current_frame]) - { - VkBuffer vertex_buffers[]{skeletal_mesh->vertex_buffer->buffer}; - VkDeviceSize offsets[]{0}; - - vkCmdBindPipeline( - draw_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, - this->graphic_pipeline); - vkCmdBindVertexBuffers( - draw_command_buffer, 0, 1, vertex_buffers, offsets); - vkCmdBindIndexBuffer( - draw_command_buffer, skeletal_mesh->index_buffer->buffer, 0, - VK_INDEX_TYPE_UINT32); - - for(auto &instance: instances) - { // Object matrix. - glm::mat4 translation_matrix{1.0f}; - translation_matrix = glm::translate( - translation_matrix, *instance->position); - glm::mat4 rotation_matrix{glm::toMat4(*instance->orientation)}; - - std::array<VkDescriptorSet, 4> vk_descriptor_sets{ - cg_core.vk_light->descriptor_sets_world[image_index], - view->descriptor_sets_3d[image_index], - instance->texture->descriptor_sets[image_index], - instance->descriptor_sets[image_index]}; - - vkCmdBindDescriptorSets( - draw_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, - cg_core.vk_graphics_pipeline_3d_layout->pipeline, 0, - vk_descriptor_sets.size(), vk_descriptor_sets.data(), 0, nullptr); - - vkCmdDrawIndexed( - draw_command_buffer, skeletal_mesh->index_count, 1, 0, 0, 0); - - VK::UDOSkeletalModel udo_skeletal_model{}; - instance->tick(cg_core.delta_time); - udo_skeletal_model.base_matrix = translation_matrix * rotation_matrix; - std::copy(instance->bone_transforms.begin(), - instance->bone_transforms.end(), - udo_skeletal_model.bone_matrices); - instance->uniform_buffers[image_index].copy_data(&udo_skeletal_model); - } + { + VkBuffer vertex_buffers[]{skeletal_mesh->vertex_buffer->buffer}; + VkDeviceSize offsets[]{0}; + + vkCmdBindVertexBuffers( + draw_command_buffer, 0, 1, vertex_buffers, offsets); + vkCmdBindIndexBuffer( + draw_command_buffer, skeletal_mesh->index_buffer->buffer, 0, + VK_INDEX_TYPE_UINT32); + + for(auto &instance: instances) + { // Object matrix. + glm::mat4 translation_matrix{1.0f}; + translation_matrix = glm::translate( + translation_matrix, *instance->position); + glm::mat4 rotation_matrix{glm::toMat4(*instance->orientation)}; + + std::array<VkDescriptorSet, 4> vk_descriptor_sets{ + cg_core.vk_light->descriptor_sets_world[image_index], + view->descriptor_sets_3d[image_index], + instance->descriptor_sets[image_index], + instance->texture->descriptor_sets[image_index]}; + + vkCmdBindDescriptorSets( + draw_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, + cg_core.vk_graphics_pipeline_3d_layout->pipeline, 0, + vk_descriptor_sets.size(), vk_descriptor_sets.data(), 0, nullptr); + + vkCmdDrawIndexed( + draw_command_buffer, skeletal_mesh->index_count, 1, 0, 0, 0); + + VK::UDOSkeletalModel udo_skeletal_model{}; + instance->tick(cg_core.delta_time); + udo_skeletal_model.base_matrix = translation_matrix * rotation_matrix; + std::copy(instance->bone_transforms.begin(), + instance->bone_transforms.end(), + udo_skeletal_model.bone_matrices); + instance->uniform_buffers[image_index].copy_data(&udo_skeletal_model); + } } { // Update view uniform buffers diff --git a/src/vk/renderer.cpp b/src/vk/renderer.cpp index d4e25ba..21e0421 100644 --- a/src/vk/renderer.cpp +++ b/src/vk/renderer.cpp @@ -252,6 +252,11 @@ Renderer::draw() view, draw_command_buffer, cg_core.vk_swapchain->current_frame, image_index); + for(auto &view: this->views_3d) + cg_core.vk_graphics_pipeline_3d_skeletal->draw( + view, draw_command_buffer, cg_core.vk_swapchain->current_frame, + image_index); + vkCmdEndRenderPass(draw_command_buffer); { // 2D render pass |