diff --git a/src/main.cpp b/src/main.cpp index 74f6947..f8a3a19 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ //========== Includes ========== // General includes #include +#include #include #include #include // std::clamp @@ -16,6 +17,7 @@ #include #include #include +#include // Vulkan Includes #include "vulkan/vulkan.hpp" #include @@ -32,6 +34,8 @@ import vulkan_hpp; #define GLM_FORCE_DEPTH_ZERO_TO_ONE #include #include +#define GLM_ENABLE_EXPERIMENTAL +#include #include // Images #define STB_IMAGE_IMPLEMENTATION @@ -74,6 +78,18 @@ struct Vertex { vk::VertexInputAttributeDescription( 2, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, texCoord) ) }; } + + bool operator==(const Vertex& other) const { + return pos == other.pos && color == other.color && texCoord == other.texCoord; + } +}; + +template <> struct std::hash { + size_t operator() (Vertex const& vertex) const noexcept { + return ((hash() (vertex.pos) ^ + (hash() (vertex.color) << 1 )) >> 1) ^ + (hash() (vertex.texCoord) << 1 ); + } }; /* @@ -95,8 +111,6 @@ const std::vector indices = { }; */ - - //Uniform buffer struct UniformBufferObject { glm::mat4 model; @@ -832,9 +846,11 @@ class HelloTriangleApplication { throw std::runtime_error(warn + err); } + std::unordered_map uniqueVertices{}; + for (const auto& shape : shapes) { for (const auto& index : shape.mesh.indices) { - Vertex vertex; + Vertex vertex{}; vertex.pos = { attrib.vertices[3 * index.vertex_index + 0], @@ -849,13 +865,18 @@ class HelloTriangleApplication { vertex.color = {1.0f, 1.0f, 1.0f}; - vertices.push_back(vertex); - indices.push_back(indices.size()); + if (uniqueVertices.count(vertex) == 0) { + uniqueVertices[vertex] = static_cast(vertices.size()); + vertices.push_back(vertex); + } + + indices.push_back(uniqueVertices[vertex]); } } #ifndef NDEBUG std::cout << "3D model loaded !\n"; + std::cout << "3D model has " << vertices.size() << " vertices and " << indices.size() << " index \n"; #endif } @@ -896,7 +917,7 @@ class HelloTriangleApplication { vk::raii::Buffer & srcBuffer, vk::raii::Buffer & dstBuffer, vk::DeviceSize size - ) { + ){ #ifndef NDEBUG std::cout << "Copying buffer " << vk::to_string(srcBuffer.objectType) << " to " << vk::to_string(dstBuffer.objectType) << "...\n"; #endif @@ -1274,7 +1295,7 @@ class HelloTriangleApplication { float time = std::chrono::duration(currentTime - startTime).count(); UniformBufferObject ubo {}; - ubo.model = rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f)); + ubo.model = rotate(glm::mat4(1.0f), time/10 * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f)); ubo.view = lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)); ubo.proj = glm::perspective(glm::radians(45.0f), static_cast(swapChainExtent.width) / static_cast(swapChainExtent.height), 0.1f, 10.0f); ubo.proj[1][1] *= -1;