first_app.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "first_app.h"
  2. // CORAL
  3. #include "coral_camera.h"
  4. #include "coral_buffer.h"
  5. #include "coral_texture.h"
  6. #include "voxel_renderer.h"
  7. // STD
  8. #include <stdexcept>
  9. #include <array>
  10. #include <chrono>
  11. #include <iostream>
  12. #include <iomanip>
  13. using namespace coral_3d;
  14. struct GlobalUBO
  15. {
  16. glm::mat4 view_projeciton{1.f};
  17. // GLOBAL LIGHT
  18. glm::vec4 global_light_direction{ glm::normalize(glm::vec4{ 0.577f, 0.377f, -0.577f, 0.f})}; // w is ignored
  19. };
  20. first_app::first_app()
  21. {
  22. global_descriptor_pool_ = coral_descriptor_pool::Builder(device_)
  23. .set_max_sets(coral_swapchain::MAX_FRAMES_IN_FLIGHT)
  24. .add_pool_size(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, coral_swapchain::MAX_FRAMES_IN_FLIGHT)
  25. .add_pool_size(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, coral_swapchain::MAX_FRAMES_IN_FLIGHT)
  26. .build();
  27. load_chunks();
  28. }
  29. first_app::~first_app()
  30. {}
  31. void first_app::run()
  32. {
  33. coral_buffer global_ubo
  34. {
  35. device_,
  36. sizeof(GlobalUBO),
  37. coral_swapchain::MAX_FRAMES_IN_FLIGHT,
  38. VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
  39. VMA_MEMORY_USAGE_CPU_ONLY,
  40. 0,
  41. device_.properties.limits.minUniformBufferOffsetAlignment
  42. };
  43. global_ubo.map();
  44. auto global_set_layout = coral_descriptor_set_layout::Builder(device_)
  45. .add_binding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_ALL_GRAPHICS)
  46. .add_binding(1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT)
  47. .build();
  48. std::vector<VkDescriptorSet> global_descriptor_sets{coral_swapchain::MAX_FRAMES_IN_FLIGHT};
  49. for (int i = 0; i < global_descriptor_sets.size(); i++)
  50. {
  51. auto buffer_info = global_ubo.descriptor_info_index(i);
  52. auto image_info = atlas_texture_->get_descriptor_info();
  53. coral_descriptor_writer(*global_set_layout, *global_descriptor_pool_)
  54. .write_buffer(0, &buffer_info)
  55. .write_image(1, &image_info)
  56. .build(global_descriptor_sets[i]);
  57. }
  58. voxel_renderer render_system{ device_, renderer_.get_swapchain_render_pass(), global_set_layout->get_descriptor_set_layout() };
  59. coral_camera camera{ {0.f, 0.f, -2.f} };
  60. auto last_time{ std::chrono::high_resolution_clock::now() };
  61. while (!window_.should_close())
  62. {
  63. glfwPollEvents();
  64. auto current_time{ std::chrono::high_resolution_clock::now() };
  65. auto frame_time{ std::chrono::duration<float, std::chrono::seconds::period>(current_time - last_time).count() };
  66. last_time = current_time;
  67. // MOVE CAMERA
  68. camera.update_input(window_.get_glfw_window(), frame_time);
  69. float aspect{ renderer_.get_aspect_ratio() };
  70. camera.set_perspective_projection(glm::radians(60.f), aspect, 0.1f, 1000.f);
  71. if (auto command_buffer = renderer_.begin_frame())
  72. {
  73. const int frame_index{ renderer_.get_frame_index() };
  74. VoxelRenderInfo render_info
  75. {
  76. frame_index,
  77. frame_time,
  78. command_buffer,
  79. global_descriptor_sets[frame_index],
  80. chunks_
  81. };
  82. // UPDATE GLOBAL UBO
  83. GlobalUBO ubo{};
  84. ubo.view_projeciton = camera.get_projection() * camera.get_view();
  85. global_ubo.write_to_index(&ubo, frame_index);
  86. global_ubo.flush_index(frame_index);
  87. // RENDER
  88. renderer_.begin_swapchain_render_pass(command_buffer);
  89. render_system.render_chunks(render_info);
  90. renderer_.end_swapchain_render_pass(command_buffer);
  91. renderer_.end_frame();
  92. }
  93. }
  94. vkDeviceWaitIdle(device_.device());
  95. }
  96. void first_app::load_chunks()
  97. {
  98. chunk chunk{ device_ };
  99. chunks_.emplace_back(chunk);
  100. atlas_texture_ = coral_texture::create_texture_from_file(
  101. device_,
  102. "assets/textures/atlas_texture.png",
  103. VK_FORMAT_R8G8B8A8_SRGB
  104. );
  105. }