Added mesh type and updated colors

This commit is contained in:
Pasha Bibko
2025-11-23 18:05:59 +00:00
parent 02fe285a9d
commit ed9d01010a
6 changed files with 63 additions and 29 deletions

View File

@@ -61,6 +61,7 @@ add_executable(VulkanRenderer
src/managers/vulkan/VulkanManager.h
src/VulkanRenderer.h
src/RendererTypes.h
src/Mesh.h
)
add_dependencies(VulkanRenderer CompileShaders)

11
src/Mesh.h Normal file
View File

@@ -0,0 +1,11 @@
#pragma once
#include <vector>
namespace PB::Renderer
{
struct Mesh
{
std::vector<float> vertices;
std::vector<int> indices;
};
}

View File

@@ -6,4 +6,15 @@ namespace PB::Renderer
{
float r = 0.0f, g = 0.0f, b = 0.0f, a = 0.0f;
};
namespace Colors
{
constexpr Color white = {1.0f, 1.0f, 1.0f, 1.0f};
constexpr Color red = {1.0f, 0.0f, 0.0f, 1.0f};
constexpr Color green = {0.0f, 1.0f, 0.0f, 1.0f};
constexpr Color blue = {0.0f, 0.0f, 1.0f, 1.0f};
constexpr Color yellow = {1.0f, 1.0f, 0.0f, 1.0f};
constexpr Color cyan = {0.0f, 1.0f, 1.0f, 1.0f};
constexpr Color magenta = {1.0f, 0.0f, 1.0f, 1.0f};
}
}

View File

@@ -1,3 +1,4 @@
#include "Mesh.h"
#include "VulkanRenderer.h"
#include "managers/GLFWManager.h"
@@ -31,23 +32,35 @@ int main()
CleanupAllAndExit(EXIT_FAILURE);
/* Adds runtime objects */
const std::vector vertices = {
0.0f, -0.5f,
0.5f, 0.9f,
-0.5f, 0.5f
};
VulkanManager::CreateNewRenderObject
(
Colors::red, Mesh
{
.vertices =
{
0.0f, -0.5f,
0.5f, 0.9f,
-0.5f, 0.5f
},
const std::vector<uint32_t> indices = {0, 1, 2};
.indices = { 0, 1, 2 }
}
);
VulkanManager::CreateNewRenderObject({1.0f, 0.0f, 0.0f, 1.0f}, vertices, indices);
VulkanManager::CreateNewRenderObject
(
Colors::yellow, Mesh
{
.vertices =
{
-0.9f, 0.0f,
-0.9f, -0.9f,
0.0f, -0.9f
},
const std::vector otherVertices = {
-0.9f, 0.0f,
-0.9f, -0.9f,
0.0f, -0.9f
};
VulkanManager::CreateNewRenderObject({1.0f, 1.0f, 0.0f, 1.0f}, otherVertices, indices);
.indices = { 0, 1, 2 }
}
);
/* Polls window events whilst it is still open */
while (!glfwWindowShouldClose(window))

View File

@@ -4,10 +4,7 @@
namespace PB::Renderer
{
struct Vertex2D
{
float x, y;
};
struct Mesh;
class VulkanManager
{
@@ -95,7 +92,7 @@ namespace PB::Renderer
*/
static bool RenderPass(GLFWwindow* window);
static void CreateNewRenderObject(Color color, const std::vector<float>& vertices, const std::vector<uint32_t>& indices);
static void CreateNewRenderObject(Color color, Mesh mesh);
private:
// === Internal helper structs === //

View File

@@ -1,4 +1,5 @@
#include "VulkanManager.h"
#include "../../Mesh.h"
#define CHECK_RESULT(res) if (res != VK_SUCCESS) { return res; }
@@ -18,31 +19,31 @@ namespace PB::Renderer
}
}
void VulkanManager::CreateNewRenderObject(const Color color, const std::vector<float>& vertices, const std::vector<uint32_t>& indices)
void VulkanManager::CreateNewRenderObject(const Color color, Mesh mesh)
{
RenderObject obj{};
obj.IndexCount = indices.size();
obj.IndexCount = mesh.indices.size();
obj.DrawColor = color;
BufferCreationInfo vertexCreationInfo;
vertexCreationInfo.size = vertices.size() * sizeof(float);;
BufferCreationInfo vertexCreationInfo{};
vertexCreationInfo.size = mesh.vertices.size() * sizeof(float);;
vertexCreationInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
vertexCreationInfo.properties = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
CreateBuffer(obj.VertexBuffer, obj.VertexBufferMemory, vertexCreationInfo);
void* data;
vkMapMemory(s_Device, obj.VertexBufferMemory, 0, vertices.size() * sizeof(float), 0, &data);
std::memcpy(data, vertices.data(), vertices.size() * sizeof(float));
vkMapMemory(s_Device, obj.VertexBufferMemory, 0, mesh.vertices.size() * sizeof(float), 0, &data);
std::memcpy(data, mesh.vertices.data(), mesh.vertices.size() * sizeof(float));
vkUnmapMemory(s_Device, obj.VertexBufferMemory);
BufferCreationInfo indexCreationInfo;
indexCreationInfo.size = indices.size() * sizeof(uint32_t);
BufferCreationInfo indexCreationInfo{};
indexCreationInfo.size = mesh.indices.size() * sizeof(uint32_t);
indexCreationInfo.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
indexCreationInfo.properties = VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT;
CreateBuffer(obj.IndexBuffer, obj.IndexBufferMemory, indexCreationInfo);
vkMapMemory(s_Device, obj.IndexBufferMemory, 0, indices.size() * sizeof(uint32_t), 0, &data);
std::memcpy(data, indices.data(), indices.size() * sizeof(uint32_t));
vkMapMemory(s_Device, obj.IndexBufferMemory, 0, mesh.indices.size() * sizeof(uint32_t), 0, &data);
std::memcpy(data, mesh.indices.data(), mesh.indices.size() * sizeof(uint32_t));
vkUnmapMemory(s_Device, obj.IndexBufferMemory);
s_RenderObjects.push_back(obj);