diff --git a/assets/player.fbx b/assets/player.fbx index 8d9bef7..5576d9e 100644 Binary files a/assets/player.fbx and b/assets/player.fbx differ diff --git a/include/vulkan/graphics.h b/include/vulkan/graphics.h index ee6e298..7a41c31 100644 --- a/include/vulkan/graphics.h +++ b/include/vulkan/graphics.h @@ -39,6 +39,7 @@ class Graphics final { BufferHandle CreateIndexBuffer(gsl::span indices); void DestroyBuffer(BufferHandle handle); TextureHandle CreateTexture(gsl::czstring path); + TextureHandle CreateTexture(gsl::span image_file_data); void DestroyTexture(TextureHandle handle); private: diff --git a/include/vulkan/model.h b/include/vulkan/model.h new file mode 100644 index 0000000..038fe4b --- /dev/null +++ b/include/vulkan/model.h @@ -0,0 +1,12 @@ +#pragma once +#include + +#include "vertex.h" + +namespace veng { +struct Model { + std::vector vertices; + std::vector indices; + glm::mat4 transform; +}; +} // namespace veng diff --git a/src/main.cpp b/src/main.cpp index 02e1dee..82865d1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include "assimp/Importer.hpp" #include "assimp/postprocess.h" +#include "assimp/scene.h" #include "glfw/glfw_initialization.h" #include "glfw/glfw_monitor.h" #include "glfw/glfw_window.h" @@ -23,21 +24,31 @@ std::int32_t main(std::int32_t argc, gsl::zstring* argv) { const struct aiScene* scene = assetImporter.ReadFile( "assets/player.fbx", aiProcess_CalcTangentSpace | aiProcess_Triangulate | - aiProcess_JoinIdenticalVertices | aiProcess_SortByPType); + aiProcess_JoinIdenticalVertices | + aiProcess_SortByPType); - if (nullptr == scene) { + if (scene == nullptr || !scene->HasMeshes()) { spdlog::error(assetImporter.GetErrorString()); return false; } - std::array vertices = { - veng::Vertex{{-.5f, -.5f, 0.f}, {0.f, 1.f}}, - veng::Vertex{{.5f, -.5f, 0.f}, {1.f, 1.f}}, - veng::Vertex{{-.5f, .5f, 0.f}, {0.f, 0.f}}, - veng::Vertex{{.5f, .5f, 0.f}, {1.f, 0.f}}}; + std::vector vertices; + aiMesh* mesh = scene->mMeshes[0]; + for (std::uint32_t i = 0; i < mesh->mNumVertices; i++) { + vertices.emplace_back( + glm::vec3{mesh->mVertices[i].x, mesh->mVertices[i].y, + mesh->mVertices[i].z}, + glm::vec2{mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y}); + } veng::BufferHandle buffer = graphics.CreateVertexBuffer(vertices); - std::array indices = {0, 3, 2, 0, 1, 3}; + std::vector indices; + for (std::uint32_t i = 0; i < mesh->mNumFaces; i++) { + aiFace face = mesh->mFaces[i]; + for (unsigned int j = 0; j < face.mNumIndices; ++j) { + indices.push_back(face.mIndices[j]); + } + } veng::BufferHandle index_buffer = graphics.CreateIndexBuffer(indices); glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), glm::radians(45.f), @@ -46,7 +57,9 @@ std::int32_t main(std::int32_t argc, gsl::zstring* argv) { glm::mat4 rotation2 = glm::rotate(glm::mat4(1.0f), glm::radians(0.f), glm::vec3(0.f, 0.f, 1.f)); - glm::mat4 view = glm::translate(glm::mat4(1.f), glm::vec3(0.f, 0.f, -2.f)); + glm::mat4 view = + glm::lookAt(glm::vec3(0.f, 0.f, 10.f), glm::vec3(0.f, 0.f, 0.f), + glm::vec3(0.f, 1.f, 0.f)); glm::mat4 projection = glm::perspective(glm::radians(150.f), 800.f / 600.f, 0.1f, 100.f); graphics.SetViewProjection(view, projection); @@ -61,7 +74,8 @@ std::int32_t main(std::int32_t argc, gsl::zstring* argv) { glfwPollEvents(); glm::ivec2 current_window_size = window.GetFramebufferSize(); - if (current_window_size != window_size && current_window_size.x != 0 && current_window_size.y != 0) { + if (current_window_size != window_size && current_window_size.x != 0 && + current_window_size.y != 0) { window_size = current_window_size; projection = glm::perspective( glm::radians(90.f), diff --git a/src/vulkan/texture.cpp b/src/vulkan/texture.cpp index 927a558..0fe3fab 100644 --- a/src/vulkan/texture.cpp +++ b/src/vulkan/texture.cpp @@ -31,9 +31,13 @@ void Graphics::CreateTextureSampler() { } TextureHandle Graphics::CreateTexture(gsl::czstring path) { + std::vector data = ReadFile(path); + return CreateTexture({data.data(), data.size()}); +} + +TextureHandle Graphics::CreateTexture(gsl::span image_file_data) { glm::ivec2 image_extents; std::int32_t channels; - std::vector image_file_data = ReadFile(path); stbi_uc* pixel_data = stbi_load_from_memory( image_file_data.data(), image_file_data.size(), &image_extents.x, &image_extents.y, &channels, STBI_rgb_alpha);