Fix index buffer

This commit is contained in:
Lillian Salehi 2024-10-14 16:24:48 -05:00
parent 43fd780e0b
commit 56fb496ec7
3 changed files with 29 additions and 10 deletions

View File

@ -86,6 +86,9 @@ namespace Global {
attributeDescriptions[2].offset = offsetof(Vertex, texCoord); attributeDescriptions[2].offset = offsetof(Vertex, texCoord);
return attributeDescriptions; return attributeDescriptions;
} }
bool operator==(const Vertex& other) const {
return pos == other.pos && color == other.color && texCoord == other.texCoord;
}
}; };
const uint32_t WIDTH = 800; const uint32_t WIDTH = 800;

View File

@ -208,10 +208,10 @@ namespace BuffersLibraries {
Global::UniformBufferObject ubo{}; Global::UniformBufferObject ubo{};
ubo.time = time; ubo.time = time;
// Modify the model projection transformation to rotate around the Z over 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. // 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. // 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. // 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); ubo.proj = glm::perspective(glm::radians(45.0f), deviceLibrary.getSwapChainExtent().width / (float) deviceLibrary.getSwapChainExtent().height, 0.1f, 10.0f);

View File

@ -1,11 +1,23 @@
#include "buffers.h"
#include <stdexcept>
#include <string>
#define TINY_OBJ_IMPLEMENTATION #define TINY_OBJ_IMPLEMENTATION
#include <tiny_obj_loader.h> #include <tiny_obj_loader.h>
#include "model.h" #include "model.h"
#include <unordered_map>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/hash.hpp>
namespace std {
template<> struct hash<Global::Vertex> {
size_t operator()(Global::Vertex const& vertex) const {
return ((hash<glm::vec3>()(vertex.pos) ^
(hash<glm::vec3>()(vertex.color) << 1)) >> 1) ^
(hash<glm::vec2>()(vertex.texCoord) << 1);
}
};
}
namespace ModelLib { namespace ModelLib {
BuffersLibraries::buffers buf;
std::unordered_map<Global::Vertex, uint32_t> uniqueVertices{};
void model::loadModel() { void model::loadModel() {
tinyobj::ObjReaderConfig readerConfig; tinyobj::ObjReaderConfig readerConfig;
@ -24,7 +36,7 @@ namespace ModelLib {
for (const auto& shape : shapes) { for (const auto& shape : shapes) {
for (const auto& index : shape.mesh.indices) { for (const auto& index : shape.mesh.indices) {
Global::Vertex vertex; Global::Vertex vertex{};
vertex.pos = { vertex.pos = {
attrib.vertices[3 * index.vertex_index + 0], attrib.vertices[3 * index.vertex_index + 0],
@ -39,8 +51,12 @@ namespace ModelLib {
vertex.color = {1.0f, 1.0f, 1.0f}; vertex.color = {1.0f, 1.0f, 1.0f};
Global::vertices.push_back(vertex); if (uniqueVertices.count(vertex) == 0) {
Global::indices.push_back(Global::indices.size()); uniqueVertices[vertex] = static_cast<uint32_t>(Global::vertices.size());
Global::vertices.push_back(vertex);
}
Global::indices.push_back(uniqueVertices[vertex]);
} }
} }
} }