|
@@ -26,12 +26,16 @@
|
26
|
26
|
#include <glbinding/gl/gl.h>
|
27
|
27
|
#include <glm/gtc/matrix_transform.hpp>
|
28
|
28
|
|
|
29
|
+#define OFFSETOF(TYPE, ELEMENT) (&(static_cast<TYPE *>(nullptr)->ELEMENT))
|
|
30
|
+
|
29
|
31
|
Shader UI::imguiShader;
|
30
|
32
|
bool UI::visible = false;
|
31
|
33
|
unsigned int UI::fontTex;
|
32
|
34
|
std::string UI::iniFilename;
|
33
|
35
|
std::string UI::logFilename;
|
34
|
36
|
bool UI::metaKeyIsActive = false;
|
|
37
|
+unsigned int UI::vboHandle = 0;
|
|
38
|
+unsigned int UI::elementHandle = 0;
|
35
|
39
|
|
36
|
40
|
std::list<std::tuple<KeyboardButton, bool>> UI::keyboardEvents;
|
37
|
41
|
std::list<std::tuple<unsigned int, unsigned int, KeyboardButton, bool>> UI::clickEvents;
|
|
@@ -43,6 +47,8 @@ void UI::setSize(glm::i32vec2 s) {
|
43
|
47
|
io.DisplaySize = ImVec2(s.x, s.y);
|
44
|
48
|
}
|
45
|
49
|
|
|
50
|
+static int attribPos, attribUV, attribCol;
|
|
51
|
+
|
46
|
52
|
int UI::initialize() {
|
47
|
53
|
if (imguiShader.compile(imguiShaderVertex, imguiShaderFragment) < 0)
|
48
|
54
|
return -1;
|
|
@@ -51,6 +57,10 @@ int UI::initialize() {
|
51
|
57
|
if (imguiShader.addUniform("textureSampler") < 0)
|
52
|
58
|
return -3;
|
53
|
59
|
|
|
60
|
+ attribPos = imguiShader.getAttrib("vertexPosition_screen");
|
|
61
|
+ attribUV = imguiShader.getAttrib("vertexUV");
|
|
62
|
+ attribCol = imguiShader.getAttrib("vertexColor");
|
|
63
|
+
|
54
|
64
|
iniFilename = RunTime::getBaseDir() + "/imgui.ini";
|
55
|
65
|
logFilename = RunTime::getBaseDir() + "/imgui_log.txt";
|
56
|
66
|
|
|
@@ -94,7 +104,11 @@ int UI::initialize() {
|
94
|
104
|
auto bm = TextureManager::getBufferManager(fontTex, TextureStorage::SYSTEM);
|
95
|
105
|
io.Fonts->TexID = bm;
|
96
|
106
|
|
|
107
|
+ gl::glGenBuffers(1, &vboHandle);
|
|
108
|
+ gl::glGenBuffers(1, &elementHandle);
|
|
109
|
+
|
97
|
110
|
// Set up OpenRaider style
|
|
111
|
+ /*
|
98
|
112
|
ImGuiStyle& style = ImGui::GetStyle();
|
99
|
113
|
style.Colors[ImGuiCol_Text] = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
|
100
|
114
|
style.Colors[ImGuiCol_WindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
|
|
@@ -142,6 +156,7 @@ int UI::initialize() {
|
142
|
156
|
style.TouchExtraPadding = ImVec2(0, 0);
|
143
|
157
|
style.IndentSpacing = 3;
|
144
|
158
|
style.ScrollbarWidth = 10;
|
|
159
|
+ */
|
145
|
160
|
|
146
|
161
|
return 0;
|
147
|
162
|
}
|
|
@@ -375,6 +390,9 @@ void UI::display() {
|
375
|
390
|
|
376
|
391
|
void UI::shutdown() {
|
377
|
392
|
ImGui::Shutdown();
|
|
393
|
+
|
|
394
|
+ gl::glDeleteBuffers(1, &vboHandle);
|
|
395
|
+ gl::glDeleteBuffers(1, &elementHandle);
|
378
|
396
|
}
|
379
|
397
|
|
380
|
398
|
void UI::handleKeyboard(KeyboardButton key, bool pressed) {
|
|
@@ -482,70 +500,59 @@ void UI::handleControllerButton(KeyboardButton button, bool released) {
|
482
|
500
|
}
|
483
|
501
|
}
|
484
|
502
|
|
485
|
|
-void UI::renderImGui(ImDrawList** const cmd_lists, int cmd_lists_count) {
|
486
|
|
- if (cmd_lists_count == 0)
|
|
503
|
+void UI::renderImGui(ImDrawData* draw_data) {
|
|
504
|
+ if (draw_data->CmdListsCount == 0)
|
487
|
505
|
return;
|
488
|
506
|
|
489
|
|
- static ShaderBuffer vert, uv, col;
|
490
|
|
-
|
491
|
507
|
gl::glEnable(gl::GL_SCISSOR_TEST);
|
492
|
508
|
Shader::set2DState(true);
|
493
|
509
|
|
|
510
|
+ gl::glEnableVertexAttribArray(attribPos);
|
|
511
|
+ gl::glEnableVertexAttribArray(attribUV);
|
|
512
|
+ gl::glEnableVertexAttribArray(attribCol);
|
|
513
|
+
|
|
514
|
+ gl::glBindBuffer(gl::GL_ARRAY_BUFFER, vboHandle);
|
|
515
|
+
|
|
516
|
+ gl::glVertexAttribPointer(attribPos, 2, gl::GL_FLOAT, gl::GL_FALSE, sizeof(ImDrawVert), OFFSETOF(ImDrawVert, pos));
|
|
517
|
+ gl::glVertexAttribPointer(attribUV, 2, gl::GL_FLOAT, gl::GL_FALSE, sizeof(ImDrawVert), OFFSETOF(ImDrawVert, uv));
|
|
518
|
+ gl::glVertexAttribPointer(attribCol, 4, gl::GL_UNSIGNED_BYTE, gl::GL_TRUE, sizeof(ImDrawVert), OFFSETOF(ImDrawVert, col));
|
|
519
|
+
|
494
|
520
|
imguiShader.use();
|
495
|
521
|
imguiShader.loadUniform(0, Window::getSize());
|
496
|
|
- vert.bindBuffer(0, 2);
|
497
|
|
- uv.bindBuffer(1, 2);
|
498
|
|
- col.bindBuffer(2, 4);
|
499
|
|
-
|
500
|
|
- /*! \fixme Don't copy data
|
501
|
|
- * The GL calls and the shaders can probably be slightly altered
|
502
|
|
- * to avoid copying all the vertices, uvs and colors again here.
|
503
|
|
- */
|
504
|
|
-
|
505
|
|
- for (int i = 0; i < cmd_lists_count; i++) {
|
506
|
|
- auto& commands = cmd_lists[i]->commands;
|
507
|
|
- auto& buffer = cmd_lists[i]->vtx_buffer;
|
508
|
|
-
|
509
|
|
- int offset = 0;
|
510
|
|
- for (int n = 0; n < commands.size(); n++) {
|
511
|
|
- std::vector<glm::vec2> vertices;
|
512
|
|
- std::vector<glm::vec2> uvs;
|
513
|
|
- std::vector<glm::vec4> colors;
|
514
|
|
-
|
515
|
|
- for (int v = 0; v < commands[n].vtx_count; v++) {
|
516
|
|
- vertices.push_back(glm::vec2(buffer[offset + v].pos.x, buffer[offset + v].pos.y));
|
517
|
|
- uvs.push_back(glm::vec2(buffer[offset + v].uv.x, buffer[offset + v].uv.y));
|
518
|
|
-
|
519
|
|
- float r, g, b, a;
|
520
|
|
- a = ((buffer[offset + v].col & 0xFF000000) >> 24) / 255.0f;
|
521
|
|
- b = ((buffer[offset + v].col & 0x00FF0000) >> 16) / 255.0f;
|
522
|
|
- g = ((buffer[offset + v].col & 0x0000FF00) >> 8) / 255.0f;
|
523
|
|
- r = (buffer[offset + v].col & 0x000000FF) / 255.0f;
|
524
|
|
- colors.push_back(glm::vec4(r, g, b, a));
|
525
|
|
- }
|
526
|
522
|
|
527
|
|
- offset += commands[n].vtx_count;
|
|
523
|
+ for (int i = 0; i < draw_data->CmdListsCount; i++) {
|
|
524
|
+ const ImDrawList* cmd_list = draw_data->CmdLists[i];
|
|
525
|
+ const ImDrawIdx* idx_buffer_offset = 0;
|
528
|
526
|
|
529
|
|
- vert.bufferData(vertices);
|
530
|
|
- uv.bufferData(uvs);
|
531
|
|
- col.bufferData(colors);
|
|
527
|
+ gl::glBindBuffer(gl::GL_ARRAY_BUFFER, vboHandle);
|
|
528
|
+ gl::glBufferData(gl::GL_ARRAY_BUFFER, cmd_list->VtxBuffer.size() * sizeof(ImDrawVert), &cmd_list->VtxBuffer.front(), gl::GL_STREAM_DRAW);
|
532
|
529
|
|
533
|
|
- auto bm = static_cast<BufferManager*>(commands[n].texture_id);
|
534
|
|
- orAssert(bm != nullptr);
|
535
|
|
- imguiShader.loadUniform(1, bm->getTextureID(), bm->getTextureStorage());
|
|
530
|
+ gl::glBindBuffer(gl::GL_ELEMENT_ARRAY_BUFFER, elementHandle);
|
|
531
|
+ gl::glBufferData(gl::GL_ELEMENT_ARRAY_BUFFER, cmd_list->IdxBuffer.size() * sizeof(ImDrawIdx), &cmd_list->IdxBuffer.front(), gl::GL_STREAM_DRAW);
|
536
|
532
|
|
537
|
|
- gl::glScissor(commands[n].clip_rect.x,
|
538
|
|
- Window::getSize().y - commands[n].clip_rect.w,
|
539
|
|
- commands[n].clip_rect.z - commands[n].clip_rect.x,
|
540
|
|
- commands[n].clip_rect.w - commands[n].clip_rect.y);
|
|
533
|
+ for (const ImDrawCmd* pcmd = cmd_list->CmdBuffer.begin(); pcmd != cmd_list->CmdBuffer.end(); pcmd++) {
|
|
534
|
+ if (pcmd->UserCallback) {
|
|
535
|
+ pcmd->UserCallback(cmd_list, pcmd);
|
|
536
|
+ Log::get(LOG_INFO) << "renderImGui: did not draw (Callback)" << Log::endl;
|
|
537
|
+ } else {
|
|
538
|
+ auto bm = static_cast<BufferManager*>(pcmd->TextureId);
|
|
539
|
+ orAssert(bm != nullptr);
|
|
540
|
+ imguiShader.loadUniform(1, bm->getTextureID(), bm->getTextureStorage());
|
|
541
|
+
|
|
542
|
+ gl::glScissor(pcmd->ClipRect.x,
|
|
543
|
+ Window::getSize().y - pcmd->ClipRect.w,
|
|
544
|
+ pcmd->ClipRect.z - pcmd->ClipRect.x,
|
|
545
|
+ pcmd->ClipRect.w - pcmd->ClipRect.y);
|
541
|
546
|
|
542
|
|
- gl::glDrawArrays(gl::GL_TRIANGLES, 0, vertices.size());
|
|
547
|
+ gl::glDrawElements(gl::GL_TRIANGLES, pcmd->ElemCount, gl::GL_UNSIGNED_SHORT, idx_buffer_offset);
|
|
548
|
+ }
|
|
549
|
+ idx_buffer_offset += pcmd->ElemCount;
|
543
|
550
|
}
|
544
|
551
|
}
|
545
|
552
|
|
546
|
|
- vert.unbind(0);
|
547
|
|
- uv.unbind(1);
|
548
|
|
- col.unbind(2);
|
|
553
|
+ gl::glDisableVertexAttribArray(attribPos);
|
|
554
|
+ gl::glDisableVertexAttribArray(attribUV);
|
|
555
|
+ gl::glDisableVertexAttribArray(attribCol);
|
549
|
556
|
|
550
|
557
|
Shader::set2DState(false);
|
551
|
558
|
gl::glDisable(gl::GL_SCISSOR_TEST);
|