diff options
author | chai <chaifix@163.com> | 2021-11-02 09:30:25 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-11-02 09:30:25 +0800 |
commit | b7511abf1a1f302b5c7ebf50faaf65b62d7bb6ed (patch) | |
tree | 7265398e248b55ede4b3550ec47660be334bf1ff | |
parent | a11097e51fcaef4488218411f2d7b3ee34550000 (diff) |
* TextMesh
-rw-r--r-- | Data/Resources/Shaders/Editor-Text.glsl | 3 | ||||
-rw-r--r-- | Runtime/GUI/Font.h | 12 | ||||
-rw-r--r-- | Runtime/GUI/TextMesh.cpp | 90 | ||||
-rw-r--r-- | Runtime/GUI/TextMesh.h | 23 | ||||
-rw-r--r-- | Runtime/GUI/TextMeshGenerator.h | 14 | ||||
-rw-r--r-- | Runtime/Graphics/Color.h | 14 | ||||
-rw-r--r-- | Runtime/Graphics/VertexAttribute.cpp | 3 | ||||
-rw-r--r-- | Runtime/Graphics/VertexAttribute.h | 9 | ||||
-rw-r--r-- | Runtime/Graphics/VertexBuffer.cpp | 6 | ||||
-rw-r--r-- | Runtime/Graphics/VertexBuffer.h | 4 | ||||
-rw-r--r-- | Runtime/Rendering/Quad.cpp | 3 | ||||
-rw-r--r-- | Runtime/Utilities/Exception.h | 16 |
12 files changed, 171 insertions, 26 deletions
diff --git a/Data/Resources/Shaders/Editor-Text.glsl b/Data/Resources/Shaders/Editor-Text.glsl index bc71fff..e2c2ae7 100644 --- a/Data/Resources/Shaders/Editor-Text.glsl +++ b/Data/Resources/Shaders/Editor-Text.glsl @@ -5,7 +5,7 @@ Cull Off Blend SrcAlpha OneMinusSrcAlpha DepthTest Off CMD_END - + uniform mat4 gamelab_mat_mvp; uniform sampler2D uiTex; uniform vec2 uiText_ST; @@ -13,6 +13,7 @@ uniform vec2 uiText_ST; VSH_BEGIN layout (location = 0) in vec2 vPos; layout (location = 1) in vec2 vUV; +layout (location = 2) in vec4 vColor; out vec2 uv; diff --git a/Runtime/GUI/Font.h b/Runtime/GUI/Font.h index 344cc00..9fea14e 100644 --- a/Runtime/GUI/Font.h +++ b/Runtime/GUI/Font.h @@ -15,10 +15,10 @@ //https://github.com/kaienfr/Font struct Character { - unsigned int atlas; // atlas索引 - Rect position; // 在altas里的位置 + unsigned int atlas; // atlas索引 + Rect position; // 在altas里的位置 Vector2 bearing; // 左上角相对于原点的偏移 - unsigned int advance; // 总宽,算上了间隔 + unsigned int advance; // 总宽,算上了间隔 }; namespace character @@ -42,6 +42,12 @@ namespace character }; } +struct UnicodeString +{ + character::Codepoint* str; + int length; +}; + namespace std { template <> diff --git a/Runtime/GUI/TextMesh.cpp b/Runtime/GUI/TextMesh.cpp index e69de29..6f3b3d6 100644 --- a/Runtime/GUI/TextMesh.cpp +++ b/Runtime/GUI/TextMesh.cpp @@ -0,0 +1,90 @@ +#include "../Graphics/CustomVertexLayout.h" +#include "Runtime/Utilities/StaticInitiator.h" +#include "../Math/Math.h" +#include "../Graphics/Color.h" +#include "TextMesh.h" +#include <vector> +#include <unordered_map> + +using namespace std; + +struct TextMeshVBOLayout +{ + Vector2 position; + Vector2 uv; + Color32 color; +}; + +static CustomVertexLayout s_TextMeshVBOLayout; + +static unsigned int s_VertexPerText; +static unsigned int s_SizePerVertex; +static unsigned int s_SizePerText; +static unsigned int s_IndicesPerText; + +struct TextInfo { + const Character* ch; + float offset; +}; + +static unordered_map<unsigned int, vector<TextInfo>> s_TextInfos; + +InitializeStaticVariables([]() { + VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 2, VertexAttrFormat_Float, sizeof(TextMeshVBOLayout)); + VertexAttributeDescriptor UV = VertexAttributeDescriptor(sizeof(Vector2), 2, VertexAttrFormat_Float, sizeof(TextMeshVBOLayout)); + VertexAttributeDescriptor COLOR = VertexAttributeDescriptor(sizeof(Vector2)*2, 4, VertexAttrFormat_Unsigned_Byte, sizeof(TextMeshVBOLayout), true); + s_TextMeshVBOLayout.attributes.push_back(POSITION); + s_TextMeshVBOLayout.attributes.push_back(UV); + s_TextMeshVBOLayout.attributes.push_back(COLOR); + + s_VertexPerText = 4; + s_SizePerVertex = sizeof(TextMeshVBOLayout); + s_SizePerText = sizeof(TextMeshVBOLayout) * 4; + s_IndicesPerText = 6; +}); + +TextMesh::TextMesh(const UnicodeString& str, Font* font,int pixelSize, ETextAnchor anchor, ETextAlignment alignment) +{ + m_Font = font; + s_TextInfos.clear(); + + float offset = 0; + for (int i = 0; i < str.length; ++i) + { + character::Codepoint c = str.str[i]; + const Character* ch = font->GetCharacter(c, pixelSize); + unsigned int index = ch->atlas; + + TextInfo info; + info.ch = ch; + info.offset = offset; + + auto list = s_TextInfos.find(index); + if (list == s_TextInfos.end()) + { + s_TextInfos.insert(std::pair<unsigned int, vector<TextInfo>>(index, vector<TextInfo>())); + list = s_TextInfos.find(index); + } + vector<TextInfo>& v = list->second; + v.push_back(info); + + offset += ch->advance; + } + + if (s_TextInfos.size() == 0) + return; + + for (auto iter : s_TextInfos) { + unsigned int index = iter.first; + vector<TextInfo>& texts = iter.second; + int count = texts.size(); + + VertexBuffer* vb = new VertexBuffer(count * s_SizePerText, count * s_IndicesPerText, VertexBuffer::VertexBufferType_Static); + + } +} + +void TextMesh::Draw() +{ + +} diff --git a/Runtime/GUI/TextMesh.h b/Runtime/GUI/TextMesh.h index 2ca4618..8a29c92 100644 --- a/Runtime/GUI/TextMesh.h +++ b/Runtime/GUI/TextMesh.h @@ -1,5 +1,10 @@ #pragma once
#include "../Graphics/VertexBuffer.h"
+#include "Font.h"
+#include "Runtime/Utilities/Exception.h"
+#include <unordered_map>
+
+CustomException(TextMeshException);
enum ETextAnchor
{
@@ -22,11 +27,27 @@ enum ETextAlignment { TextAlignment_Auto, }; +typedef unsigned long long TextMeshHash;
+
+namespace TextHelper
+{
+ TextMeshHash GetTextMeshHash();
+}
+ +// 一段文字里面的网格可能会来自不同的atlas,在生成TextMesh时做好合批 + class TextMesh { public: + TextMesh(const UnicodeString& str, Font* font, int pixelSize, ETextAnchor anchor, ETextAlignment alignment)/*throw TextMeshException*/; + + ~TextMesh(); + + void Draw(); private: - VertexBuffer* m_VBO; + Font* m_Font; + std::unordered_map<int/*IndexOfAtlas*/, VertexBuffer*> m_VBOs; + }; diff --git a/Runtime/GUI/TextMeshGenerator.h b/Runtime/GUI/TextMeshGenerator.h index c5992bd..baeaf16 100644 --- a/Runtime/GUI/TextMeshGenerator.h +++ b/Runtime/GUI/TextMeshGenerator.h @@ -1,6 +1,8 @@ #pragma once
+#include "TextMesh.h"
#include "Runtime/Utilities/IIncrementalTask.h"
+#include "Font.h"
// 回收长期没有被使用的text mesh
class GraduallyReleaseTextMesh : public IIncrementalTask
@@ -18,3 +20,15 @@ public: };
+
+class TextMeshGenerator : public Singleton<TextMeshGenerator>
+{
+public:
+ TextMesh* GetTextMesh(const UnicodeString& str);
+
+private:
+
+
+};
+
+#define g_TextMeshGenerator (*TextMeshGenerator::Instance())
\ No newline at end of file diff --git a/Runtime/Graphics/Color.h b/Runtime/Graphics/Color.h index abc0724..af8d3ba 100644 --- a/Runtime/Graphics/Color.h +++ b/Runtime/Graphics/Color.h @@ -5,9 +5,8 @@ namespace Internal { - class Color + struct Color { - public: Color(float r = 0, float g = 0, float b = 0, float a = 0) { this->r = r; @@ -18,18 +17,19 @@ namespace Internal float r, g, b, a; }; - class Color32 + struct Color32 { - public: - Color32(int r = 0, int g = 0, int b = 0, int a = 0) + Color32(unsigned char r = 0, unsigned char g = 0, unsigned char b = 0, unsigned char a = 0) { this->r = r; this->g = g; this->b = b; this->a = a; } - int r, g, b, a; - + unsigned char r, g, b, a; }; } + +typedef Internal::Color Color; +typedef Internal::Color32 Color32; diff --git a/Runtime/Graphics/VertexAttribute.cpp b/Runtime/Graphics/VertexAttribute.cpp index 0b0e8e4..e5f2ea7 100644 --- a/Runtime/Graphics/VertexAttribute.cpp +++ b/Runtime/Graphics/VertexAttribute.cpp @@ -8,7 +8,8 @@ namespace VertexAttribute GL_FLOAT, // VertexAttrFormat_Float GL_HALF_FLOAT, // VertexAttrFormat_Float16 GL_UNSIGNED_BYTE, // VertexAttrFormat_Color - GL_BYTE // VertexAttrFormat_Byte + GL_BYTE, // VertexAttrFormat_Byte + GL_UNSIGNED_BYTE, // VertexAttrFormat_Unsigned_Byte }; GLenum ConvertAttrFormatToGLFormat(uint fmt) diff --git a/Runtime/Graphics/VertexAttribute.h b/Runtime/Graphics/VertexAttribute.h index 2757c70..ae6d178 100644 --- a/Runtime/Graphics/VertexAttribute.h +++ b/Runtime/Graphics/VertexAttribute.h @@ -12,11 +12,12 @@ enum VertexAttrFormat { VertexAttrFormat_Float = 0, // position\normal\tangent\uv\uv2\uv3\uv4 - VertexAttrFormat_Float16 = 1, - VertexAttrFormat_Color = 2, // color - VertexAttrFormat_Byte = 3, + VertexAttrFormat_Float16, + VertexAttrFormat_Color, // color + VertexAttrFormat_Byte, + VertexAttrFormat_Unsigned_Byte, - VertexAttrFormat_Count = 4 + VertexAttrFormat_Count }; struct VertexAttributeDescriptor diff --git a/Runtime/Graphics/VertexBuffer.cpp b/Runtime/Graphics/VertexBuffer.cpp index 8423c2b..5d136ee 100644 --- a/Runtime/Graphics/VertexBuffer.cpp +++ b/Runtime/Graphics/VertexBuffer.cpp @@ -1,10 +1,14 @@ #include "VertexBuffer.h" #include "../Profiling/FrameStats.h" -VertexBuffer::VertexBuffer(VertexBufferType type) +VertexBuffer::VertexBuffer(int vbSize, int ibSize, VertexBufferType type) { + m_VB = GPU::ClaimBuffer(vbSize, GL_ARRAY_BUFFER); + m_IB = GPU::ClaimBuffer(ibSize, GL_ELEMENT_ARRAY_BUFFER); } VertexBuffer::~VertexBuffer() { + GPU::ReleaseBuffer(m_VB); + GPU::ReleaseBuffer(m_IB); } diff --git a/Runtime/Graphics/VertexBuffer.h b/Runtime/Graphics/VertexBuffer.h index b03df3b..c651bdb 100644 --- a/Runtime/Graphics/VertexBuffer.h +++ b/Runtime/Graphics/VertexBuffer.h @@ -23,9 +23,11 @@ public: VertexBufferType_Dynamic,// device }; - VertexBuffer(VertexBufferType type); + VertexBuffer(int vbSize, int ibSize, VertexBufferType type); ~VertexBuffer(); + + void Draw(); private: diff --git a/Runtime/Rendering/Quad.cpp b/Runtime/Rendering/Quad.cpp index 6378bf0..4c58674 100644 --- a/Runtime/Rendering/Quad.cpp +++ b/Runtime/Rendering/Quad.cpp @@ -1,5 +1,4 @@ -#include "../Math/Vector2.h" -#include "../Math/Vector3.h" +#include "../Math/Math.h" #include "Quad.h" #include "../Graphics/GfxDevice.h" diff --git a/Runtime/Utilities/Exception.h b/Runtime/Utilities/Exception.h index bd73c6b..3795bc6 100644 --- a/Runtime/Utilities/Exception.h +++ b/Runtime/Utilities/Exception.h @@ -1,6 +1,12 @@ -#ifndef EXCEPTION_H -#define EXCEPTION_H +#pragma once +#include <exception> - - -#endif
\ No newline at end of file +#define CustomException(Name) \ +class Name : public std::exception \ +{ \ +public: \ + Name(const char* what) \ + : std::exception(what) \ + { \ + } \ +} |