瀏覽代碼

first test for lolimgui

legacy
touky 8 年之前
父節點
當前提交
3b255d4137
共有 11 個檔案被更改,包括 243 行新增43 行删除
  1. +15
    -0
      build/Lol (vs2015).sln
  2. +86
    -0
      doc/tutorial/15_lolimgui.cpp
  3. +67
    -0
      doc/tutorial/15_lolimgui.vcxproj
  4. +10
    -0
      doc/tutorial/imgui.ini
  5. +1
    -1
      src/3rdparty/imgui
  6. +5
    -0
      src/gpu/vertexbuffer.cpp
  7. +3
    -1
      src/lol-core.vcxproj
  8. +2
    -1
      src/lol/gpu/vertexbuffer.h
  9. +48
    -35
      src/lolimgui.cpp
  10. +5
    -4
      src/lolimgui.h
  11. +1
    -1
      src/mesh/mesh.cpp

+ 15
- 0
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

+ 86
- 0
doc/tutorial/15_lolimgui.cpp 查看文件

@@ -0,0 +1,86 @@
//
// Lol Engine — Shader builder tutorial
//
// Copyright © 2012—2015 Sam Hocevar <sam@hocevar.net>
//
// 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 <lol/engine.h>
#include "loldebug.h"
#include <cstdio>

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;
}


+ 67
- 0
doc/tutorial/15_lolimgui.vcxproj 查看文件

@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="LolMacros">
<LolDir Condition="Exists('$(SolutionDir)\lol')">$(SolutionDir)\lol</LolDir>
<LolDir Condition="!Exists('$(SolutionDir)\lol')">$(SolutionDir)\..</LolDir>
</PropertyGroup>
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|ORBIS">
<Configuration>Debug</Configuration>
<Platform>ORBIS</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|ORBIS">
<Configuration>Release</Configuration>
<Platform>ORBIS</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(LolDir)\src\lol-core.vcxproj">
<Project>{9e62f2fe-3408-4eae-8238-fd84238ceeda}</Project>
</ProjectReference>
<ProjectReference Include="$(LolDir)\src\3rdparty\lol-bullet.vcxproj">
<Project>{83d3b207-c601-4025-8f41-01dedc354661}</Project>
</ProjectReference>
<ProjectReference Include="$(LolDir)\src\3rdparty\lol-lua.vcxproj">
<Project>{d84021ca-b233-4e0f-8a52-071b83bbccc4}</Project>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClCompile Include="15_lolimgui.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{81C83B42-D00A-4FA3-9A3D-80F9D46524BF}</ProjectGuid>
<ConfigurationType>Application</ConfigurationType>
<Keyword>Win32Proj</Keyword>
</PropertyGroup>
<Import Project="$(LolDir)\build\msbuild\lol.config.props" />
<ImportGroup Label="ExtensionSettings">
<Import Project="$(LolDir)\build\msbuild\lolfx.props" />
</ImportGroup>
<ImportGroup Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
<Import Project="$(LolDir)\build\msbuild\lol.vars.props" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<Import Project="$(LolDir)\build\msbuild\lol.rules.props" />
<ItemDefinitionGroup />
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
<Import Project="$(LolDir)\build\msbuild\lolfx.targets" />
</ImportGroup>
</Project>

+ 10
- 0
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


+ 1
- 1
src/3rdparty/imgui

@@ -1 +1 @@
Subproject commit 0123fc8c0f3f9b9d55b31d7403dea32d568032fe
Subproject commit cb7e1c18b57092da146307557b3e9d1fead7430f

+ 5
- 0
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)


+ 3
- 1
src/lol-core.vcxproj 查看文件

@@ -76,6 +76,8 @@
</ClCompile>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="3rdparty\imgui\imgui_demo.cpp" />
<ClCompile Include="3rdparty\imgui\imgui_draw.cpp" />
<ClCompile Include="application\application.cpp" />
<ClCompile Include="audio\audio.cpp" />
<ClCompile Include="audio\sample.cpp" />
@@ -395,4 +397,4 @@
<ImportGroup Label="ExtensionTargets">
<Import Project="$(LolDir)\build\msbuild\lolfx.targets" />
</ImportGroup>
</Project>
</Project>

+ 2
- 1
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();


+ 48
- 35
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();
}


+ 5
- 4
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<ShaderAttrib> 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;


+ 1
- 1
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,


Loading…
取消
儲存