From 3b255d4137d9587d7f7f21b9deeb45090abfc380 Mon Sep 17 00:00:00 2001 From: touky Date: Thu, 20 Oct 2016 01:23:57 -0400 Subject: [PATCH] first test for lolimgui --- build/Lol (vs2015).sln | 15 ++++++ doc/tutorial/15_lolimgui.cpp | 86 ++++++++++++++++++++++++++++++++ doc/tutorial/15_lolimgui.vcxproj | 67 +++++++++++++++++++++++++ doc/tutorial/imgui.ini | 10 ++++ src/3rdparty/imgui | 2 +- src/gpu/vertexbuffer.cpp | 5 ++ src/lol-core.vcxproj | 4 +- src/lol/gpu/vertexbuffer.h | 3 +- src/lolimgui.cpp | 83 +++++++++++++++++------------- src/lolimgui.h | 9 ++-- src/mesh/mesh.cpp | 2 +- 11 files changed, 243 insertions(+), 43 deletions(-) create mode 100644 doc/tutorial/15_lolimgui.cpp create mode 100644 doc/tutorial/15_lolimgui.vcxproj create mode 100644 doc/tutorial/imgui.ini diff --git a/build/Lol (vs2015).sln b/build/Lol (vs2015).sln index 05eab031..22c0ffe0 100644 --- a/build/Lol (vs2015).sln +++ b/build/Lol (vs2015).sln @@ -64,6 +64,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "lolremez", "..\tools\lolrem EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{B6297FF2-63D0-41EE-BE13-EFF720C9B0FA}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "15_lolimgui", "..\doc\tutorial\15_lolimgui.vcxproj", "{81C83B42-D00A-4FA3-9A3D-80F9D46524BF}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|ORBIS = Debug|ORBIS @@ -294,6 +296,18 @@ Global {73F1A804-1116-46C3-922A-9C0ADEB33F52}.Release|Win32.Build.0 = Release|Win32 {73F1A804-1116-46C3-922A-9C0ADEB33F52}.Release|x64.ActiveCfg = Release|x64 {73F1A804-1116-46C3-922A-9C0ADEB33F52}.Release|x64.Build.0 = Release|x64 + {81C83B42-D00A-4FA3-9A3D-80F9D46524BF}.Debug|ORBIS.ActiveCfg = Debug|ORBIS + {81C83B42-D00A-4FA3-9A3D-80F9D46524BF}.Debug|ORBIS.Build.0 = Debug|ORBIS + {81C83B42-D00A-4FA3-9A3D-80F9D46524BF}.Debug|Win32.ActiveCfg = Debug|Win32 + {81C83B42-D00A-4FA3-9A3D-80F9D46524BF}.Debug|Win32.Build.0 = Debug|Win32 + {81C83B42-D00A-4FA3-9A3D-80F9D46524BF}.Debug|x64.ActiveCfg = Debug|x64 + {81C83B42-D00A-4FA3-9A3D-80F9D46524BF}.Debug|x64.Build.0 = Debug|x64 + {81C83B42-D00A-4FA3-9A3D-80F9D46524BF}.Release|ORBIS.ActiveCfg = Release|ORBIS + {81C83B42-D00A-4FA3-9A3D-80F9D46524BF}.Release|ORBIS.Build.0 = Release|ORBIS + {81C83B42-D00A-4FA3-9A3D-80F9D46524BF}.Release|Win32.ActiveCfg = Release|Win32 + {81C83B42-D00A-4FA3-9A3D-80F9D46524BF}.Release|Win32.Build.0 = Release|Win32 + {81C83B42-D00A-4FA3-9A3D-80F9D46524BF}.Release|x64.ActiveCfg = Release|x64 + {81C83B42-D00A-4FA3-9A3D-80F9D46524BF}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -326,5 +340,6 @@ Global {4C4BD478-3767-4C27-BD91-DAAFE7CD03A2} = {3D341D8A-E400-4B1D-BC05-B5C35487D9B5} {73F1A804-1116-46C3-922A-9C0ADEB33F52} = {4C4BD478-3767-4C27-BD91-DAAFE7CD03A2} {B6297FF2-63D0-41EE-BE13-EFF720C9B0FA} = {1AFD580B-98B8-4689-B661-38C41132C60E} + {81C83B42-D00A-4FA3-9A3D-80F9D46524BF} = {E74CF679-CA2A-47E9-B1F4-3779D6AC6B04} EndGlobalSection EndGlobal diff --git a/doc/tutorial/15_lolimgui.cpp b/doc/tutorial/15_lolimgui.cpp new file mode 100644 index 00000000..1f6a36a6 --- /dev/null +++ b/doc/tutorial/15_lolimgui.cpp @@ -0,0 +1,86 @@ +// +// Lol Engine — Shader builder tutorial +// +// Copyright © 2012—2015 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#if HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include "loldebug.h" +#include + +using namespace lol; + +class LolImGuiDemo : public WorldEntity +{ +public: + LolImGuiDemo() + { + LolImGui::Init(); + } + + ~LolImGuiDemo() + { + LolImGui::Shutdown(); + } + + virtual void TickGame(float seconds) + { + WorldEntity::TickGame(seconds); + + static float f; + static vec3 clear_color; + static char buf[512]; + ImGuiIO& io = ImGui::GetIO(); + ImGui::SetNextWindowFocus(); + ImGui::Begin("testature"); + { + ImGui::Text("Hello, world!"); + ImGui::Text("prout!"); + ImGui::Text("prout!%i", 100); + ImGui::Text("MousePos!%.2f/%.2f", io.MousePos.x, io.MousePos.y); + ImGui::Button("Test Window"); + ImGui::Text("Slider: %.2f", f); + ImGui::SliderFloat("float", &f, 0.0f, 1.0f); + //ImGui::ColorEdit3("clear color", (float*)&clear_color); + ImGui::Text("Left Mouse: %s", io.MouseDown[0] ? "true" : "false"); + ImGui::Text("Scroll: %f", io.MouseWheel); + ImGui::Text("Maj: %s", io.KeyShift ? "true" : "false"); + ImGui::Text("Ctrl: %s", io.KeyCtrl ? "true" : "false"); + ImGui::Text("Clipboard %s", LolImGui::GetClipboard()); + ImGui::InputText("base input", buf, 512); + if (ImGui::IsItemActive()) + ImGui::Text("Focused: true"); + else + ImGui::Text("Focused: false"); + } + ImGui::End(); + } + + virtual void TickDraw(float seconds, Scene &scene) + { + WorldEntity::TickDraw(seconds, scene); + } +}; + +int main(int argc, char **argv) +{ + sys::init(argc, argv); + + Application app("Tutorial 15: LolImGui", ivec2(800, 600), 60.0f); + + new LolImGuiDemo(); + + app.Run(); + return EXIT_SUCCESS; +} + diff --git a/doc/tutorial/15_lolimgui.vcxproj b/doc/tutorial/15_lolimgui.vcxproj new file mode 100644 index 00000000..d27d0c1f --- /dev/null +++ b/doc/tutorial/15_lolimgui.vcxproj @@ -0,0 +1,67 @@ + + + + $(SolutionDir)\lol + $(SolutionDir)\.. + + + + Debug + ORBIS + + + Debug + Win32 + + + Debug + x64 + + + Release + ORBIS + + + Release + Win32 + + + Release + x64 + + + + + {9e62f2fe-3408-4eae-8238-fd84238ceeda} + + + {83d3b207-c601-4025-8f41-01dedc354661} + + + {d84021ca-b233-4e0f-8a52-071b83bbccc4} + + + + + + + {81C83B42-D00A-4FA3-9A3D-80F9D46524BF} + Application + Win32Proj + + + + + + + + + + + + + + + + + diff --git a/doc/tutorial/imgui.ini b/doc/tutorial/imgui.ini new file mode 100644 index 00000000..d24a4f98 --- /dev/null +++ b/doc/tutorial/imgui.ini @@ -0,0 +1,10 @@ +[Debug] +Pos=60,60 +Size=400,400 +Collapsed=0 + +[testature] +Pos=140,75 +Size=197,287 +Collapsed=0 + diff --git a/src/3rdparty/imgui b/src/3rdparty/imgui index 0123fc8c..cb7e1c18 160000 --- a/src/3rdparty/imgui +++ b/src/3rdparty/imgui @@ -1 +1 @@ -Subproject commit 0123fc8c0f3f9b9d55b31d7403dea32d568032fe +Subproject commit cb7e1c18b57092da146307557b3e9d1fead7430f diff --git a/src/gpu/vertexbuffer.cpp b/src/gpu/vertexbuffer.cpp index eadf577d..31629446 100644 --- a/src/gpu/vertexbuffer.cpp +++ b/src/gpu/vertexbuffer.cpp @@ -103,6 +103,11 @@ void VertexDeclaration::DrawElements(MeshPrimitive type, int skip, int count) } } +void VertexDeclaration::DrawIndexedElements_(MeshPrimitive type, int count, const short* skip) +{ + glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, skip); +} + void VertexDeclaration::DrawIndexedElements(MeshPrimitive type, int vbase, int vskip, int vcount, int skip, int count) diff --git a/src/lol-core.vcxproj b/src/lol-core.vcxproj index e3464923..954bdd1c 100644 --- a/src/lol-core.vcxproj +++ b/src/lol-core.vcxproj @@ -76,6 +76,8 @@ + + @@ -395,4 +397,4 @@ - + \ No newline at end of file diff --git a/src/lol/gpu/vertexbuffer.h b/src/lol/gpu/vertexbuffer.h index d697d09a..c00b56ef 100644 --- a/src/lol/gpu/vertexbuffer.h +++ b/src/lol/gpu/vertexbuffer.h @@ -212,7 +212,8 @@ public: /* Draw elements. See MeshPrimitive for a list of all available * types. Both skip and count are numbers of indices, not primitives. */ - void DrawIndexedElements(MeshPrimitive type, int vbase, int vskip, + void DrawIndexedElements_(MeshPrimitive type, int count, const short* skip); + void DrawIndexedElements(MeshPrimitive type, int vbase, int vskip, int vcount, int skip, int count); void Unbind(); diff --git a/src/lolimgui.cpp b/src/lolimgui.cpp index 2e07d3ce..021181b7 100644 --- a/src/lolimgui.cpp +++ b/src/lolimgui.cpp @@ -287,13 +287,13 @@ void PrimitiveLolImGui::Render(Scene& scene, PrimitiveSource* primitive) // If text or lines are blurry when integrating ImGui in your engine: // - in your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f) //------------------------------------------------------------------------- -void LolImGui::RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) +void LolImGui::RenderDrawLists(ImDrawData* draw_data) { - g_lolimgui->RenderDrawListsMethod(cmd_lists, cmd_lists_count); + g_lolimgui->RenderDrawListsMethod(draw_data); } -void LolImGui::RenderDrawListsMethod(ImDrawList** const cmd_lists, int cmd_lists_count) +void LolImGui::RenderDrawListsMethod(ImDrawData* draw_data) { - if (cmd_lists_count == 0) + if (draw_data == nullptr) return; vec2 size = vec2(Video::GetSize()); @@ -307,10 +307,12 @@ void LolImGui::RenderDrawListsMethod(ImDrawList** const cmd_lists, int cmd_lists if (!m_shader) { String code; - m_builder.Build(code); + m_builder.Build(code); - m_shader = Shader::Create(m_builder.GetName(), code); + msg::debug("\nCREATED SHADER:\n%s\n", code.C()); + m_shader = Shader::Create(m_builder.GetName(), code); ASSERT(m_shader); + msg::debug("\nPATCHED SHADER:\n%s\n", code.C()); m_ortho.m_uniform = m_shader->GetUniformLocation(m_ortho.m_var); m_texture.m_uniform = m_shader->GetUniformLocation(m_texture.m_var); @@ -336,28 +338,40 @@ void LolImGui::RenderDrawListsMethod(ImDrawList** const cmd_lists, int cmd_lists rc.SetDepthFunc(DepthFunc::Disabled); m_shader->Bind(); - for (int n = 0; n < cmd_lists_count; n++) + for (int n = 0; n < draw_data->CmdListsCount; n++) { - const ImDrawList* cmd_list = cmd_lists[n]; - const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->vtx_buffer.front(); + const ImDrawList* cmd_list = draw_data->CmdLists[n]; + const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->VtxBuffer.front(); //Register uniforms m_shader->SetUniform(m_ortho, ortho); m_shader->SetUniform(m_texture, m_font->GetTexture()->GetTextureUniform(), 0); - int vtx_offset = 0; - for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++) + struct Vertex + { + vec2 pos, tex; + u8vec4 color; + }; + + VertexBuffer* vbo = new VertexBuffer(cmd_list->VtxBuffer.Size * sizeof(ImDrawVert)); + ImDrawVert *vert = (ImDrawVert *)vbo->Lock(0, 0); + memcpy(vert, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert)); + vbo->Unlock(); + + IndexBuffer *ibo = new IndexBuffer(cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx)); + uint16_t *indices = (uint16_t *)ibo->Lock(0, 0); + memcpy(vert, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx)); + ibo->Unlock(); + + m_font->Bind(); + ibo->Bind(); + m_vdecl->Bind(); + m_vdecl->SetStream(vbo, m_attribs[0], m_attribs[1], m_attribs[2]); + + const ImDrawIdx* idx_buffer_offset = 0; + for (size_t cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { - const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i]; - - struct Vertex - { - vec2 pos, tex; - u8vec4 color; - }; - - VertexBuffer* vbo = new VertexBuffer(pcmd->vtx_count * sizeof(Vertex)); - Vertex *vert = (Vertex *)vbo->Lock(0, 0); + const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; #ifdef SHOW_IMGUI_DEBUG //----------------------------------------------------------------- @@ -374,7 +388,7 @@ void LolImGui::RenderDrawListsMethod(ImDrawList** const cmd_lists, int cmd_lists for (int i = 0; i < 4; ++i) Debug::DrawLine(pos[i], pos[(i + 1) % 4], Color::white); ImDrawVert* buf = (ImDrawVert*)(vtx_buffer + vtx_offset); - for (uint16_t i = 0; i < pcmd->vtx_count; i += 3) + for (uint16_t i = 0; i < pcmd->ElemCount; i += 3) { vec2 pos[3]; pos[0] = vec2(buf[i + 0].pos.x, buf[i + 0].pos.y); @@ -393,21 +407,20 @@ void LolImGui::RenderDrawListsMethod(ImDrawList** const cmd_lists, int cmd_lists //----------------------------------------------------------------- #endif //SHOW_IMGUI_DEBUG - memcpy(vert, vtx_buffer + vtx_offset, pcmd->vtx_count * sizeof(Vertex)); - vbo->Unlock(); + m_vdecl->DrawIndexedElements_(MeshPrimitive::Triangles, pcmd->ElemCount, (const short*)idx_buffer_offset); + //m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0, 0, 0, pcmd->ElemCount); + //m_vdecl->DrawElements(MeshPrimitive::Triangles, (int)idx_buffer_offset, pcmd->ElemCount); + idx_buffer_offset += pcmd->ElemCount; + } - m_font->Bind(); - m_vdecl->Bind(); - m_vdecl->SetStream(vbo, m_attribs[0], m_attribs[1], m_attribs[2]); - m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, pcmd->vtx_count); - m_vdecl->Unbind(); - m_font->Unbind(); + m_vdecl->Unbind(); + ibo->Unbind(); + m_font->Unbind(); - vtx_offset += pcmd->vtx_count * sizeof(Vertex); + delete vbo; + delete ibo; + } - delete vbo; - } - } - m_shader->Unbind(); + m_shader->Unbind(); } diff --git a/src/lolimgui.h b/src/lolimgui.h index e1415090..1fd3b0b6 100644 --- a/src/lolimgui.h +++ b/src/lolimgui.h @@ -144,8 +144,8 @@ protected: virtual void TickGame(float seconds); virtual void TickDraw(float seconds, Scene &scene); - static void RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count); - void RenderDrawListsMethod(ImDrawList** const cmd_lists, int cmd_lists_count); + static void RenderDrawLists(ImDrawData* draw_data); + void RenderDrawListsMethod(ImDrawData* draw_data); struct Uniform { @@ -165,8 +165,9 @@ protected: Uniform m_ortho; Uniform m_texture; array m_attribs; - VertexDeclaration* m_vdecl = nullptr; - Controller* m_controller = nullptr; + VertexDeclaration* m_vdecl = nullptr; + IndexBuffer* m_ibuff = nullptr; + Controller* m_controller = nullptr; InputDevice* m_mouse = nullptr; InputDevice* m_keyboard = nullptr; InputProfile m_profile; diff --git a/src/mesh/mesh.cpp b/src/mesh/mesh.cpp index f1f488fc..b32c8b26 100644 --- a/src/mesh/mesh.cpp +++ b/src/mesh/mesh.cpp @@ -143,7 +143,7 @@ void SubMesh::Render() ShaderUniform u_tex = m_shader->GetUniformLocation(m_textures[i].m1.C()); m_shader->SetUniform(u_tex, m_textures[i].m2->GetTextureUniform(), i); } - + m_ibo->Bind(); m_vdecl->Bind(); m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, 0, 0, vertex_count,