8 #include <OgreHardwareBufferManager.h>
9 #include <OgreHardwarePixelBuffer.h>
10 #include <OgreRenderSystem.h>
11 #include <OgreTextureManager.h>
12 #include <OgreMaterialManager.h>
13 #include <OgreOverlayManager.h>
14 #include <OgreFontManager.h>
15 #include <OgreTechnique.h>
16 #include <OgreTextureUnitState.h>
18 #include <OgreRenderQueue.h>
19 #include <OgreFrameListener.h>
27 ImGui::CreateContext();
28 ImGuiIO& io = ImGui::GetIO();
30 io.BackendPlatformName =
"OGRE";
34 ImGui::DestroyContext();
54 queue->addRenderable(&
mRenderable, RENDER_QUEUE_OVERLAY, mZOrder * 100);
59 mMaterial = MaterialManager::getSingleton().create(
"ImGui/material", RGN_INTERNAL);
60 Pass* mPass =
mMaterial->getTechnique(0)->getPass(0);
61 mPass->setCullingMode(CULL_NONE);
62 mPass->setVertexColourTracking(TVC_DIFFUSE);
63 mPass->setSceneBlending(SBT_TRANSPARENT_ALPHA);
64 mPass->setSeparateSceneBlendingOperation(SBO_ADD, SBO_ADD);
65 mPass->setSeparateSceneBlending(SBF_SOURCE_ALPHA, SBF_ONE_MINUS_SOURCE_ALPHA,
66 SBF_ONE_MINUS_SOURCE_ALPHA, SBF_ZERO);
68 TextureUnitState* mTexUnit = mPass->createTextureUnitState();
70 mTexUnit->setTextureFiltering(TFO_NONE);
79 FontPtr font = FontManager::getSingleton().getByName(name, group);
80 OgreAssert(font,
"font does not exist");
81 OgreAssert(font->getType() == FT_TRUETYPE,
"font must be of FT_TRUETYPE");
82 DataStreamPtr dataStreamPtr =
83 ResourceGroupManager::getSingleton().openResource(font->getSource(), font->getGroup());
84 MemoryDataStream ttfchunk(dataStreamPtr,
false);
88 for (
const auto& r : font->getCodePointRangeList())
90 cprange.push_back(r.first);
91 cprange.push_back(r.second);
94 ImGuiIO& io = ImGui::GetIO();
95 const ImWchar* cprangePtr = io.Fonts->GetGlyphRangesAll();
105 strncpy(cfg.Name, name.c_str(), 40);
106 return io.Fonts->AddFontFromMemoryTTF(ttfchunk.getPtr(), (
int)ttfchunk.size(), font->getTrueTypeSize(), &cfg,
113 ImGuiIO& io = ImGui::GetIO();
114 if (io.Fonts->Fonts.empty())
115 io.Fonts->AddFontDefault();
117 unsigned char* pixels;
119 io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
121 mFontTex = TextureManager::getSingleton().createManual(
"ImGui/FontTex", RGN_INTERNAL, TEX_TYPE_2D,
122 width, height, 1, 1, PF_BYTE_RGBA);
124 mFontTex->getBuffer()->blitFromMemory(PixelBox(Box(0, 0, width, height), PF_BYTE_RGBA, pixels));
128 ImGuiIO& io = ImGui::GetIO();
129 io.DeltaTime = std::max<float>(
130 evt.timeSinceLastFrame,
137 OverlayManager& oMgr = OverlayManager::getSingleton();
140 io.DisplaySize = ImVec2(oMgr.getViewportWidth(), oMgr.getViewportHeight());
148 if (mMaterial->getSupportedTechniques().empty())
153 RenderSystem* rSys = Root::getSingleton().getRenderSystem();
154 OverlayManager& oMgr = OverlayManager::getSingleton();
161 float texelOffsetX = rSys->getHorizontalTexelOffset();
162 float texelOffsetY = rSys->getVerticalTexelOffset();
163 float L = texelOffsetX;
164 float R = oMgr.getViewportWidth() + texelOffsetX;
165 float T = texelOffsetY;
166 float B = oMgr.getViewportHeight() + texelOffsetY;
168 mXform = Matrix4(2.0f / (R - L), 0.0f, 0.0f, (L + R) / (L - R), 0.0f, -2.0f / (B - T), 0.0f,
169 (T + B) / (B - T), 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
174 Viewport* vp = rsys->_getViewport();
180 ImDrawData* draw_data = ImGui::GetDrawData();
181 int vpWidth = vp->getActualWidth();
182 int vpHeight = vp->getActualHeight();
184 TextureUnitState* tu = mMaterial->getBestTechnique()->getPass(0)->getTextureUnitState(0);
186 for (
int i = 0; i < draw_data->CmdListsCount; ++i)
188 const ImDrawList* draw_list = draw_data->CmdLists[i];
189 updateVertexData(draw_list->VtxBuffer, draw_list->IdxBuffer);
191 unsigned int startIdx = 0;
193 for (
int j = 0; j < draw_list->CmdBuffer.Size; ++j)
196 const ImDrawCmd* drawCmd = &draw_list->CmdBuffer[j];
199 Rect scissor(drawCmd->ClipRect.x, drawCmd->ClipRect.y, drawCmd->ClipRect.z,
200 drawCmd->ClipRect.w);
203 scissor = scissor.intersect(Rect(0, 0, vpWidth, vpHeight));
205 if (drawCmd->TextureId)
207 auto handle = (ResourceHandle)drawCmd->TextureId;
208 auto tex = static_pointer_cast<Texture>(TextureManager::getSingleton().getByHandle(handle));
211 rsys->_setTexture(0,
true, tex);
212 rsys->_setSampler(0, *TextureManager::getSingleton().getDefaultSampler());
216 rsys->setScissorTest(
true, scissor.left, scissor.top, scissor.right, scissor.bottom);
219 mRenderOp.indexData->indexStart = startIdx;
220 mRenderOp.indexData->indexCount = drawCmd->ElemCount;
222 rsys->_render(mRenderOp);
224 if (drawCmd->TextureId)
227 rsys->_setTexture(0,
true, mFontTex);
228 rsys->_setSampler(0, *tu->getSampler());
232 startIdx += drawCmd->ElemCount;
235 rsys->setScissorTest(
false);
249 mPolygonModeOverrideable =
false;
252 mUseIdentityProjection =
true;
253 mUseIdentityView =
true;
255 mConvertToBGR =
false;
263 mRenderOp.vertexData = OGRE_NEW VertexData();
264 mRenderOp.indexData = OGRE_NEW IndexData();
266 mRenderOp.vertexData->vertexCount = 0;
267 mRenderOp.vertexData->vertexStart = 0;
269 mRenderOp.indexData->indexCount = 0;
270 mRenderOp.indexData->indexStart = 0;
271 mRenderOp.operationType = RenderOperation::OT_TRIANGLE_LIST;
272 mRenderOp.useIndexes =
true;
273 mRenderOp.useGlobalInstancingVertexBufferIsAvailable =
false;
275 VertexDeclaration* decl = mRenderOp.vertexData->vertexDeclaration;
279 decl->addElement(0, offset, VET_FLOAT2, VES_POSITION);
280 offset += VertexElement::getTypeSize(VET_FLOAT2);
281 decl->addElement(0, offset, VET_FLOAT2, VES_TEXTURE_COORDINATES, 0);
282 offset += VertexElement::getTypeSize(VET_FLOAT2);
283 decl->addElement(0, offset, VET_COLOUR, VES_DIFFUSE);
285 if (Root::getSingleton().getRenderSystem()->getName().find(
"Direct3D9") != String::npos)
286 mConvertToBGR =
true;
291 OGRE_DELETE mRenderOp.vertexData;
292 OGRE_DELETE mRenderOp.indexData;
296 const ImVector<ImDrawIdx>& idxBuf)
298 VertexBufferBinding* bind = mRenderOp.vertexData->vertexBufferBinding;
300 if (bind->getBindings().empty() || bind->getBuffer(0)->getNumVertices() !=
size_t(vtxBuf.size()))
302 bind->setBinding(0, HardwareBufferManager::getSingleton().createVertexBuffer(
303 sizeof(ImDrawVert), vtxBuf.size(), HardwareBuffer::HBU_WRITE_ONLY));
305 if (!mRenderOp.indexData->indexBuffer ||
306 mRenderOp.indexData->indexBuffer->getNumIndexes() !=
size_t(idxBuf.size()))
308 mRenderOp.indexData->indexBuffer = HardwareBufferManager::getSingleton().createIndexBuffer(
309 HardwareIndexBuffer::IT_32BIT, idxBuf.size(), HardwareBuffer::HBU_WRITE_ONLY);
315 PixelBox src(1, vtxBuf.size(), 1, PF_A8B8G8R8, (
char*)vtxBuf.Data + offsetof(ImDrawVert, col));
316 src.rowPitch =
sizeof(ImDrawVert) /
sizeof(ImU32);
318 dst.format = PF_A8R8G8B8;
319 PixelUtil::bulkPixelConversion(src, dst);
323 bind->getBuffer(0)->writeData(0, vtxBuf.size_in_bytes(), vtxBuf.Data,
true);
324 mRenderOp.indexData->indexBuffer->writeData(0, idxBuf.size_in_bytes(), idxBuf.Data,
true);
326 mRenderOp.vertexData->vertexStart = 0;
327 mRenderOp.vertexData->vertexCount = vtxBuf.size();