Merge pull request 'Label Drawable' (#114) from label_drawable into master

Reviewed-on: https://git.madvoxel.net/OpenVulkano/OpenVulkano/pulls/114
Reviewed-by: Georg Hagen <georg.hagen@madvoxel.com>
This commit is contained in:
Oleksii_Hyzha
2024-08-28 15:08:14 +02:00
28 changed files with 936 additions and 148 deletions

View File

@@ -11,6 +11,7 @@
#include "ExampleApps/TexturedCubeExampleApp.hpp"
#include "ExampleApps/BillboardExampleApp.hpp"
#include "ExampleApps/TextExampleApp.hpp"
#include "ExampleApps/LabelDrawableExampleApp.hpp"
#include <vector>
namespace OpenVulkano
@@ -20,6 +21,7 @@ namespace OpenVulkano
{ "Moving Cube Example App", &MovingCubeApp::Create },
{ "Textured Cube Example App", &TexturedCubeExampleApp::Create },
{ "Billboard Example App", &BillboardExampleApp::Create },
{ "Text Example App", &TextExampleApp::Create }
{ "Text Example App", &TextExampleApp::Create },
{ "Label Example App", &LabelDrawableExampleApp::Create }
};
}

View File

@@ -14,6 +14,7 @@
#include "Scene/SimpleDrawable.hpp"
#include "Scene/UI/PerformanceInfo.hpp"
#include "Scene/UniformBuffer.hpp"
#include "Scene/BillboardControlBlock.hpp"
#include "Input/InputManager.hpp"
#include "Host/GraphicsAppManager.hpp"
#include "Host/GLFW/WindowGLFW.hpp"
@@ -31,12 +32,6 @@ namespace OpenVulkano
{
public:
struct BillboardControlBlock
{
Math::Vector2f quadSize;
bool isFixedSize;
};
void Init() override
{
auto engineConfig = OpenVulkano::EngineConfiguration::GetEngineConfiguration();
@@ -55,14 +50,14 @@ namespace OpenVulkano
m_quadBillboardShader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/basic");
m_quadBillboardShader.AddVertexInputDescription(OpenVulkano::Vertex::GetVertexInputDescription());
m_quadBillboardShader.AddDescriptorSetLayoutBinding(Texture::DESCRIPTOR_SET_LAYOUT_BINDING);
m_quadBillboardShader.AddDescriptorSetLayoutBinding(UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING);
m_quadBillboardShader.AddDescriptorSetLayoutBinding(UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING, 4);
m_quadBillboardShader.topology = Topology::POINT_LIST;
m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/billboard");
m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/basic");
m_shader.AddVertexInputDescription(OpenVulkano::Vertex::GetVertexInputDescription());
m_shader.AddDescriptorSetLayoutBinding(Texture::DESCRIPTOR_SET_LAYOUT_BINDING);
m_shader.AddDescriptorSetLayoutBinding(UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING);
m_shader.AddDescriptorSetLayoutBinding(UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING, 4);
m_shader.cullMode = CullMode::NONE;
constexpr int quadsCnt = 7;
@@ -72,7 +67,7 @@ namespace OpenVulkano
m_bbContolBlock.quadSize = { 100.f, 100.f };
m_bbContolBlock.isFixedSize = false;
m_uniBuffer.Init(sizeof(BillboardControlBlock), &m_bbContolBlock);
m_uniBuffer.setId = 3;
m_uniBuffer.setId = 4;
m_drawablesPool.resize(cntDrawables);
m_nodesPool.resize(cntDrawables);
m_geo.resize(cntDrawables);

View File

@@ -0,0 +1,136 @@
/*
* 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 "LabelDrawableExampleApp.hpp"
#include "Scene/Scene.hpp"
#include "Scene/Shader/Shader.hpp"
#include "Scene/Geometry.hpp"
#include "Scene/TextDrawable.hpp"
#include "Scene/GeometryFactory.hpp"
#include "Scene/Material.hpp"
#include "Scene/Vertex.hpp"
#include "Scene/SimpleDrawable.hpp"
#include "Scene/UI/PerformanceInfo.hpp"
#include "Scene/UniformBuffer.hpp"
#include "Scene/Prefabs/LabelDrawable.hpp"
#include "Input/InputManager.hpp"
#include "Host/GraphicsAppManager.hpp"
#include "Host/GLFW/WindowGLFW.hpp"
#include "Host/ResourceLoader.hpp"
#include "Math/Math.hpp"
#include "Base/EngineConfiguration.hpp"
#include "Controller/FreeCamCameraController.hpp"
#include "Image/ImageLoaderPng.hpp"
#include "Scene/FontAtlasGenerator.hpp"
#include "Scene/IFontAtlasGenerator.hpp"
#include <filesystem>
#ifdef _WIN32
#undef TRANSPARENT
#endif
namespace OpenVulkano
{
using namespace Scene;
using namespace Input;
using namespace Math;
namespace fs = std::filesystem;
class LabelDrawableExampleAppImpl final : public LabelDrawableExampleApp
{
public:
void Init() override
{
auto engineConfig = OpenVulkano::EngineConfiguration::GetEngineConfiguration();
engineConfig->SetNumThreads(1);
engineConfig->SetPreferFramebufferFormatSRGB(false);
std::srand(1); // Fix seed for random numbers
m_scene.Init();
m_cam.Init(70, 16, 9, 0.1, 100);
m_scene.SetCamera(&m_cam);
auto& resourceLoader = ResourceLoader::GetInstance();
auto sdfMetadataInfo = resourceLoader.GetResource("sdf_atlas_packed.png");
TextDrawable textDrawable(sdfMetadataInfo);
const std::vector<std::string> texts = { "_!?{}.#@", "1", "XYZ", "12345" };
const int N = texts.size();
m_nodesPool.resize(N);
m_drawablesPool.reserve(N);
BillboardControlBlock billboardSettings;
LabelDrawableSettings labelSettings;
for (int i = 0; i < N; i++)
{
if (i == 3)
{
labelSettings.hasRoundedCorners = labelSettings.hasArrow = true;
}
else
{
labelSettings.hasRoundedCorners = (i % 2 == 0 ? 0 : 1);
labelSettings.hasArrow = (i % 2 == 0 ? 1 : 0);
}
bool isBillboard = (i % 2 == 0 ? 1 : 0);
LabelDrawable& label = m_drawablesPool.emplace_back(textDrawable.GetAtlasData(), labelSettings, isBillboard);
label.SetBillboardSettings(billboardSettings);
label.AddText(texts[i]);
if (i == 2)
{
for (int j = 0; j < 3; j++)
{
label.AddText("Additional text" + std::to_string(j));
}
}
m_scene.GetRoot()->AddChild(&m_nodesPool[i]);
m_nodesPool[i].SetMatrix(Math::Utils::translate(glm::mat4x4(1.f), Vector3f(-5 + std::rand() % 5, -5 + std::rand() % 5, -std::rand() % 10)));
m_nodesPool[i].AddDrawable(&m_drawablesPool[i]);
}
GetGraphicsAppManager()->GetRenderer()->SetScene(&m_scene);
m_camController.Init(&m_cam);
m_camController.SetDefaultKeybindings();
m_camController.SetPosition({ 0, 0, 10 });
m_camController.SetBoostFactor(5);
std::shared_ptr<OpenVulkano::Scene::UI::PerformanceInfo> m_perfInfo =
std::make_shared<OpenVulkano::Scene::UI::PerformanceInfo>();
m_ui.AddElement(m_perfInfo);
GetGraphicsAppManager()->GetRenderer()->SetActiveUi(&m_ui);
}
void Close() override {}
void Tick() override
{
m_camController.Tick();
}
private:
OpenVulkano::Scene::Scene m_scene;
PerspectiveCamera m_cam;
OpenVulkano::FreeCamCameraController m_camController;
std::vector<LabelDrawable> m_drawablesPool;
std::vector<Node> m_nodesPool;
Vector3f_SIMD m_position = { 0, 0, -10 };
OpenVulkano::Scene::UI::SimpleUi m_ui;
std::shared_ptr<OpenVulkano::Scene::UI::PerformanceInfo> m_perfInfo;
};
IGraphicsApp* LabelDrawableExampleApp::Create() { return new LabelDrawableExampleAppImpl(); }
std::unique_ptr<IGraphicsApp> LabelDrawableExampleApp::CreateUnique()
{
return std::make_unique<LabelDrawableExampleAppImpl>();
}
}
#pragma clang diagnostic pop
#pragma clang diagnostic pop

View File

@@ -0,0 +1,25 @@
/*
* 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 "Base/IGraphicsApp.hpp"
#include <memory>
namespace OpenVulkano
{
class LabelDrawableExampleApp : public IGraphicsApp
{
public:
static IGraphicsApp* Create();
static std::unique_ptr<IGraphicsApp> CreateUnique();
[[nodiscard]] std::string GetAppName() const final { return "Label drawable ExampleApp"; }
[[nodiscard]] OpenVulkano::Version GetAppVersion() const final { return { "v1.0" }; }
};
}

View File

@@ -38,6 +38,8 @@ namespace OpenVulkano
using namespace Math;
namespace fs = std::filesystem;
//#define CREATE_NEW_ATLAS 1
class TextExampleAppImpl final : public TextExampleApp
{
public:
@@ -68,12 +70,14 @@ namespace OpenVulkano
m_nodesPool.resize(N * 2);
m_drawablesPool.resize(N * 2);
#ifdef MSDFGEN_AVAILABLE
#if defined(MSDFGEN_AVAILABLE) && defined(CREATE_NEW_ATLAS)
msdf_atlas::Charset charset = SdfFontAtlasGenerator::LoadAllGlyphs(fontPath);
m_atlasGenerator.GenerateAtlas(fontPath, charset);
m_msdfAtlasGenerator.GenerateAtlas(fontPath, charset);
m_atlasGenerator.SaveAtlasMetadataInfo("sdf_atlas.png");
m_msdfAtlasGenerator.SaveAtlasMetadataInfo("msdf_atlas");
#else
auto sdfMetadataInfo = resourceLoader.GetResource("sdf_atlas_packed");
auto sdfMetadataInfo = resourceLoader.GetResource("sdf_atlas_packed.png");
auto msdfMetadataInfo = resourceLoader.GetResource("msdf_atlas_packed.png");
#endif
@@ -81,15 +85,15 @@ namespace OpenVulkano
{
int textIdx = i % texts.size();
TextDrawable* t = nullptr;
#ifdef MSDFGEN_AVAILABLE
#if defined(MSDFGEN_AVAILABLE) && defined(CREATE_NEW_ATLAS)
if (i < texts.size())
{
t = new TextDrawable(&m_atlasGenerator, texts[textIdx].second);
t = new TextDrawable(m_atlasGenerator.GetAtlasData(), texts[textIdx].second);
t->SetShader(&TextDrawable::GetSdfDefaultShader());
}
else
{
t = new TextDrawable(&m_msdfAtlasGenerator, texts[textIdx].second);
t = new TextDrawable(m_msdfAtlasGenerator.GetAtlasData(), texts[textIdx].second);
t->SetShader(&TextDrawable::GetMsdfDefaultShader());
}
#else

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1012 KiB

After

Width:  |  Height:  |  Size: 1012 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 449 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 631 KiB