diff --git a/src/global.h b/src/global.h index 92f0880..8813357 100644 --- a/src/global.h +++ b/src/global.h @@ -86,6 +86,9 @@ namespace Global { attributeDescriptions[2].offset = offsetof(Vertex, texCoord); return attributeDescriptions; } + bool operator==(const Vertex& other) const { + return pos == other.pos && color == other.color && texCoord == other.texCoord; + } }; const uint32_t WIDTH = 800; diff --git a/src/graphics/buffers.cpp b/src/graphics/buffers.cpp index fc049ff..1770a38 100644 --- a/src/graphics/buffers.cpp +++ b/src/graphics/buffers.cpp @@ -208,10 +208,10 @@ namespace BuffersLibraries { Global::UniformBufferObject ubo{}; ubo.time = time; // Modify the model projection transformation to rotate around the Z over time. - ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(20.0f), glm::vec3(0.0f, 0.0f, 1.0f)); + ubo.model = glm::rotate(glm::mat4(1.0f), glm::radians(180.0f), glm::vec3(0.0f, 0.0f, 1.0f)); // Modify the view transformation to look at the object from above at a 45 degree angle. // This takes the eye position, center position, and the up direction. - ubo.view = glm::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.view = glm::lookAt(glm::vec3(4.0f, 4.0f, 3.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)); // 45 degree field of view, set aspect ratio, and near and far clipping range. ubo.proj = glm::perspective(glm::radians(45.0f), deviceLibrary.getSwapChainExtent().width / (float) deviceLibrary.getSwapChainExtent().height, 0.1f, 10.0f); diff --git a/src/graphics/model.cpp b/src/graphics/model.cpp index 6d5646b..2c6136c 100644 --- a/src/graphics/model.cpp +++ b/src/graphics/model.cpp @@ -1,11 +1,23 @@ -#include "buffers.h" -#include -#include #define TINY_OBJ_IMPLEMENTATION #include #include "model.h" +#include +#define GLM_ENABLE_EXPERIMENTAL +#include + +namespace std { + template<> struct hash { + size_t operator()(Global::Vertex const& vertex) const { + return ((hash()(vertex.pos) ^ + (hash()(vertex.color) << 1)) >> 1) ^ + (hash()(vertex.texCoord) << 1); + } + }; +} + namespace ModelLib { - BuffersLibraries::buffers buf; + + std::unordered_map uniqueVertices{}; void model::loadModel() { tinyobj::ObjReaderConfig readerConfig; @@ -24,7 +36,7 @@ namespace ModelLib { for (const auto& shape : shapes) { for (const auto& index : shape.mesh.indices) { - Global::Vertex vertex; + Global::Vertex vertex{}; vertex.pos = { attrib.vertices[3 * index.vertex_index + 0], @@ -39,9 +51,13 @@ namespace ModelLib { vertex.color = {1.0f, 1.0f, 1.0f}; - Global::vertices.push_back(vertex); - Global::indices.push_back(Global::indices.size()); - } + if (uniqueVertices.count(vertex) == 0) { + uniqueVertices[vertex] = static_cast(Global::vertices.size()); + Global::vertices.push_back(vertex); + } + + Global::indices.push_back(uniqueVertices[vertex]); + } } } }