Newer
Older
ForwardPlusRenderer / src / VulkanRenderer.cpp
#include <stdafx.h>
#include "VulkanRenderer.h"
#include "Context.h"
#include "glm/gtc/random.hpp"
#include "RenderPass.h"
#include "Vertex.h"
#include "Scene.h"
#include "Model.h"
#include "RNG.h"
#include "Camera.h"

namespace fpr
{



bool VulkanRenderer::IsRunning() const
{
  return !glfwWindowShouldClose(fpr::Context::Get().GetWindow()->GetWindowHandle());
}

VulkanRenderer::VulkanRenderer()
{

}

void VulkanRenderer::SetScene(fpr::Scene* scene)
{
  fpr::Context& context = fpr::Context::Get();
  context.m_loaded_scene = scene;
  context.GetSwapChain()->RecordCommands();
  for(auto& model : scene->GetModels())
  {
    for(auto& mesh : model->GetMeshes())
    {
       auto matrix = mesh.GetMatrix();
      //auto matrix = glm::scale(glm::mat4(1), {100,100,100});
      auto index  = mesh.GetMeshIndex();
      memcpy(&context.PER_MODEL_UBO.model_matrix[index], &matrix, sizeof(glm::mat4));
      //auto debug = PER_MODEL_UBO.model_matrix[index];
      //(debug);
    }
  }

 
}


void VulkanRenderer::Draw(fpr::Camera* camera)
{
  glfwPollEvents();


  static double now;
  static double delta;
  static double last_time;


  auto& context_ref = fpr::Context::Get();
  auto  debug       = context_ref.PER_MODEL_UBO.model_matrix[0];
  (debug);


  context_ref.m_render_graph->Buffers.Get("Model" + std::to_string(context_ref.GetSwapChain()->GetCurrentFrameIdx()))
      ->Update(context_ref.PER_MODEL_UBO.model_matrix);

  camera->Update();
  m_per_frame_UBO.camera_position   = camera->GetPosition();
  m_per_frame_UBO.projection_matrix = camera->GetProjectionMatrix();
  m_per_frame_UBO.view_matrix       = camera->GetViewMatrix();


  auto result = context_ref.GetSwapChain()->SubmitFrame();
  if(result != vk::Result::eSuccess) [[unlikely]]
  {
    context_ref.RecreateSwapchain();
  }

  context_ref.m_render_graph->Buffers.Get("Camera" + std::to_string(context_ref.GetSwapChain()->GetCurrentFrameIdx()))
      ->Update(&m_per_frame_UBO);

  fpr::InputHandler::SetDeltaTime(delta);
  fpr::InputHandler::Update(context_ref.GetWindow()->GetWindowHandle());
  fpr::PointLight::Update();

  now       = glfwGetTime();
  delta     = now - last_time;
  last_time = now;
}


VulkanRenderer::~VulkanRenderer()
{
  auto&                       context_ref = fpr::Context::Get();
  [[maybe_unused]] vk::Result result      = context_ref.GetDevice()->GetLogicalDeviceHandle().waitIdle();
  assert(("Device idle state not reached upon cleanup", result == vk::Result::eSuccess));
}



} // namespace fpr