diff --git a/CMakeLists.txt b/CMakeLists.txt index 636970f..c449caa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -189,6 +189,8 @@ endfunction () add_chapter (main SHADER shaders/main TEXTURES texture.jpg + TEXTURES viking_room.png + MODELS viking_room.obj ) #add_chapter (38_ray_tracing diff --git a/src/main.cpp b/src/main.cpp index 05009e9..713a5fb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include #include // std::numeric_limits #include // to load shaders +#include #include #include #include @@ -35,11 +36,16 @@ import vulkan_hpp; // Images #define STB_IMAGE_IMPLEMENTATION #include +// OBJ loader +#define TINYOBJLOADER_IMPLEMENTATION +#include // Consts constexpr uint32_t WIDTH = 800; constexpr uint32_t HEIGHT = 600; constexpr int MAX_FRAMES_IN_FLIGHT = 2; +const std::string MODEL_PATH = "models/viking_room.obj"; +const std::string TEXTURE_PATH = "textures/viking_room.png"; const std::vector validationLayers = { "VK_LAYER_KHRONOS_validation" @@ -70,6 +76,7 @@ struct Vertex { } }; +/* const std::vector vertices = { {{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}}, {{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}}, @@ -86,6 +93,8 @@ const std::vector indices = { 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4 }; +*/ + //Uniform buffer @@ -142,7 +151,7 @@ class HelloTriangleApplication { vk::raii::DeviceMemory vertexBufferMemory = nullptr; // Index - Buffers vk::raii::Buffer indexBuffer = nullptr; - vk::raii::DeviceMemory indexBufferMemory = nullptr; + vk::raii::DeviceMemory indexBufferMemory = nullptr;s // Uniform Buffers - Buffers std::vector uniformBuffers; std::vector uniformBuffersMemory; @@ -163,6 +172,9 @@ class HelloTriangleApplication { // Indexes uint32_t queueIndex = ~0; uint32_t frameIndex = 0; + // Vertices + std::vector vertices; + std::vector indices; // Bools bool framebufferResized = false; @@ -679,7 +691,7 @@ class HelloTriangleApplication { std::cout << "Creating a texture image...\n"; #endif int texWidth, texHeight, texChannels; - stbi_uc* pixels = stbi_load("textures/texture.jpg", &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); + stbi_uc* pixels = stbi_load(TEXTURE_PATH.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); vk::DeviceSize imageSize = texWidth * texHeight * 4; if (!pixels) { @@ -805,6 +817,25 @@ class HelloTriangleApplication { #endif } + void loadModel() { + #ifdef DEBUG + std::cout << "Loading the 3D model...\n"; + #endif + + tinyobj::attrib_t attrib; + std::vector shapes; + std::vector materials; + std::string warn, err; + + if (!tinyobj::LoadObj(&attrib, &matrials, &warn, &err, MODEL_PATH.c_str())) { + throw std::runtime_error(warn + err); + } + + #ifdef DEBUG + std::cout << "3D model loaded !\n"; + #endif + } + void createVertexBuffer() { #ifdef DEBUG std::cout << "Creating a vertex buffer...\n"; @@ -1075,7 +1106,7 @@ class HelloTriangleApplication { commandBuffer.beginRendering(renderingInfo); commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, graphicsPipeline ); commandBuffer.bindVertexBuffers(0, *vertexBuffer, {0}); - commandBuffer.bindIndexBuffer( *indexBuffer, 0, vk::IndexType::eUint16 ); + commandBuffer.bindIndexBuffer( *indexBuffer, 0, vk::IndexType::eUint32 ); // Dynamic ViewPort commandBuffer.setViewport(0, vk::Viewport(0.0f, 0.0f, static_cast(swapChainExtent.width), static_cast(swapChainExtent.height), 0.0f, 1.0f)); commandBuffer.setScissor(0, vk::Rect2D(vk::Offset2D(0,0), swapChainExtent)); diff --git a/textures/viking_room.png b/textures/viking_room.png new file mode 100644 index 0000000..6a879ea Binary files /dev/null and b/textures/viking_room.png differ