working version of glyphs rendering with example
This commit is contained in:
172
examples/ExampleApps/TextExampleApp.cpp
Normal file
172
examples/ExampleApps/TextExampleApp.cpp
Normal file
@@ -0,0 +1,172 @@
|
||||
/*
|
||||
* 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/Text.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 "Math/Math.hpp"
|
||||
#include "Base/EngineConfiguration.hpp"
|
||||
#include "Controller/FreeCamCameraController.hpp"
|
||||
#include "Image/ImageLoaderPng.hpp"
|
||||
|
||||
#ifdef _WIN32
|
||||
#undef TRANSPARENT
|
||||
#endif
|
||||
|
||||
namespace OpenVulkano
|
||||
{
|
||||
using namespace Scene;
|
||||
using namespace Input;
|
||||
using namespace Math;
|
||||
|
||||
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);
|
||||
|
||||
m_cfg.applyBorder = true;
|
||||
//m_cfg.smoothing = 1 / 16.f;
|
||||
|
||||
const std::string symbols = "Ak?";
|
||||
const int N = symbols.size();
|
||||
const std::string font = (OpenVulkano::Utils::GetFontsDirectory() / "arial.ttf").string();
|
||||
Image::ImageLoaderPng pngLoader;
|
||||
m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3);
|
||||
m_materials.resize(N);
|
||||
m_geos.resize(N);
|
||||
m_nodesPool.resize(N);
|
||||
m_drawablesPool.resize(N);
|
||||
m_textures.resize(N);
|
||||
m_sdfs.resize(N);
|
||||
|
||||
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;
|
||||
|
||||
static float vertices[] = {
|
||||
// positions // texture coords
|
||||
-0.5f, 0.5f, 0.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 1.0f, 1.0f
|
||||
};
|
||||
uint32_t indices[] = { 1, 2, 3, 1, 3, 0 };
|
||||
|
||||
Text text = Text(m_cfg);
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
m_drawablesPool[i].SetDrawPhase(OpenVulkano::Scene::DrawPhase::BACKGROUND);
|
||||
const std::string fileName = std::string("output") + std::to_string(i + 1) + ".png";
|
||||
const std::string pngOutput = (OpenVulkano::Utils::GetBuildDirectory() / fileName).string();
|
||||
|
||||
auto& drawable = m_drawablesPool[i];
|
||||
auto& mat = m_materials[i];
|
||||
auto& tex = m_textures[i];
|
||||
auto& geo = m_geos[i];
|
||||
drawable.Init(&m_shader, &geo, &mat, &m_uniBuffer);
|
||||
|
||||
text.Init(font, symbols[i], pngOutput);
|
||||
m_sdfs[i] = pngLoader.loadFromFile(pngOutput);
|
||||
auto& sdf = m_sdfs[i];
|
||||
tex.resolution = sdf->resolution;
|
||||
tex.textureBuffer = sdf->data.Data();
|
||||
tex.format = sdf->dataFormat;
|
||||
tex.size = sdf->data.Size();
|
||||
mat.texture = &tex;
|
||||
|
||||
geo.Init(4, 6);
|
||||
for (int j = 0; j < geo.vertexCount; j++)
|
||||
{
|
||||
geo.vertices[j].position.x = vertices[j * 4];
|
||||
geo.vertices[j].position.y = vertices[j * 4 + 1];
|
||||
geo.vertices[j].position.z = 0.f;
|
||||
geo.vertices[j].textureCoordinates.x = vertices[j * 4 + 2];
|
||||
geo.vertices[j].textureCoordinates.y = vertices[j * 4 + 3];
|
||||
}
|
||||
geo.SetIndices(indices, 6);
|
||||
|
||||
m_nodesPool[i].Init();
|
||||
m_nodesPool[i].SetMatrix(Math::Utils::translate(
|
||||
glm::mat4x4(1.f), Vector3f(-3 + std::rand() % 3, -2 + std::rand() % 2, 2)));
|
||||
m_nodesPool[i].AddDrawable(&drawable);
|
||||
m_scene.GetRoot()->AddChild(&m_nodesPool[i]);
|
||||
}
|
||||
|
||||
GetGraphicsAppManager()->GetRenderer()->SetScene(&m_scene);
|
||||
m_camController.Init(&m_cam);
|
||||
m_camController.SetDefaultKeybindings();
|
||||
m_camController.SetPosition({ -2, -1, 5 });
|
||||
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
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
OpenVulkano::Scene::Scene m_scene;
|
||||
PerspectiveCamera m_cam;
|
||||
UniformBuffer m_uniBuffer;
|
||||
OpenVulkano::FreeCamCameraController m_camController;
|
||||
Shader m_shader;
|
||||
TextConfig m_cfg;
|
||||
std::vector<Material> m_materials;
|
||||
std::vector<SimpleDrawable> m_drawablesPool;
|
||||
std::vector<Geometry> m_geos;
|
||||
std::vector<Node> m_nodesPool;
|
||||
std::vector<Texture> m_textures;
|
||||
std::vector<std::unique_ptr<Image::Image>> m_sdfs;
|
||||
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
|
||||
Reference in New Issue
Block a user