From cea2f8da9e90223c91013a564c1f390ed6191a65 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Tue, 4 Aug 2020 01:27:35 +0800 Subject: [PATCH] fix draw text example. --- CMCore | 2 +- example/Vulkan/DrawText.cpp | 23 +++-- example/Vulkan/DrawTile.cpp | 2 +- example/Vulkan/RectanglePrimitive.cpp | 2 +- inc/hgl/graph/RenderableCreater.h | 1 + inc/hgl/graph/font/FontSource.h | 8 +- inc/hgl/graph/font/TextLayout.h | 20 +++-- inc/hgl/graph/vulkan/VKShaderModule.h | 4 +- src/RenderDevice/Vulkan/VKDeviceCreater.cpp | 2 +- src/RenderDevice/Vulkan/VKTileFont.cpp | 2 +- src/SceneGraph/font/FontSource.cpp | 2 +- src/SceneGraph/font/FontSourceWin.cpp | 40 +++++---- src/SceneGraph/font/TextLayout.cpp | 95 ++++++++++++--------- 13 files changed, 116 insertions(+), 87 deletions(-) diff --git a/CMCore b/CMCore index 2d6883be..c2c7c42c 160000 --- a/CMCore +++ b/CMCore @@ -1 +1 @@ -Subproject commit 2d6883be633abae7f0f10810e2b61367175bcfc4 +Subproject commit c2c7c42cd43759a9a613b6ad2c890a2e1d948b37 diff --git a/example/Vulkan/DrawText.cpp b/example/Vulkan/DrawText.cpp index 83d477c3..ded16948 100644 --- a/example/Vulkan/DrawText.cpp +++ b/example/Vulkan/DrawText.cpp @@ -1,4 +1,4 @@ -// DrawTile +// DrawTile // 该示例使用TileData,演示多个tile图片在一张纹理上 #include @@ -15,13 +15,6 @@ using namespace hgl; using namespace hgl::graph; -/** - * 文本绘制技术流程: - * - * 1.由TextLayout模块排版所有的字符,并向FontSource获取所有字符的Bitmap。生成文本可渲染对象的vertex position数据 - * 2.由TextLayout向TileData提交需要渲染的所有字符的Bitmap,并得到每个Bitmap对应的UV数据。生成文本可渲染对象的uv数据 - */ - constexpr uint32_t SCREEN_WIDTH =1280; constexpr uint32_t SCREEN_HEIGHT=960; @@ -31,6 +24,8 @@ constexpr uint CHAR_BITMAP_BORDER=1; //边界象素尺寸 class TestApp:public VulkanApplicationFramework { Camera cam; + + Color4f color; private: @@ -39,6 +34,7 @@ private: vulkan::MaterialInstance * material_instance =nullptr; vulkan::Renderable * render_obj =nullptr; vulkan::Buffer * ubo_world_matrix =nullptr; + vulkan::Buffer * ubo_color =nullptr; vulkan::Pipeline * pipeline =nullptr; @@ -71,7 +67,7 @@ private: { material=shader_manage->CreateMaterial( OS_TEXT("res/shader/DrawRect2D.vert"), OS_TEXT("res/shader/DrawRect2D.geom"), - OS_TEXT("res/shader/FlatTexture.frag")); + OS_TEXT("res/shader/FlatLumTexture.frag")); if(!material) return(false); @@ -81,6 +77,7 @@ private: material_instance->BindSampler("tex",tile_font->GetTexture(),sampler); material_instance->BindUBO("world",ubo_world_matrix); + material_instance->BindUBO("color_material",ubo_color); material_instance->Update(); db->Add(material); @@ -102,6 +99,13 @@ private: if(!ubo_world_matrix) return(false); + color.One(); + + ubo_color=db->CreateUBO(sizeof(Color4f),&color); + + if(!ubo_color) + return(false); + return(true); } @@ -130,6 +134,7 @@ private: text_rc=new RenderableCreater(db,material); + tl_engine.Set(tile_font->GetFontSource()); tl_engine.Set(text_rc); tl_engine.Set(&tla); tl_engine.SetTextDirection(0); diff --git a/example/Vulkan/DrawTile.cpp b/example/Vulkan/DrawTile.cpp index c2a1c2f5..d3362c82 100644 --- a/example/Vulkan/DrawTile.cpp +++ b/example/Vulkan/DrawTile.cpp @@ -1,4 +1,4 @@ -// DrawTile +// DrawTile // 该示例使用TileData,演示多个tile图片在一张纹理上 #include diff --git a/example/Vulkan/RectanglePrimitive.cpp b/example/Vulkan/RectanglePrimitive.cpp index 75a9aa2a..5c928eab 100644 --- a/example/Vulkan/RectanglePrimitive.cpp +++ b/example/Vulkan/RectanglePrimitive.cpp @@ -1,4 +1,4 @@ -// 2.RectanglePrimivate +// 2.RectanglePrimivate // 该示例是texture_rect的进化,演示使用GeometryShader画矩形 #include"VulkanAppFramework.h" diff --git a/inc/hgl/graph/RenderableCreater.h b/inc/hgl/graph/RenderableCreater.h index d187cf40..b1937144 100644 --- a/inc/hgl/graph/RenderableCreater.h +++ b/inc/hgl/graph/RenderableCreater.h @@ -3,6 +3,7 @@ #include #include +#include namespace hgl { namespace graph diff --git a/inc/hgl/graph/font/FontSource.h b/inc/hgl/graph/font/FontSource.h index 95ea0ff6..47851f03 100644 --- a/inc/hgl/graph/font/FontSource.h +++ b/inc/hgl/graph/font/FontSource.h @@ -79,7 +79,7 @@ namespace hgl virtual ~FontSource()=default; virtual FontBitmap *GetCharBitmap (const u32char &)=0; ///<取得字符位图数据 - virtual const bool GetCharMetrics (CharMetricsInfo &,const u32char &); ///<取得字符绘制信息 + virtual const bool GetCharMetrics (CharMetricsInfo &,const u32char &)=0; ///<取得字符绘制信息 const CLA * GetCLA (const u32char &); ///<取得字符排版信息 virtual int GetCharHeight ()const=0; ///<取得字符高度 @@ -109,11 +109,11 @@ namespace hgl virtual ~FontSourceSingle()=default; FontBitmap *GetCharBitmap (const u32char &ch) override; ///<取得字符位图数据 - const bool GetCharMetrics (CharMetricsInfo &,const u32char &); ///<取得字符绘制信息 + const bool GetCharMetrics (CharMetricsInfo &,const u32char &)override;///<取得字符绘制信息 virtual int GetCharHeight ()const override{return fnt.height;} ///<取得字符高度 };//class FontSourceSingle:public FontSource - FontSourceSingle *CreateFontSource(const Font &f); + FontSource *CreateFontSource(const Font &f); /** * 文字位图多重数据源 @@ -148,7 +148,7 @@ namespace hgl public: FontBitmap *GetCharBitmap (const u32char &ch) override; - const bool GetCharMetrics (CharMetricsInfo &,const u32char &); ///<取得字符绘制信息 + const bool GetCharMetrics (CharMetricsInfo &,const u32char &)override; ///<取得字符绘制信息 int GetCharHeight ()const override; ///<取得字符高度 };//class FontSourceMulti:public FontSource }//namespace graph diff --git a/inc/hgl/graph/font/TextLayout.h b/inc/hgl/graph/font/TextLayout.h index 2839e6c9..59ae5739 100644 --- a/inc/hgl/graph/font/TextLayout.h +++ b/inc/hgl/graph/font/TextLayout.h @@ -85,6 +85,7 @@ namespace hgl { protected: + FontSource *font_source; RenderableCreater *rc; TextLayoutAttributes tla; @@ -102,13 +103,13 @@ namespace hgl struct CharDrawAttr { - CLA *cla; + const CLA *cla; TileUVFloat uv; }; ObjectList draw_chars_list; ///<所有字符属性列表 - template int preprocess(const T *,const int); + template bool preprocess(TileFont *,const T *,const int); protected: @@ -120,6 +121,9 @@ namespace hgl int sl_v_r2l(); int sl_v_l2r(); + template + int SimpleLayout (TileFont *,const BaseString &); ///<简易排版 + protected: TEXT_COORD_TYPE x,y; @@ -134,8 +138,8 @@ namespace hgl protected: - AutoDelete vertex, - tex_coord; + VB4f *vertex; + VB4f *tex_coord; public: @@ -154,7 +158,7 @@ namespace hgl void Set (RenderableCreater *_rc) {if(_rc)rc=_rc;} void Set (const TextLayoutAttributes *_tla) {if(_tla)memcpy(&tla,_tla,sizeof(TextLayoutAttributes));} - void Set (FontSource *fs) {if(fs)tla.font_source=fs;} + void Set (FontSource *fs) {if(fs)font_source=fs;} void SetTextDirection (const uint8 &td) {tla.text_direction=td;} void Set (const TextAlign &ta) {tla.align=ta;} void SetMaxWidth (const float mw) {tla.max_width=mw;} @@ -162,10 +166,8 @@ namespace hgl virtual bool Init (); ///<初始化排版 -// virtual int Layout (const int max_chars,const BaseString &)=0; ///<排版 - - template - int SimpleLayout (TileFont *,const BaseString &); ///<简易排版 + int SimpleLayout (TileFont *,const UTF16String &); ///<简易排版 + int SimpleLayout (TileFont *,const UTF32String &); ///<简易排版 };//class TextLayout }//namespace graph }//namespace hgl diff --git a/inc/hgl/graph/vulkan/VKShaderModule.h b/inc/hgl/graph/vulkan/VKShaderModule.h index d704d330..22c2998f 100644 --- a/inc/hgl/graph/vulkan/VKShaderModule.h +++ b/inc/hgl/graph/vulkan/VKShaderModule.h @@ -72,8 +72,8 @@ public: * 获取输入流绑定点,需要注意的时,这里获取的binding并非是shader中的binding/location,而是绑定顺序的序列号。对应vkCmdBindVertexBuffer的缓冲区序列号 */ const int GetStageInputBinding(const AnsiString &name)const{return shader_resource->GetStageInputBinding(name);} - const ShaderStage * GetStageInput(const AnsiString &name)const{return shader_resource->GetStageInput(name);} - const uint GetStageInputCount()const{return shader_resource->GetStageInputCount();} + const ShaderStage * GetStageInput (const AnsiString &name)const{return shader_resource->GetStageInput(name);} + const uint GetStageInputCount () const{return shader_resource->GetStageInputCount();} const uint32_t GetAttrCount()const{return attr_count;} diff --git a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp index ad800fee..9d822530 100644 --- a/src/RenderDevice/Vulkan/VKDeviceCreater.cpp +++ b/src/RenderDevice/Vulkan/VKDeviceCreater.cpp @@ -426,7 +426,7 @@ Device *CreateRenderDevice(VkInstance inst,const PhysicalDevice *physical_device if(!device_attr->pipeline_cache) return(nullptr); - auto_delete.Clear(); + auto_delete.Discard(); return(new Device(device_attr)); } diff --git a/src/RenderDevice/Vulkan/VKTileFont.cpp b/src/RenderDevice/Vulkan/VKTileFont.cpp index 5f2e8614..025cb5f4 100644 --- a/src/RenderDevice/Vulkan/VKTileFont.cpp +++ b/src/RenderDevice/Vulkan/VKTileFont.cpp @@ -1,4 +1,4 @@ -#include +#include #include namespace hgl diff --git a/src/SceneGraph/font/FontSource.cpp b/src/SceneGraph/font/FontSource.cpp index 39af8332..93a5aae8 100644 --- a/src/SceneGraph/font/FontSource.cpp +++ b/src/SceneGraph/font/FontSource.cpp @@ -1,4 +1,4 @@ -#include +#include namespace hgl { diff --git a/src/SceneGraph/font/FontSourceWin.cpp b/src/SceneGraph/font/FontSourceWin.cpp index 9fa675ef..8a7b3498 100644 --- a/src/SceneGraph/font/FontSourceWin.cpp +++ b/src/SceneGraph/font/FontSourceWin.cpp @@ -6,43 +6,48 @@ namespace hgl { namespace { - void Convert8BitGrey(uint8 *dst,uint8 *src,int w,int h,int line_bytes) + void Convert8BitGrey(uint8 *dst,int dst_w,uint8 *src,int src_w,int src_h,int src_line_bytes) { int pos; uint8 *sp=src,*p; + uint8 *tp; - while(h--) + while(src_h--) { - pos=w; + pos=src_w; p=sp; + tp=dst; while(pos--) { - if(*p==64)*dst=255; - else *dst=(*p)<<2; + if(*p==64)*tp=255; + else *tp=(*p)<<2; - dst++; + tp++; p++; } - sp+=line_bytes; + sp+=src_line_bytes; + dst+=dst_w; } } - void ConvertBitmap(uint8 *dst,uint8 *src,int w,int h,int line_bytes) + void ConvertBitmap(uint8 *dst,int dst_w,uint8 *src,int src_w,int src_h,int src_line_bytes) { uint8 *sp=src,*p; + uint8 *tp; uint8 bit; - while(h--) + while(src_h--) { p=sp; + tp=dst; bit=1<<7; - for(int i=0;i>=1; } - sp+=line_bytes; + sp+=src_line_bytes; + dst+=dst_w; }//while(h--) } }//namespace @@ -120,8 +126,8 @@ namespace hgl GetGlyphOutlineW(hdc,ch,ggo,&gm,buffer_size,buffer,&mat); - bmp->metrics_info.w =gm.gmBlackBoxX; - bmp->metrics_info.h =gm.gmBlackBoxY; + bmp->metrics_info.w =((gm.gmBlackBoxX+3)>>2)<<2; + bmp->metrics_info.h =((gm.gmBlackBoxY+3)>>2)<<2; bmp->metrics_info.x =gm.gmptGlyphOrigin.x; bmp->metrics_info.y =gm.gmptGlyphOrigin.y; @@ -132,14 +138,14 @@ namespace hgl bmp->data=new uint8[bmp->metrics_info.w*bmp->metrics_info.h]; if(ggo==GGO_GRAY8_BITMAP) - Convert8BitGrey(bmp->data,buffer,gm.gmBlackBoxX,gm.gmBlackBoxY,size/bmp->metrics_info.h); + Convert8BitGrey(bmp->data,bmp->metrics_info.w,buffer,gm.gmBlackBoxX,gm.gmBlackBoxY,size/gm.gmBlackBoxY); else - ConvertBitmap(bmp->data,buffer,gm.gmBlackBoxX,gm.gmBlackBoxY,size/bmp->metrics_info.h); + ConvertBitmap(bmp->data,bmp->metrics_info.w,buffer,gm.gmBlackBoxX,gm.gmBlackBoxY,size/gm.gmBlackBoxY); return(true); } - FontSourceSingle *CreateFontSource(const Font &f) + FontSource *CreateFontSource(const Font &f) { return(new WinBitmapFont(f)); } diff --git a/src/SceneGraph/font/TextLayout.cpp b/src/SceneGraph/font/TextLayout.cpp index 4e6cb580..7f6ec3cc 100644 --- a/src/SceneGraph/font/TextLayout.cpp +++ b/src/SceneGraph/font/TextLayout.cpp @@ -1,6 +1,6 @@ #include +#include #include -#include namespace hgl { @@ -9,8 +9,8 @@ namespace hgl bool TextLayout::Init() { if(!rc - ||!tla.font_source - ||!tla.char_attributes) + ||(!tla.font_source&&!font_source) + ||!tla.char_layout_attr) return(false); direction.text_direction=tla.text_direction; @@ -26,7 +26,10 @@ namespace hgl splite_line_max_limit = tla.max_height; } - const float origin_char_height=tla.font_source->GetCharHeight(); + if(!font_source) + font_source=tla.font_source; + + const float origin_char_height=font_source->GetCharHeight(); x=y=0; char_height =ceil(origin_char_height); @@ -45,36 +48,42 @@ namespace hgl * 预处理所有的字符,获取所有字符的宽高,以及是否标点符号等信息 */ template - int TextLayout::preprocess(const T *str,const int str_length) + bool TextLayout::preprocess(TileFont *tile_font,const T *str,const int str_length) { - if(!str||!*str||str_length<=0) - return 0; - - draw_chars_count=0; - alone_chars.ClearData(); + if(!tile_font + ||!str||!*str||str_length<=0 + ||!font_source) + return(false); - draw_chars_list.ClearData(); - draw_chars_list.SetCount(str_length); - - CharDrawAttr **cda=draw_chars_list.GetData(); - const T *cp=str; - - for(int i=0;icla=tla->font_source.GetCLA(*cp); + draw_chars_count=0; + alone_chars.ClearData(); + draw_chars_list.ClearData(); - if((*cda)->cla->visible) + const T *cp=str; + CharDrawAttr *cda; + + for(int i=0;icla=font_source->GetCLA(*cp); + + if(cda->cla->visible) + { + alone_chars.Add(*cp); //统计所有不重复字符 + ++draw_chars_count; + } + + draw_chars_list.Add(cda); + + ++cp; + } } //注册不重复字符给tile font系统,获取所有字符的UV - if(!tf->Registry(alone_chars_uv,alone_chars.GetData(),alone_chars.GetCount())) + if(!tile_font->Registry(alone_chars_uv,alone_chars.GetData(),alone_chars.GetCount())) { draw_chars_list.ClearData(); alone_chars.ClearData(); @@ -83,15 +92,18 @@ namespace hgl } //为可绘制字符列表中的字符获取UV - cda=draw_chars_list.GetData(); - for(int i=0;icla->ch,(*cda)->uv); + CharDrawAttr **cda=draw_chars_list.GetData(); - ++cda; + for(int i=0;icla->ch,(*cda)->uv); + + ++cda; + } } - return count; + return(true); } /** @@ -161,8 +173,8 @@ namespace hgl int cur_size=0; int left=0,top=0; - float *tp=vertex->Begin(); - float *tcp=tex_coord->Begin(); + float *tp=vertex->Get(); + float *tcp=tex_coord->Get(); for(int i=0;i(str.c_str(),str.Length())<=0) - return(-3); + if(!preprocess(tf,str.c_str(),str.Length())) + return(-2); if(draw_chars_count<=0) //可绘制字符为0???这是全空格? - return(-4); + return(-3); if(!rc->Init(draw_chars_count)) //创建 - return(-5); + return(-4); vertex =rc->CreateVADA(VAN::Vertex); tex_coord =rc->CreateVADA(VAN::TexCoord); @@ -222,19 +234,22 @@ namespace hgl if(direction.vertical) { if(direction.right_to_left) - return pl_v_r2l(); + return sl_v_r2l(); else - return pl_v_l2r(); + return sl_v_l2r(); } else { if(direction.right_to_left) - return pl_h_r2l(); + return sl_h_r2l(); else - return pl_h_l2r(); + return sl_h_l2r(); } return 0; } + + int TextLayout::SimpleLayout(TileFont *tf,const UTF16String &str){return this->SimpleLayout(tf,str);} + int TextLayout::SimpleLayout(TileFont *tf,const UTF32String &str){return this->SimpleLayout(tf,str);} }//namespace graph }//namespace hgl