From dac610042ef8de8b043d2fa6678a0e5a328c6070 Mon Sep 17 00:00:00 2001 From: Michael Piccirillo <70709374+Khrysys@users.noreply.github.com> Date: Fri, 27 Oct 2023 14:33:35 -0400 Subject: [PATCH] Add beginnings of model classes --- CMakeLists.txt | 15 +++++++++---- include/dragon/graphics/engine.hpp | 4 +++- include/dragon/graphics/model.hpp | 11 ++++++++-- include/dragon/graphics/vertex.hpp | 34 +++++++++++++++++++++++++++++- src/model.cpp | 23 ++++++++++++++++++++ src/shaders/2d.frag.in | 4 ++-- src/shaders/2d.vert.in | 19 +++++------------ src/window_finalize.cpp | 10 ++++++--- 8 files changed, 93 insertions(+), 27 deletions(-) create mode 100644 src/model.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 83965e8..334cc37 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ add_subdirectory(external/glfw) add_library(dggraphics-${DragonEngine_VERSION_MAJOR} "src/engine.cpp" + "src/model.cpp" "src/rotation.cpp" "src/window_core.cpp" "src/window_finalize.cpp" @@ -42,9 +43,15 @@ target_include_directories(dggraphics-${DragonEngine_VERSION_MAJOR} PUBLIC target_link_libraries(dggraphics-${DragonEngine_VERSION_MAJOR} PUBLIC - $ - glfw - Dragon::Core + $ + $ ) if(BUILD_SHARED_LIBS) @@ -53,7 +60,7 @@ if(BUILD_SHARED_LIBS) endif() if(Dragon_2D_RENDER) - target_compile_definitions(dggraphics-${DragonEngine_VERSION_MAJOR} PUBLIC Dragon_2D_RENDER) + target_compile_definitions(dggraphics-${DragonEngine_VERSION_MAJOR} PUBLIC DRAGON_2D_SHADER) endif() if(Dragon_INSTALL) diff --git a/include/dragon/graphics/engine.hpp b/include/dragon/graphics/engine.hpp index be0f921..a6ff095 100644 --- a/include/dragon/graphics/engine.hpp +++ b/include/dragon/graphics/engine.hpp @@ -10,6 +10,7 @@ namespace Dragon::Graphics { class Engine; } +#include "model.hpp" #include "window.hpp" /** @@ -75,7 +76,8 @@ namespace Dragon::Graphics * @throws std::string */ Window* createWindow(int width, int height, std::string title); - + Model* createModel(Window* window, int width, int height, std::string title); + Model* createModel(size_t index, int width, int height, std::string title); /** * Reimplemented from Dragon::Submodule */ diff --git a/include/dragon/graphics/model.hpp b/include/dragon/graphics/model.hpp index 3ba06bf..2b89641 100644 --- a/include/dragon/graphics/model.hpp +++ b/include/dragon/graphics/model.hpp @@ -1,8 +1,10 @@ #pragma once #include -#include "vertex.hpp" + #include "rotation.hpp" +#include "vertex.hpp" +#include "window.hpp" namespace Dragon::Graphics { /** @@ -11,9 +13,14 @@ namespace Dragon::Graphics { class DGGRAPHICSAPI Model { private: - Rotation rotation; /**; /**Child models that will be moved an equal proportion when this model moves*/ /* data */ + std::vector verts; + + Dragon::Buffer vertexBuffer; public: + Model(Engine* parent, Window* owner, std::vector &verts); + void close(Engine* parent, Window* owner); }; } \ No newline at end of file diff --git a/include/dragon/graphics/vertex.hpp b/include/dragon/graphics/vertex.hpp index 7784670..f4afab8 100644 --- a/include/dragon/graphics/vertex.hpp +++ b/include/dragon/graphics/vertex.hpp @@ -1,4 +1,7 @@ #pragma once +#include + +#include #include "aliases.hpp" #include "rotation.hpp" @@ -19,8 +22,37 @@ namespace Dragon::Graphics { #ifndef DRAGON_2D_SHADER Dragon::vec3 position; #else - DRAGON::vec2 position; + Dragon::vec2 position; #endif + Dragon::vec4 color; + + static inline VkVertexInputBindingDescription getBindingDescription() { + VkVertexInputBindingDescription bindingDescription{}; + bindingDescription.binding = 0; + bindingDescription.stride = sizeof(Vertex); + bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; + return bindingDescription; + } + + static inline std::array getAttributeDescriptions() { + std::array attributeDescriptions{}; + + attributeDescriptions[0].binding = 0; + attributeDescriptions[0].location = 0; + #ifndef DRAGON_2D_SHADER + attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT + #else + attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT; + #endif + attributeDescriptions[0].offset = offsetof(Vertex, position); + + attributeDescriptions[1].binding = 0; + attributeDescriptions[1].location = 1; + attributeDescriptions[1].format = VK_FORMAT_R32G32B32A32_SFLOAT; + attributeDescriptions[1].offset = offsetof(Vertex, color); + + return attributeDescriptions; + } }; } \ No newline at end of file diff --git a/src/model.cpp b/src/model.cpp new file mode 100644 index 0000000..d827563 --- /dev/null +++ b/src/model.cpp @@ -0,0 +1,23 @@ +#include + +Dragon::Graphics::Model::Model(Dragon::Graphics::Engine* parent, Dragon::Graphics::Window* owner, std::vector &verts) { + VkBufferCreateInfo bufferInfo{}; + bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; + bufferInfo.size = sizeof(Dragon::Graphics::Vertex) * verts.size(); + bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; + bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; + + VmaAllocationCreateInfo allocInfo{}; + allocInfo.usage = VMA_MEMORY_USAGE_AUTO; + + VkResult result = vmaCreateBuffer(parent->getParent()->getAllocator(), &bufferInfo, &allocInfo, &this->vertexBuffer.buffer, &this->vertexBuffer.allocation, nullptr); + + if (result != VK_SUCCESS) { + throw fmt::format("vmaCreateBuffer failed with {}", string_VkResult(result)); + } +} + +void Dragon::Graphics::Model::close(Dragon::Graphics::Engine* parent, Dragon::Graphics::Window* owner) { + +} + diff --git a/src/shaders/2d.frag.in b/src/shaders/2d.frag.in index 13009da..93b04ad 100644 --- a/src/shaders/2d.frag.in +++ b/src/shaders/2d.frag.in @@ -1,9 +1,9 @@ #version 450 -layout(location = 0) in vec3 fragColor; +layout(location = 0) in vec4 fragColor; layout(location = 0) out vec4 outColor; void main() { - outColor = vec4(fragColor, 1.0); + outColor = fragColor; } \ No newline at end of file diff --git a/src/shaders/2d.vert.in b/src/shaders/2d.vert.in index 66d6766..c28f5b6 100644 --- a/src/shaders/2d.vert.in +++ b/src/shaders/2d.vert.in @@ -1,20 +1,11 @@ #version 450 -layout(location = 0) out vec3 fragColor; +layout(location = 0) in vec2 inPosition; +layout(location = 1) in vec4 inColor; -vec2 positions[3] = vec2[]( - vec2(0.0, -0.5), - vec2(0.5, 0.5), - vec2(-0.5, 0.5) -); - -vec3 colors[3] = vec3[]( - vec3(1.0, 0.0, 0.0), - vec3(0.0, 1.0, 0.0), - vec3(0.0, 0.0, 1.0) -); +layout(location = 0) out vec4 fragColor; void main() { - gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); - fragColor = colors[gl_VertexIndex]; + gl_Position = vec4(inPosition, 0.0, 1.0); + fragColor = inColor; } \ No newline at end of file diff --git a/src/window_finalize.cpp b/src/window_finalize.cpp index 0a92838..67c0114 100644 --- a/src/window_finalize.cpp +++ b/src/window_finalize.cpp @@ -92,13 +92,17 @@ void createPipeline(Dragon::Device device, VkPipelineLayout pipelineLayout, VkRe fragShaderStageInfo.pName = "main"; std::array shaderStages = {vertShaderStageInfo, fragShaderStageInfo}; - pipelineInfo.pStages = shaderStages.data(); + auto bindingDescription = Dragon::Graphics::Vertex::getBindingDescription(); + auto attributeDescriptions = Dragon::Graphics::Vertex::getAttributeDescriptions(); + VkPipelineVertexInputStateCreateInfo vertexInputInfo{}; vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; - vertexInputInfo.vertexBindingDescriptionCount = 0; - vertexInputInfo.vertexAttributeDescriptionCount = 0; + vertexInputInfo.vertexBindingDescriptionCount = 1; + vertexInputInfo.vertexAttributeDescriptionCount = static_cast(attributeDescriptions.size()); + vertexInputInfo.pVertexBindingDescriptions = &bindingDescription; + vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data(); pipelineInfo.pVertexInputState = &vertexInputInfo; VkPipelineInputAssemblyStateCreateInfo inputAssembly{};