mirror of
https://github.com/Drezil/imgui.git
synced 2025-07-13 16:29:54 +02:00
Compare commits
33 Commits
Author | SHA1 | Date | |
---|---|---|---|
cda3aecc6a | |||
b6d1d85d86 | |||
9a426faf4f | |||
b0d5600ffb | |||
c52a54ef43 | |||
cc9d63b46a | |||
d3ad5ce475 | |||
cb3d503941 | |||
ddc7f8b0b0 | |||
57ac561ecb | |||
2573ffb6fc | |||
c938affcac | |||
a1e176fb07 | |||
901e9890d4 | |||
f4ee74b312 | |||
64e5c2f127 | |||
e2655d104e | |||
8825545653 | |||
150ad95bd6 | |||
ab8561e6fc | |||
2a1ba33263 | |||
4fa623c43b | |||
080eb69e68 | |||
1b330f420e | |||
28df6f39d9 | |||
98a000055e | |||
4020ef7b58 | |||
86d2c9d232 | |||
8ab2942716 | |||
680a5a9b54 | |||
900dd3bd0f | |||
678f6d3a3d | |||
42419b59c0 |
14
.gitignore
vendored
Normal file
14
.gitignore
vendored
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
## Visual Studio files
|
||||||
|
examples/directx9_example/Debug/*
|
||||||
|
examples/directx9_example/Release/*
|
||||||
|
examples/directx9_example/ipch/*
|
||||||
|
examples/opengl_example/Debug/*
|
||||||
|
examples/opengl_example/Release/*
|
||||||
|
examples/opengl_example/ipch/*
|
||||||
|
*.opensdf
|
||||||
|
*.sdf
|
||||||
|
*.suo
|
||||||
|
*.vcxproj.user
|
||||||
|
|
||||||
|
## Ini files
|
||||||
|
imgui.ini
|
19
README.md
19
README.md
@ -1,17 +1,18 @@
|
|||||||
ImGui
|
ImGui
|
||||||
=====
|
=====
|
||||||
|
|
||||||
ImGui is a bloat-free graphical user interface library for C/C++. It is portable, renderer agnostic and carries minimal amount of dependencies (only 3 files are needed). It is based on an "immediate" graphical user interface paradigm which allows you to build simple user interfaces with ease.
|
ImGui is a bloat-free graphical user interface library for C++. It outputs vertex buffers that you can render in your 3D-pipeline enabled application. It is portable, renderer agnostic and carries minimal amount of dependencies (only 3 files are needed). It is based on an "immediate" graphical user interface paradigm which allows you to build simple user interfaces with ease.
|
||||||
|
|
||||||
ImGui is designed to allow programmers to create "content creation" or "debug" tools (as opposed to tools for the average end-user). It favors simplicity and thus lacks certain features normally found in more high-level libraries, such as string localisation.
|
ImGui is designed to enable fast iteration and allow programmers to create "content creation" or "debug" tools (as opposed to tools for the average end-user). It favors simplicity and thus lacks certain features normally found in more high-level libraries, such as string localisation.
|
||||||
|
|
||||||
Usage example:
|
ImGui is particularly suited to integration in 3D applications, fullscreen applications, embedded applications, games, or any applications on consoles platforms where operating system features are non-standard.
|
||||||
|
|
||||||
|
After ImGui is setup in your engine, you can use it like in this example:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
ImGui outputs vertex buffers and simple command-lists that you can render in your application. Because it doesn't know or touch graphics state directly, you can call ImGui commands anywhere in your code (e.g. in the middle of a running algorithm, or in the middle of your own rendering process). Refer to the sample applications in the examples/ folder for instructions on how to integrate ImGui with your existing codebase.
|
ImGui outputs vertex buffers and simple command-lists that you can render in your application. Because it doesn't know or touch graphics state directly, you can call ImGui commands anywhere in your code (e.g. in the middle of a running algorithm, or in the middle of your own rendering process). Refer to the sample applications in the examples/ folder for instructions on how to integrate ImGui with your existing codebase.
|
||||||
|
|
||||||
|
|
||||||
Gallery
|
Gallery
|
||||||
-------
|
-------
|
||||||
|
|
||||||
@ -20,6 +21,16 @@ Gallery
|
|||||||

|

|
||||||

|

|
||||||
|
|
||||||
|
References
|
||||||
|
----------
|
||||||
|
|
||||||
|
The Immediate Mode GUI paradigm may at first appear unusual to some users. This is mainly because "Retained Mode" GUIs have been so widespread and predominant. The following links can give you a better understanding about how Immediate Mode GUIs works.
|
||||||
|
- [Johannes 'johno' Norneby's article](http://www.johno.se/book/imgui.html).
|
||||||
|
- [A presentation by Rickard Gustafsson and Johannes Algelind](http://www.cse.chalmers.se/edu/year/2011/course/TDA361/Advanced%20Computer%20Graphics/IMGUI.pdf).
|
||||||
|
- [Jari Komppa's tutorial on building an ImGui library](http://iki.fi/sol/imgui/).
|
||||||
|
- [Casey Muratori's original video that popularized the concept](https://mollyrocket.com/861).
|
||||||
|
|
||||||
|
|
||||||
Credits
|
Credits
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
@ -12,16 +12,15 @@ static LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
|
|||||||
static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
|
static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
|
||||||
static LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices
|
static LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices
|
||||||
static LPDIRECT3DTEXTURE9 g_pTexture = NULL; // Our texture
|
static LPDIRECT3DTEXTURE9 g_pTexture = NULL; // Our texture
|
||||||
|
|
||||||
struct CUSTOMVERTEX
|
struct CUSTOMVERTEX
|
||||||
{
|
{
|
||||||
D3DXVECTOR3 position;
|
D3DXVECTOR3 position;
|
||||||
D3DCOLOR color;
|
D3DCOLOR color;
|
||||||
float tu, tv;
|
float tu, tv;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
|
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
|
||||||
|
|
||||||
|
// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structuer)
|
||||||
static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)
|
static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)
|
||||||
{
|
{
|
||||||
size_t total_vtx_count = 0;
|
size_t total_vtx_count = 0;
|
||||||
@ -30,31 +29,13 @@ static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_c
|
|||||||
if (total_vtx_count == 0)
|
if (total_vtx_count == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ImVector<ImVec4> clip_rect_stack;
|
// Copy and convert all vertices into a single contiguous buffer
|
||||||
clip_rect_stack.push_back(ImVec4(-9999,-9999,+9999,+9999));
|
|
||||||
|
|
||||||
// Setup orthographic projection
|
|
||||||
// Set up world matrix
|
|
||||||
D3DXMATRIXA16 mat;
|
|
||||||
D3DXMatrixIdentity(&mat);
|
|
||||||
g_pd3dDevice->SetTransform(D3DTS_WORLD, &mat);
|
|
||||||
g_pd3dDevice->SetTransform(D3DTS_VIEW, &mat);
|
|
||||||
D3DXMatrixOrthoOffCenterLH(&mat, 0.0f, ImGui::GetIO().DisplaySize.x, ImGui::GetIO().DisplaySize.y, 0.0f, -1.0f, +1.0f);
|
|
||||||
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &mat);
|
|
||||||
|
|
||||||
D3DSURFACE_DESC texture_desc;
|
|
||||||
g_pTexture->GetLevelDesc(0, &texture_desc);
|
|
||||||
|
|
||||||
// Fill the vertex buffer
|
|
||||||
CUSTOMVERTEX* vtx_dst;
|
CUSTOMVERTEX* vtx_dst;
|
||||||
if (g_pVB->Lock(0, total_vtx_count, (void**)&vtx_dst, D3DLOCK_DISCARD) < 0)
|
if (g_pVB->Lock(0, total_vtx_count, (void**)&vtx_dst, D3DLOCK_DISCARD) < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for (int n = 0; n < cmd_lists_count; n++)
|
for (int n = 0; n < cmd_lists_count; n++)
|
||||||
{
|
{
|
||||||
const ImDrawList* cmd_list = cmd_lists[n];
|
const ImDrawList* cmd_list = cmd_lists[n];
|
||||||
if (cmd_list->commands.empty() || cmd_list->vtx_buffer.empty())
|
|
||||||
continue;
|
|
||||||
const ImDrawVert* vtx_src = &cmd_list->vtx_buffer[0];
|
const ImDrawVert* vtx_src = &cmd_list->vtx_buffer[0];
|
||||||
for (size_t i = 0; i < cmd_list->vtx_buffer.size(); i++)
|
for (size_t i = 0; i < cmd_list->vtx_buffer.size(); i++)
|
||||||
{
|
{
|
||||||
@ -73,11 +54,10 @@ static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_c
|
|||||||
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
|
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
|
||||||
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
|
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
|
||||||
|
|
||||||
// Setup render state: alpha-blending enabled, no face culling, no depth testing
|
// Setup render state: alpha-blending, no face culling, no depth testing
|
||||||
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
|
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
|
||||||
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, false );
|
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, false );
|
||||||
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, false );
|
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, false );
|
||||||
|
|
||||||
g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, true );
|
g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, true );
|
||||||
g_pd3dDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD );
|
g_pd3dDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD );
|
||||||
g_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, false );
|
g_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, false );
|
||||||
@ -94,44 +74,27 @@ static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_c
|
|||||||
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
|
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
|
||||||
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
|
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
|
||||||
|
|
||||||
int vtx_consumed = 0; // offset in vertex buffer. each command consume ImDrawCmd::vtx_count of those
|
// Setup orthographic projection matrix
|
||||||
bool clip_rect_dirty = true;
|
D3DXMATRIXA16 mat;
|
||||||
|
D3DXMatrixIdentity(&mat);
|
||||||
|
g_pd3dDevice->SetTransform(D3DTS_WORLD, &mat);
|
||||||
|
g_pd3dDevice->SetTransform(D3DTS_VIEW, &mat);
|
||||||
|
D3DXMatrixOrthoOffCenterLH(&mat, 0.0f, ImGui::GetIO().DisplaySize.x, ImGui::GetIO().DisplaySize.y, 0.0f, -1.0f, +1.0f);
|
||||||
|
g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &mat);
|
||||||
|
|
||||||
|
// Render command lists
|
||||||
|
int vtx_offset = 0;
|
||||||
for (int n = 0; n < cmd_lists_count; n++)
|
for (int n = 0; n < cmd_lists_count; n++)
|
||||||
{
|
{
|
||||||
|
// Render command list
|
||||||
const ImDrawList* cmd_list = cmd_lists[n];
|
const ImDrawList* cmd_list = cmd_lists[n];
|
||||||
if (cmd_list->commands.empty() || cmd_list->vtx_buffer.empty())
|
const ImDrawCmd* pcmd_end = cmd_list->commands.end();
|
||||||
continue;
|
for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++)
|
||||||
const ImDrawCmd* pcmd = &cmd_list->commands.front();
|
|
||||||
const ImDrawCmd* pcmd_end = &cmd_list->commands.back();
|
|
||||||
int clip_rect_buf_consumed = 0; // offset in cmd_list->clip_rect_buffer. each PushClipRect command consume 1 of those.
|
|
||||||
while (pcmd <= pcmd_end)
|
|
||||||
{
|
{
|
||||||
const ImDrawCmd& cmd = *pcmd++;
|
const RECT r = { (LONG)pcmd->clip_rect.x, (LONG)pcmd->clip_rect.y, (LONG)pcmd->clip_rect.z, (LONG)pcmd->clip_rect.w };
|
||||||
switch (cmd.cmd_type)
|
g_pd3dDevice->SetScissorRect(&r);
|
||||||
{
|
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, vtx_offset, pcmd->vtx_count/3);
|
||||||
case ImDrawCmdType_DrawTriangleList:
|
vtx_offset += pcmd->vtx_count;
|
||||||
if (clip_rect_dirty)
|
|
||||||
{
|
|
||||||
const ImVec4& clip_rect = clip_rect_stack.back();
|
|
||||||
const RECT r = { (LONG)clip_rect.x, (LONG)clip_rect.y, (LONG)clip_rect.z, (LONG)clip_rect.w };
|
|
||||||
g_pd3dDevice->SetScissorRect(&r);
|
|
||||||
clip_rect_dirty = false;
|
|
||||||
}
|
|
||||||
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, vtx_consumed, cmd.vtx_count/3);
|
|
||||||
vtx_consumed += cmd.vtx_count;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ImDrawCmdType_PushClipRect:
|
|
||||||
clip_rect_stack.push_back(cmd_list->clip_rect_buffer[clip_rect_buf_consumed++]);
|
|
||||||
clip_rect_dirty = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ImDrawCmdType_PopClipRect:
|
|
||||||
clip_rect_stack.pop_back();
|
|
||||||
clip_rect_dirty = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -203,17 +166,6 @@ HRESULT InitD3D(HWND hWnd)
|
|||||||
if (g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice) < 0)
|
if (g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice) < 0)
|
||||||
return E_FAIL;
|
return E_FAIL;
|
||||||
|
|
||||||
// Create the vertex buffer.
|
|
||||||
if (g_pd3dDevice->CreateVertexBuffer(10000 * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL) < 0)
|
|
||||||
return E_FAIL;
|
|
||||||
|
|
||||||
// Load font texture
|
|
||||||
const void* png_data;
|
|
||||||
unsigned int png_size;
|
|
||||||
ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
|
|
||||||
if (D3DXCreateTextureFromFileInMemory(g_pd3dDevice, png_data, png_size, &g_pTexture) < 0)
|
|
||||||
return E_FAIL;
|
|
||||||
|
|
||||||
return S_OK;
|
return S_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,9 +199,11 @@ LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
|||||||
io.MouseDown[1] = false;
|
io.MouseDown[1] = false;
|
||||||
return true;
|
return true;
|
||||||
case WM_MOUSEWHEEL:
|
case WM_MOUSEWHEEL:
|
||||||
|
// Mouse wheel: -1,0,+1
|
||||||
io.MouseWheel = GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1 : -1;
|
io.MouseWheel = GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1 : -1;
|
||||||
return true;
|
return true;
|
||||||
case WM_MOUSEMOVE:
|
case WM_MOUSEMOVE:
|
||||||
|
// Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
|
||||||
io.MousePos.x = (signed short)(lParam);
|
io.MousePos.x = (signed short)(lParam);
|
||||||
io.MousePos.y = (signed short)(lParam >> 16);
|
io.MousePos.y = (signed short)(lParam >> 16);
|
||||||
return true;
|
return true;
|
||||||
@ -274,9 +228,10 @@ void InitImGui()
|
|||||||
GetClientRect(hWnd, &rect);
|
GetClientRect(hWnd, &rect);
|
||||||
|
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
|
io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top)); // Display size, in pixels. For clamping windows positions.
|
||||||
io.DeltaTime = 1.0f/60.0f;
|
io.DeltaTime = 1.0f/60.0f; // Time elapsed since last frame, in seconds (in this sample app we'll override this every frame because our timestep is variable)
|
||||||
io.KeyMap[ImGuiKey_Tab] = VK_TAB;
|
io.PixelCenterOffset = 0.0f; // Align Direct3D Texels
|
||||||
|
io.KeyMap[ImGuiKey_Tab] = VK_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array that we will update during the application lifetime.
|
||||||
io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
|
io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
|
||||||
io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
|
io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
|
||||||
io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
|
io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
|
||||||
@ -297,6 +252,23 @@ void InitImGui()
|
|||||||
io.RenderDrawListsFn = ImImpl_RenderDrawLists;
|
io.RenderDrawListsFn = ImImpl_RenderDrawLists;
|
||||||
io.SetClipboardTextFn = ImImpl_SetClipboardTextFn;
|
io.SetClipboardTextFn = ImImpl_SetClipboardTextFn;
|
||||||
io.GetClipboardTextFn = ImImpl_GetClipboardTextFn;
|
io.GetClipboardTextFn = ImImpl_GetClipboardTextFn;
|
||||||
|
|
||||||
|
// Create the vertex buffer
|
||||||
|
if (g_pd3dDevice->CreateVertexBuffer(10000 * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL) < 0)
|
||||||
|
{
|
||||||
|
IM_ASSERT(0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load font texture
|
||||||
|
const void* png_data;
|
||||||
|
unsigned int png_size;
|
||||||
|
ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
|
||||||
|
if (D3DXCreateTextureFromFileInMemory(g_pd3dDevice, png_data, png_size, &g_pTexture) < 0)
|
||||||
|
{
|
||||||
|
IM_ASSERT(0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int)
|
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int)
|
||||||
@ -386,13 +358,15 @@ int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, LPWSTR, int)
|
|||||||
ImGui::End();
|
ImGui::End();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3) Render
|
// 3) Rendering
|
||||||
|
// Clear frame buffer
|
||||||
g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false);
|
g_pd3dDevice->SetRenderState(D3DRS_ZENABLE, false);
|
||||||
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
|
g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
|
||||||
g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, false);
|
g_pd3dDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, false);
|
||||||
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(204, 153, 153), 1.0f, 0); // Clear the backbuffer and the zbuffer
|
g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(204, 153, 153), 1.0f, 0);
|
||||||
if (g_pd3dDevice->BeginScene() >= 0)
|
if (g_pd3dDevice->BeginScene() >= 0)
|
||||||
{
|
{
|
||||||
|
// Render ImGui
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
g_pd3dDevice->EndScene();
|
g_pd3dDevice->EndScene();
|
||||||
}
|
}
|
||||||
|
18
examples/opengl_example/Makefile.Linux
Normal file
18
examples/opengl_example/Makefile.Linux
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#
|
||||||
|
# Quick and dirty makefile to build on Linux
|
||||||
|
# tested on Ubuntu 14.04.1 32bit
|
||||||
|
#
|
||||||
|
|
||||||
|
SRC = main.cpp ../../imgui.cpp
|
||||||
|
|
||||||
|
OBJ = $(SRC:.cpp=.o)
|
||||||
|
|
||||||
|
CXXFLAGS = -I../../ `pkg-config --cflags glfw3`
|
||||||
|
|
||||||
|
LIBS = `pkg-config --static --libs glfw3` -lGLEW
|
||||||
|
|
||||||
|
all: $(OBJ)
|
||||||
|
$(CXX) $(OBJ) $(LIBS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) -f $(OBJ)
|
17
examples/opengl_example/Makefile.Macosx
Normal file
17
examples/opengl_example/Makefile.Macosx
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# This makefile currently only works for mac os
|
||||||
|
# You should install via homebrew:
|
||||||
|
# brew install glew
|
||||||
|
# brew install glfw3
|
||||||
|
#
|
||||||
|
|
||||||
|
CXXFLAGS=-framework OpenGL -framework Cocoa -framework IOKit
|
||||||
|
#CXXFLAGS+=-L/usr/local/Cellar/glew/1.10.0/lib -L/usr/local/Cellar/glfw3/3.0.4/lib
|
||||||
|
CXXFLAGS+=-lglew -lglfw3
|
||||||
|
CXXFLAGS+=-I../../
|
||||||
|
CXXFLAGS+= -D__APPLE__
|
||||||
|
|
||||||
|
main: main.cpp ../../imgui.cpp
|
||||||
|
$(CXX) $(CXXFLAGS) -o $@ $^
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm main
|
@ -2,116 +2,65 @@
|
|||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#include "stb_image.h" // for .png loading
|
#include "stb_image.h" // for .png loading
|
||||||
#include "../../imgui.h"
|
#include "../../imgui.h"
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen
|
#pragma warning (disable: 4996) // 'This function or variable may be unsafe': strcpy, strdup, sprintf, vsnprintf, sscanf, fopen
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static GLFWwindow* window;
|
static GLFWwindow* window;
|
||||||
static GLuint vbo;
|
|
||||||
static GLuint vao;
|
|
||||||
static GLuint vertexShader;
|
|
||||||
static GLuint fragmentShader;
|
|
||||||
static GLuint shaderProgram;
|
|
||||||
static GLuint fontTex;
|
static GLuint fontTex;
|
||||||
static GLint uniMVP;
|
|
||||||
static GLint uniClipRect;
|
|
||||||
|
|
||||||
|
// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structuer)
|
||||||
|
// We are using the fixed pipeline.
|
||||||
|
// A faster way would be to collate all vertices from all cmd_lists into a single vertex buffer
|
||||||
static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)
|
static void ImImpl_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count)
|
||||||
{
|
{
|
||||||
size_t total_vtx_count = 0;
|
if (cmd_lists_count == 0)
|
||||||
for (int n = 0; n < cmd_lists_count; n++)
|
|
||||||
total_vtx_count += cmd_lists[n]->vtx_buffer.size();
|
|
||||||
if (total_vtx_count == 0)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int read_pos_clip_rect_buf = 0; // offset in 'clip_rect_buffer'. each PushClipRect command consume 1 of those.
|
|
||||||
|
|
||||||
ImVector<ImVec4> clip_rect_stack;
|
|
||||||
clip_rect_stack.push_back(ImVec4(-9999,-9999,+9999,+9999));
|
|
||||||
|
|
||||||
// Setup orthographic projection
|
|
||||||
const float L = 0.0f;
|
|
||||||
const float R = ImGui::GetIO().DisplaySize.x;
|
|
||||||
const float B = ImGui::GetIO().DisplaySize.y;
|
|
||||||
const float T = 0.0f;
|
|
||||||
const float mvp[4][4] =
|
|
||||||
{
|
|
||||||
{ 2.0f/(R-L), 0.0f, 0.0f, 0.0f },
|
|
||||||
{ 0.0f, 2.0f/(T-B), 0.0f, 0.0f },
|
|
||||||
{ 0.0f, 0.0f, -1.0f, 0.0f },
|
|
||||||
{ -(R+L)/(R-L), -(T+B)/(T-B), 0.0f, 1.0f },
|
|
||||||
};
|
|
||||||
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
||||||
glBindVertexArray(vao);
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, total_vtx_count * sizeof(ImDrawVert), NULL, GL_STREAM_DRAW);
|
|
||||||
unsigned char* buffer_data = (unsigned char*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
|
||||||
if (!buffer_data)
|
|
||||||
return;
|
|
||||||
int vtx_consumed = 0;
|
|
||||||
for (int n = 0; n < cmd_lists_count; n++)
|
|
||||||
{
|
|
||||||
const ImDrawList* cmd_list = cmd_lists[n];
|
|
||||||
if (!cmd_list->vtx_buffer.empty())
|
|
||||||
{
|
|
||||||
memcpy(buffer_data, &cmd_list->vtx_buffer[0], cmd_list->vtx_buffer.size() * sizeof(ImDrawVert));
|
|
||||||
buffer_data += cmd_list->vtx_buffer.size() * sizeof(ImDrawVert);
|
|
||||||
vtx_consumed += cmd_list->vtx_buffer.size();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
glUnmapBuffer(GL_ARRAY_BUFFER);
|
|
||||||
|
|
||||||
glUseProgram(shaderProgram);
|
|
||||||
glUniformMatrix4fv(uniMVP, 1, GL_FALSE, &mvp[0][0]);
|
|
||||||
|
|
||||||
// Setup render state: alpha-blending enabled, no face culling, no depth testing
|
// Setup render state: alpha-blending enabled, no face culling, no depth testing
|
||||||
glEnable(GL_BLEND);
|
glEnable(GL_BLEND);
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
glDisable(GL_CULL_FACE);
|
glDisable(GL_CULL_FACE);
|
||||||
glDisable(GL_DEPTH_TEST);
|
glDisable(GL_DEPTH_TEST);
|
||||||
|
glEnable(GL_SCISSOR_TEST);
|
||||||
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
|
glEnableClientState(GL_COLOR_ARRAY);
|
||||||
|
|
||||||
|
// Bind texture
|
||||||
glBindTexture(GL_TEXTURE_2D, fontTex);
|
glBindTexture(GL_TEXTURE_2D, fontTex);
|
||||||
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
|
||||||
vtx_consumed = 0; // offset in vertex buffer. each command consume ImDrawCmd::vtx_count of those
|
// Setup matrices
|
||||||
bool clip_rect_dirty = true;
|
const float width = ImGui::GetIO().DisplaySize.x;
|
||||||
|
const float height = ImGui::GetIO().DisplaySize.y;
|
||||||
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
glLoadIdentity();
|
||||||
|
glOrtho(0.0f, width, height, 0.0f, -1.0f, +1.0f);
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
glLoadIdentity();
|
||||||
|
|
||||||
|
// Render command lists
|
||||||
for (int n = 0; n < cmd_lists_count; n++)
|
for (int n = 0; n < cmd_lists_count; n++)
|
||||||
{
|
{
|
||||||
const ImDrawList* cmd_list = cmd_lists[n];
|
const ImDrawList* cmd_list = cmd_lists[n];
|
||||||
if (cmd_list->commands.empty() || cmd_list->vtx_buffer.empty())
|
const unsigned char* vtx_buffer = (const unsigned char*)cmd_list->vtx_buffer.begin();
|
||||||
continue;
|
glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer));
|
||||||
const ImDrawCmd* pcmd = &cmd_list->commands.front();
|
glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer+8));
|
||||||
const ImDrawCmd* pcmd_end = &cmd_list->commands.back();
|
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer+16));
|
||||||
int clip_rect_buf_consumed = 0; // offset in cmd_list->clip_rect_buffer. each PushClipRect command consume 1 of those.
|
|
||||||
while (pcmd <= pcmd_end)
|
int vtx_offset = 0;
|
||||||
|
const ImDrawCmd* pcmd_end = cmd_list->commands.end();
|
||||||
|
for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++)
|
||||||
{
|
{
|
||||||
const ImDrawCmd& cmd = *pcmd++;
|
glScissor((int)pcmd->clip_rect.x, (int)(height - pcmd->clip_rect.w), (int)(pcmd->clip_rect.z - pcmd->clip_rect.x), (int)(pcmd->clip_rect.w - pcmd->clip_rect.y));
|
||||||
switch (cmd.cmd_type)
|
glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count);
|
||||||
{
|
vtx_offset += pcmd->vtx_count;
|
||||||
case ImDrawCmdType_DrawTriangleList:
|
|
||||||
if (clip_rect_dirty)
|
|
||||||
{
|
|
||||||
glUniform4fv(uniClipRect, 1, (float*)&clip_rect_stack.back());
|
|
||||||
clip_rect_dirty = false;
|
|
||||||
}
|
|
||||||
glDrawArrays(GL_TRIANGLES, vtx_consumed, cmd.vtx_count);
|
|
||||||
vtx_consumed += cmd.vtx_count;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ImDrawCmdType_PushClipRect:
|
|
||||||
clip_rect_stack.push_back(cmd_list->clip_rect_buffer[clip_rect_buf_consumed++]);
|
|
||||||
clip_rect_dirty = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case ImDrawCmdType_PopClipRect:
|
|
||||||
clip_rect_stack.pop_back();
|
|
||||||
clip_rect_dirty = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
glDisable(GL_SCISSOR_TEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char* ImImpl_GetClipboardTextFn()
|
static const char* ImImpl_GetClipboardTextFn()
|
||||||
@ -124,6 +73,7 @@ static void ImImpl_SetClipboardTextFn(const char* text, const char* text_end)
|
|||||||
if (!text_end)
|
if (!text_end)
|
||||||
text_end = text + strlen(text);
|
text_end = text + strlen(text);
|
||||||
|
|
||||||
|
// Add a zero-terminator because glfw function doesn't take a size
|
||||||
char* buf = (char*)malloc(text_end - text + 1);
|
char* buf = (char*)malloc(text_end - text + 1);
|
||||||
memcpy(buf, text, text_end-text);
|
memcpy(buf, text, text_end-text);
|
||||||
buf[text_end-text] = '\0';
|
buf[text_end-text] = '\0';
|
||||||
@ -131,42 +81,10 @@ static void ImImpl_SetClipboardTextFn(const char* text, const char* text_end)
|
|||||||
free(buf);
|
free(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shader sources
|
// GLFW callbacks to get events
|
||||||
// FIXME-OPT: clip at vertex level
|
|
||||||
const GLchar* vertexSource =
|
|
||||||
"#version 150 core\n"
|
|
||||||
"uniform mat4 MVP;"
|
|
||||||
"in vec2 i_pos;"
|
|
||||||
"in vec2 i_uv;"
|
|
||||||
"in vec4 i_col;"
|
|
||||||
"out vec4 col;"
|
|
||||||
"out vec2 pixel_pos;"
|
|
||||||
"out vec2 uv;"
|
|
||||||
"void main() {"
|
|
||||||
" col = i_col;"
|
|
||||||
" pixel_pos = i_pos;"
|
|
||||||
" uv = i_uv;"
|
|
||||||
" gl_Position = MVP * vec4(i_pos.x, i_pos.y, 0.0f, 1.0f);"
|
|
||||||
"}";
|
|
||||||
|
|
||||||
const GLchar* fragmentSource =
|
|
||||||
"#version 150 core\n"
|
|
||||||
"uniform sampler2D Tex;"
|
|
||||||
"uniform vec4 ClipRect;"
|
|
||||||
"in vec4 col;"
|
|
||||||
"in vec2 pixel_pos;"
|
|
||||||
"in vec2 uv;"
|
|
||||||
"out vec4 o_col;"
|
|
||||||
"void main() {"
|
|
||||||
" o_col = texture(Tex, uv) * col;"
|
|
||||||
//" if (pixel_pos.x < ClipRect.x || pixel_pos.y < ClipRect.y || pixel_pos.x > ClipRect.z || pixel_pos.y > ClipRect.w) discard;" // Clipping: using discard
|
|
||||||
//" if (step(ClipRect.x,pixel_pos.x) * step(ClipRect.y,pixel_pos.y) * step(pixel_pos.x,ClipRect.z) * step(pixel_pos.y,ClipRect.w) < 1.0f) discard;" // Clipping: using discard and step
|
|
||||||
" o_col.w *= (step(ClipRect.x,pixel_pos.x) * step(ClipRect.y,pixel_pos.y) * step(pixel_pos.x,ClipRect.z) * step(pixel_pos.y,ClipRect.w));" // Clipping: branch-less, set alpha 0.0f
|
|
||||||
"}";
|
|
||||||
|
|
||||||
static void glfw_error_callback(int error, const char* description)
|
static void glfw_error_callback(int error, const char* description)
|
||||||
{
|
{
|
||||||
fputs(description, stderr);
|
fputs(description, stderr);
|
||||||
}
|
}
|
||||||
|
|
||||||
static float mouse_wheel = 0.0f;
|
static float mouse_wheel = 0.0f;
|
||||||
@ -197,79 +115,17 @@ void InitGL()
|
|||||||
{
|
{
|
||||||
glfwSetErrorCallback(glfw_error_callback);
|
glfwSetErrorCallback(glfw_error_callback);
|
||||||
|
|
||||||
if (!glfwInit())
|
if (!glfwInit())
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|
||||||
//glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
||||||
//glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
|
|
||||||
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
||||||
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
||||||
glfwWindowHint(GLFW_REFRESH_RATE, 60);
|
|
||||||
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
|
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
|
||||||
window = glfwCreateWindow(1280, 720, "ImGui OpenGL example", nullptr, nullptr);
|
window = glfwCreateWindow(1280, 720, "ImGui OpenGL example", NULL, NULL);
|
||||||
glfwMakeContextCurrent(window);
|
glfwMakeContextCurrent(window);
|
||||||
|
|
||||||
glfwSetKeyCallback(window, glfw_key_callback);
|
glfwSetKeyCallback(window, glfw_key_callback);
|
||||||
glfwSetScrollCallback(window, glfw_scroll_callback);
|
glfwSetScrollCallback(window, glfw_scroll_callback);
|
||||||
glfwSetCharCallback(window, glfw_char_callback);
|
glfwSetCharCallback(window, glfw_char_callback);
|
||||||
|
|
||||||
glewExperimental = GL_TRUE;
|
|
||||||
glewInit();
|
glewInit();
|
||||||
|
|
||||||
GLenum err = GL_NO_ERROR;
|
|
||||||
GLint status = GL_TRUE;
|
|
||||||
err = glGetError(); IM_ASSERT(err == GL_NO_ERROR);
|
|
||||||
|
|
||||||
// Create and compile the vertex shader
|
|
||||||
vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
|
||||||
glShaderSource(vertexShader, 1, &vertexSource, NULL);
|
|
||||||
glCompileShader(vertexShader);
|
|
||||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
|
|
||||||
if (status != GL_TRUE)
|
|
||||||
{
|
|
||||||
char buffer[512];
|
|
||||||
glGetShaderInfoLog(vertexShader, 1024, NULL, buffer);
|
|
||||||
printf("%s", buffer);
|
|
||||||
IM_ASSERT(status == GL_TRUE);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create and compile the fragment shader
|
|
||||||
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
|
||||||
glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
|
|
||||||
glCompileShader(fragmentShader);
|
|
||||||
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
|
|
||||||
IM_ASSERT(status == GL_TRUE);
|
|
||||||
|
|
||||||
// Link the vertex and fragment shader into a shader program
|
|
||||||
shaderProgram = glCreateProgram();
|
|
||||||
glAttachShader(shaderProgram, vertexShader);
|
|
||||||
glAttachShader(shaderProgram, fragmentShader);
|
|
||||||
glBindFragDataLocation(shaderProgram, 0, "o_col");
|
|
||||||
glLinkProgram(shaderProgram);
|
|
||||||
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &status);
|
|
||||||
IM_ASSERT(status == GL_TRUE);
|
|
||||||
|
|
||||||
uniMVP = glGetUniformLocation(shaderProgram, "MVP");
|
|
||||||
uniClipRect = glGetUniformLocation(shaderProgram, "ClipRect");
|
|
||||||
|
|
||||||
// Create Vertex Buffer Objects & Vertex Array Objects
|
|
||||||
glGenBuffers(1, &vbo);
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
||||||
glGenVertexArrays(1, &vao);
|
|
||||||
glBindVertexArray(vao);
|
|
||||||
|
|
||||||
GLint posAttrib = glGetAttribLocation(shaderProgram, "i_pos");
|
|
||||||
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), 0);
|
|
||||||
glEnableVertexAttribArray(posAttrib);
|
|
||||||
|
|
||||||
GLint uvAttrib = glGetAttribLocation(shaderProgram, "i_uv");
|
|
||||||
glEnableVertexAttribArray(uvAttrib);
|
|
||||||
glVertexAttribPointer(uvAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (void*)(2*sizeof(float)));
|
|
||||||
|
|
||||||
GLint colAttrib = glGetAttribLocation(shaderProgram, "i_col");
|
|
||||||
glVertexAttribPointer(colAttrib, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (void*)(4*sizeof(float)));
|
|
||||||
glEnableVertexAttribArray(colAttrib);
|
|
||||||
err = glGetError(); IM_ASSERT(err == GL_NO_ERROR);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitImGui()
|
void InitImGui()
|
||||||
@ -278,9 +134,10 @@ void InitImGui()
|
|||||||
glfwGetWindowSize(window, &w, &h);
|
glfwGetWindowSize(window, &w, &h);
|
||||||
|
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
io.DisplaySize = ImVec2((float)w, (float)h);
|
io.DisplaySize = ImVec2((float)w, (float)h); // Display size, in pixels. For clamping windows positions.
|
||||||
io.DeltaTime = 1.0f/60.0f;
|
io.DeltaTime = 1.0f/60.0f; // Time elapsed since last frame, in seconds (in this sample app we'll override this every frame because our timestep is variable)
|
||||||
io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB;
|
io.PixelCenterOffset = 0.5f; // Align OpenGL texels
|
||||||
|
io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
|
||||||
io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
|
io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
|
||||||
io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
|
io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
|
||||||
io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
|
io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
|
||||||
@ -307,7 +164,6 @@ void InitImGui()
|
|||||||
glBindTexture(GL_TEXTURE_2D, fontTex);
|
glBindTexture(GL_TEXTURE_2D, fontTex);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
|
||||||
const void* png_data;
|
const void* png_data;
|
||||||
unsigned int png_size;
|
unsigned int png_size;
|
||||||
ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
|
ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
|
||||||
@ -320,13 +176,6 @@ void InitImGui()
|
|||||||
void Shutdown()
|
void Shutdown()
|
||||||
{
|
{
|
||||||
ImGui::Shutdown();
|
ImGui::Shutdown();
|
||||||
|
|
||||||
glDeleteProgram(shaderProgram);
|
|
||||||
glDeleteShader(fragmentShader);
|
|
||||||
glDeleteShader(vertexShader);
|
|
||||||
glDeleteBuffers(1, &vbo);
|
|
||||||
glDeleteVertexArrays(1, &vao);
|
|
||||||
|
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -347,10 +196,10 @@ int main(int argc, char** argv)
|
|||||||
time = current_time;
|
time = current_time;
|
||||||
double mouse_x, mouse_y;
|
double mouse_x, mouse_y;
|
||||||
glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
glfwGetCursorPos(window, &mouse_x, &mouse_y);
|
||||||
io.MousePos = ImVec2((float)mouse_x, (float)mouse_y);
|
io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
|
||||||
io.MouseDown[0] = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) != 0;
|
io.MouseDown[0] = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) != 0;
|
||||||
io.MouseDown[1] = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) != 0;
|
io.MouseDown[1] = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_RIGHT) != 0;
|
||||||
io.MouseWheel = (mouse_wheel != 0) ? mouse_wheel > 0.0f ? 1 : - 1 : 0;
|
io.MouseWheel = (mouse_wheel != 0) ? mouse_wheel > 0.0f ? 1 : - 1 : 0; // Mouse wheel: -1,0,+1
|
||||||
mouse_wheel = 0.0f;
|
mouse_wheel = 0.0f;
|
||||||
ImGui::NewFrame();
|
ImGui::NewFrame();
|
||||||
|
|
||||||
@ -377,7 +226,7 @@ int main(int argc, char** argv)
|
|||||||
if (show_test_window)
|
if (show_test_window)
|
||||||
{
|
{
|
||||||
// More example code in ShowTestWindow()
|
// More example code in ShowTestWindow()
|
||||||
ImGui::SetNewWindowDefaultPos(ImVec2(650, 20)); // Normally user code doesn't need/want to call it because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly!
|
ImGui::SetNewWindowDefaultPos(ImVec2(650, 20)); // Normally user code doesn't need/want to call it because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly!
|
||||||
ImGui::ShowTestWindow(&show_test_window);
|
ImGui::ShowTestWindow(&show_test_window);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -388,15 +237,15 @@ int main(int argc, char** argv)
|
|||||||
ImGui::End();
|
ImGui::End();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3) Render
|
// 3) Rendering
|
||||||
glClearColor(0.8f, 0.6f, 0.6f, 1.0f);
|
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClearColor(0.8f, 0.6f, 0.6f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
Shutdown();
|
Shutdown();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
/*
|
/*
|
||||||
namespace ImGui
|
namespace ImGui
|
||||||
{
|
{
|
||||||
void Value(const char* prefix, cosnt MyVec2& v, const char* float_format = NULL);
|
void Value(const char* prefix, const MyVec2& v, const char* float_format = NULL);
|
||||||
void Value(const char* prefix, cosnt MyVec4& v, const char* float_format = NULL);
|
void Value(const char* prefix, const MyVec4& v, const char* float_format = NULL);
|
||||||
};
|
};
|
||||||
*/
|
*/
|
||||||
|
52
imgui.h
52
imgui.h
@ -29,8 +29,8 @@ typedef ImU32 ImGuiID;
|
|||||||
typedef int ImGuiCol; // enum ImGuiCol_
|
typedef int ImGuiCol; // enum ImGuiCol_
|
||||||
typedef int ImGuiKey; // enum ImGuiKey_
|
typedef int ImGuiKey; // enum ImGuiKey_
|
||||||
typedef int ImGuiColorEditMode; // enum ImGuiColorEditMode_
|
typedef int ImGuiColorEditMode; // enum ImGuiColorEditMode_
|
||||||
typedef ImU32 ImGuiWindowFlags; // enum ImGuiWindowFlags_
|
typedef int ImGuiWindowFlags; // enum ImGuiWindowFlags_
|
||||||
typedef ImU32 ImGuiInputTextFlags; // enum ImGuiInputTextFlags_
|
typedef int ImGuiInputTextFlags; // enum ImGuiInputTextFlags_
|
||||||
typedef ImBitmapFont* ImFont;
|
typedef ImBitmapFont* ImFont;
|
||||||
|
|
||||||
struct ImVec2
|
struct ImVec2
|
||||||
@ -85,9 +85,9 @@ public:
|
|||||||
|
|
||||||
inline void clear() { if (_data) { _size = _capacity = 0; free(_data); _data = NULL; } }
|
inline void clear() { if (_data) { _size = _capacity = 0; free(_data); _data = NULL; } }
|
||||||
inline iterator begin() { return _data; }
|
inline iterator begin() { return _data; }
|
||||||
inline const iterator begin() const { return _data; }
|
inline const_iterator begin() const { return _data; }
|
||||||
inline iterator end() { return _data + _size; }
|
inline iterator end() { return _data + _size; }
|
||||||
inline const iterator end() const { return _data + _size; }
|
inline const_iterator end() const { return _data + _size; }
|
||||||
inline value_type& front() { return at(0); }
|
inline value_type& front() { return at(0); }
|
||||||
inline const value_type& front() const { return at(0); }
|
inline const value_type& front() const { return at(0); }
|
||||||
inline value_type& back() { IM_ASSERT(_size > 0); return at(_size-1); }
|
inline value_type& back() { IM_ASSERT(_size > 0); return at(_size-1); }
|
||||||
@ -143,8 +143,10 @@ namespace ImGui
|
|||||||
void SetFontScale(float scale);
|
void SetFontScale(float scale);
|
||||||
void SetScrollPosHere();
|
void SetScrollPosHere();
|
||||||
void SetTreeStateStorage(ImGuiStorage* tree);
|
void SetTreeStateStorage(ImGuiStorage* tree);
|
||||||
|
ImGuiStorage* GetTreeStateStorage();
|
||||||
void PushItemWidth(float item_width);
|
void PushItemWidth(float item_width);
|
||||||
void PopItemWidth();
|
void PopItemWidth();
|
||||||
|
float GetItemWidth();
|
||||||
void PushAllowKeyboardFocus(bool v);
|
void PushAllowKeyboardFocus(bool v);
|
||||||
void PopAllowKeyboardFocus();
|
void PopAllowKeyboardFocus();
|
||||||
void PushStyleColor(ImGuiCol idx, ImVec4 col);
|
void PushStyleColor(ImGuiCol idx, ImVec4 col);
|
||||||
@ -181,6 +183,7 @@ namespace ImGui
|
|||||||
bool SmallButton(const char* label);
|
bool SmallButton(const char* label);
|
||||||
bool CollapsingHeader(const char* label, const char* str_id = NULL, const bool display_frame = true, const bool default_open = false);
|
bool CollapsingHeader(const char* label, const char* str_id = NULL, const bool display_frame = true, const bool default_open = false);
|
||||||
bool SliderFloat(const char* label, float* v, float v_min, float v_max, const char* display_format = "%.3f", float power = 1.0f);
|
bool SliderFloat(const char* label, float* v, float v_min, float v_max, const char* display_format = "%.3f", float power = 1.0f);
|
||||||
|
bool SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* display_format = "%.3f", float power = 1.0f);
|
||||||
bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* display_format = "%.3f", float power = 1.0f);
|
bool SliderFloat3(const char* label, float v[3], float v_min, float v_max, const char* display_format = "%.3f", float power = 1.0f);
|
||||||
bool SliderAngle(const char* label, float* v, float v_degrees_min = -360.0f, float v_degrees_max = +360.0f); // *v in radians
|
bool SliderAngle(const char* label, float* v, float v_degrees_min = -360.0f, float v_degrees_max = +360.0f); // *v in radians
|
||||||
bool SliderInt(const char* label, int* v, int v_min, int v_max, const char* display_format = "%.0f");
|
bool SliderInt(const char* label, int* v, int v_min, int v_max, const char* display_format = "%.0f");
|
||||||
@ -191,6 +194,7 @@ namespace ImGui
|
|||||||
bool RadioButton(const char* label, bool active);
|
bool RadioButton(const char* label, bool active);
|
||||||
bool RadioButton(const char* label, int* v, int v_button);
|
bool RadioButton(const char* label, int* v, int v_button);
|
||||||
bool InputFloat(const char* label, float* v, float step = 0.0f, float step_fast = 0.0f, int decimal_precision = -1);
|
bool InputFloat(const char* label, float* v, float step = 0.0f, float step_fast = 0.0f, int decimal_precision = -1);
|
||||||
|
bool InputFloat2(const char* label, float v[2], int decimal_precision = -1);
|
||||||
bool InputFloat3(const char* label, float v[3], int decimal_precision = -1);
|
bool InputFloat3(const char* label, float v[3], int decimal_precision = -1);
|
||||||
bool InputInt(const char* label, int* v, int step = 1, int step_fast = 100);
|
bool InputInt(const char* label, int* v, int step = 1, int step_fast = 100);
|
||||||
bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0);
|
bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0);
|
||||||
@ -220,9 +224,9 @@ namespace ImGui
|
|||||||
|
|
||||||
// Logging
|
// Logging
|
||||||
void LogButtons();
|
void LogButtons();
|
||||||
void LogToTTY(int max_depth);
|
void LogToTTY(int max_depth = -1);
|
||||||
void LogToFile(int max_depth, const char* filename);
|
void LogToFile(int max_depth = -1, const char* filename = NULL);
|
||||||
void LogToClipboard(int max_depth);
|
void LogToClipboard(int max_depth = -1);
|
||||||
|
|
||||||
// Utilities
|
// Utilities
|
||||||
void SetTooltip(const char* fmt, ...); // set tooltip under mouse-cursor, typically use with ImGui::IsHovered(). (currently no contention handling, last call win)
|
void SetTooltip(const char* fmt, ...); // set tooltip under mouse-cursor, typically use with ImGui::IsHovered(). (currently no contention handling, last call win)
|
||||||
@ -231,6 +235,8 @@ namespace ImGui
|
|||||||
bool IsClipped(ImVec2 item_size); // to perform coarse clipping on user's side (as an optimisation)
|
bool IsClipped(ImVec2 item_size); // to perform coarse clipping on user's side (as an optimisation)
|
||||||
bool IsKeyPressed(int key_index, bool repeat = true); // key_index into the keys_down[512] array, imgui doesn't know the semantic of each entry
|
bool IsKeyPressed(int key_index, bool repeat = true); // key_index into the keys_down[512] array, imgui doesn't know the semantic of each entry
|
||||||
bool IsMouseClicked(int button, bool repeat = false);
|
bool IsMouseClicked(int button, bool repeat = false);
|
||||||
|
bool IsMouseDoubleClicked(int button);
|
||||||
|
bool IsMouseHoveringBox(const ImVec2& box_min, const ImVec2& box_max);
|
||||||
ImVec2 GetMousePos();
|
ImVec2 GetMousePos();
|
||||||
float GetTime();
|
float GetTime();
|
||||||
int GetFrameCount();
|
int GetFrameCount();
|
||||||
@ -302,6 +308,7 @@ enum ImGuiCol_
|
|||||||
ImGuiCol_ScrollbarGrabHovered,
|
ImGuiCol_ScrollbarGrabHovered,
|
||||||
ImGuiCol_ScrollbarGrabActive,
|
ImGuiCol_ScrollbarGrabActive,
|
||||||
ImGuiCol_ComboBg,
|
ImGuiCol_ComboBg,
|
||||||
|
ImGuiCol_CheckHovered,
|
||||||
ImGuiCol_CheckActive,
|
ImGuiCol_CheckActive,
|
||||||
ImGuiCol_SliderGrab,
|
ImGuiCol_SliderGrab,
|
||||||
ImGuiCol_SliderGrabActive,
|
ImGuiCol_SliderGrabActive,
|
||||||
@ -373,6 +380,7 @@ struct ImGuiIO
|
|||||||
ImFont Font; // <auto> // Gets passed to text functions. Typedef ImFont to the type you want (ImBitmapFont* or your own font).
|
ImFont Font; // <auto> // Gets passed to text functions. Typedef ImFont to the type you want (ImBitmapFont* or your own font).
|
||||||
float FontHeight; // <auto> // Default font height, must be the vertical distance between two lines of text, aka == CalcTextSize(" ").y
|
float FontHeight; // <auto> // Default font height, must be the vertical distance between two lines of text, aka == CalcTextSize(" ").y
|
||||||
bool FontAllowScaling; // = false // Set to allow scaling text with CTRL+Wheel.
|
bool FontAllowScaling; // = false // Set to allow scaling text with CTRL+Wheel.
|
||||||
|
float PixelCenterOffset; // = 0.5f // Set to 0.0f for DirectX <= 9, 0.5f for Direct3D >= 10 and OpenGL.
|
||||||
|
|
||||||
// Settings - Functions (fill once)
|
// Settings - Functions (fill once)
|
||||||
void (*RenderDrawListsFn)(ImDrawList** const draw_lists, int count); // Required
|
void (*RenderDrawListsFn)(ImDrawList** const draw_lists, int count); // Required
|
||||||
@ -380,7 +388,7 @@ struct ImGuiIO
|
|||||||
void (*SetClipboardTextFn)(const char* text, const char* text_end); // Required for clipboard support (nb- the string is *NOT* zero-terminated at 'text_end')
|
void (*SetClipboardTextFn)(const char* text, const char* text_end); // Required for clipboard support (nb- the string is *NOT* zero-terminated at 'text_end')
|
||||||
|
|
||||||
// Input - Fill before calling NewFrame()
|
// Input - Fill before calling NewFrame()
|
||||||
ImVec2 MousePos; // Mouse position (set to -1,-1 if no mouse / on another screen, etc.)
|
ImVec2 MousePos; // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
|
||||||
bool MouseDown[2]; // Mouse buttons
|
bool MouseDown[2]; // Mouse buttons
|
||||||
int MouseWheel; // Mouse wheel: -1,0,+1
|
int MouseWheel; // Mouse wheel: -1,0,+1
|
||||||
bool KeyCtrl; // Keyboard modifier pressed: Control
|
bool KeyCtrl; // Keyboard modifier pressed: Control
|
||||||
@ -388,10 +396,13 @@ struct ImGuiIO
|
|||||||
bool KeysDown[512]; // Keyboard keys that are pressed (in whatever order user naturally has access to keyboard data)
|
bool KeysDown[512]; // Keyboard keys that are pressed (in whatever order user naturally has access to keyboard data)
|
||||||
char InputCharacters[16]; // List of characters input (translated by user from keypress+keyboard state). Fill using AddInputCharacter() helper.
|
char InputCharacters[16]; // List of characters input (translated by user from keypress+keyboard state). Fill using AddInputCharacter() helper.
|
||||||
|
|
||||||
// Output - Retrieve after calling NewFrame(), you can use them to discard inputs for the rest of your application
|
// Output - Retrieve after calling NewFrame(), you can use them to discard inputs or hide them from the rest of your application
|
||||||
bool WantCaptureMouse; // ImGui is using your mouse input (= window is being hovered or widget is active).
|
bool WantCaptureMouse; // ImGui is using your mouse input (= window is being hovered or widget is active).
|
||||||
bool WantCaptureKeyboard; // imGui is using your keyboard input (= widget is active).
|
bool WantCaptureKeyboard; // imGui is using your keyboard input (= widget is active).
|
||||||
|
|
||||||
|
// Function
|
||||||
|
void AddInputCharacter(char c); // Helper to add a new character into InputCharacters[]
|
||||||
|
|
||||||
// [Internal] ImGui will maintain those fields for you
|
// [Internal] ImGui will maintain those fields for you
|
||||||
ImVec2 MousePosPrev;
|
ImVec2 MousePosPrev;
|
||||||
ImVec2 MouseDelta;
|
ImVec2 MouseDelta;
|
||||||
@ -403,7 +414,6 @@ struct ImGuiIO
|
|||||||
float KeysDownTime[512];
|
float KeysDownTime[512];
|
||||||
|
|
||||||
ImGuiIO();
|
ImGuiIO();
|
||||||
void AddInputCharacter(char c); // Helper to add a new character into InputCharacters[]
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
@ -464,7 +474,7 @@ struct ImGuiTextBuffer
|
|||||||
size_t size() const { return Buf.size()-1; }
|
size_t size() const { return Buf.size()-1; }
|
||||||
bool empty() { return Buf.empty(); }
|
bool empty() { return Buf.empty(); }
|
||||||
void clear() { Buf.clear(); Buf.push_back(0); }
|
void clear() { Buf.clear(); Buf.push_back(0); }
|
||||||
void Append(const char* fmt, ...);
|
void append(const char* fmt, ...);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Helper: Key->value storage
|
// Helper: Key->value storage
|
||||||
@ -491,19 +501,10 @@ struct ImGuiStorage
|
|||||||
// Hold a series of drawing commands. The user provide a renderer for ImDrawList
|
// Hold a series of drawing commands. The user provide a renderer for ImDrawList
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
enum ImDrawCmdType
|
|
||||||
{
|
|
||||||
ImDrawCmdType_DrawTriangleList,
|
|
||||||
ImDrawCmdType_PushClipRect,
|
|
||||||
ImDrawCmdType_PopClipRect,
|
|
||||||
};
|
|
||||||
|
|
||||||
// sizeof() == 4
|
|
||||||
struct ImDrawCmd
|
struct ImDrawCmd
|
||||||
{
|
{
|
||||||
ImDrawCmdType cmd_type : 16;
|
unsigned int vtx_count;
|
||||||
unsigned int vtx_count : 16;
|
ImVec4 clip_rect;
|
||||||
ImDrawCmd(ImDrawCmdType _cmd_type = ImDrawCmdType_DrawTriangleList, unsigned int _vtx_count = 0) { cmd_type = _cmd_type; vtx_count = _vtx_count; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifndef IMDRAW_TEX_UV_FOR_WHITE
|
#ifndef IMDRAW_TEX_UV_FOR_WHITE
|
||||||
@ -524,16 +525,15 @@ struct ImDrawList
|
|||||||
{
|
{
|
||||||
ImVector<ImDrawCmd> commands;
|
ImVector<ImDrawCmd> commands;
|
||||||
ImVector<ImDrawVert> vtx_buffer; // each command consume ImDrawCmd::vtx_count of those
|
ImVector<ImDrawVert> vtx_buffer; // each command consume ImDrawCmd::vtx_count of those
|
||||||
ImVector<ImVec4> clip_rect_buffer; // each PushClipRect command consume 1 of those
|
ImVector<ImVec4> clip_rect_stack; // [internal] clip rect stack while building the command-list (so text command can perform clipping early on)
|
||||||
ImVector<ImVec4> clip_rect_stack_; // [internal] clip rect stack while building the command-list (so text command can perform clipping early on)
|
ImDrawVert* vtx_write; // [internal] point within vtx_buffer after each add command (to avoid using the ImVector<> operators too much)
|
||||||
ImDrawVert* vtx_write_; // [internal] point within vtx_buffer after each add command. allow us to use less [] and .resize on the vector (often slow on windows/debug)
|
|
||||||
|
|
||||||
ImDrawList() { Clear(); }
|
ImDrawList() { Clear(); }
|
||||||
|
|
||||||
void Clear();
|
void Clear();
|
||||||
void PushClipRect(const ImVec4& clip_rect);
|
void PushClipRect(const ImVec4& clip_rect);
|
||||||
void PopClipRect();
|
void PopClipRect();
|
||||||
void AddCommand(ImDrawCmdType cmd_type, int vtx_count);
|
void ReserveVertices(unsigned int vtx_count);
|
||||||
void AddVtx(const ImVec2& pos, ImU32 col);
|
void AddVtx(const ImVec2& pos, ImU32 col);
|
||||||
void AddVtxLine(const ImVec2& a, const ImVec2& b, ImU32 col);
|
void AddVtxLine(const ImVec2& a, const ImVec2& b, ImU32 col);
|
||||||
|
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 7.8 KiB |
Reference in New Issue
Block a user