Added models

This commit is contained in:
Alexandre 2026-04-13 17:27:35 +02:00
parent 4a49e774ec
commit 7ba0db3e0f

View File

@ -1,6 +1,7 @@
//========== Includes ========== //========== Includes ==========
// General includes // General includes
#include <cstring> #include <cstring>
#include <functional>
#include <iostream> #include <iostream>
#include <array> #include <array>
#include <algorithm> // std::clamp #include <algorithm> // std::clamp
@ -16,6 +17,7 @@
#include <vector> #include <vector>
#include <stdexcept> #include <stdexcept>
#include <cstdlib> #include <cstdlib>
#include <unordered_map>
// Vulkan Includes // Vulkan Includes
#include "vulkan/vulkan.hpp" #include "vulkan/vulkan.hpp"
#include <vulkan/vulkan_core.h> #include <vulkan/vulkan_core.h>
@ -32,6 +34,8 @@ import vulkan_hpp;
#define GLM_FORCE_DEPTH_ZERO_TO_ONE #define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/hash.hpp>
#include <chrono> #include <chrono>
// Images // Images
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
@ -74,6 +78,18 @@ struct Vertex {
vk::VertexInputAttributeDescription( 2, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, texCoord) ) 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<Vertex> {
size_t operator() (Vertex const& vertex) const noexcept {
return ((hash<glm::vec3>() (vertex.pos) ^
(hash<glm::vec3>() (vertex.color) << 1 )) >> 1) ^
(hash<glm::vec2>() (vertex.texCoord) << 1 );
}
}; };
/* /*
@ -95,8 +111,6 @@ const std::vector<uint16_t> indices = {
}; };
*/ */
//Uniform buffer //Uniform buffer
struct UniformBufferObject { struct UniformBufferObject {
glm::mat4 model; glm::mat4 model;
@ -832,9 +846,11 @@ class HelloTriangleApplication {
throw std::runtime_error(warn + err); throw std::runtime_error(warn + err);
} }
std::unordered_map<Vertex, uint32_t> uniqueVertices{};
for (const auto& shape : shapes) { for (const auto& shape : shapes) {
for (const auto& index : shape.mesh.indices) { for (const auto& index : shape.mesh.indices) {
Vertex vertex; Vertex vertex{};
vertex.pos = { vertex.pos = {
attrib.vertices[3 * index.vertex_index + 0], attrib.vertices[3 * index.vertex_index + 0],
@ -849,13 +865,18 @@ class HelloTriangleApplication {
vertex.color = {1.0f, 1.0f, 1.0f}; 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(indices.size()); }
indices.push_back(uniqueVertices[vertex]);
} }
} }
#ifndef NDEBUG #ifndef NDEBUG
std::cout << "3D model loaded !\n"; std::cout << "3D model loaded !\n";
std::cout << "3D model has " << vertices.size() << " vertices and " << indices.size() << " index \n";
#endif #endif
} }
@ -1274,7 +1295,7 @@ class HelloTriangleApplication {
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count(); float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
UniformBufferObject ubo {}; 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.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<float>(swapChainExtent.width) / static_cast<float>(swapChainExtent.height), 0.1f, 10.0f); ubo.proj = glm::perspective(glm::radians(45.0f), static_cast<float>(swapChainExtent.width) / static_cast<float>(swapChainExtent.height), 0.1f, 10.0f);
ubo.proj[1][1] *= -1; ubo.proj[1][1] *= -1;