Added custom colors

This commit is contained in:
Pasha Bibko
2025-11-21 23:04:51 +00:00
parent ff7f3e86ee
commit 2eb42da357
8 changed files with 29 additions and 5 deletions

1
.idea/vcs.xml generated
View File

@@ -3,5 +3,6 @@
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/cmake-build-debug/_deps/glfw-src" vcs="Git" />
<mapping directory="$PROJECT_DIR$/cmake-build-release/_deps/glfw-src" vcs="Git" />
</component>
</project>

View File

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

View File

@@ -39,7 +39,7 @@ int main()
const std::vector<uint32_t> indices = {0, 1, 2};
VulkanManager::CreateNewRenderObject(vertices, indices);
VulkanManager::CreateNewRenderObject({1.0f, 0.0f, 0.0f, 1.0f}, vertices, indices);
const std::vector otherVertices = {
-0.9f, 0.0f,
@@ -47,7 +47,7 @@ int main()
0.0f, -0.9f
};
VulkanManager::CreateNewRenderObject(otherVertices, indices);
VulkanManager::CreateNewRenderObject({1.0f, 1.0f, 0.0f, 1.0f}, otherVertices, indices);
/* Polls window events whilst it is still open */
while (!glfwWindowShouldClose(window))

View File

@@ -1,7 +1,11 @@
#version 450
layout(location = 0) out vec4 outColor;
layout(push_constant) uniform PC {
vec4 color;
} pc;
void main()
{
outColor = vec4(1.0, 0.0, 0.0, 1.0); // red
outColor = pc.color;
}

9
src/RendererTypes.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
namespace PB::Renderer
{
struct Color
{
float r = 0.0f, g = 0.0f, b = 0.0f, a = 0.0f;
};
}

View File

@@ -1,5 +1,9 @@
#pragma once
/* Includes general project .h files */
#include "RendererTypes.h"
/* Includes dependencies */
#include <vulkan/vulkan.h>

View File

@@ -95,7 +95,7 @@ namespace PB::Renderer
*/
static bool RenderPass(GLFWwindow* window);
static void CreateNewRenderObject(const std::vector<float>& vertices, const std::vector<uint32_t>& indices);
static void CreateNewRenderObject(Color color, const std::vector<float>& vertices, const std::vector<uint32_t>& indices);
private:
// === Internal helper structs === //
@@ -108,6 +108,8 @@ namespace PB::Renderer
VkBuffer IndexBuffer;
VkDeviceMemory IndexBufferMemory;
Color DrawColor;
uint32_t IndexCount;
};

View File

@@ -18,10 +18,11 @@ namespace PB::Renderer
}
}
void VulkanManager::CreateNewRenderObject(const std::vector<float>& vertices, const std::vector<uint32_t>& indices)
void VulkanManager::CreateNewRenderObject(const Color color, const std::vector<float>& vertices, const std::vector<uint32_t>& indices)
{
RenderObject obj{};
obj.IndexCount = indices.size();
obj.DrawColor = color;
BufferCreationInfo vertexCreationInfo;
vertexCreationInfo.size = vertices.size() * sizeof(float);;
@@ -130,6 +131,8 @@ namespace PB::Renderer
vkCmdBindVertexBuffers(cmd, 0, 1, &vertexBuffer, offsets);
vkCmdBindIndexBuffer(cmd, renderObject.IndexBuffer, 0, VK_INDEX_TYPE_UINT32);
vkCmdPushConstants(cmd, s_PipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(Color), &renderObject.DrawColor);
vkCmdDrawIndexed(cmd, renderObject.IndexCount, 1, 0, 0, 0);
}