Skip to content

Commit baf2a4b

Browse files
committed
Overlay: update imgui to 1.92
1 parent f629d22 commit baf2a4b

File tree

5 files changed

+62
-33
lines changed

5 files changed

+62
-33
lines changed

Components/Bites/src/OgreApplicationContextBase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ void ApplicationContextBase::runRenderingSettingsDialog()
306306
float vpScale = getDisplayDPI()/96;
307307
Ogre::OverlayManager::getSingleton().setPixelRatio(vpScale);
308308
auto overlay = initialiseImGui();
309-
ImGui::GetIO().FontGlobalScale = std::round(vpScale); // default font does not work with fractional scaling
309+
ImGui::GetStyle().FontScaleMain = std::round(vpScale); // default font does not work with fractional scaling
310310
overlay->show();
311311

312312
addInputListener(getImGuiInputListener());

Components/Overlay/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ list(APPEND HEADER_FILES
1919
file(GLOB SOURCE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
2020

2121
if(OGRE_BUILD_COMPONENT_OVERLAY_IMGUI)
22-
set(IMGUI_DIR "${PROJECT_BINARY_DIR}/imgui-1.91.9b" CACHE PATH "")
22+
set(IMGUI_DIR "${PROJECT_BINARY_DIR}/imgui-1.92.3" CACHE PATH "")
2323
if(NOT EXISTS ${IMGUI_DIR})
2424
message(STATUS "Downloading imgui")
2525
file(DOWNLOAD
26-
https://github.com/ocornut/imgui/archive/v1.91.9b.tar.gz
26+
https://github.com/ocornut/imgui/archive/v1.92.3.tar.gz
2727
${PROJECT_BINARY_DIR}/imgui.tar.gz)
2828
execute_process(COMMAND ${CMAKE_COMMAND}
2929
-E tar xf imgui.tar.gz WORKING_DIRECTORY ${PROJECT_BINARY_DIR})

Components/Overlay/include/OgreImGuiOverlay.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ class _OgreOverlayExport ImGuiOverlay : public Overlay
7070
const LightList& getLights(void) const override;
7171

7272
void createMaterial();
73-
void createFontTexture();
7473

7574
const MaterialPtr& getMaterial() const override { return mMaterial; }
7675

@@ -80,7 +79,6 @@ class _OgreOverlayExport ImGuiOverlay : public Overlay
8079

8180
Matrix4 mXform;
8281
RenderOperation mRenderOp;
83-
TexturePtr mFontTex;
8482
MaterialPtr mMaterial;
8583
};
8684

Components/Overlay/src/OgreImGuiOverlay.cpp

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ ImGuiOverlay::ImGuiOverlay() : Overlay("ImGuiOverlay")
6868
ImGuiIO& io = ImGui::GetIO();
6969

7070
io.BackendPlatformName = "OGRE";
71+
io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures;
7172
}
7273
ImGuiOverlay::~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

@@ -175,21 +175,6 @@ ImFont* ImGuiOverlay::addFont(const String& name, const String& group)
175175
return ret;
176176
}
177177

178-
void ImGuiOverlay::ImGUIRenderable::createFontTexture()
179-
{
180-
// Build texture atlas
181-
ImGuiIO& io = ImGui::GetIO();
182-
if (io.Fonts->Fonts.empty())
183-
io.Fonts->AddFontDefault();
184-
unsigned char* pixels;
185-
int width, height;
186-
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
187-
188-
mFontTex = TextureManager::getSingleton().createManual("ImGui/FontTex", RGN_INTERNAL, TEX_TYPE_2D,
189-
width, height, 1, 1, PF_BYTE_RGBA);
190-
191-
mFontTex->getBuffer()->blitFromMemory(PixelBox(Box(0, 0, width, height), PF_BYTE_RGBA, pixels));
192-
}
193178
void ImGuiOverlay::NewFrame()
194179
{
195180
static auto lastTime = Root::getSingleton().getTimer()->getMilliseconds();
@@ -216,6 +201,51 @@ void ImGuiOverlay::NewFrame()
216201
ImGui::NewFrame();
217202
}
218203

204+
#if IMGUI_VERSION_NUM >= 19200
205+
static void updateTextureData(ImVector<ImTextureData*>& textures)
206+
{
207+
static int texCounter = 0;
208+
209+
for (auto tex : textures)
210+
{
211+
if (tex->Status == ImTextureStatus_OK)
212+
continue; // nothing to do
213+
214+
if (tex->Status == ImTextureStatus_WantCreate)
215+
{
216+
OgreAssert(tex->Format == ImTextureFormat_RGBA32, "ImGuiOverlay only supports RGBA32 textures");
217+
auto otex = TextureManager::getSingleton().createManual(StringUtil::format("ImGui/Tex%d", texCounter++),
218+
RGN_INTERNAL, TEX_TYPE_2D, tex->Width, tex->Height,
219+
1, 0, PF_BYTE_RGBA);
220+
221+
otex->getBuffer()->blitFromMemory(
222+
PixelBox(Box(0, 0, tex->Width, tex->Height), PF_BYTE_RGBA, tex->GetPixels()));
223+
224+
tex->SetTexID((ImTextureID)otex->getHandle());
225+
tex->SetStatus(ImTextureStatus_OK);
226+
}
227+
else if (tex->Status == ImTextureStatus_WantUpdates)
228+
{
229+
auto otex =
230+
static_pointer_cast<Texture>(TextureManager::getSingleton().getByHandle((ResourceHandle)tex->TexID));
231+
232+
auto r = tex->UpdateRect;
233+
PixelBox pb(r.w, r.h, 1, PF_BYTE_RGBA, tex->GetPixelsAt(r.x, r.y));
234+
pb.rowPitch = tex->Width;
235+
otex->getBuffer()->blitFromMemory(pb, Box(r.x, r.y, r.x + r.w, r.y + r.h));
236+
237+
tex->SetStatus(ImTextureStatus_OK);
238+
}
239+
else if (tex->Status == ImTextureStatus_WantDestroy)
240+
{
241+
TextureManager::getSingleton().remove((ResourceHandle)tex->TexID);
242+
tex->SetTexID(ImTextureID_Invalid);
243+
tex->SetStatus(ImTextureStatus_Destroyed);
244+
}
245+
}
246+
}
247+
#endif
248+
219249
void ImGuiOverlay::ImGUIRenderable::_update()
220250
{
221251
if (mMaterial->getSupportedTechniques().empty())
@@ -227,6 +257,13 @@ void ImGuiOverlay::ImGUIRenderable::_update()
227257
ImDrawData* draw_data = ImGui::GetDrawData();
228258
updateVertexData(draw_data);
229259

260+
#if IMGUI_VERSION_NUM >= 19200
261+
if (draw_data->Textures)
262+
{
263+
updateTextureData(*draw_data->Textures);
264+
}
265+
#endif
266+
230267
RenderSystem* rSys = Root::getSingleton().getRenderSystem();
231268

232269
// Construct projection matrix, taking texel offset corrections in account (important for DirectX9)
@@ -285,7 +322,7 @@ bool ImGuiOverlay::ImGUIRenderable::preRender(SceneManager* sm, RenderSystem* rs
285322
if (tex)
286323
{
287324
rsys->_setTexture(0, true, tex);
288-
rsys->_setSampler(0, *TextureManager::getSingleton().getDefaultSampler());
325+
rsys->_setSampler(0, *tu->getSampler());
289326
}
290327
}
291328

@@ -296,13 +333,6 @@ bool ImGuiOverlay::ImGUIRenderable::preRender(SceneManager* sm, RenderSystem* rs
296333

297334
rsys->_render(mRenderOp);
298335

299-
if (drawCmd->GetTexID())
300-
{
301-
// reset to pass state
302-
rsys->_setTexture(0, true, mFontTex);
303-
rsys->_setSampler(0, *tu->getSampler());
304-
}
305-
306336
// Update counts
307337
mRenderOp.indexData->indexStart += drawCmd->ElemCount;
308338
}
@@ -331,7 +361,10 @@ ImGuiOverlay::ImGUIRenderable::ImGUIRenderable()
331361
//-----------------------------------------------------------------------------------
332362
void ImGuiOverlay::ImGUIRenderable::initialise(void)
333363
{
334-
createFontTexture();
364+
ImGuiIO& io = ImGui::GetIO();
365+
if (io.Fonts->Fonts.empty())
366+
io.Fonts->AddFontDefault();
367+
335368
createMaterial();
336369

337370
mRenderOp.vertexData = OGRE_NEW VertexData();
@@ -359,8 +392,6 @@ void ImGuiOverlay::ImGUIRenderable::initialise(void)
359392
//-----------------------------------------------------------------------------------
360393
ImGuiOverlay::ImGUIRenderable::~ImGUIRenderable()
361394
{
362-
if(mFontTex)
363-
TextureManager::getSingleton().remove(mFontTex);
364395
if(mMaterial)
365396
MaterialManager::getSingleton().remove(mMaterial);
366397
OGRE_DELETE mRenderOp.vertexData;

Samples/Simple/include/ImGuiDemo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class _OgreSampleClassExport Sample_ImGui : public SdkSample, public RenderTarge
3434
auto imguiOverlay = mContext->initialiseImGui();
3535

3636
float vpScale = OverlayManager::getSingleton().getPixelRatio();
37-
ImGui::GetIO().FontGlobalScale = std::round(vpScale); // default font does not work with fractional scaling
37+
ImGui::GetStyle().FontScaleMain = std::round(vpScale); // default font does not work with fractional scaling
3838

3939
imguiOverlay->setZOrder(300);
4040
imguiOverlay->show();

0 commit comments

Comments
 (0)