@@ -68,6 +68,7 @@ ImGuiOverlay::ImGuiOverlay() : Overlay("ImGuiOverlay")
6868 ImGuiIO& io = ImGui::GetIO ();
6969
7070 io.BackendPlatformName = " OGRE" ;
71+ io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures;
7172}
7273ImGuiOverlay::~ImGuiOverlay ()
7374{
@@ -104,7 +105,6 @@ void ImGuiOverlay::ImGUIRenderable::createMaterial()
104105 mPass ->setSeparateSceneBlending (SBF_SOURCE_ALPHA, SBF_ONE_MINUS_SOURCE_ALPHA, SBF_ONE, SBF_ONE_MINUS_SOURCE_ALPHA);
105106
106107 TextureUnitState* mTexUnit = mPass ->createTextureUnitState ();
107- mTexUnit ->setTexture (mFontTex );
108108 mTexUnit ->setTextureFiltering (TFO_NONE);
109109 mTexUnit ->setTextureAddressingMode (TAM_CLAMP);
110110
@@ -152,21 +152,6 @@ ImFont* ImGuiOverlay::addFont(const String& name, const String& group)
152152 cprangePtr);
153153}
154154
155- void ImGuiOverlay::ImGUIRenderable::createFontTexture ()
156- {
157- // Build texture atlas
158- ImGuiIO& io = ImGui::GetIO ();
159- if (io.Fonts ->Fonts .empty ())
160- io.Fonts ->AddFontDefault ();
161- unsigned char * pixels;
162- int width, height;
163- io.Fonts ->GetTexDataAsRGBA32 (&pixels, &width, &height);
164-
165- mFontTex = TextureManager::getSingleton ().createManual (" ImGui/FontTex" , RGN_INTERNAL, TEX_TYPE_2D,
166- width, height, 1 , 1 , PF_BYTE_RGBA);
167-
168- mFontTex ->getBuffer ()->blitFromMemory (PixelBox (Box (0 , 0 , width, height), PF_BYTE_RGBA, pixels));
169- }
170155void ImGuiOverlay::NewFrame ()
171156{
172157 static auto lastTime = Root::getSingleton ().getTimer ()->getMilliseconds ();
@@ -193,6 +178,51 @@ void ImGuiOverlay::NewFrame()
193178 ImGui::NewFrame ();
194179}
195180
181+ #if IMGUI_VERSION_NUM >= 19200
182+ static void updateTextureData (ImVector<ImTextureData*>& textures)
183+ {
184+ static int texCounter = 0 ;
185+
186+ for (auto tex : textures)
187+ {
188+ if (tex->Status == ImTextureStatus_OK)
189+ continue ; // nothing to do
190+
191+ if (tex->Status == ImTextureStatus_WantCreate)
192+ {
193+ OgreAssert (tex->Format == ImTextureFormat_RGBA32, " ImGuiOverlay only supports RGBA32 textures" );
194+ auto otex = TextureManager::getSingleton ().createManual (StringUtil::format (" ImGui/Tex%d" , texCounter++),
195+ RGN_INTERNAL, TEX_TYPE_2D, tex->Width , tex->Height ,
196+ 1 , 0 , PF_BYTE_RGBA);
197+
198+ otex->getBuffer ()->blitFromMemory (
199+ PixelBox (Box (0 , 0 , tex->Width , tex->Height ), PF_BYTE_RGBA, tex->GetPixels ()));
200+
201+ tex->SetTexID ((ImTextureID)otex->getHandle ());
202+ tex->SetStatus (ImTextureStatus_OK);
203+ }
204+ else if (tex->Status == ImTextureStatus_WantUpdates)
205+ {
206+ auto otex =
207+ static_pointer_cast<Texture>(TextureManager::getSingleton ().getByHandle ((ResourceHandle)tex->TexID ));
208+
209+ auto r = tex->UpdateRect ;
210+ PixelBox pb (r.w , r.h , 1 , PF_BYTE_RGBA, tex->GetPixelsAt (r.x , r.y ));
211+ pb.rowPitch = tex->Width ;
212+ otex->getBuffer ()->blitFromMemory (pb, Box (r.x , r.y , r.x + r.w , r.y + r.h ));
213+
214+ tex->SetStatus (ImTextureStatus_OK);
215+ }
216+ else if (tex->Status == ImTextureStatus_WantDestroy)
217+ {
218+ TextureManager::getSingleton ().remove ((ResourceHandle)tex->TexID );
219+ tex->SetTexID (ImTextureID_Invalid);
220+ tex->SetStatus (ImTextureStatus_Destroyed);
221+ }
222+ }
223+ }
224+ #endif
225+
196226void ImGuiOverlay::ImGUIRenderable::_update ()
197227{
198228 if (mMaterial ->getSupportedTechniques ().empty ())
@@ -204,6 +234,13 @@ void ImGuiOverlay::ImGUIRenderable::_update()
204234 ImDrawData* draw_data = ImGui::GetDrawData ();
205235 updateVertexData (draw_data);
206236
237+ #if IMGUI_VERSION_NUM >= 19200
238+ if (draw_data->Textures )
239+ {
240+ updateTextureData (*draw_data->Textures );
241+ }
242+ #endif
243+
207244 RenderSystem* rSys = Root::getSingleton ().getRenderSystem ();
208245
209246 // Construct projection matrix, taking texel offset corrections in account (important for DirectX9)
@@ -262,7 +299,7 @@ bool ImGuiOverlay::ImGUIRenderable::preRender(SceneManager* sm, RenderSystem* rs
262299 if (tex)
263300 {
264301 rsys->_setTexture (0 , true , tex);
265- rsys->_setSampler (0 , *TextureManager::getSingleton (). getDefaultSampler ());
302+ rsys->_setSampler (0 , *tu-> getSampler ());
266303 }
267304 }
268305
@@ -273,13 +310,6 @@ bool ImGuiOverlay::ImGUIRenderable::preRender(SceneManager* sm, RenderSystem* rs
273310
274311 rsys->_render (mRenderOp );
275312
276- if (drawCmd->GetTexID ())
277- {
278- // reset to pass state
279- rsys->_setTexture (0 , true , mFontTex );
280- rsys->_setSampler (0 , *tu->getSampler ());
281- }
282-
283313 // Update counts
284314 mRenderOp .indexData ->indexStart += drawCmd->ElemCount ;
285315 }
@@ -308,7 +338,10 @@ ImGuiOverlay::ImGUIRenderable::ImGUIRenderable()
308338// -----------------------------------------------------------------------------------
309339void ImGuiOverlay::ImGUIRenderable::initialise (void )
310340{
311- createFontTexture ();
341+ ImGuiIO& io = ImGui::GetIO ();
342+ if (io.Fonts ->Fonts .empty ())
343+ io.Fonts ->AddFontDefault ();
344+
312345 createMaterial ();
313346
314347 mRenderOp .vertexData = OGRE_NEW VertexData ();
@@ -336,8 +369,6 @@ void ImGuiOverlay::ImGUIRenderable::initialise(void)
336369// -----------------------------------------------------------------------------------
337370ImGuiOverlay::ImGUIRenderable::~ImGUIRenderable ()
338371{
339- if (mFontTex )
340- TextureManager::getSingleton ().remove (mFontTex );
341372 if (mMaterial )
342373 MaterialManager::getSingleton ().remove (mMaterial );
343374 OGRE_DELETE mRenderOp .vertexData ;
0 commit comments