-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathimgui-glfw.cc
393 lines (363 loc) · 16 KB
/
imgui-glfw.cc
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
//------------------------------------------------------------------------------
// imgui-glfw
// Demonstrates basic integration with Dear Imgui (without custom
// texture or custom font support).
//------------------------------------------------------------------------------
#include "imgui.h"
#define SOKOL_IMPL
#define SOKOL_GLCORE
#include "sokol_gfx.h"
#include "sokol_time.h"
#include "sokol_log.h"
#include "glfw_glue.h"
const int Width = 1024;
const int Height = 768;
const int MaxVertices = (1<<16);
const int MaxIndices = MaxVertices * 3;
uint64_t last_time = 0;
bool show_test_window = true;
bool show_another_window = false;
sg_pass_action pass_action;
sg_pipeline pip;
sg_bindings bind;
typedef struct {
ImVec2 disp_size;
} vs_params_t;
static void draw_imgui(ImDrawData*);
static ImGuiKey glfw_key_to_imgui_key(int glfw_key);
int main() {
// window and GL context via GLFW and flextGL
glfw_desc_t glfw_desc = {};
glfw_desc.title = "imgui-glfw.cc";
glfw_desc.width = Width;
glfw_desc.height = Height;
glfw_init(&glfw_desc);
GLFWwindow* w = glfw_window();
// GLFW to ImGui input forwarding
glfwSetMouseButtonCallback(w, [](GLFWwindow*, int btn, int action, int /*mods*/) {
if ((btn >= 0) && (btn < ImGuiMouseButton_COUNT)) {
ImGui::GetIO().AddMouseButtonEvent(btn, action == GLFW_PRESS);
}
});
glfwSetCursorPosCallback(w, [](GLFWwindow*, double pos_x, double pos_y) {
ImGui::GetIO().AddMousePosEvent((float)pos_x, (float)pos_y);
});
glfwSetScrollCallback(w, [](GLFWwindow*, double wheel_x, double wheel_y){
ImGui::GetIO().AddMouseWheelEvent((float)wheel_x, (float)wheel_y);
});
glfwSetKeyCallback(w, [](GLFWwindow*, int key, int /*scancode*/, int action, int mods){
ImGuiIO& io = ImGui::GetIO();
io.AddKeyEvent(glfw_key_to_imgui_key(key), (action == GLFW_PRESS));
io.KeyCtrl = (0 != (mods & GLFW_MOD_CONTROL));
io.KeyAlt = (0 != (mods & GLFW_MOD_ALT));
io.KeyShift = (0 != (mods & GLFW_MOD_SHIFT));
});
glfwSetCharCallback(w, [](GLFWwindow*, unsigned int codepoint){
ImGui::GetIO().AddInputCharacter((ImWchar)codepoint);
});
// setup sokol_gfx and sokol_time
stm_setup();
sg_desc desc = { };
desc.environment = glfw_environment();
desc.logger.func = slog_func;
sg_setup(&desc);
assert(sg_isvalid());
// setup Dear Imgui
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = nullptr;
io.Fonts->AddFontDefault();
// dynamic vertex- and index-buffers for imgui-generated geometry
sg_buffer_desc vbuf_desc = { };
vbuf_desc.usage = SG_USAGE_STREAM;
vbuf_desc.size = MaxVertices * sizeof(ImDrawVert);
bind.vertex_buffers[0] = sg_make_buffer(&vbuf_desc);
sg_buffer_desc ibuf_desc = { };
ibuf_desc.type = SG_BUFFERTYPE_INDEXBUFFER;
ibuf_desc.usage = SG_USAGE_STREAM;
ibuf_desc.size = MaxIndices * sizeof(ImDrawIdx);
bind.index_buffer = sg_make_buffer(&ibuf_desc);
// font texture and sampler for imgui's default font
unsigned char* font_pixels;
int font_width, font_height;
io.Fonts->GetTexDataAsRGBA32(&font_pixels, &font_width, &font_height);
sg_image_desc img_desc = { };
img_desc.width = font_width;
img_desc.height = font_height;
img_desc.pixel_format = SG_PIXELFORMAT_RGBA8;
img_desc.data.subimage[0][0] = sg_range{ font_pixels, size_t(font_width * font_height * 4) };
bind.images[0] = sg_make_image(&img_desc);
sg_sampler_desc smp_desc = { };
smp_desc.wrap_u = SG_WRAP_CLAMP_TO_EDGE;
smp_desc.wrap_v = SG_WRAP_CLAMP_TO_EDGE;
bind.samplers[0] = sg_make_sampler(&smp_desc);
// shader object for imgui rendering
sg_shader_desc shd_desc = { };
auto& ub = shd_desc.uniform_blocks[0];
ub.stage = SG_SHADERSTAGE_VERTEX;
ub.size = sizeof(vs_params_t);
ub.glsl_uniforms[0].glsl_name = "disp_size";
ub.glsl_uniforms[0].type = SG_UNIFORMTYPE_FLOAT2;
shd_desc.vertex_func.source =
"#version 410\n"
"uniform vec2 disp_size;\n"
"layout(location=0) in vec2 position;\n"
"layout(location=1) in vec2 texcoord0;\n"
"layout(location=2) in vec4 color0;\n"
"out vec2 uv;\n"
"out vec4 color;\n"
"void main() {\n"
" gl_Position = vec4(((position/disp_size)-0.5)*vec2(2.0,-2.0), 0.5, 1.0);\n"
" uv = texcoord0;\n"
" color = color0;\n"
"}\n";
shd_desc.fragment_func.source =
"#version 410\n"
"uniform sampler2D tex;\n"
"in vec2 uv;\n"
"in vec4 color;\n"
"out vec4 frag_color;\n"
"void main() {\n"
" frag_color = texture(tex, uv) * color;\n"
"}\n";
shd_desc.images[0].stage = SG_SHADERSTAGE_FRAGMENT;
shd_desc.samplers[0].stage = SG_SHADERSTAGE_FRAGMENT;
shd_desc.image_sampler_pairs[0].stage = SG_SHADERSTAGE_FRAGMENT;
shd_desc.image_sampler_pairs[0].glsl_name = "tex";
shd_desc.image_sampler_pairs[0].image_slot = 0;
shd_desc.image_sampler_pairs[0].sampler_slot = 0;
sg_shader shd = sg_make_shader(&shd_desc);
// pipeline object for imgui rendering
sg_pipeline_desc pip_desc = { };
pip_desc.layout.buffers[0].stride = sizeof(ImDrawVert);
auto& attrs = pip_desc.layout.attrs;
attrs[0].format = SG_VERTEXFORMAT_FLOAT2;
attrs[1].format = SG_VERTEXFORMAT_FLOAT2;
attrs[2].format = SG_VERTEXFORMAT_UBYTE4N;
pip_desc.shader = shd;
pip_desc.index_type = SG_INDEXTYPE_UINT16;
pip_desc.colors[0].blend.enabled = true;
pip_desc.colors[0].blend.src_factor_rgb = SG_BLENDFACTOR_SRC_ALPHA;
pip_desc.colors[0].blend.dst_factor_rgb = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA;
pip_desc.colors[0].write_mask = SG_COLORMASK_RGB;
pip = sg_make_pipeline(&pip_desc);
// initial clear color
pass_action.colors[0].load_action = SG_LOADACTION_CLEAR;
pass_action.colors[0].clear_value = { 0.0f, 0.5f, 0.7f, 1.0f };
// draw loop
while (!glfwWindowShouldClose(w)) {
// this is standard ImGui demo code
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2(float(glfw_width()), float(glfw_height()));
io.DeltaTime = (float) stm_sec(stm_laptime(&last_time));
ImGui::NewFrame();
// 1. Show a simple window
// Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
static float f = 0.0f;
ImGui::Text("Hello, world!");
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
ImGui::ColorEdit3("clear color", &pass_action.colors[0].clear_value.r);
if (ImGui::Button("Test Window")) show_test_window ^= 1;
if (ImGui::Button("Another Window")) show_another_window ^= 1;
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
// 2. Show another simple window, this time using an explicit Begin/End pair
if (show_another_window) {
ImGui::SetNextWindowSize(ImVec2(200,100), ImGuiCond_FirstUseEver);
ImGui::Begin("Another Window", &show_another_window);
ImGui::Text("Hello");
ImGui::End();
}
// 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowDemoWindow()
if (show_test_window) {
ImGui::SetNextWindowPos(ImVec2(460, 20), ImGuiCond_FirstUseEver);
ImGui::ShowDemoWindow();
}
// the sokol_gfx draw pass
sg_pass pass = {};
pass.action = pass_action;
pass.swapchain = glfw_swapchain();
sg_begin_pass(pass);
ImGui::Render();
draw_imgui(ImGui::GetDrawData());
sg_end_pass();
sg_commit();
glfwSwapBuffers(w);
glfwPollEvents();
}
/* cleanup */
ImGui::DestroyContext();
sg_shutdown();
glfwTerminate();
return 0;
}
// draw ImGui draw lists via sokol-gfx
void draw_imgui(ImDrawData* draw_data) {
assert(draw_data);
if (draw_data->CmdListsCount == 0) {
return;
}
// render the command list
sg_apply_pipeline(pip);
vs_params_t vs_params;
vs_params.disp_size.x = ImGui::GetIO().DisplaySize.x;
vs_params.disp_size.y = ImGui::GetIO().DisplaySize.y;
sg_apply_uniforms(0, SG_RANGE(vs_params));
for (int cl_index = 0; cl_index < draw_data->CmdListsCount; cl_index++) {
const ImDrawList* cl = draw_data->CmdLists[cl_index];
// append vertices and indices to buffers, record start offsets in resource binding struct
const uint32_t vtx_size = cl->VtxBuffer.size() * sizeof(ImDrawVert);
const uint32_t idx_size = cl->IdxBuffer.size() * sizeof(ImDrawIdx);
const uint32_t vb_offset = sg_append_buffer(bind.vertex_buffers[0], { &cl->VtxBuffer.front(), vtx_size });
const uint32_t ib_offset = sg_append_buffer(bind.index_buffer, { &cl->IdxBuffer.front(), idx_size });
/* don't render anything if the buffer is in overflow state (this is also
checked internally in sokol_gfx, draw calls that attempt from
overflowed buffers will be silently dropped)
*/
if (sg_query_buffer_overflow(bind.vertex_buffers[0]) ||
sg_query_buffer_overflow(bind.index_buffer))
{
continue;
}
bind.vertex_buffer_offsets[0] = vb_offset;
bind.index_buffer_offset = ib_offset;
sg_apply_bindings(&bind);
int base_element = 0;
for (const ImDrawCmd& pcmd : cl->CmdBuffer) {
if (pcmd.UserCallback) {
pcmd.UserCallback(cl, &pcmd);
}
else {
const int scissor_x = (int) (pcmd.ClipRect.x);
const int scissor_y = (int) (pcmd.ClipRect.y);
const int scissor_w = (int) (pcmd.ClipRect.z - pcmd.ClipRect.x);
const int scissor_h = (int) (pcmd.ClipRect.w - pcmd.ClipRect.y);
sg_apply_scissor_rect(scissor_x, scissor_y, scissor_w, scissor_h, true);
sg_draw(base_element, pcmd.ElemCount, 1);
}
base_element += pcmd.ElemCount;
}
}
}
static ImGuiKey glfw_key_to_imgui_key(int glfw_key) {
switch (glfw_key) {
case GLFW_KEY_TAB: return ImGuiKey_Tab;
case GLFW_KEY_LEFT: return ImGuiKey_LeftArrow;
case GLFW_KEY_RIGHT: return ImGuiKey_RightArrow;
case GLFW_KEY_UP: return ImGuiKey_UpArrow;
case GLFW_KEY_DOWN: return ImGuiKey_DownArrow;
case GLFW_KEY_PAGE_UP: return ImGuiKey_PageUp;
case GLFW_KEY_PAGE_DOWN: return ImGuiKey_PageDown;
case GLFW_KEY_HOME: return ImGuiKey_Home;
case GLFW_KEY_END: return ImGuiKey_End;
case GLFW_KEY_INSERT: return ImGuiKey_Insert;
case GLFW_KEY_DELETE: return ImGuiKey_Delete;
case GLFW_KEY_BACKSPACE: return ImGuiKey_Backspace;
case GLFW_KEY_SPACE: return ImGuiKey_Space;
case GLFW_KEY_ENTER: return ImGuiKey_Enter;
case GLFW_KEY_ESCAPE: return ImGuiKey_Escape;
case GLFW_KEY_APOSTROPHE: return ImGuiKey_Apostrophe;
case GLFW_KEY_COMMA: return ImGuiKey_Comma;
case GLFW_KEY_MINUS: return ImGuiKey_Minus;
case GLFW_KEY_PERIOD: return ImGuiKey_Period;
case GLFW_KEY_SLASH: return ImGuiKey_Slash;
case GLFW_KEY_SEMICOLON: return ImGuiKey_Semicolon;
case GLFW_KEY_EQUAL: return ImGuiKey_Equal;
case GLFW_KEY_LEFT_BRACKET: return ImGuiKey_LeftBracket;
case GLFW_KEY_BACKSLASH: return ImGuiKey_Backslash;
case GLFW_KEY_RIGHT_BRACKET: return ImGuiKey_RightBracket;
case GLFW_KEY_GRAVE_ACCENT: return ImGuiKey_GraveAccent;
case GLFW_KEY_CAPS_LOCK: return ImGuiKey_CapsLock;
case GLFW_KEY_SCROLL_LOCK: return ImGuiKey_ScrollLock;
case GLFW_KEY_NUM_LOCK: return ImGuiKey_NumLock;
case GLFW_KEY_PRINT_SCREEN: return ImGuiKey_PrintScreen;
case GLFW_KEY_PAUSE: return ImGuiKey_Pause;
case GLFW_KEY_KP_0: return ImGuiKey_Keypad0;
case GLFW_KEY_KP_1: return ImGuiKey_Keypad1;
case GLFW_KEY_KP_2: return ImGuiKey_Keypad2;
case GLFW_KEY_KP_3: return ImGuiKey_Keypad3;
case GLFW_KEY_KP_4: return ImGuiKey_Keypad4;
case GLFW_KEY_KP_5: return ImGuiKey_Keypad5;
case GLFW_KEY_KP_6: return ImGuiKey_Keypad6;
case GLFW_KEY_KP_7: return ImGuiKey_Keypad7;
case GLFW_KEY_KP_8: return ImGuiKey_Keypad8;
case GLFW_KEY_KP_9: return ImGuiKey_Keypad9;
case GLFW_KEY_KP_DECIMAL: return ImGuiKey_KeypadDecimal;
case GLFW_KEY_KP_DIVIDE: return ImGuiKey_KeypadDivide;
case GLFW_KEY_KP_MULTIPLY: return ImGuiKey_KeypadMultiply;
case GLFW_KEY_KP_SUBTRACT: return ImGuiKey_KeypadSubtract;
case GLFW_KEY_KP_ADD: return ImGuiKey_KeypadAdd;
case GLFW_KEY_KP_ENTER: return ImGuiKey_KeypadEnter;
case GLFW_KEY_KP_EQUAL: return ImGuiKey_KeypadEqual;
case GLFW_KEY_LEFT_SHIFT: return ImGuiKey_LeftShift;
case GLFW_KEY_LEFT_CONTROL: return ImGuiKey_LeftCtrl;
case GLFW_KEY_LEFT_ALT: return ImGuiKey_LeftAlt;
case GLFW_KEY_LEFT_SUPER: return ImGuiKey_LeftSuper;
case GLFW_KEY_RIGHT_SHIFT: return ImGuiKey_RightShift;
case GLFW_KEY_RIGHT_CONTROL: return ImGuiKey_RightCtrl;
case GLFW_KEY_RIGHT_ALT: return ImGuiKey_RightAlt;
case GLFW_KEY_RIGHT_SUPER: return ImGuiKey_RightSuper;
case GLFW_KEY_MENU: return ImGuiKey_Menu;
case GLFW_KEY_0: return ImGuiKey_0;
case GLFW_KEY_1: return ImGuiKey_1;
case GLFW_KEY_2: return ImGuiKey_2;
case GLFW_KEY_3: return ImGuiKey_3;
case GLFW_KEY_4: return ImGuiKey_4;
case GLFW_KEY_5: return ImGuiKey_5;
case GLFW_KEY_6: return ImGuiKey_6;
case GLFW_KEY_7: return ImGuiKey_7;
case GLFW_KEY_8: return ImGuiKey_8;
case GLFW_KEY_9: return ImGuiKey_9;
case GLFW_KEY_A: return ImGuiKey_A;
case GLFW_KEY_B: return ImGuiKey_B;
case GLFW_KEY_C: return ImGuiKey_C;
case GLFW_KEY_D: return ImGuiKey_D;
case GLFW_KEY_E: return ImGuiKey_E;
case GLFW_KEY_F: return ImGuiKey_F;
case GLFW_KEY_G: return ImGuiKey_G;
case GLFW_KEY_H: return ImGuiKey_H;
case GLFW_KEY_I: return ImGuiKey_I;
case GLFW_KEY_J: return ImGuiKey_J;
case GLFW_KEY_K: return ImGuiKey_K;
case GLFW_KEY_L: return ImGuiKey_L;
case GLFW_KEY_M: return ImGuiKey_M;
case GLFW_KEY_N: return ImGuiKey_N;
case GLFW_KEY_O: return ImGuiKey_O;
case GLFW_KEY_P: return ImGuiKey_P;
case GLFW_KEY_Q: return ImGuiKey_Q;
case GLFW_KEY_R: return ImGuiKey_R;
case GLFW_KEY_S: return ImGuiKey_S;
case GLFW_KEY_T: return ImGuiKey_T;
case GLFW_KEY_U: return ImGuiKey_U;
case GLFW_KEY_V: return ImGuiKey_V;
case GLFW_KEY_W: return ImGuiKey_W;
case GLFW_KEY_X: return ImGuiKey_X;
case GLFW_KEY_Y: return ImGuiKey_Y;
case GLFW_KEY_Z: return ImGuiKey_Z;
case GLFW_KEY_F1: return ImGuiKey_F1;
case GLFW_KEY_F2: return ImGuiKey_F2;
case GLFW_KEY_F3: return ImGuiKey_F3;
case GLFW_KEY_F4: return ImGuiKey_F4;
case GLFW_KEY_F5: return ImGuiKey_F5;
case GLFW_KEY_F6: return ImGuiKey_F6;
case GLFW_KEY_F7: return ImGuiKey_F7;
case GLFW_KEY_F8: return ImGuiKey_F8;
case GLFW_KEY_F9: return ImGuiKey_F9;
case GLFW_KEY_F10: return ImGuiKey_F10;
case GLFW_KEY_F11: return ImGuiKey_F11;
case GLFW_KEY_F12: return ImGuiKey_F12;
case GLFW_KEY_F13: return ImGuiKey_F13;
case GLFW_KEY_F14: return ImGuiKey_F14;
case GLFW_KEY_F15: return ImGuiKey_F15;
case GLFW_KEY_F16: return ImGuiKey_F16;
case GLFW_KEY_F17: return ImGuiKey_F17;
case GLFW_KEY_F18: return ImGuiKey_F18;
case GLFW_KEY_F19: return ImGuiKey_F19;
case GLFW_KEY_F20: return ImGuiKey_F20;
case GLFW_KEY_F21: return ImGuiKey_F21;
case GLFW_KEY_F22: return ImGuiKey_F22;
case GLFW_KEY_F23: return ImGuiKey_F23;
case GLFW_KEY_F24: return ImGuiKey_F24;
default: return ImGuiKey_None;
}
}