-
-
Notifications
You must be signed in to change notification settings - Fork 982
/
OgreImGuiOverlay.cpp
321 lines (269 loc) · 11.3 KB
/
OgreImGuiOverlay.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// This file is part of the OGRE project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at https://www.ogre3d.org/licensing.
#include <imgui.h>
#include <imgui_freetype.h>
#include <OgreImGuiOverlay.h>
#include <OgreHardwareBufferManager.h>
#include <OgreHardwarePixelBuffer.h>
#include <OgreRenderSystem.h>
#include <OgreTextureManager.h>
#include <OgreMaterialManager.h>
#include <OgreOverlayManager.h>
#include <OgreFontManager.h>
#include <OgreTechnique.h>
#include <OgreTextureUnitState.h>
#include <OgreFont.h>
#include <OgreRenderQueue.h>
#include <OgreFrameListener.h>
#include <OgreRoot.h>
#include <OgreTimer.h>
namespace Ogre
{
ImGuiOverlay::ImGuiOverlay() : Overlay("ImGuiOverlay")
{
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.BackendPlatformName = "OGRE";
}
ImGuiOverlay::~ImGuiOverlay()
{
ImGui::DestroyContext();
}
void ImGuiOverlay::initialise()
{
if (!mInitialised)
{
mRenderable.initialise();
mCodePointRanges.clear();
}
mInitialised = true;
}
//-----------------------------------------------------------------------------------
void ImGuiOverlay::_findVisibleObjects(Camera* cam, RenderQueue* queue, Viewport* vp)
{
if (!mVisible)
return;
mRenderable._update();
queue->addRenderable(&mRenderable, RENDER_QUEUE_OVERLAY, mZOrder * 100);
}
//-----------------------------------------------------------------------------------
void ImGuiOverlay::ImGUIRenderable::createMaterial()
{
mMaterial = MaterialManager::getSingleton().create("ImGui/material", RGN_INTERNAL);
Pass* mPass = mMaterial->getTechnique(0)->getPass(0);
mPass->setCullingMode(CULL_NONE);
mPass->setVertexColourTracking(TVC_DIFFUSE);
mPass->setSceneBlending(SBT_TRANSPARENT_ALPHA);
mPass->setSeparateSceneBlendingOperation(SBO_ADD, SBO_ADD);
mPass->setSeparateSceneBlending(SBF_SOURCE_ALPHA, SBF_ONE_MINUS_SOURCE_ALPHA,
SBF_ONE_MINUS_SOURCE_ALPHA, SBF_ZERO);
TextureUnitState* mTexUnit = mPass->createTextureUnitState();
mTexUnit->setTexture(mFontTex);
mTexUnit->setTextureFiltering(TFO_NONE);
mMaterial->load();
mMaterial->setLightingEnabled(false);
mMaterial->setDepthCheckEnabled(false);
mMaterial->setReceiveShadows(false);
}
ImFont* ImGuiOverlay::addFont(const String& name, const String& group)
{
FontPtr font = FontManager::getSingleton().getByName(name, group);
OgreAssert(font, "font does not exist");
OgreAssert(font->getType() == FT_TRUETYPE, "font must be of FT_TRUETYPE");
DataStreamPtr dataStreamPtr =
ResourceGroupManager::getSingleton().openResource(font->getSource(), font->getGroup());
MemoryDataStream ttfchunk(dataStreamPtr, false); // transfer ownership to imgui
// convert codepoint ranges for imgui
CodePointRange cprange;
for (const auto& r : font->getCodePointRangeList())
{
cprange.push_back(r.first);
cprange.push_back(r.second);
}
ImGuiIO& io = ImGui::GetIO();
const ImWchar* cprangePtr = io.Fonts->GetGlyphRangesDefault();
if (!cprange.empty())
{
cprange.push_back(0); // terminate
mCodePointRanges.push_back(cprange);
// ptr must persist until createFontTexture
cprangePtr = mCodePointRanges.back().data();
}
ImFontConfig cfg;
strncpy(cfg.Name, name.c_str(), IM_ARRAYSIZE(cfg.Name) - 1);
return io.Fonts->AddFontFromMemoryTTF(ttfchunk.getPtr(), ttfchunk.size(), font->getTrueTypeSize(), &cfg,
cprangePtr);
}
void ImGuiOverlay::ImGUIRenderable::createFontTexture()
{
// Build texture atlas
ImGuiIO& io = ImGui::GetIO();
if (io.Fonts->Fonts.empty())
io.Fonts->AddFontDefault();
unsigned char* pixels;
int width, height;
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
mFontTex = TextureManager::getSingleton().createManual("ImGui/FontTex", RGN_INTERNAL, TEX_TYPE_2D,
width, height, 1, 1, PF_BYTE_RGBA);
mFontTex->getBuffer()->blitFromMemory(PixelBox(Box(0, 0, width, height), PF_BYTE_RGBA, pixels));
}
void ImGuiOverlay::NewFrame()
{
static auto lastTime = Root::getSingleton().getTimer()->getMilliseconds();
auto now = Root::getSingleton().getTimer()->getMilliseconds();
ImGuiIO& io = ImGui::GetIO();
io.DeltaTime = std::max<float>(
float(now - lastTime)/1000,
1e-4f); // see https://github.com/ocornut/imgui/commit/3c07ec6a6126fb6b98523a9685d1f0f78ca3c40c
lastTime = now;
// Read keyboard modifiers inputs
io.KeyAlt = false;
io.KeySuper = false;
OverlayManager& oMgr = OverlayManager::getSingleton();
// Setup display size (every frame to accommodate for window resizing)
io.DisplaySize = ImVec2(oMgr.getViewportWidth(), oMgr.getViewportHeight());
// Start the frame
ImGui::NewFrame();
}
void ImGuiOverlay::ImGUIRenderable::_update()
{
if (mMaterial->getSupportedTechniques().empty())
{
mMaterial->load(); // Support for adding lights run time
}
RenderSystem* rSys = Root::getSingleton().getRenderSystem();
OverlayManager& oMgr = OverlayManager::getSingleton();
// Construct projection matrix, taking texel offset corrections in account (important for DirectX9)
// See also:
// - OGRE-API specific hint: http://www.ogre3d.org/forums/viewtopic.php?f=5&p=536881#p536881
// - IMGUI Dx9 demo solution:
// https://github.com/ocornut/imgui/blob/master/examples/directx9_example/imgui_impl_dx9.cpp#L127-L138
float texelOffsetX = rSys->getHorizontalTexelOffset();
float texelOffsetY = rSys->getVerticalTexelOffset();
float L = texelOffsetX;
float R = oMgr.getViewportWidth() + texelOffsetX;
float T = texelOffsetY;
float B = oMgr.getViewportHeight() + texelOffsetY;
mXform = Matrix4(2.0f / (R - L), 0.0f, 0.0f, (L + R) / (L - R), 0.0f, -2.0f / (B - T), 0.0f,
(T + B) / (B - T), 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
}
bool ImGuiOverlay::ImGUIRenderable::preRender(SceneManager* sm, RenderSystem* rsys)
{
Viewport* vp = rsys->_getViewport();
// Instruct ImGui to Render() and process the resulting CmdList-s
// Adopted from https://bitbucket.org/ChaosCreator/imgui-ogre2.1-binding
// ... Commentary on OGRE forums: http://www.ogre3d.org/forums/viewtopic.php?f=5&t=89081#p531059
ImGui::Render();
ImDrawData* draw_data = ImGui::GetDrawData();
int vpWidth = vp->getActualWidth();
int vpHeight = vp->getActualHeight();
TextureUnitState* tu = mMaterial->getBestTechnique()->getPass(0)->getTextureUnitState(0);
for (int i = 0; i < draw_data->CmdListsCount; ++i)
{
const ImDrawList* draw_list = draw_data->CmdLists[i];
updateVertexData(draw_list->VtxBuffer, draw_list->IdxBuffer);
unsigned int startIdx = 0;
for (int j = 0; j < draw_list->CmdBuffer.Size; ++j)
{
// Create a renderable and fill it's buffers
const ImDrawCmd* drawCmd = &draw_list->CmdBuffer[j];
// Set scissoring
Rect scissor(drawCmd->ClipRect.x, drawCmd->ClipRect.y, drawCmd->ClipRect.z,
drawCmd->ClipRect.w);
// Clamp bounds to viewport dimensions
scissor = scissor.intersect(Rect(0, 0, vpWidth, vpHeight));
if (drawCmd->TextureId)
{
auto handle = (ResourceHandle)drawCmd->TextureId;
auto tex = static_pointer_cast<Texture>(TextureManager::getSingleton().getByHandle(handle));
if (tex)
{
rsys->_setTexture(0, true, tex);
rsys->_setSampler(0, *TextureManager::getSingleton().getDefaultSampler());
}
}
rsys->setScissorTest(true, scissor);
// Render!
mRenderOp.indexData->indexStart = startIdx;
mRenderOp.indexData->indexCount = drawCmd->ElemCount;
rsys->_render(mRenderOp);
if (drawCmd->TextureId)
{
// reset to pass state
rsys->_setTexture(0, true, mFontTex);
rsys->_setSampler(0, *tu->getSampler());
}
// Update counts
startIdx += drawCmd->ElemCount;
}
}
rsys->setScissorTest(false);
return false;
}
const LightList& ImGuiOverlay::ImGUIRenderable::getLights() const
{
// Overlayelements should not be lit by the scene, this will not get called
static LightList ll;
return ll;
}
ImGuiOverlay::ImGUIRenderable::ImGUIRenderable()
{
// default overlays to preserve their own detail level
mPolygonModeOverrideable = false;
// use identity projection and view matrices
mUseIdentityProjection = true;
mUseIdentityView = true;
}
//-----------------------------------------------------------------------------------
void ImGuiOverlay::ImGUIRenderable::initialise(void)
{
createFontTexture();
createMaterial();
mRenderOp.vertexData = OGRE_NEW VertexData();
mRenderOp.indexData = OGRE_NEW IndexData();
mRenderOp.vertexData->vertexCount = 0;
mRenderOp.vertexData->vertexStart = 0;
mRenderOp.indexData->indexCount = 0;
mRenderOp.indexData->indexStart = 0;
mRenderOp.operationType = RenderOperation::OT_TRIANGLE_LIST;
mRenderOp.useIndexes = true;
mRenderOp.useGlobalInstancingVertexBufferIsAvailable = false;
VertexDeclaration* decl = mRenderOp.vertexData->vertexDeclaration;
// vertex declaration
size_t offset = 0;
decl->addElement(0, offset, VET_FLOAT2, VES_POSITION);
offset += VertexElement::getTypeSize(VET_FLOAT2);
decl->addElement(0, offset, VET_FLOAT2, VES_TEXTURE_COORDINATES, 0);
offset += VertexElement::getTypeSize(VET_FLOAT2);
decl->addElement(0, offset, VET_UBYTE4_NORM, VES_DIFFUSE);
}
//-----------------------------------------------------------------------------------
ImGuiOverlay::ImGUIRenderable::~ImGUIRenderable()
{
OGRE_DELETE mRenderOp.vertexData;
OGRE_DELETE mRenderOp.indexData;
}
//-----------------------------------------------------------------------------------
void ImGuiOverlay::ImGUIRenderable::updateVertexData(const ImVector<ImDrawVert>& vtxBuf,
const ImVector<ImDrawIdx>& idxBuf)
{
VertexBufferBinding* bind = mRenderOp.vertexData->vertexBufferBinding;
if (bind->getBindings().empty() || bind->getBuffer(0)->getNumVertices() < size_t(vtxBuf.size()))
{
bind->setBinding(0, HardwareBufferManager::getSingleton().createVertexBuffer(
sizeof(ImDrawVert), vtxBuf.size(), HBU_CPU_TO_GPU));
}
if (!mRenderOp.indexData->indexBuffer ||
mRenderOp.indexData->indexBuffer->getNumIndexes() < size_t(idxBuf.size()))
{
mRenderOp.indexData->indexBuffer = HardwareBufferManager::getSingleton().createIndexBuffer(
HardwareIndexBuffer::IT_16BIT, idxBuf.size(), HBU_CPU_TO_GPU);
}
// Copy all vertices
bind->getBuffer(0)->writeData(0, vtxBuf.size_in_bytes(), vtxBuf.Data, true);
mRenderOp.indexData->indexBuffer->writeData(0, idxBuf.size_in_bytes(), idxBuf.Data, true);
mRenderOp.vertexData->vertexStart = 0;
mRenderOp.vertexData->vertexCount = vtxBuf.size();
}
} // namespace Ogre