Skip to content

Commit

Permalink
Add beginnings of model classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Khrysys committed Oct 27, 2023
1 parent 7736fdf commit dac6100
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 27 deletions.
15 changes: 11 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -42,9 +43,15 @@ target_include_directories(dggraphics-${DragonEngine_VERSION_MAJOR} PUBLIC

target_link_libraries(dggraphics-${DragonEngine_VERSION_MAJOR}
PUBLIC
$<BUILD_INTERFACE:glm::glm>
glfw
Dragon::Core
$<BUILD_INTERFACE:
glm::glm
glfw
Dragon::Core
>
$<INSTALL_INTERFACE:
glfw
Dragon::Core
>
)

if(BUILD_SHARED_LIBS)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion include/dragon/graphics/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Dragon::Graphics {
class Engine;
}

#include "model.hpp"
#include "window.hpp"

/**
Expand Down Expand Up @@ -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
*/
Expand Down
11 changes: 9 additions & 2 deletions include/dragon/graphics/model.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once

#include <vector>
#include "vertex.hpp"

#include "rotation.hpp"
#include "vertex.hpp"
#include "window.hpp"

namespace Dragon::Graphics {
/**
Expand All @@ -11,9 +13,14 @@ namespace Dragon::Graphics {
class DGGRAPHICSAPI Model
{
private:
Rotation rotation; /**<Represents this model's current rotation.*/
//Rotation rotation; /**<Represents this model's current rotation.*/
//std::vector<Model>; /**Child models that will be moved an equal proportion when this model moves*/
/* data */
std::vector<Vertex> verts;

Dragon::Buffer vertexBuffer;
public:
Model(Engine* parent, Window* owner, std::vector<Vertex> &verts);
void close(Engine* parent, Window* owner);
};
}
34 changes: 33 additions & 1 deletion include/dragon/graphics/vertex.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#pragma once
#include <array>

#include <dragon/core.hpp>

#include "aliases.hpp"
#include "rotation.hpp"
Expand All @@ -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<VkVertexInputAttributeDescription, 2> getAttributeDescriptions() {
std::array<VkVertexInputAttributeDescription, 2> 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;
}
};

}
23 changes: 23 additions & 0 deletions src/model.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <dragon/graphics.hpp>

Dragon::Graphics::Model::Model(Dragon::Graphics::Engine* parent, Dragon::Graphics::Window* owner, std::vector<Dragon::Graphics::Vertex> &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) {

}

4 changes: 2 additions & 2 deletions src/shaders/2d.frag.in
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 5 additions & 14 deletions src/shaders/2d.vert.in
Original file line number Diff line number Diff line change
@@ -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;
}
10 changes: 7 additions & 3 deletions src/window_finalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,17 @@ void createPipeline(Dragon::Device device, VkPipelineLayout pipelineLayout, VkRe
fragShaderStageInfo.pName = "main";

std::array<VkPipelineShaderStageCreateInfo, 2> 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<uint32_t>(attributeDescriptions.size());
vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data();
pipelineInfo.pVertexInputState = &vertexInputInfo;

VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
Expand Down

0 comments on commit dac6100

Please sign in to comment.