From 3ad6037fe222c8c96725ba34625b7f39f48293f2 Mon Sep 17 00:00:00 2001 From: Georg Hagen Date: Fri, 18 Apr 2025 17:55:11 +0200 Subject: [PATCH] Add Origin Indicator drawable --- .../Scene/Prefabs/OriginIndicatorDrawable.hpp | 46 +++++++++++++++++++ openVulkanoCpp/Shader/originIndicator.vert | 23 ++++++++++ .../OriginIndicatorDrawableVulkanEncoder.cpp | 29 ++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 openVulkanoCpp/Scene/Prefabs/OriginIndicatorDrawable.hpp create mode 100644 openVulkanoCpp/Shader/originIndicator.vert create mode 100644 openVulkanoCpp/Vulkan/Scene/OriginIndicatorDrawableVulkanEncoder.cpp diff --git a/openVulkanoCpp/Scene/Prefabs/OriginIndicatorDrawable.hpp b/openVulkanoCpp/Scene/Prefabs/OriginIndicatorDrawable.hpp new file mode 100644 index 0000000..95c33a4 --- /dev/null +++ b/openVulkanoCpp/Scene/Prefabs/OriginIndicatorDrawable.hpp @@ -0,0 +1,46 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include "Scene/Drawable.hpp" +#include "Scene/Shader/Shader.hpp" +#include "Scene/Node.hpp" + +namespace OpenVulkano::Scene +{ + class OriginIndicatorDrawable final : public Drawable + { + Shader m_shader; + + public: + OriginIndicatorDrawable() + : Drawable(DrawEncoder::GetDrawEncoder(), DrawPhase::MAIN) + { + m_shader.cullMode = CullMode::NONE; + m_shader.topology = Topology::LINE_LIST; + m_shader.AddShaderProgram(ShaderProgramType::VERTEX, "Shader/originIndicator"); + m_shader.AddShaderProgram(ShaderProgramType::FRAGMENT, "Shader/basic"); + SetShader(&m_shader); + } + }; + + class OriginIndicatorNode final : public Node + { + OriginIndicatorDrawable drawable; + + public: + OriginIndicatorNode(float size = 0.25f) : Node(Math::Utils::scale(Math::Vector3f(size, size, size))) + { + AddChild(&drawable); + } + + void SetSize(float size = 0.25f) + { + SetMatrix(Math::Utils::scale(Math::Vector3f(size, size, size))); + } + }; +} diff --git a/openVulkanoCpp/Shader/originIndicator.vert b/openVulkanoCpp/Shader/originIndicator.vert new file mode 100644 index 0000000..0c51ec5 --- /dev/null +++ b/openVulkanoCpp/Shader/originIndicator.vert @@ -0,0 +1,23 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable + +layout(set = 0, binding = 0) uniform NodeData +{ + mat4 world; +} node; + +layout(set = 1, binding = 0) uniform CameraData +{ + mat4 viewProjection; +} cam; + +layout(location = 0) out vec4 color; + +void main() +{ + int i = gl_VertexIndex & -2; + color = vec4(i == 0, i == 2, i == 4, 1); + vec4 position = color * (gl_VertexIndex & 1); + position.w = 1; + gl_Position = cam.viewProjection * node.world * position; +} diff --git a/openVulkanoCpp/Vulkan/Scene/OriginIndicatorDrawableVulkanEncoder.cpp b/openVulkanoCpp/Vulkan/Scene/OriginIndicatorDrawableVulkanEncoder.cpp new file mode 100644 index 0000000..dafb540 --- /dev/null +++ b/openVulkanoCpp/Vulkan/Scene/OriginIndicatorDrawableVulkanEncoder.cpp @@ -0,0 +1,29 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include "Scene/Prefabs/OriginIndicatorDrawable.hpp" +#include "VulkanGeometry.hpp" +#include "Vulkan/VulkanDrawContext.hpp" + +using namespace OpenVulkano::Scene; + +namespace OpenVulkano::Vulkan +{ + void EncodeOriginIndicatorDrawable(Drawable* instance, Vulkan::VulkanDrawContext* drawContext) + { + for(Node* node : instance->GetNodes()) + { + if (!node->IsEnabled()) [[unlikely]] continue; + drawContext->EncodeNode(node); + drawContext->commandBuffer.draw(6, 1, 0, 0); + } + } +} + +namespace +{ + void* gridDrawableVulkanEncoderReg = DrawEncoder::RegisterVulkanEncodeFunction(&OpenVulkano::Vulkan::EncodeOriginIndicatorDrawable); +} \ No newline at end of file