Model loading added
This commit is contained in:
131
src/main.cpp
131
src/main.cpp
@@ -1,26 +1,32 @@
|
||||
#include "GlfwWindowManager.h"
|
||||
#include "VulkanContext.hpp"
|
||||
#include "VulkanDeviceManager.h"
|
||||
#include "Primitives.h"
|
||||
#include "Logger.h"
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vulkan/vulkan_core.h>
|
||||
#include "VulkanDeviceManager.h"
|
||||
#include "imgui.h"
|
||||
#include "imgui_impl_glfw.h"
|
||||
#include "imgui_impl_vulkan.h"
|
||||
#include "tiny_obj_loader.h"
|
||||
|
||||
#include "utilities/Logger.h"
|
||||
#include "GlfwWindowManager.h"
|
||||
#include "VulkanContext.hpp"
|
||||
#include "Primitives.h"
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
struct AppConfig
|
||||
{
|
||||
std::string Title = "Learning Vulkan";
|
||||
uint32_t Width = 800;
|
||||
uint32_t Height = 600;
|
||||
bool bResizable = true;
|
||||
bool bFullscreen = false;
|
||||
bool bValidationEnabled = false;
|
||||
bool bVerboseLogging = false;
|
||||
std::string Title = "Learning Vulkan";
|
||||
uint32_t Width = 800;
|
||||
uint32_t Height = 600;
|
||||
bool bResizable = true;
|
||||
bool bFullscreen = false;
|
||||
bool bValidationEnabled = false;
|
||||
bool bVerboseLogging = false;
|
||||
const std::string MODEL_PATH = "models/viking_room.obj";
|
||||
const std::string TEXTURE_PATH = "textures/viking_room.png";
|
||||
};
|
||||
|
||||
// const std::vector<Vertex> TriangleVertices = {
|
||||
@@ -29,25 +35,26 @@ struct AppConfig
|
||||
// { { -0.5f, 0.5f }, { 0.0f, 0.0f, 1.0f } }
|
||||
// };
|
||||
|
||||
const std::vector<Vertex> SquareVertices = {
|
||||
{ { -0.5f, -0.5f, 0.0f }, { 1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f } },
|
||||
{ { 0.5f, -0.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f } },
|
||||
{ { 0.5f, 0.5f, 0.0f }, { 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f } },
|
||||
{ { -0.5f, 0.5f, 0.0f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f } },
|
||||
// const std::vector<Vertex> SquareVertices = {
|
||||
// { { -0.5f, -0.5f, 0.0f }, { 1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f } },
|
||||
// { { 0.5f, -0.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f } },
|
||||
// { { 0.5f, 0.5f, 0.0f }, { 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f } },
|
||||
// { { -0.5f, 0.5f, 0.0f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f } },
|
||||
//
|
||||
// { { -0.5f, -0.5f, -0.5f }, { 1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f } },
|
||||
// { { 0.5f, -0.5f, -0.5f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f } },
|
||||
// { { 0.5f, 0.5f, -0.5f }, { 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f } },
|
||||
// { { -0.5f, 0.5f, -0.5f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f } }
|
||||
// };
|
||||
|
||||
{ { -0.5f, -0.5f, -0.5f }, { 1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f } },
|
||||
{ { 0.5f, -0.5f, -0.5f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f } },
|
||||
{ { 0.5f, 0.5f, -0.5f }, { 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f } },
|
||||
{ { -0.5f, 0.5f, -0.5f }, { 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f } }
|
||||
};
|
||||
|
||||
const std::vector<uint16_t> SquareIndices = { 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4 };
|
||||
// const std::vector<uint16_t> SquareIndices = { 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4 };
|
||||
|
||||
class HelloTriangleApplication
|
||||
{
|
||||
public:
|
||||
void Run()
|
||||
{
|
||||
LoadModel();
|
||||
Initialization();
|
||||
Log::Info("Initialization finished...");
|
||||
MainLoop();
|
||||
@@ -63,6 +70,9 @@ private:
|
||||
bool bShowImGui = false;
|
||||
bool bShowDemo = false;
|
||||
|
||||
std::vector<Vertex> Vertices;
|
||||
std::vector<uint32_t> Indices;
|
||||
|
||||
static void KeyCallback(GLFWwindow* Window, int Key, int Scancode, int Action, int Mods)
|
||||
{
|
||||
auto App = reinterpret_cast<HelloTriangleApplication*>(glfwGetWindowUserPointer(Window));
|
||||
@@ -87,6 +97,62 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
void LoadModel()
|
||||
{
|
||||
tinyobj::attrib_t Attribute;
|
||||
std::vector<tinyobj::shape_t> Shapes;
|
||||
std::vector<tinyobj::material_t> Materials;
|
||||
std::string Error;
|
||||
std::string Warning;
|
||||
|
||||
if (!tinyobj::LoadObj(
|
||||
&Attribute,
|
||||
&Shapes,
|
||||
&Materials,
|
||||
&Warning,
|
||||
&Error,
|
||||
Settings.MODEL_PATH.c_str()
|
||||
))
|
||||
{
|
||||
Log::Error(Error);
|
||||
}
|
||||
|
||||
Log::Info("Parsing model...");
|
||||
Log::Info("Models : " + std::to_string(Shapes.size()));
|
||||
|
||||
std::unordered_map<Vertex, uint32_t> UniqueVertices{};
|
||||
|
||||
for (const auto& Shape : Shapes)
|
||||
{
|
||||
Log::Info("Models vertices : " + std::to_string(Shape.mesh.indices.size()));
|
||||
for (const auto& Index : Shape.mesh.indices)
|
||||
{
|
||||
Vertex Vertex{};
|
||||
|
||||
Vertex.Position = { Attribute.vertices[3 * Index.vertex_index + 0],
|
||||
Attribute.vertices[3 * Index.vertex_index + 1],
|
||||
Attribute.vertices[3 * Index.vertex_index + 2] };
|
||||
|
||||
Vertex.TextureCoordinates = {
|
||||
Attribute.texcoords[2 * Index.texcoord_index + 0],
|
||||
1.0 - Attribute.texcoords[2 * Index.texcoord_index + 1]
|
||||
};
|
||||
|
||||
Vertex.Color = { 1.0f, 1.0f, 1.0f };
|
||||
|
||||
if (UniqueVertices.count(Vertex) == 0)
|
||||
{
|
||||
UniqueVertices[Vertex] = static_cast<uint32_t>(Vertices.size());
|
||||
Vertices.push_back(Vertex);
|
||||
}
|
||||
|
||||
// Vertices.push_back(Vertex);
|
||||
Indices.push_back(UniqueVertices[Vertex]);
|
||||
}
|
||||
}
|
||||
Log::Info("Final size : " + std::to_string(Vertices.size()));
|
||||
}
|
||||
|
||||
void Initialization()
|
||||
{
|
||||
InitGlfw();
|
||||
@@ -100,12 +166,13 @@ private:
|
||||
|
||||
void InitVulkan()
|
||||
{
|
||||
FVulkanConfig Config = {
|
||||
Settings.bValidationEnabled,
|
||||
Settings.bVerboseLogging,
|
||||
WindowManager.GetWindow(),
|
||||
};
|
||||
VkContext.Initialize(Config, SquareVertices, SquareIndices);
|
||||
FVulkanConfig Config = { Settings.bValidationEnabled,
|
||||
Settings.bVerboseLogging,
|
||||
WindowManager.GetWindow(),
|
||||
Settings.MODEL_PATH,
|
||||
Settings.TEXTURE_PATH };
|
||||
|
||||
VkContext.Initialize(Config, Vertices, Indices);
|
||||
}
|
||||
|
||||
void InitGlfw()
|
||||
@@ -155,7 +222,7 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
VkContext.DrawFrame(bShowImGui, SquareVertices.size(), SquareIndices.size());
|
||||
VkContext.DrawFrame(bShowImGui, Vertices.size(), Indices.size());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -120,7 +120,7 @@ void VulkanBuffers::RecordCommandBuffer(FRecordBuffersParams& Params)
|
||||
VkBuffer VertexBuffers[] = { Params.InVertexBuffer };
|
||||
VkDeviceSize Offsets[] = { 0 };
|
||||
vkCmdBindVertexBuffers(Params.InCommandBuffer, 0, 1, VertexBuffers, Offsets);
|
||||
vkCmdBindIndexBuffer(Params.InCommandBuffer, Params.InIndexBuffer, 0, VK_INDEX_TYPE_UINT16);
|
||||
vkCmdBindIndexBuffer(Params.InCommandBuffer, Params.InIndexBuffer, 0, VK_INDEX_TYPE_UINT32);
|
||||
|
||||
vkCmdBindDescriptorSets(
|
||||
Params.InCommandBuffer,
|
||||
@@ -397,7 +397,7 @@ void VulkanBuffers::CreateVertexBuffer(const std::vector<Vertex>& InVertices)
|
||||
vkFreeMemory(Config.Device, StagingBufferMemory, nullptr);
|
||||
}
|
||||
|
||||
void VulkanBuffers::CreateIndexBuffer(const std::vector<uint16_t>& InIndices)
|
||||
void VulkanBuffers::CreateIndexBuffer(const std::vector<uint32_t>& InIndices)
|
||||
{
|
||||
VkDeviceSize BufferSize = sizeof(InIndices[0]) * InIndices.size();
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ VulkanContext::~VulkanContext() {}
|
||||
void VulkanContext::Initialize(
|
||||
FVulkanConfig& InConfig,
|
||||
const std::vector<Vertex>& InVertices,
|
||||
const std::vector<uint16_t>& InIndices
|
||||
const std::vector<uint32_t>& InIndices
|
||||
)
|
||||
{
|
||||
Config = InConfig;
|
||||
@@ -134,8 +134,7 @@ void VulkanContext::Initialize(
|
||||
));
|
||||
Framebuffers->CreateFramebuffers(Textures->GetDepthImageView());
|
||||
|
||||
Log::Info("textures");
|
||||
Textures->LoadFromFile("textures/texture.jpg");
|
||||
Textures->LoadFromFile(Config.TexturePath.c_str());
|
||||
Textures->CreateImageView();
|
||||
Textures->CreateSampler();
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@ public:
|
||||
VkDeviceMemory& BufferMemory
|
||||
);
|
||||
void CreateVertexBuffer(const std::vector<Vertex>& InVertices);
|
||||
void CreateIndexBuffer(const std::vector<uint16_t>& InIndices);
|
||||
void CreateIndexBuffer(const std::vector<uint32_t>& InIndices);
|
||||
void CreateUniformBuffers(const uint32_t MAX_FRAMES_IN_FLIGHT);
|
||||
|
||||
uint32_t FindMemoryType(uint32_t TypeFilter, VkMemoryPropertyFlags Properties);
|
||||
|
||||
@@ -27,6 +27,8 @@ struct FVulkanConfig
|
||||
bool bValidationEnabled = true;
|
||||
bool bVerboseLogging = false;
|
||||
GLFWwindow* Window = nullptr;
|
||||
std::string ModelPath;
|
||||
std::string TexturePath;
|
||||
// std::vector<Vertex> Vertices;
|
||||
};
|
||||
|
||||
@@ -50,7 +52,7 @@ public:
|
||||
void Initialize(
|
||||
FVulkanConfig& InConfig,
|
||||
const std::vector<Vertex>& InVertices,
|
||||
const std::vector<uint16_t>& InIndices
|
||||
const std::vector<uint32_t>& InIndices
|
||||
);
|
||||
void Cleanup();
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
#include <glm/glm.hpp>
|
||||
#include <vulkan/vulkan_core.h>
|
||||
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
#include <glm/gtx/hash.hpp>
|
||||
|
||||
#include <array>
|
||||
|
||||
struct Vertex
|
||||
@@ -40,8 +43,27 @@ struct Vertex
|
||||
|
||||
return AttributeDescriptions;
|
||||
}
|
||||
|
||||
bool operator==(const Vertex& Other) const
|
||||
{
|
||||
return Position == Other.Position && Color == Other.Color
|
||||
&& TextureCoordinates == Other.TextureCoordinates;
|
||||
}
|
||||
};
|
||||
|
||||
namespace std
|
||||
{
|
||||
template <> struct hash<Vertex>
|
||||
{
|
||||
size_t operator()(Vertex const& Vertex) const
|
||||
{
|
||||
return ((hash<glm::vec3>()(Vertex.Position) ^ (hash<glm::vec3>()(Vertex.Color) << 1))
|
||||
>> 1)
|
||||
^ (hash<glm::vec2>()(Vertex.TextureCoordinates) << 1);
|
||||
}
|
||||
};
|
||||
} // namespace std
|
||||
|
||||
struct UniformBufferObject
|
||||
{
|
||||
alignas(16) glm::mat4 Model;
|
||||
|
||||
3
src/utilities/tiny_obj_loader_impl.cpp
Normal file
3
src/utilities/tiny_obj_loader_impl.cpp
Normal file
@@ -0,0 +1,3 @@
|
||||
// tinyobj_loader_impl.cpp
|
||||
#define TINYOBJLOADER_IMPLEMENTATION
|
||||
#include "tiny_obj_loader.h"
|
||||
Reference in New Issue
Block a user