diff --git a/Engine/Engine.cpp b/Engine/Engine.cpp index d76750b..de01b89 100644 --- a/Engine/Engine.cpp +++ b/Engine/Engine.cpp @@ -4,11 +4,11 @@ Engine::Engine() { m_last_time = std::chrono::high_resolution_clock::now(); - mp_camera = std::make_shared(); - mp_player = std::make_shared(mp_camera); + mp_main_camera = std::make_shared(); + mp_controller = std::make_shared(mp_main_camera); mp_config = std::make_shared(); - mp_window = std::make_unique(mp_config, *mp_player.get()); + mp_window = std::make_unique(mp_config, *mp_controller.get()); mp_renderer = std::make_shared(mp_window->getHandle(), mp_config->width, mp_config->height); @@ -80,6 +80,16 @@ const std::shared_ptr Engine::CreateGameObject(const std::string& ob return go; } +void Engine::BindKeyToFunc(const int& key, std::function& func) +{ + mp_controller->SetKeyToFunc(key, func); +} + +const std::shared_ptr Engine::GetMainCamera() +{ + return mp_main_camera; +} + const bool& Engine::shouldClose() { return glfwWindowShouldClose(&mp_window->getHandle()); @@ -92,23 +102,22 @@ void Engine::update() std::chrono::steady_clock::time_point current_time = std::chrono::high_resolution_clock::now(); float delta_time = std::chrono::duration(current_time - m_last_time).count(); - mp_player->setDeltaTime(delta_time); - mp_player->updatePosition(); + mp_controller->Update(delta_time); m_last_time = current_time; - const int frame = mp_renderer->getFrameIndex(); - if (mp_scene->isUpdate(frame)) + const int32_t frame = mp_renderer->AcquireNextImage(); + if (frame != -1 && (mp_scene->isUpdate(frame) || mp_renderer->IsUpdated(frame))) { mp_renderer->beginRecordCommandBuffers(mp_renderer->getCommandBuffer(frame), mp_renderer->getFrameBuffer(frame)); - mp_scene->render(mp_renderer->getCommandBuffer(frame), frame); - mp_renderer->endRecordCommandBuffers(mp_renderer->getCommandBuffer(frame)); + + mp_renderer->SetUpdate(frame); } - mp_camera->updateUBO(static_cast(mp_config->width), static_cast(mp_config->height)); - mp_scene->updateUBO(mp_camera, mp_renderer); + mp_main_camera->UpdateUBO(static_cast(mp_config->width), static_cast(mp_config->height)); + mp_scene->updateUBO(mp_main_camera, mp_renderer); //std::this_thread::sleep_for(std::chrono::nanoseconds(500));//delete when not streaming } diff --git a/Engine/Engine.h b/Engine/Engine.h index c971bae..d4a25e0 100644 --- a/Engine/Engine.h +++ b/Engine/Engine.h @@ -27,6 +27,12 @@ class Engine const std::shared_ptr CreateGameObject(); const std::shared_ptr CreateGameObject(const std::string& object_file); + // CONTROLLER + void BindKeyToFunc(const int& key, std::function& func); + + // CAMERA + const std::shared_ptr GetMainCamera(); + const bool& shouldClose(); private: @@ -35,8 +41,8 @@ class Engine std::shared_ptr mp_renderer; std::shared_ptr mp_scene; - std::shared_ptr mp_camera; - std::shared_ptr mp_player; + std::shared_ptr mp_main_camera; + std::shared_ptr mp_controller; std::shared_ptr mp_config; std::chrono::steady_clock::time_point m_last_time; diff --git a/Engine/Engine.vcxproj b/Engine/Engine.vcxproj index 930dbe4..7336756 100644 --- a/Engine/Engine.vcxproj +++ b/Engine/Engine.vcxproj @@ -44,7 +44,7 @@ - + @@ -67,7 +67,7 @@ - + diff --git a/Engine/Engine.vcxproj.filters b/Engine/Engine.vcxproj.filters index 9127757..fd8b66f 100644 --- a/Engine/Engine.vcxproj.filters +++ b/Engine/Engine.vcxproj.filters @@ -87,7 +87,7 @@ Engine\World - + Engine\Cameras @@ -152,7 +152,7 @@ Engine\World - + Engine\Cameras diff --git a/Engine/Source.cpp b/Engine/Source.cpp index f186c9b..c3e4d50 100644 --- a/Engine/Source.cpp +++ b/Engine/Source.cpp @@ -15,15 +15,29 @@ int main() room->bindMatToMesh(0, room_texture); room->setPosition({ 3.0f, 0.0f, 0.0f }); + std::shared_ptr room2_texture = engine.CreateMaterial(TSHADER::NO_TEXTURE); + std::shared_ptr room2 = engine.CreateGameObject(); + room2->loadMesh("assets/models/viking_room.obj"); + room2->bindMatToMesh(0, room2_texture); + room2->setPosition({ 6.0f, 0.0f, 0.0f }); + std::shared_ptr scene = std::make_shared(); scene->addGameObject(room); engine.setScene(scene); + std::function my_func = []() {std::clog << "Test d'un key bind" << std::endl; }; + engine.BindKeyToFunc(GLFW_KEY_Q, my_func); + int init = 0; // running loop do { + if (init++ == 30000) + { + scene->addGameObject(room2); + } + engine.update(); engine.draw(); } while (!engine.shouldClose()); diff --git a/Engine/Window.cpp b/Engine/Window.cpp index 8842274..25c20c3 100644 --- a/Engine/Window.cpp +++ b/Engine/Window.cpp @@ -2,7 +2,7 @@ -Window::Window(std::shared_ptr& config, Player& player) +Window::Window(std::shared_ptr& config, Controller& controller) { glfwInit(); @@ -12,7 +12,7 @@ Window::Window(std::shared_ptr& config, Player& player) m_handle = glfwCreateWindow(config->width, config->height, "Vulkan", nullptr, nullptr); glfwSetInputMode(m_handle, GLFW_CURSOR, GLFW_CURSOR_DISABLED); - glfwSetWindowUserPointer(m_handle, &player); + glfwSetWindowUserPointer(m_handle, &controller); glfwSetKeyCallback(m_handle, key_callback); glfwSetCursorPosCallback(m_handle, mouse_callback); glfwSetCursorPos(m_handle, 0.0, 0.0); @@ -27,9 +27,9 @@ Window::~Window() void Window::key_callback(GLFWwindow * window, int key, int scancode, int action, int mods) { - Player* pPlayer = static_cast(glfwGetWindowUserPointer(window)); + Controller* pController = static_cast(glfwGetWindowUserPointer(window)); - pPlayer->setInput(static_cast(key), static_cast(scancode), static_cast(mods), static_cast(action)); + pController->setInput(static_cast(key), static_cast(scancode), static_cast(mods), static_cast(action)); if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) { @@ -39,9 +39,9 @@ void Window::key_callback(GLFWwindow * window, int key, int scancode, int action void Window::mouse_callback(GLFWwindow * window, double xpos, double ypos) { - Player* pPlayer = static_cast(glfwGetWindowUserPointer(window)); + Controller* pController = static_cast(glfwGetWindowUserPointer(window)); - pPlayer->updateRotation(xpos, ypos); + pController->updateRotation(xpos, ypos); glfwSetCursorPos(window, 0.0, 0.0); } diff --git a/Engine/Window.h b/Engine/Window.h index b75c515..bcfa6b8 100644 --- a/Engine/Window.h +++ b/Engine/Window.h @@ -11,12 +11,12 @@ #include #include "graphics/Config.h" -#include "cameras/Player.h" +#include "cameras/Controller.h" class Window { public: - Window(std::shared_ptr& config, Player& player); + Window(std::shared_ptr& config, Controller& controller); ~Window(); static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); diff --git a/Engine/cameras/Camera.cpp b/Engine/cameras/Camera.cpp index 0689e96..62581b7 100644 --- a/Engine/cameras/Camera.cpp +++ b/Engine/cameras/Camera.cpp @@ -12,6 +12,7 @@ Camera::Camera() m_position = { 0.0f, 0.0f, 0.0f }; m_rotation = { 0.0f, 0.0f, 0.0f }; + m_delta_position = { 0.f, 0.f, 0.f }; m_ubo.model = {}; m_ubo.proj = {}; @@ -23,30 +24,94 @@ Camera::~Camera() { } -void Camera::updateUBO(const float& width, const float& height) +void Camera::UpdateUBO(const float& width, const float& height) { m_ubo.view = updateView(m_pitch, m_yaw, m_position); - //m_ubo.model = createModelMatrix(glm::vec3(0.f, 0.f, 0.f), m_position); // not used m_ubo.proj = createProjMatrix(width, height); m_ubo.proj[1][1] *= -1; } -void Camera::setPosition(const glm::vec3& position) +void Camera::UpdatePosition(const float& dt) +{ + m_position += m_delta_position * dt; + + m_delta_position = glm::vec3{ 0.f, 0.f, 0.f }; +} + +void Camera::MoveForward(const float& speed) +{ + glm::vec3 change = { 0.0f, 0.0f, 0.0f }; + + change.x -= -glm::sin(m_yaw) * speed; + change.z -= glm::cos(m_yaw) * speed; + + m_delta_position += change; +} + +void Camera::MoveBackward(const float& speed) +{ + glm::vec3 change = { 0.0f, 0.0f, 0.0f }; + + change.x += -glm::sin(m_yaw) * speed; + change.z += glm::cos(m_yaw) * speed; + + m_delta_position += change; +} + +void Camera::MoveRight(const float& speed) +{ + glm::vec3 change = { 0.0f, 0.0f, 0.0f }; + + change.x -= -glm::sin(m_yaw + glm::radians(90.0f)) * speed; + change.z -= glm::cos(m_yaw + glm::radians(90.0f)) * speed; + + m_delta_position += change; +} + +void Camera::MoveLeft(const float& speed) +{ + glm::vec3 change = { 0.0f, 0.0f, 0.0f }; + + change.x += -glm::sin(m_yaw + glm::radians(90.0f)) * speed; + change.z += glm::cos(m_yaw + glm::radians(90.0f)) * speed; + + m_delta_position += change; +} + +void Camera::MoveUp(const float& speed) +{ + glm::vec3 change = { 0.0f, 0.0f, 0.0f }; + + change.y += 1.0f * speed; + + m_delta_position += change; +} + +void Camera::MoveDown(const float& speed) +{ + glm::vec3 change = { 0.0f, 0.0f, 0.0f }; + + change.y -= 1.0f * speed; + + m_delta_position += change; +} + +void Camera::SetPosition(const glm::vec3& position) { m_position = position; } -const glm::vec3& Camera::getPosition() +const glm::vec3& Camera::GetPosition() { return m_position; } -void Camera::setRotation(const glm::vec3& rotation) +void Camera::SetRotation(const glm::vec3& rotation) { m_rotation = rotation; } -const glm::vec3& Camera::getRotation() +const glm::vec3& Camera::GetRotation() { return m_rotation; } diff --git a/Engine/cameras/Camera.h b/Engine/cameras/Camera.h index dbd6e53..eb06af4 100644 --- a/Engine/cameras/Camera.h +++ b/Engine/cameras/Camera.h @@ -14,13 +14,21 @@ class Camera Camera(); ~Camera(); - void updateUBO(const float& width, const float& height); + void UpdateUBO(const float& width, const float& height); + void UpdatePosition(const float& dt); - void setPosition(const glm::vec3& position); - const glm::vec3& getPosition(); + void MoveForward(const float& speed); + void MoveBackward(const float& speed); + void MoveRight(const float& speed); + void MoveLeft(const float& speed); + void MoveUp(const float& speed); + void MoveDown(const float& speed); - void setRotation(const glm::vec3& rotation); - const glm::vec3& getRotation(); + void SetPosition(const glm::vec3& position); + const glm::vec3& GetPosition(); + + void SetRotation(const glm::vec3& rotation); + const glm::vec3& GetRotation(); void setPitch(const float& pitch); const float& getPitch(); @@ -36,6 +44,7 @@ class Camera glm::vec3 m_position; glm::vec3 m_rotation; + glm::vec3 m_delta_position; float m_yaw; float m_pitch; diff --git a/Engine/cameras/Controller.cpp b/Engine/cameras/Controller.cpp new file mode 100644 index 0000000..6df04ac --- /dev/null +++ b/Engine/cameras/Controller.cpp @@ -0,0 +1,76 @@ +#include "Controller.h" + + +Controller::Controller(std::shared_ptr p_camera) +{ + mp_camera = p_camera; + + // Set default controls + std::function default_func = [this]() { mp_camera->MoveForward(2.0f); }; + SetKeyToFunc(GLFW_KEY_W, default_func); + + default_func = [this]() { mp_camera->MoveBackward(2.0f); }; + SetKeyToFunc(GLFW_KEY_S, default_func); + + default_func = [this]() { mp_camera->MoveLeft(2.0f); }; + SetKeyToFunc(GLFW_KEY_A, default_func); + + default_func = [this]() { mp_camera->MoveRight(2.0f); }; + SetKeyToFunc(GLFW_KEY_D, default_func); + + default_func = [this]() { mp_camera->MoveUp(2.0f); }; + SetKeyToFunc(GLFW_KEY_LEFT_SHIFT, default_func); + + default_func = [this]() { mp_camera->MoveDown(2.0f); }; + SetKeyToFunc(GLFW_KEY_LEFT_CONTROL, default_func); +} + + +Controller::~Controller() +{ +} + +void Controller::setInput(int32_t key, int32_t scancode, int32_t mods, int32_t action) +{ + if (action == GLFW_PRESS) + { + m_keyboard_press.set(key, true); + } + else if (action == GLFW_RELEASE) + { + m_keyboard_press.set(key, false); + } +} + +void Controller::SetKeyToFunc(const int32_t& key, std::function& func) +{ + m_actions.push_back({ key, func }); +} + +void Controller::updateRotation(const double& xpos, const double& ypos) +{ + const float sensibility = 0.005f; + + glm::vec2 mouse_delta = glm::vec2(xpos, ypos); + + mp_camera->setYaw(mp_camera->getYaw() + (mouse_delta.x * sensibility)); + mp_camera->setPitch(mp_camera->getPitch() + (mouse_delta.y * sensibility)); +} + +void Controller::Update(const float& dt) +{ + for (size_t i = 0; i < m_actions.size(); i++) + { + if (m_keyboard_press[m_actions[i].key]) + { + m_actions[i].func(); + } + } + + mp_camera->UpdatePosition(dt); +} + +uint32_t Controller::getLoadRadius() +{ + return m_load_radius; +} diff --git a/Engine/cameras/Player.h b/Engine/cameras/Controller.h similarity index 55% rename from Engine/cameras/Player.h rename to Engine/cameras/Controller.h index 4e9e0a7..216821c 100644 --- a/Engine/cameras/Player.h +++ b/Engine/cameras/Controller.h @@ -1,5 +1,5 @@ -#ifndef _PLAYER_H -#define _PLAYER_H +#ifndef _CONTROLLER_H +#define _CONTROLLER_H #include #include @@ -8,26 +8,32 @@ #include "Camera.h" -class Player +struct Actions +{ + const int32_t key; + const std::function func; +}; + +class Controller { public: - Player(std::shared_ptr p_camera); - ~Player(); + Controller(std::shared_ptr p_camera); + ~Controller(); - void setDeltaTime(const float& delta_time); void setInput(int32_t key, int32_t scancode, int32_t mods, int32_t action); + void SetKeyToFunc(const int32_t& key, std::function& func); + void Update(const float& dt); void updateRotation(const double& xpos, const double& ypos); - void updatePosition(); uint32_t getLoadRadius(); private: + std::vector m_actions; std::bitset<348> m_keyboard_press; - float m_delta_time; uint32_t m_load_radius = 4; std::shared_ptr mp_camera; }; -#endif _PLAYER_H \ No newline at end of file +#endif //!_CONTROLLER_H \ No newline at end of file diff --git a/Engine/cameras/Player.cpp b/Engine/cameras/Player.cpp deleted file mode 100644 index 10c6eed..0000000 --- a/Engine/cameras/Player.cpp +++ /dev/null @@ -1,98 +0,0 @@ -#include "Player.h" - - -Player::Player(std::shared_ptr p_camera) -{ - mp_camera = p_camera; -} - - -Player::~Player() -{ -} - -void Player::setDeltaTime(const float& delta_time) -{ - m_delta_time = delta_time; -} - -void Player::setInput(int32_t key, int32_t scancode, int32_t mods, int32_t action) -{ - if (action == GLFW_PRESS) - { - m_keyboard_press.set(key, true); - } - else if (action == GLFW_RELEASE) - { - m_keyboard_press.set(key, false); - } -} - -void Player::updateRotation(const double& xpos, const double& ypos) -{ - const float sensibility = 0.005f; - - glm::vec2 mouse_delta = glm::vec2(xpos, ypos); - - mp_camera->setYaw(mp_camera->getYaw() + (mouse_delta.x * sensibility)); - mp_camera->setPitch(mp_camera->getPitch() + (mouse_delta.y * sensibility)); - - - /*glm::vec3 forward = { 0.0f, 0.0f, 0.0f }; - forward.x = glm::cos(mp_camera->getYaw()) * glm::sin(mp_camera->getPitch()); - forward.y = glm::sin(mp_camera->getYaw()); - forward.z = glm::cos(mp_camera->getYaw()) * glm::cos(mp_camera->getPitch()); - - glm::vec3 normal = glm::vec3(glm::sin(mp_camera->getPitch() - 3.14f / 2.0f), 0, glm::cos(mp_camera->getPitch() - 3.14f / 2.0f)); - - glm::vec3 up = glm::cross(normal, forward); - - mp_camera->setRotation(up);*/ -} - -void Player::updatePosition() -{ - glm::vec3 change = { 0.0f, 0.0f, 0.0f }; - const float speed = 2.0f; - - if (m_keyboard_press[GLFW_KEY_W] == true) - { - change.x -= -glm::sin(mp_camera->getYaw()) * speed; - change.z -= glm::cos(mp_camera->getYaw()) * speed; - } - - if (m_keyboard_press[GLFW_KEY_S] == true) - { - change.x += -glm::sin(mp_camera->getYaw()) * speed; - change.z += glm::cos(mp_camera->getYaw()) * speed; - } - - if (m_keyboard_press[GLFW_KEY_A] == true) - { - change.x += -glm::sin(mp_camera->getYaw() + glm::radians(90.0f)) * speed; - change.z += glm::cos(mp_camera->getYaw() + glm::radians(90.0f)) * speed; - } - - if (m_keyboard_press[GLFW_KEY_D] == true) - { - change.x -= -glm::sin(mp_camera->getYaw() + glm::radians(90.0f)) * speed; - change.z -= glm::cos(mp_camera->getYaw() + glm::radians(90.0f)) * speed; - } - - if (m_keyboard_press[GLFW_KEY_LEFT_SHIFT] == true) - { - change.y += 1.0f * speed; - } - - if (m_keyboard_press[GLFW_KEY_LEFT_CONTROL] == true) - { - change.y -= 1.0f * speed; - } - - mp_camera->setPosition((change * m_delta_time) + mp_camera->getPosition()); -} - -uint32_t Player::getLoadRadius() -{ - return m_load_radius; -} diff --git a/Engine/graphics/Graphics.h b/Engine/graphics/Graphics.h index 834a6b0..2686602 100644 --- a/Engine/graphics/Graphics.h +++ b/Engine/graphics/Graphics.h @@ -79,6 +79,8 @@ struct Graphics VkImage depth_image; VkDeviceMemory depth_memory; VkImageView depth_view; + + VkPolygonMode polygone_mode = VK_POLYGON_MODE_FILL; }; struct Pipeline diff --git a/Engine/graphics/Shaders.h b/Engine/graphics/Shaders.h index 5d979e7..173a59c 100644 --- a/Engine/graphics/Shaders.h +++ b/Engine/graphics/Shaders.h @@ -4,7 +4,8 @@ #include #include -struct Shaders { +struct Shaders +{ std::array fragment_shader_files = { "assets/shaders/frag.spv", "assets/shaders/no_texture_shader.spv", diff --git a/Engine/renderer/Renderer.cpp b/Engine/renderer/Renderer.cpp index 704416e..3cda564 100644 --- a/Engine/renderer/Renderer.cpp +++ b/Engine/renderer/Renderer.cpp @@ -16,6 +16,7 @@ Renderer::Renderer(GLFWwindow& window, uint32_t width, uint32_t height) { WIDTH = std::make_unique(width); HEIGHT = std::make_unique(height); + m_is_updated.fill(false); m_pBufferFactory = std::make_unique(m_graphic); @@ -54,33 +55,15 @@ Renderer::~Renderer() int32_t Renderer::draw() { - vkWaitForFences(m_graphic.device, 1, &m_graphic.fences_in_flight[m_frame_index], VK_TRUE, std::numeric_limits::max()); - vkResetFences(m_graphic.device, 1, &m_graphic.fences_in_flight[m_frame_index]); - - VkResult result; if (m_graphic.validation_layer_enable) { vkQueueWaitIdle(m_graphic.present_queue); } - uint32_t image_index = 0; - result = vkAcquireNextImageKHR(m_graphic.device, m_graphic.swapchain, std::numeric_limits::max(), m_graphic.semaphores_image_available[m_frame_index], VK_NULL_HANDLE, &image_index); - - if (result == VK_ERROR_OUT_OF_DATE_KHR) - { - recreateSwapchain(); - return 1; - } - else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) - { - throw std::runtime_error("failed to acquire swap chain image!"); - } - - VkSubmitInfo submit_info = {}; submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; - VkSemaphore waitSemaphores[] = { m_graphic.semaphores_image_available[m_frame_index] }; + VkSemaphore waitSemaphores[] = { m_graphic.semaphores_image_available[m_last_image] }; VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT }; submit_info.waitSemaphoreCount = 1; @@ -88,18 +71,20 @@ int32_t Renderer::draw() submit_info.pWaitDstStageMask = waitStages; submit_info.commandBufferCount = 1; - submit_info.pCommandBuffers = &m_graphic.command_buffers[image_index]; + submit_info.pCommandBuffers = &m_graphic.command_buffers[m_current_image]; - VkSemaphore signalSemaphores[] = { m_graphic.semaphores_render_finished[m_frame_index] }; + VkSemaphore signalSemaphores[] = { m_graphic.semaphores_render_finished[m_last_image] }; submit_info.signalSemaphoreCount = 1; submit_info.pSignalSemaphores = signalSemaphores; - result = vkQueueSubmit(m_graphic.graphics_queue, 1, &submit_info, m_graphic.fences_in_flight[m_frame_index]); + vkResetFences(m_graphic.device, 1, &m_graphic.fences_in_flight[m_current_image]); + + VkResult result; + result = vkQueueSubmit(m_graphic.graphics_queue, 1, &submit_info, m_graphic.fences_in_flight[m_current_image]); if (result != VK_SUCCESS) { - std::cerr << result; throw std::runtime_error("failed to submit draw command buffer!"); } @@ -112,23 +97,43 @@ int32_t Renderer::draw() VkSwapchainKHR swapchains[] = { m_graphic.swapchain }; present_info.swapchainCount = 1; present_info.pSwapchains = swapchains; - present_info.pImageIndices = &image_index; + present_info.pImageIndices = &m_current_image; present_info.pResults = nullptr; result = vkQueuePresentKHR(m_graphic.present_queue, &present_info); + int return_code = 0; if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) { recreateSwapchain(); - return 1; + return_code = 1; } else if (result != VK_SUCCESS) { throw std::runtime_error("failed to present swap chain image!"); } - m_frame_index = (m_frame_index + 1) % MAX_FRAMES_IN_FLIGHT; - return 0; + return return_code; +} + +int32_t Renderer::AcquireNextImage() +{ + m_last_image = m_current_image; + + VkResult result; + result = vkAcquireNextImageKHR(m_graphic.device, m_graphic.swapchain, std::numeric_limits::max(), m_graphic.semaphores_image_available[m_current_image], VK_NULL_HANDLE, &m_current_image); + + if (result == VK_ERROR_OUT_OF_DATE_KHR) + { + recreateSwapchain(); + return -1; + } + else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) + { + throw std::runtime_error("failed to acquire swapchain image!"); + } + + return m_current_image; } void Renderer::setupInstance(GLFWwindow& window) @@ -166,6 +171,13 @@ void Renderer::setupCommandPool() m_commandPool = std::make_unique(m_graphic); } +void Renderer::SetPolygonFillingMode(const VkPolygonMode& mode) +{ + m_graphic.polygone_mode = mode; + + recreateSwapchain(); +} + VkDevice& Renderer::getDevice() { return m_graphic.device; @@ -208,7 +220,22 @@ std::unique_ptr& Renderer::GetPipelineFactory() const int Renderer::getFrameIndex() { - return m_frame_index; + return m_current_image; +} + +const bool& Renderer::IsUpdated(const size_t& i) +{ + return m_is_updated[i]; +} + +void Renderer::SetUpdate(const size_t& i) +{ + m_is_updated[i] = false; +} + +const uint32_t& Renderer::GetHeight() +{ + return m_swapchain->GetHeigth(); } void Renderer::destroyBuffers(Buffer & buffer) @@ -297,6 +324,8 @@ void Renderer::allocateCommandBuffers() void Renderer::beginRecordCommandBuffers(VkCommandBuffer & commandBuffer, VkFramebuffer& frameBuffer) { + vkWaitForFences(m_graphic.device, 1, &m_graphic.fences_in_flight[m_current_image], VK_TRUE, std::numeric_limits::max()); + std::array clear_values = {}; clear_values[0].color = { 0.0f, 0.0f, 0.0f, 1.0f }; clear_values[1].depthStencil = { 1.0f, 0 }; @@ -627,10 +656,8 @@ void Renderer::createImage(uint32_t width, uint32_t height, VkFormat format, VkI void Renderer::recreateSwapchain() { - vkDeviceWaitIdle(m_graphic.device); cleanSwapchain(); - m_swapchain->createSwapchain(); VkFormat format = findSupportedFormat({ VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT }, VK_IMAGE_TILING_OPTIMAL, VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT); @@ -640,10 +667,16 @@ void Renderer::recreateSwapchain() createDepthResources(); createFramebuffer(); allocateCommandBuffers(); + + m_is_updated.fill(true); } void Renderer::cleanSwapchain() { + vkDeviceWaitIdle(m_graphic.device); + vkQueueWaitIdle(m_graphic.graphics_queue); + vkQueueWaitIdle(m_graphic.present_queue); + vkDestroyImageView(m_graphic.device, m_graphic.depth_view, nullptr); vkDestroyImage(m_graphic.device, m_graphic.depth_image, nullptr); vkFreeMemory(m_graphic.device, m_graphic.depth_memory, nullptr); diff --git a/Engine/renderer/Renderer.h b/Engine/renderer/Renderer.h index e08375c..c2bab6d 100644 --- a/Engine/renderer/Renderer.h +++ b/Engine/renderer/Renderer.h @@ -36,6 +36,7 @@ class Renderer ~Renderer(); int32_t draw(); + int32_t AcquireNextImage(); void setupInstance(GLFWwindow& window); void setupDevice(); @@ -44,6 +45,8 @@ class Renderer void setupDescriptorSetLayout(); void setupCommandPool(); + void SetPolygonFillingMode(const VkPolygonMode& mode); + //getters VkDevice& getDevice(); VkDescriptorPool& getDescriptorPool(); @@ -54,6 +57,9 @@ class Renderer std::unique_ptr& getBufferFactory(); std::unique_ptr& GetPipelineFactory(); const int getFrameIndex(); + const bool& IsUpdated(const size_t& i); + void SetUpdate(const size_t& i); + const uint32_t& GetHeight(); //rendering void createVerticesBuffer(std::shared_ptr> vertices, Buffer& buffer); @@ -140,7 +146,9 @@ class Renderer std::unique_ptr mp_pipelines_manager; std::unique_ptr m_pBufferFactory; - size_t m_frame_index = 0; + uint32_t m_current_image = 0; + uint32_t m_last_image = 0; + std::array m_is_updated; }; #endif _RENDERER_H \ No newline at end of file diff --git a/Engine/renderer/VulkanPipeline.cpp b/Engine/renderer/VulkanPipeline.cpp index e68a545..b4f3e7e 100644 --- a/Engine/renderer/VulkanPipeline.cpp +++ b/Engine/renderer/VulkanPipeline.cpp @@ -105,7 +105,7 @@ void VulkanPipeline::createPipeline(Pipeline& pipeline, const std::string& shade rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; rasterizer.depthClampEnable = VK_FALSE; rasterizer.rasterizerDiscardEnable = VK_FALSE; - rasterizer.polygonMode = VK_POLYGON_MODE_FILL;//filling mode + rasterizer.polygonMode = m_graphic.polygone_mode; rasterizer.lineWidth = 1.0f; rasterizer.cullMode = VK_CULL_MODE_BACK_BIT; rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; diff --git a/Engine/renderer/VulkanSwapchain.cpp b/Engine/renderer/VulkanSwapchain.cpp index 97d8c7c..8acf8f2 100644 --- a/Engine/renderer/VulkanSwapchain.cpp +++ b/Engine/renderer/VulkanSwapchain.cpp @@ -19,6 +19,9 @@ void VulkanSwapchain::createSwapchain() VkPresentModeKHR present_mode = chooseSwapPresentMode(swapchain_support.presentModes); VkExtent2D extent = chooseSwapExtent(swapchain_support.capabilities); + WIDTH = extent.width; + HEIGHT = extent.height; + uint32_t image_count = swapchain_support.capabilities.minImageCount + 1; if (swapchain_support.capabilities.maxImageCount > 0 && image_count > swapchain_support.capabilities.maxImageCount) { @@ -79,6 +82,11 @@ void VulkanSwapchain::createSwapchain() createImagesView(); } +const uint32_t& VulkanSwapchain::GetHeigth() +{ + return HEIGHT; +} + void VulkanSwapchain::createImagesView() { m_graphic.images_view.resize(m_graphic.swapchain_images.size()); diff --git a/Engine/renderer/VulkanSwapchain.h b/Engine/renderer/VulkanSwapchain.h index 6c60fc1..f28617f 100644 --- a/Engine/renderer/VulkanSwapchain.h +++ b/Engine/renderer/VulkanSwapchain.h @@ -14,6 +14,7 @@ class VulkanSwapchain ~VulkanSwapchain(); void createSwapchain(); + const uint32_t& GetHeigth(); private: void createImagesView(); diff --git a/Engine/world/Scene.h b/Engine/world/Scene.h index 6c153b7..a1f15b0 100644 --- a/Engine/world/Scene.h +++ b/Engine/world/Scene.h @@ -5,7 +5,7 @@ #include #include "GameObject.h" -#include "../cameras/Player.h" +#include "../cameras/Controller.h" #include #include