forked from andyborrell/imgui_tex_inspect
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgui_tex_inspect_demo.cpp
440 lines (382 loc) · 16.3 KB
/
imgui_tex_inspect_demo.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// ImGuiTexInspect, a texture inspector widget for dear imgui
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "imgui_tex_inspect_internal.h"
#include "imgui_tex_inspect_demo.h"
#include "imgui.h"
#include "imgui_tex_inspect.h"
#include <iostream>
namespace ImGuiTexInspect
{
Texture testTex;
Texture fontTexture;
bool testInitted = false;
//-------------------------------------------------------------------------
// [SECTION] FORWARD DECLARATIONS
//-------------------------------------------------------------------------
void Demo_ColorFilters();
void Demo_ColorMatrix();
void Demo_TextureAnnotations();
void Demo_AlphaMode();
void Demo_WrapAndFilter();
void ShowDemoWindow();
Texture LoadDemoTexture();
void DemoInit();
//-------------------------------------------------------------------------
// [SECTION] EXAMPLE USAGES
//-------------------------------------------------------------------------
/* Each of the following Demo_* functions is a standalone demo showing an example
* usage of ImGuiTexInspect. You'll notice the structure is essentially the same in
* all the examples, i.e. a call to BeginInspectorPanel & a call to
* EndInspectorPanel, possibly with some annotation functions in between,
* followed by some controls to manipulate the inspector.
*
* Each Demo_* function corresponds to one of the large buttons at the top of
* the demo window.
*/
/* Demo_ColorFilters
* An example showing controls to filter red, green and blue channels
* independently. These controls are provided by the DrawColorChannelSelector
* funtions. The example also uses the DrawGridEditor function to allow the
* user to control grid appearance.
*
* The Draw_* functions are provided for convenience, it is of course possible
* to control these aspects programmatically.
*/
void Demo_ColorFilters()
{
/* BeginInspectorPanel & EndInspectorPanel is all you need to draw an
* inspector (assuming you are already in between an ImGui::Begin and
* ImGui::End pair)
* */
static bool flipX = false;
static bool flipY = false;
ImGuiTexInspect::InspectorFlags flags = 0;
if (flipX) SetFlag(flags, ImGuiTexInspect::InspectorFlags_FlipX);
if (flipY) SetFlag(flags, ImGuiTexInspect::InspectorFlags_FlipY);
if (ImGuiTexInspect::BeginInspectorPanel("##ColorFilters", testTex.texture, testTex.size, flags))
{
// Draw some text showing color value of each texel (you must be zoomed in to see this)
ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::ValueText(ImGuiTexInspect::ValueText::BytesDec));
}
ImGuiTexInspect::EndInspectorPanel();
// Now some ordinary ImGui elements to provide some explanation
ImGui::BeginChild("Controls", ImVec2(600, 100));
ImGui::TextWrapped("Basics:");
ImGui::BulletText("Use mouse wheel to zoom in and out. Click and drag to pan.");
ImGui::BulletText("Use the demo select buttons at the top of the window to explore");
ImGui::BulletText("Use the controls below to change basic color filtering options");
ImGui::EndChild();
/* DrawColorChannelSelector & DrawGridEditor are convenience functions that
* draw ImGui controls to manipulate config of the most recently drawn
* texture inspector
**/
ImGuiTexInspect::DrawColorChannelSelector();
ImGui::SameLine(200);
ImGuiTexInspect::DrawGridEditor();
ImGui::Separator();
ImGui::Checkbox("Flip X", &flipX);
ImGui::Checkbox("Flip Y", &flipY);
}
//-------------------------------------------------------------------------
/* Demo_ColorMatrix
* An example showing usage of the ColorMatrix. See comments at the
* declaration of CurrentInspector_SetColorMatrix for details. This example
* shows how to set the matrix directly, as well as how to use the
* DrawColorMatrixEditor convenience function to draw ImGui controls to
* manipulate it.
*/
void Demo_ColorMatrix()
{
if (ImGuiTexInspect::BeginInspectorPanel("##ColorMatrix", testTex.texture, testTex.size))
{
// Draw some text showing color value of each texel (you must be zoomed in to see this)
ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::ValueText(ImGuiTexInspect::ValueText::BytesDec));
}
ImGuiTexInspect::EndInspectorPanel();
ImGui::BeginGroup();
ImGui::Text("Colour Matrix Editor:");
// Draw Matrix editor to allow user to manipulate the ColorMatrix
ImGuiTexInspect::DrawColorMatrixEditor();
ImGui::EndGroup();
ImGui::SameLine();
// Provide some presets that can be used to set the ColorMatrix for example purposes
ImGui::BeginGroup();
ImGui::PushItemWidth(200);
ImGui::Indent(50);
const ImVec2 buttonSize = ImVec2(160, 0);
ImGui::Text("Example Presets:");
// clang-format off
if (ImGui::Button("Negative", buttonSize))
{
// Matrix which inverts each of the red, green, blue channels and leaves Alpha untouched
float matrix[] = {-1.000f, 0.000f, 0.000f, 0.000f,
0.000f, -1.000f, 0.000f, 0.000f,
0.000f, 0.000f, -1.000f, 0.000f,
0.000f, 0.000f, 0.000f, 1.000f};
float colorOffset[] = {1, 1, 1, 0};
ImGuiTexInspect::CurrentInspector_SetColorMatrix(matrix, colorOffset);
}
if (ImGui::Button("Swap Red & Blue", buttonSize))
{
// Matrix which swaps red and blue channels but leaves green and alpha untouched
float matrix[] = { 0.000f, 0.000f, 1.000f, 0.000f,
0.000f, 1.000f, 0.000f, 0.000f,
1.000f, 0.000f, 0.000f, 0.000f,
0.000f, 0.000f, 0.000f, 1.000f};
float colorOffset[] = {0, 0, 0, 0};
ImGuiTexInspect::CurrentInspector_SetColorMatrix(matrix, colorOffset);
}
if (ImGui::Button("Alpha", buttonSize))
{
// Red, green and blue channels are set based on alpha value so that alpha = 1 shows as white.
// output alpha is set to 1
float highlightTransparencyMatrix[] = {0.000f, 0.000f, 0.000f, 0.000f,
0.000f, 0.000f, 0.000f, 0.000f,
0.000f, 0.000f, 0.000f, 0.000f,
1.000, 1.000, 1.000, 1.000f};
float highlightTransparencyOffset[] = {0, 0, 0, 1};
ImGuiTexInspect::CurrentInspector_SetColorMatrix(highlightTransparencyMatrix, highlightTransparencyOffset);
}
if (ImGui::Button("Transparency", buttonSize))
{
// Red, green and blue channels are scaled by 0.1f. Low alpha values are shown as magenta
float highlightTransparencyMatrix[] = {0.100f, 0.100f, 0.100f, 0.000f,
0.100f, 0.100f, 0.100f, 0.000f,
0.100f, 0.100f, 0.100f, 0.000f,
-1.000f, 0.000f, -1.000f, 0.000f};
float highlightTransparencyOffset[] = {1, 0, 1, 1};
ImGuiTexInspect::CurrentInspector_SetColorMatrix(highlightTransparencyMatrix, highlightTransparencyOffset);
}
if (ImGui::Button("Default", buttonSize))
{
// Default "identity" matrix that doesn't modify colors at all
float matrix[] = {1.000f, 0.000f, 0.000f, 0.000f,
0.000f, 1.000f, 0.000f, 0.000f,
0.000f, 0.000f, 1.000f, 0.000f,
0.000f, 0.000f, 0.000f, 1.000f};
float colorOffset[] = {0, 0, 0, 0};
ImGuiTexInspect::CurrentInspector_SetColorMatrix(matrix, colorOffset);
}
// clang-format on
ImGui::PopItemWidth();
ImGui::EndGroup();
}
/* Demo_AlphaMode
* Very simple example that calls DrawAlphaModeSelector to draw controls to
* allow user to select alpha mode for the inpsector. See InspectorAlphaMode
* enum for details on what the different modes are. */
void Demo_AlphaMode()
{
if (ImGuiTexInspect::BeginInspectorPanel("##AlphaModeDemo", testTex.texture, testTex.size))
{
// Add annotations here
}
ImGuiTexInspect::EndInspectorPanel();
ImGuiTexInspect::DrawAlphaModeSelector();
}
/* Demo_WrapAndFilter
* Demo showing the effect of the InspectorFlags_ShowWrap & InspectorFlags_NoForceFilterNearest flags.
* See InspectorFlags_ enum for details on these flags.
*/
void Demo_WrapAndFilter()
{
static bool showWrap = false;
static bool forceNearestTexel = true;
if (ImGuiTexInspect::BeginInspectorPanel("##WrapAndFilter", testTex.texture, testTex.size))
{
}
ImGuiTexInspect::InspectorFlags flags = 0;
if (showWrap)
flags |= ImGuiTexInspect::InspectorFlags_ShowWrap;
if (!forceNearestTexel)
flags |= ImGuiTexInspect::InspectorFlags_NoForceFilterNearest;
ImGuiTexInspect::CurrentInspector_SetFlags(flags, ~flags);
ImGuiTexInspect::EndInspectorPanel();
ImGui::BeginChild("Controls", ImVec2(600, 0));
ImGui::TextWrapped("The following option can be enabled to render texture outside of the [0,1] UV range, what you actually "
"see outside of this range will depend on the mode of the texture. For example you may see the texture repeat, or "
"it might be clamped to the colour of the edge pixels.\nIn this demo the texture is set to wrap.");
ImGui::Checkbox("Show Wrapping Mode", &showWrap);
ImGui::TextWrapped("The following option is enabled by default and forces a nearest texel filter, implemented at the shader level. "
"By disabling this you can the currently set mode for this texture.");
ImGui::Checkbox("Force Nearest Texel", &forceNearestTexel);
ImGui::EndChild();
}
// This class is used in Demo_TextureAnnotations to show the process of creating a new texture annotation.
class CustomAnnotationExample
{
public:
void DrawAnnotation(ImDrawList *drawList, ImVec2 texel, ImGuiTexInspect::Transform2D texelsToPixels, ImVec4 value)
{
/* A silly example to show the process of creating a new annotation
* We'll see which primary colour is the dominant colour in the texel
* then draw a different shape for each primary colour. The radius
* will be based on the overall brightness.
*/
int numSegments;
if (value.x > value.y && value.x > value.z)
{
// Red pixel - draw a triangle!
numSegments = 3;
}
else
{
if (value.y > value.z)
{
// Green pixel - draw a diamond!
numSegments = 4;
}
else
{
// Blue pixel - draw a hexagon!
numSegments = 6;
}
}
// Don't go larger than whole texel
const float maxRadius = texelsToPixels.Scale.x * 0.5f;
// Scale radius based on texel brightness
const float radius = maxRadius * (value.x + value.y + value.z) / 3;
drawList->AddNgon(texelsToPixels * texel, radius, 0xFFFFFFFF, numSegments);
}
};
void Demo_TextureAnnotations()
{
static bool annotationEnabled_arrow = true;
static bool annotationEnabled_valueText = false;
static bool annotationEnabled_customExample = false;
static ImGuiTexInspect::ValueText::Format textFormat = ImGuiTexInspect::ValueText::BytesHex;
const int maxAnnotatedTexels = 1000;
if (ImGuiTexInspect::BeginInspectorPanel("##TextureAnnotations", testTex.texture, testTex.size))
{
// Draw the currently enabled annotations...
if (annotationEnabled_arrow)
{
ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::Arrow().UsePreset(ImGuiTexInspect::Arrow::NormalMap), maxAnnotatedTexels);
}
if (annotationEnabled_valueText)
{
ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::ValueText(textFormat), maxAnnotatedTexels);
}
if (annotationEnabled_customExample)
{
ImGuiTexInspect::DrawAnnotations(CustomAnnotationExample(), maxAnnotatedTexels);
}
}
ImGuiTexInspect::EndInspectorPanel();
// Checkboxes to toggle each type of annotation on and off
ImGui::BeginChild("Controls", ImVec2(600, 0));
ImGui::Checkbox("Arrow (Hint: zoom in on the normal-map part of the texture)", &annotationEnabled_arrow);
ImGui::Checkbox("Value Text", &annotationEnabled_valueText);
ImGui::Checkbox("Custom Annotation Example", &annotationEnabled_customExample);
ImGui::EndChild();
if (annotationEnabled_valueText)
{
// Show a combo to select the text formatting mode
ImGui::SameLine();
ImGui::BeginGroup();
const char *textOptions[] = {"Hex String", "Bytes in Hex", "Bytes in Decimal", "Floats"};
ImGui::SetNextItemWidth(200);
int textFormatInt = (int)(textFormat);
ImGui::Combo("Text Mode", &textFormatInt, textOptions, IM_ARRAYSIZE(textOptions));
textFormat = (ImGuiTexInspect::ValueText::Format)textFormatInt;
ImGui::EndGroup();
}
}
//-------------------------------------------------------------------------
// [SECTION] MAIN DEMO WINDOW FUNCTION
//-------------------------------------------------------------------------
void ShowDemoWindow()
{
if (!testInitted)
{
DemoInit();
}
ImGui::SetNextWindowPos(ImVec2(50, 50), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(1000, 1000), ImGuiCond_FirstUseEver);
struct DemoConfig
{
const char *buttonName; // Button text to display to user for a demo
void (*DrawDemoFn)(); // Function which implements the demo
};
const DemoConfig demos[] = {
{"Basics", Demo_ColorFilters},
{"Color Matrix", Demo_ColorMatrix},
{"Annotations", Demo_TextureAnnotations},
{"Alpha Mode", Demo_AlphaMode},
{"Wrap & Filter", Demo_WrapAndFilter},
};
if (ImGui::Begin("ImGuiTexInspect Demo"))
{
ImGui::Text("Select Demo:");
ImGui::Spacing();
//Custom color values to example-select buttons to make them stand out
ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor::HSV(0.59f, 0.7f, 0.8f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, (ImVec4)ImColor::HSV(0.59f, 0.8f, 0.8f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, (ImVec4)ImColor::HSV(0.59f, 0.9f, 1.0f));
// Draw row of buttons, one for each demo scene
static int selectedDemo = 0;
for (int i = 0; i < IM_ARRAYSIZE(demos); i++)
{
if (i != 0)
{
ImGui::SameLine();
}
if (ImGui::Button(demos[i].buttonName, ImVec2(140, 60)))
{
selectedDemo = i;
}
}
ImGui::PopStyleColor();
ImGui::PopStyleColor();
ImGui::PopStyleColor();
ImGui::Spacing();
// Call function to render currently example scene
demos[selectedDemo].DrawDemoFn();
}
ImGui::End();
}
//-------------------------------------------------------------------------
// [SECTION] INIT & TEXTURE LOAD
//-------------------------------------------------------------------------
void DemoInit()
{
ImGuiTexInspect::Init();
ImGuiTexInspect::CreateContext();
ImGuiIO &io = ImGui::GetIO();
fontTexture.texture = io.Fonts->TexID;
fontTexture.size = ImVec2((float)io.Fonts->TexWidth, (float)io.Fonts->TexHeight);
testTex = LoadDemoTexture();
testInitted = true;
}
// Check if file at path is accessible
bool CanAccessFile(const char *path)
{
if (FILE *file = fopen(path, "r"))
{
fclose(file);
return true;
}
else
{
return false;
}
}
Texture LoadDemoTexture()
{
/* To be a bit forgiving to build different build & test processes we check
* a few different paths to find the demo texture.
*/
const char *pathsToTry[] = {"demo_1.png", "../demo_1.png", "examples/demo_1.png"};
for (int i = 0; i < IM_ARRAYSIZE(pathsToTry); ++i)
{
if (CanAccessFile(pathsToTry[i]))
{
return LoadTexture(pathsToTry[i]);
}
}
fprintf(stderr, "Unable to find demo_1.png\n");
exit(-1);
}
} //namespace