155 lines
4.9 KiB
C++
155 lines
4.9 KiB
C++
/*
|
|
* 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 "TextExampleApp.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 "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 <filesystem>
|
|
|
|
#ifdef _WIN32
|
|
#undef TRANSPARENT
|
|
#endif
|
|
|
|
namespace OpenVulkano
|
|
{
|
|
using namespace Scene;
|
|
using namespace Input;
|
|
using namespace Math;
|
|
namespace fs = std::filesystem;
|
|
|
|
class TextExampleAppImpl final : public TextExampleApp
|
|
{
|
|
public:
|
|
|
|
void Init() override
|
|
{
|
|
auto engineConfig = OpenVulkano::EngineConfiguration::GetEngineConfiguration();
|
|
engineConfig->SetNumThreads(4);
|
|
engineConfig->SetPreferFramebufferFormatSRGB(false);
|
|
|
|
std::srand(1); // Fix seed for random numbers
|
|
m_scene.Init();
|
|
m_cam.Init(70, 16, 9, 0.1f, 100);
|
|
m_scene.SetCamera(&m_cam);
|
|
|
|
static std::vector<std::pair<std::string, TextConfig>> texts;
|
|
texts.push_back(std::make_pair("ABab?.^{}_cdFGETG123)(", TextConfig()));
|
|
texts.push_back(std::make_pair("Hello, World!", TextConfig()));
|
|
texts.push_back(std::make_pair("\u0410\u0411\u0412\u041F", TextConfig()));
|
|
texts[0].second.applyBorder = true;
|
|
texts[1].second.backgroundColor.a = 1;
|
|
|
|
const int N = texts.size();
|
|
auto& resourceLoader = ResourceLoader::GetInstance();
|
|
const std::string fontPath = resourceLoader.GetResourcePath("Roboto-Regular.ttf");
|
|
const std::string atlasPath = (fs::path(fontPath).parent_path() / "roboto-regular-atlas.png").string();
|
|
|
|
m_nodesPool.resize(N);
|
|
m_drawablesPool.resize(N);
|
|
m_uniBuffers.resize(N);
|
|
|
|
for (int i = 0; i < N; i++)
|
|
{
|
|
m_uniBuffers[i].Init(sizeof(TextConfig), &texts[i].second, 3);
|
|
}
|
|
|
|
m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/text");
|
|
m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/text");
|
|
m_shader.AddVertexInputDescription(OpenVulkano::Vertex::GetVertexInputDescription());
|
|
m_shader.AddDescriptorSetLayoutBinding(Texture::DESCRIPTOR_SET_LAYOUT_BINDING);
|
|
m_shader.AddDescriptorSetLayoutBinding(UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING);
|
|
m_shader.alphaBlend = true;
|
|
m_shader.cullMode = CullMode::NONE;
|
|
|
|
Charset charset = Charset::ASCII;
|
|
for (unicode_t c = 0x0410; c <= 0x041F; c++)
|
|
{
|
|
// some unicode values
|
|
charset.add(c);
|
|
}
|
|
m_atlasGenerator.GenerateAtlas(fontPath, atlasPath, charset);
|
|
|
|
for (int i = 0; i < texts.size(); i++)
|
|
{
|
|
TextDrawable* t = new TextDrawable();
|
|
t->SetFontAtlasGenerator(&m_atlasGenerator);
|
|
t->SetConfig(texts[i].second);
|
|
t->SetUniformBuffer(&m_uniBuffers[i]);
|
|
t->SetShader(&m_shader);
|
|
t->GenerateText(texts[i].first);
|
|
m_drawablesPool[i] = t;
|
|
m_nodesPool[i].Init();
|
|
m_nodesPool[i].SetMatrix(Math::Utils::translate(glm::mat4x4(1.f), Vector3f(-5, 2 - i * 2, 0)));
|
|
m_nodesPool[i].AddDrawable(m_drawablesPool[i]);
|
|
m_scene.GetRoot()->AddChild(&m_nodesPool[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 Tick() override
|
|
{
|
|
m_camController.Tick();
|
|
}
|
|
|
|
void Close() override
|
|
{
|
|
for (SimpleDrawable* d: m_drawablesPool)
|
|
{
|
|
delete d;
|
|
}
|
|
}
|
|
|
|
private:
|
|
OpenVulkano::Scene::Scene m_scene;
|
|
PerspectiveCamera m_cam;
|
|
std::vector<UniformBuffer> m_uniBuffers;
|
|
OpenVulkano::FreeCamCameraController m_camController;
|
|
Shader m_shader;
|
|
FontAtlasGenerator m_atlasGenerator;
|
|
std::vector<SimpleDrawable*> 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* TextExampleApp::Create() { return new TextExampleAppImpl(); }
|
|
|
|
std::unique_ptr<IGraphicsApp> TextExampleApp::CreateUnique()
|
|
{
|
|
return std::make_unique<TextExampleAppImpl>();
|
|
}
|
|
|
|
}
|
|
|
|
#pragma clang diagnostic pop
|
|
#pragma clang diagnostic pop |