CText.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <GL/glew.h>
  5. #include <glm/vec2.hpp>
  6. #include <glm/vec4.hpp>
  7. #include "WallpaperEngine/Render/Objects/CRenderable.h"
  8. #include "WallpaperEngine/Render/Objects/Effects/CPass.h"
  9. #include "WallpaperEngine/Render/Objects/TextLayout.h"
  10. #include "WallpaperEngine/Scripting/ScriptEngine.h"
  11. #include "WallpaperEngine/Scripting/ScriptableObject.h"
  12. namespace WallpaperEngine::Render {
  13. class TextureProvider;
  14. }
  15. namespace WallpaperEngine::Render::Wallpapers {
  16. class CScene;
  17. }
  18. namespace WallpaperEngine::Render::Objects {
  19. using namespace WallpaperEngine::Data::Model;
  20. /**
  21. * Text objects the way wallpaper64.exe 2.8.42 draws them: HarfBuzz shaped glyphs from an atlas (plain coverage, or
  22. * MSDF when msdf/outline/blur/drop shadow is on) drawn with WE's own materials/fonts materials, an optional opaque
  23. * background quad, and when the text has effects everything goes through a buffer the size of the text box plus
  24. * padding first, the way images run their effects.
  25. */
  26. class CText final : public CRenderable, public Scripting::ScriptableObject {
  27. friend CObject;
  28. public:
  29. CText (Wallpapers::CScene& scene, const Text& text);
  30. ~CText () override;
  31. void setup () override;
  32. void render () override;
  33. [[nodiscard]] const float& getBrightness () const override;
  34. [[nodiscard]] const float& getUserAlpha () const override;
  35. [[nodiscard]] const float& getAlpha () const override;
  36. [[nodiscard]] const glm::vec3& getColor () const override;
  37. [[nodiscard]] const glm::vec4& getColor4 () const override;
  38. [[nodiscard]] const glm::vec3& getCompositeColor () const override;
  39. [[nodiscard]] const Text& getText () const { return m_text; }
  40. /** The composite quad's model view projection as of the last transform update */
  41. [[nodiscard]] const glm::mat4& getCompositeMatrix () const { return m_compositeMatrix; }
  42. /** Cursor hit test against the text's box as it is drawn, ndc is in the scene buffer's clip space */
  43. [[nodiscard]] bool hitTest (const glm::vec2& ndc);
  44. /** A cursor event's localPosition: from the top left of the box hitTest uses, where the cursor meets its plane */
  45. [[nodiscard]] glm::vec2 cursorLocalPosition (const glm::vec2& ndc);
  46. private:
  47. /** The parts of the text's settings that change which passes exist */
  48. struct PassLayout {
  49. bool msdf = false;
  50. bool outline = false;
  51. bool blur = false;
  52. bool dropShadow = false;
  53. bool background = false;
  54. /** there are colour glyphs, drawn from the colour atlas with the basefontrgba materials */
  55. bool color = false;
  56. /** the depth tested materials (3D scenes, depthtest "enabled") */
  57. bool depth = false;
  58. /** effects or a blend mode: the text goes through a buffer of its box plus padding first */
  59. bool buffered = false;
  60. int blendMode = 0;
  61. glm::ivec2 bufferSize = { 0, 0 };
  62. bool operator== (const PassLayout&) const = default;
  63. };
  64. [[nodiscard]] TextLayoutParams currentParams () const;
  65. [[nodiscard]] PassLayout currentPassLayout () const;
  66. [[nodiscard]] glm::vec2 currentPadding () const;
  67. [[nodiscard]] glm::vec2 screenAnchorOffset () const;
  68. /** Scene and buffer matrices of the glyphs and the composite for this frame's transform */
  69. void updateTransform ();
  70. void updateBufferMatrix ();
  71. void relayout (const std::string& text);
  72. void uploadGeometry ();
  73. void updateRenderVars ();
  74. void buildPasses ();
  75. void destroyPasses ();
  76. Effects::CPass* createFontPass (const std::shared_ptr<const CFBO>& destination, const glm::mat4* mvp, bool color);
  77. bool loadFont ();
  78. void initScriptLayer ();
  79. const Text& m_text;
  80. std::string m_lastRenderedText;
  81. TextLayoutParams m_params;
  82. TextLayout m_layout;
  83. TextLayoutResult m_result;
  84. PassLayout m_passLayout;
  85. Scripting::ScriptLayerHandle m_layerHandle = Scripting::kInvalidLayerHandle;
  86. /** Backs the scripts' layer.font */
  87. DynamicValue m_font;
  88. std::string m_loadedFont;
  89. std::shared_ptr<TextureProvider> m_atlas;
  90. std::shared_ptr<TextureProvider> m_colorAtlas;
  91. /** MSDF mode: the colour glyphs' colours at the layout's colour scale, g_Texture1 of the colour pass */
  92. std::shared_ptr<TextureProvider> m_colorTexture;
  93. MaterialUniquePtr m_fontMaterial;
  94. MaterialUniquePtr m_colorFontMaterial;
  95. MaterialUniquePtr m_backgroundMaterial;
  96. MaterialUniquePtr m_clearAlphaMaterial;
  97. MaterialUniquePtr m_passthroughMaterial;
  98. ImageEffectPassOverride m_passthroughOverride = { .id = -1 };
  99. ImageEffectPassOverride m_fontOverride = { .id = -1 };
  100. GLuint m_glyphPositions = 0;
  101. GLuint m_glyphTexcoords = 0;
  102. GLsizei m_glyphVertexCount = 0;
  103. GLuint m_colorGlyphPositions = 0;
  104. GLuint m_colorGlyphTexcoords = 0;
  105. GLsizei m_colorGlyphVertexCount = 0;
  106. GLuint m_backgroundPositions = 0;
  107. // effect passes run FBO -> FBO with an identity matrix like CImage, the composite quad is the buffer's size
  108. GLuint m_passSpacePosition = 0;
  109. GLuint m_compositePosition = 0;
  110. GLuint m_quadTexcoords = 0;
  111. GLuint m_compositeTexcoords = 0;
  112. std::vector<Effects::CPass*> m_passes = {};
  113. /** the last passes, the ones putting the buffer onto the scene */
  114. size_t m_compositePassCount = 0;
  115. /** another object lists this text in its dependencies: it renders through its buffer, even while hidden */
  116. bool m_isDependency = false;
  117. std::shared_ptr<const CFBO> m_mainFBO = nullptr;
  118. std::shared_ptr<const CFBO> m_subFBO = nullptr;
  119. std::shared_ptr<const CFBO> m_currentMainFBO = nullptr;
  120. std::shared_ptr<const CFBO> m_currentSubFBO = nullptr;
  121. glm::mat4 m_glyphSceneMatrix = glm::mat4 (1.0f);
  122. glm::mat4 m_glyphSceneMatrixInverse = glm::mat4 (1.0f);
  123. glm::mat4 m_glyphBufferMatrix = glm::mat4 (1.0f);
  124. glm::mat4 m_glyphBufferMatrixInverse = glm::mat4 (1.0f);
  125. glm::mat4 m_compositeMatrix = glm::mat4 (1.0f);
  126. glm::mat4 m_compositeMatrixInverse = glm::mat4 (1.0f);
  127. glm::mat4 m_modelViewProjectionPass = glm::mat4 (1.0f);
  128. glm::mat4 m_modelMatrix = glm::mat4 (1.0f);
  129. /** Composite quad -> scene, what m_compositeMatrix projects */
  130. glm::mat4 m_compositeModel = glm::mat4 (1.0f);
  131. glm::mat4 m_viewProjectionMatrix = glm::mat4 (1.0f);
  132. glm::vec4 m_renderVars[4] = {};
  133. glm::vec3 m_backgroundColor = { 0.0f, 0.0f, 0.0f };
  134. float m_backgroundAlpha = 1.0f;
  135. glm::vec4 m_backgroundColor4 = { 0.0f, 0.0f, 0.0f, 1.0f };
  136. const glm::vec4 m_white = { 1.0f, 1.0f, 1.0f, 1.0f };
  137. mutable glm::vec3 m_colorCache {};
  138. mutable glm::vec4 m_color4Cache = { 1.0f, 1.0f, 1.0f, 1.0f };
  139. bool m_initialized = false;
  140. };
  141. } // namespace WallpaperEngine::Render::Objects