features/sdl_renderer3_multiviewports
commit
378eee490b
28 changed files with 2085 additions and 1540 deletions
@ -1,16 +1,18 @@ |
||||
|
||||
directx9_example/ |
||||
DirectX9 example application (Windows). |
||||
|
||||
directx11_example/ |
||||
DirectX11 example (Windows). |
||||
|
||||
opengl_example/ |
||||
OpenGL exemple, using fixed pipeline (Windows/OSX/Linux etc., using glfw+glew) |
||||
OpenGL example, using GLFW + fixed pipeline. |
||||
This is simple and should work for all OpenGL enabled applications. |
||||
Prefer following this example since it is the shortest one! |
||||
|
||||
opengl3_example/ |
||||
OpenGL exemple, using programmable pipeline (Windows/OSX/Linux etc., using glfw+glew) |
||||
OpenGL exemple, using GLFW/GL3W + programmable pipeline. |
||||
This uses more modern calls and custom shaders. |
||||
I don't think there is an advantage using this over the "simpler example, but it is provided for information. |
||||
|
||||
I don't think there is an advantage using this over the simpler example, but it is provided for reference. |
||||
|
||||
directx9_example/ |
||||
DirectX9 example, Windows only. |
||||
|
||||
directx11_example/ |
||||
DirectX11 example, Windows only. |
||||
This is quite long and tedious, because: DirectX11. |
||||
|
||||
|
@ -0,0 +1,465 @@ |
||||
// ImGui Win32 + DirectX11 binding
|
||||
// https://github.com/ocornut/imgui
|
||||
|
||||
#include <imgui.h> |
||||
#include "imgui_impl_dx11.h" |
||||
|
||||
// DirectX
|
||||
#include <d3d11.h> |
||||
#include <d3dcompiler.h> |
||||
#define DIRECTINPUT_VERSION 0x0800 |
||||
#include <dinput.h> |
||||
|
||||
// Data
|
||||
static INT64 g_Time = 0; |
||||
static INT64 g_TicksPerSecond = 0; |
||||
|
||||
static HWND g_hWnd = 0; |
||||
static ID3D11Device* g_pd3dDevice = NULL; |
||||
static ID3D11DeviceContext* g_pd3dDeviceContext = NULL; |
||||
static ID3D11Buffer* g_pVB = NULL; |
||||
static ID3D10Blob * g_pVertexShaderBlob = NULL; |
||||
static ID3D11VertexShader* g_pVertexShader = NULL; |
||||
static ID3D11InputLayout* g_pInputLayout = NULL; |
||||
static ID3D11Buffer* g_pVertexConstantBuffer = NULL; |
||||
static ID3D10Blob * g_pPixelShaderBlob = NULL; |
||||
static ID3D11PixelShader* g_pPixelShader = NULL; |
||||
static ID3D11SamplerState* g_pFontSampler = NULL; |
||||
static ID3D11ShaderResourceView*g_pFontTextureView = NULL; |
||||
static ID3D11BlendState* g_blendState = NULL; |
||||
|
||||
struct CUSTOMVERTEX |
||||
{ |
||||
float pos[2]; |
||||
float uv[2]; |
||||
unsigned int col; |
||||
}; |
||||
|
||||
struct VERTEX_CONSTANT_BUFFER |
||||
{ |
||||
float mvp[4][4]; |
||||
}; |
||||
|
||||
// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
|
||||
// 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)
|
||||
static void ImGui_ImplDX11_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) |
||||
{ |
||||
// Copy and convert all vertices into a single contiguous buffer
|
||||
D3D11_MAPPED_SUBRESOURCE mappedResource; |
||||
if (g_pd3dDeviceContext->Map(g_pVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource) != S_OK) |
||||
return; |
||||
CUSTOMVERTEX* vtx_dst = (CUSTOMVERTEX*)mappedResource.pData; |
||||
for (int n = 0; n < cmd_lists_count; n++) |
||||
{ |
||||
const ImDrawList* cmd_list = cmd_lists[n]; |
||||
const ImDrawVert* vtx_src = &cmd_list->vtx_buffer[0]; |
||||
for (size_t i = 0; i < cmd_list->vtx_buffer.size(); i++) |
||||
{ |
||||
vtx_dst->pos[0] = vtx_src->pos.x; |
||||
vtx_dst->pos[1] = vtx_src->pos.y; |
||||
vtx_dst->uv[0] = vtx_src->uv.x; |
||||
vtx_dst->uv[1] = vtx_src->uv.y; |
||||
vtx_dst->col = vtx_src->col; |
||||
vtx_dst++; |
||||
vtx_src++; |
||||
} |
||||
} |
||||
g_pd3dDeviceContext->Unmap(g_pVB, 0); |
||||
|
||||
// Setup orthographic projection matrix into our constant buffer
|
||||
{ |
||||
D3D11_MAPPED_SUBRESOURCE mappedResource; |
||||
if (g_pd3dDeviceContext->Map(g_pVertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource) != S_OK) |
||||
return; |
||||
|
||||
VERTEX_CONSTANT_BUFFER* pConstantBuffer = (VERTEX_CONSTANT_BUFFER*)mappedResource.pData; |
||||
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, 0.5f, 0.0f }, |
||||
{ (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f }, |
||||
}; |
||||
memcpy(&pConstantBuffer->mvp, mvp, sizeof(mvp)); |
||||
g_pd3dDeviceContext->Unmap(g_pVertexConstantBuffer, 0); |
||||
} |
||||
|
||||
// Setup viewport
|
||||
{ |
||||
D3D11_VIEWPORT vp; |
||||
memset(&vp, 0, sizeof(D3D11_VIEWPORT)); |
||||
vp.Width = ImGui::GetIO().DisplaySize.x; |
||||
vp.Height = ImGui::GetIO().DisplaySize.y; |
||||
vp.MinDepth = 0.0f; |
||||
vp.MaxDepth = 1.0f; |
||||
vp.TopLeftX = 0; |
||||
vp.TopLeftY = 0; |
||||
g_pd3dDeviceContext->RSSetViewports(1, &vp); |
||||
} |
||||
|
||||
// Bind shader and vertex buffers
|
||||
unsigned int stride = sizeof(CUSTOMVERTEX); |
||||
unsigned int offset = 0; |
||||
g_pd3dDeviceContext->IASetInputLayout(g_pInputLayout); |
||||
g_pd3dDeviceContext->IASetVertexBuffers(0, 1, &g_pVB, &stride, &offset); |
||||
g_pd3dDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); |
||||
g_pd3dDeviceContext->VSSetShader(g_pVertexShader, NULL, 0); |
||||
g_pd3dDeviceContext->VSSetConstantBuffers(0, 1, &g_pVertexConstantBuffer); |
||||
g_pd3dDeviceContext->PSSetShader(g_pPixelShader, NULL, 0); |
||||
g_pd3dDeviceContext->PSSetSamplers(0, 1, &g_pFontSampler); |
||||
|
||||
// Setup render state
|
||||
const float blendFactor[4] = { 0.f, 0.f, 0.f, 0.f }; |
||||
g_pd3dDeviceContext->OMSetBlendState(g_blendState, blendFactor, 0xffffffff); |
||||
|
||||
// Render command lists
|
||||
int vtx_offset = 0; |
||||
for (int n = 0; n < cmd_lists_count; n++) |
||||
{ |
||||
const ImDrawList* cmd_list = cmd_lists[n]; |
||||
for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++) |
||||
{ |
||||
const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i]; |
||||
if (pcmd->user_callback) |
||||
{ |
||||
pcmd->user_callback(cmd_list, pcmd); |
||||
} |
||||
else |
||||
{ |
||||
const D3D11_RECT r = { (LONG)pcmd->clip_rect.x, (LONG)pcmd->clip_rect.y, (LONG)pcmd->clip_rect.z, (LONG)pcmd->clip_rect.w }; |
||||
g_pd3dDeviceContext->PSSetShaderResources(0, 1, (ID3D11ShaderResourceView**)&pcmd->texture_id); |
||||
g_pd3dDeviceContext->RSSetScissorRects(1, &r);
|
||||
g_pd3dDeviceContext->Draw(pcmd->vtx_count, vtx_offset); |
||||
} |
||||
vtx_offset += pcmd->vtx_count; |
||||
} |
||||
} |
||||
|
||||
// Restore modified state
|
||||
g_pd3dDeviceContext->IASetInputLayout(NULL); |
||||
g_pd3dDeviceContext->PSSetShader(NULL, NULL, 0); |
||||
g_pd3dDeviceContext->VSSetShader(NULL, NULL, 0); |
||||
} |
||||
|
||||
LRESULT ImGui_ImplDX11_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) |
||||
{ |
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
switch (msg) |
||||
{ |
||||
case WM_LBUTTONDOWN: |
||||
io.MouseDown[0] = true; |
||||
return true; |
||||
case WM_LBUTTONUP: |
||||
io.MouseDown[0] = false;
|
||||
return true; |
||||
case WM_RBUTTONDOWN: |
||||
io.MouseDown[1] = true;
|
||||
return true; |
||||
case WM_RBUTTONUP: |
||||
io.MouseDown[1] = false;
|
||||
return true; |
||||
case WM_MOUSEWHEEL: |
||||
io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f; |
||||
return true; |
||||
case WM_MOUSEMOVE: |
||||
io.MousePos.x = (signed short)(lParam); |
||||
io.MousePos.y = (signed short)(lParam >> 16);
|
||||
return true; |
||||
case WM_KEYDOWN: |
||||
if (wParam >= 0 && wParam < 256) |
||||
io.KeysDown[wParam] = 1; |
||||
return true; |
||||
case WM_KEYUP: |
||||
if (wParam >= 0 && wParam < 256) |
||||
io.KeysDown[wParam] = 0; |
||||
return true; |
||||
case WM_CHAR: |
||||
// You can also use ToAscii()+GetKeyboardState() to retrieve characters.
|
||||
if (wParam > 0 && wParam < 0x10000) |
||||
io.AddInputCharacter((unsigned short)wParam); |
||||
return true; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
static void ImGui_ImplDX11_CreateFontsTexture() |
||||
{ |
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
|
||||
// Build
|
||||
unsigned char* pixels; |
||||
int width, height; |
||||
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); |
||||
|
||||
// Create DX11 texture
|
||||
{ |
||||
D3D11_TEXTURE2D_DESC texDesc; |
||||
ZeroMemory(&texDesc, sizeof(texDesc)); |
||||
texDesc.Width = width; |
||||
texDesc.Height = height; |
||||
texDesc.MipLevels = 1; |
||||
texDesc.ArraySize = 1; |
||||
texDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
||||
texDesc.SampleDesc.Count = 1; |
||||
texDesc.Usage = D3D11_USAGE_DEFAULT; |
||||
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE; |
||||
texDesc.CPUAccessFlags = 0; |
||||
|
||||
ID3D11Texture2D *pTexture = NULL; |
||||
D3D11_SUBRESOURCE_DATA subResource; |
||||
subResource.pSysMem = pixels; |
||||
subResource.SysMemPitch = texDesc.Width * 4; |
||||
subResource.SysMemSlicePitch = 0; |
||||
g_pd3dDevice->CreateTexture2D(&texDesc, &subResource, &pTexture); |
||||
|
||||
// Create texture view
|
||||
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; |
||||
ZeroMemory(&srvDesc, sizeof(srvDesc)); |
||||
srvDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
||||
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; |
||||
srvDesc.Texture2D.MipLevels = texDesc.MipLevels; |
||||
srvDesc.Texture2D.MostDetailedMip = 0; |
||||
g_pd3dDevice->CreateShaderResourceView(pTexture, &srvDesc, &g_pFontTextureView); |
||||
pTexture->Release(); |
||||
} |
||||
|
||||
// Store our identifier
|
||||
io.Fonts->TexID = (void *)g_pFontTextureView; |
||||
|
||||
// Create texture sampler
|
||||
{ |
||||
D3D11_SAMPLER_DESC samplerDesc; |
||||
ZeroMemory(&samplerDesc, sizeof(samplerDesc)); |
||||
samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; |
||||
samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; |
||||
samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; |
||||
samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; |
||||
samplerDesc.MipLODBias = 0.f; |
||||
samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS; |
||||
samplerDesc.MinLOD = 0.f; |
||||
samplerDesc.MaxLOD = 0.f; |
||||
g_pd3dDevice->CreateSamplerState(&samplerDesc, &g_pFontSampler); |
||||
} |
||||
} |
||||
|
||||
bool ImGui_ImplDX11_CreateDeviceObjects() |
||||
{ |
||||
if (!g_pd3dDevice) |
||||
return false; |
||||
if (g_pVB) |
||||
ImGui_ImplDX11_InvalidateDeviceObjects(); |
||||
|
||||
// Create the vertex shader
|
||||
{ |
||||
static const char* vertexShader =
|
||||
"cbuffer vertexBuffer : register(c0) \
|
||||
{\
|
||||
float4x4 ProjectionMatrix; \
|
||||
};\
|
||||
struct VS_INPUT\
|
||||
{\
|
||||
float2 pos : POSITION;\
|
||||
float4 col : COLOR0;\
|
||||
float2 uv : TEXCOORD0;\
|
||||
};\
|
||||
\
|
||||
struct PS_INPUT\
|
||||
{\
|
||||
float4 pos : SV_POSITION;\
|
||||
float4 col : COLOR0;\
|
||||
float2 uv : TEXCOORD0;\
|
||||
};\
|
||||
\
|
||||
PS_INPUT main(VS_INPUT input)\
|
||||
{\
|
||||
PS_INPUT output;\
|
||||
output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\
|
||||
output.col = input.col;\
|
||||
output.uv = input.uv;\
|
||||
return output;\
|
||||
}"; |
||||
|
||||
D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_5_0", 0, 0, &g_pVertexShaderBlob, NULL); |
||||
if (g_pVertexShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
|
||||
return false; |
||||
if (g_pd3dDevice->CreateVertexShader((DWORD*)g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), NULL, &g_pVertexShader) != S_OK) |
||||
return false; |
||||
|
||||
// Create the input layout
|
||||
D3D11_INPUT_ELEMENT_DESC localLayout[] = { |
||||
{ "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, (size_t)(&((CUSTOMVERTEX*)0)->pos), D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
||||
{ "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((CUSTOMVERTEX*)0)->col), D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
||||
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((CUSTOMVERTEX*)0)->uv), D3D11_INPUT_PER_VERTEX_DATA, 0 }, |
||||
}; |
||||
|
||||
if (g_pd3dDevice->CreateInputLayout(localLayout, 3, g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pInputLayout) != S_OK) |
||||
return false; |
||||
|
||||
// Create the constant buffer
|
||||
{ |
||||
D3D11_BUFFER_DESC cbDesc; |
||||
cbDesc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER); |
||||
cbDesc.Usage = D3D11_USAGE_DYNAMIC; |
||||
cbDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; |
||||
cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
||||
cbDesc.MiscFlags = 0; |
||||
g_pd3dDevice->CreateBuffer(&cbDesc, NULL, &g_pVertexConstantBuffer); |
||||
} |
||||
} |
||||
|
||||
// Create the pixel shader
|
||||
{ |
||||
static const char* pixelShader =
|
||||
"struct PS_INPUT\
|
||||
{\
|
||||
float4 pos : SV_POSITION;\
|
||||
float4 col : COLOR0;\
|
||||
float2 uv : TEXCOORD0;\
|
||||
};\
|
||||
sampler sampler0;\
|
||||
Texture2D texture0;\
|
||||
\
|
||||
float4 main(PS_INPUT input) : SV_Target\
|
||||
{\
|
||||
float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \
|
||||
return out_col; \
|
||||
}"; |
||||
|
||||
D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_5_0", 0, 0, &g_pPixelShaderBlob, NULL); |
||||
if (g_pPixelShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob!
|
||||
return false; |
||||
if (g_pd3dDevice->CreatePixelShader((DWORD*)g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize(), NULL, &g_pPixelShader) != S_OK) |
||||
return false; |
||||
} |
||||
|
||||
// Create the blending setup
|
||||
{ |
||||
D3D11_BLEND_DESC desc; |
||||
ZeroMemory(&desc, sizeof(desc)); |
||||
desc.AlphaToCoverageEnable = false; |
||||
desc.RenderTarget[0].BlendEnable = true; |
||||
desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; |
||||
desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA; |
||||
desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; |
||||
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA; |
||||
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; |
||||
desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; |
||||
desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL; |
||||
g_pd3dDevice->CreateBlendState(&desc, &g_blendState); |
||||
} |
||||
|
||||
// Create the vertex buffer
|
||||
{ |
||||
D3D11_BUFFER_DESC bufferDesc; |
||||
memset(&bufferDesc, 0, sizeof(D3D11_BUFFER_DESC)); |
||||
bufferDesc.Usage = D3D11_USAGE_DYNAMIC; |
||||
bufferDesc.ByteWidth = 100000 * sizeof(CUSTOMVERTEX); // Maybe we should handle that more dynamically?
|
||||
bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; |
||||
bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
||||
bufferDesc.MiscFlags = 0; |
||||
if (g_pd3dDevice->CreateBuffer(&bufferDesc, NULL, &g_pVB) < 0) |
||||
return false; |
||||
} |
||||
|
||||
ImGui_ImplDX11_CreateFontsTexture(); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
void ImGui_ImplDX11_InvalidateDeviceObjects() |
||||
{ |
||||
if (!g_pd3dDevice) |
||||
return; |
||||
|
||||
if (g_pFontSampler) { g_pFontSampler->Release(); g_pFontSampler = NULL; } |
||||
if (g_pFontTextureView) { g_pFontTextureView->Release(); ImGui::GetIO().Fonts->TexID = 0; } |
||||
if (g_pVB) { g_pVB->Release(); g_pVB = NULL; } |
||||
|
||||
if (g_blendState) { g_blendState->Release(); g_blendState = NULL; } |
||||
if (g_pPixelShader) { g_pPixelShader->Release(); g_pPixelShader = NULL; } |
||||
if (g_pPixelShaderBlob) { g_pPixelShaderBlob->Release(); g_pPixelShaderBlob = NULL; } |
||||
if (g_pVertexConstantBuffer) { g_pVertexConstantBuffer->Release(); g_pVertexConstantBuffer = NULL; } |
||||
if (g_pInputLayout) { g_pInputLayout->Release(); g_pInputLayout = NULL; } |
||||
if (g_pVertexShader) { g_pVertexShader->Release(); g_pVertexShader = NULL; } |
||||
if (g_pVertexShaderBlob) { g_pVertexShaderBlob->Release(); g_pVertexShaderBlob = NULL; } |
||||
} |
||||
|
||||
bool ImGui_ImplDX11_Init(void* hwnd, ID3D11Device* device, ID3D11DeviceContext* device_context) |
||||
{ |
||||
g_hWnd = (HWND)hwnd; |
||||
g_pd3dDevice = device; |
||||
g_pd3dDeviceContext = device_context; |
||||
|
||||
if (!QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))
|
||||
return false; |
||||
if (!QueryPerformanceCounter((LARGE_INTEGER *)&g_Time)) |
||||
return false; |
||||
|
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
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_RightArrow] = VK_RIGHT; |
||||
io.KeyMap[ImGuiKey_UpArrow] = VK_UP; |
||||
io.KeyMap[ImGuiKey_DownArrow] = VK_UP; |
||||
io.KeyMap[ImGuiKey_Home] = VK_HOME; |
||||
io.KeyMap[ImGuiKey_End] = VK_END; |
||||
io.KeyMap[ImGuiKey_Delete] = VK_DELETE; |
||||
io.KeyMap[ImGuiKey_Backspace] = VK_BACK; |
||||
io.KeyMap[ImGuiKey_Enter] = VK_RETURN; |
||||
io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE; |
||||
io.KeyMap[ImGuiKey_A] = 'A'; |
||||
io.KeyMap[ImGuiKey_C] = 'C'; |
||||
io.KeyMap[ImGuiKey_V] = 'V'; |
||||
io.KeyMap[ImGuiKey_X] = 'X'; |
||||
io.KeyMap[ImGuiKey_Y] = 'Y'; |
||||
io.KeyMap[ImGuiKey_Z] = 'Z'; |
||||
|
||||
io.RenderDrawListsFn = ImGui_ImplDX11_RenderDrawLists; |
||||
io.ImeWindowHandle = g_hWnd; |
||||
|
||||
return true; |
||||
} |
||||
|
||||
void ImGui_ImplDX11_Shutdown() |
||||
{ |
||||
ImGui_ImplDX11_InvalidateDeviceObjects(); |
||||
ImGui::Shutdown(); |
||||
g_pd3dDevice = NULL; |
||||
g_pd3dDeviceContext = NULL; |
||||
g_hWnd = (HWND)0; |
||||
} |
||||
|
||||
void ImGui_ImplDX11_NewFrame() |
||||
{ |
||||
if (!g_pVB) |
||||
ImGui_ImplDX11_CreateDeviceObjects(); |
||||
|
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
|
||||
// Setup display size (every frame to accommodate for window resizing)
|
||||
RECT rect; |
||||
GetClientRect(g_hWnd, &rect); |
||||
io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top)); |
||||
|
||||
// Setup time step
|
||||
INT64 current_time; |
||||
QueryPerformanceCounter((LARGE_INTEGER *)¤t_time);
|
||||
io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond; |
||||
g_Time = current_time; |
||||
|
||||
// Read keyboard modifiers inputs
|
||||
io.KeyCtrl = (GetKeyState(VK_CONTROL) & 0x8000) != 0; |
||||
io.KeyShift = (GetKeyState(VK_SHIFT) & 0x8000) != 0; |
||||
// io.KeysDown : filled by WM_KEYDOWN/WM_KEYUP events
|
||||
// io.MousePos : filled by WM_MOUSEMOVE events
|
||||
// io.MouseDown : filled by WM_*BUTTON* events
|
||||
// io.MouseWheel : filled by WM_MOUSEWHEEL events
|
||||
|
||||
// Start the frame
|
||||
ImGui::NewFrame(); |
||||
} |
@ -0,0 +1,20 @@ |
||||
// ImGui Win32 + DirectX11 binding
|
||||
// https://github.com/ocornut/imgui
|
||||
|
||||
struct ID3D11Device; |
||||
struct ID3D11DeviceContext; |
||||
|
||||
bool ImGui_ImplDX11_Init(void* hwnd, ID3D11Device* device, ID3D11DeviceContext* device_context); |
||||
void ImGui_ImplDX11_Shutdown(); |
||||
void ImGui_ImplDX11_NewFrame(); |
||||
|
||||
// Use if you want to reset your rendering device without losing ImGui state.
|
||||
void ImGui_ImplDX11_InvalidateDeviceObjects(); |
||||
bool ImGui_ImplDX11_CreateDeviceObjects(); |
||||
|
||||
// Handler for Win32 messages, update mouse/keyboard data.
|
||||
// You may or not need this for your implementation, but it can serve as reference for handling inputs.
|
||||
// Commented out to avoid dragging dependencies on <windows.h> types. You can copy the extern declaration in your code.
|
||||
/*
|
||||
extern LRESULT ImGui_ImplDX11_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); |
||||
*/ |
@ -0,0 +1,286 @@ |
||||
// ImGui Win32 + DirectX9 binding
|
||||
// https://github.com/ocornut/imgui
|
||||
|
||||
#include <imgui.h> |
||||
#include "imgui_impl_dx9.h" |
||||
|
||||
// DirectX
|
||||
#include <d3dx9.h> |
||||
#define DIRECTINPUT_VERSION 0x0800 |
||||
#include <dinput.h> |
||||
|
||||
// Data
|
||||
static HWND g_hWnd = 0; |
||||
static INT64 g_Time = 0; |
||||
static INT64 g_TicksPerSecond = 0; |
||||
static LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; |
||||
static LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; |
||||
|
||||
struct CUSTOMVERTEX |
||||
{ |
||||
D3DXVECTOR3 pos; |
||||
D3DCOLOR col; |
||||
D3DXVECTOR2 uv; |
||||
}; |
||||
#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 structure)
|
||||
// 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)
|
||||
static void ImGui_ImplDX9_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) |
||||
{ |
||||
size_t total_vtx_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; |
||||
|
||||
// Copy and convert all vertices into a single contiguous buffer
|
||||
CUSTOMVERTEX* vtx_dst; |
||||
if (g_pVB->Lock(0, (UINT)total_vtx_count, (void**)&vtx_dst, D3DLOCK_DISCARD) < 0) |
||||
return; |
||||
for (int n = 0; n < cmd_lists_count; n++) |
||||
{ |
||||
const ImDrawList* cmd_list = cmd_lists[n]; |
||||
const ImDrawVert* vtx_src = &cmd_list->vtx_buffer[0]; |
||||
for (size_t i = 0; i < cmd_list->vtx_buffer.size(); i++) |
||||
{ |
||||
vtx_dst->pos.x = vtx_src->pos.x; |
||||
vtx_dst->pos.y = vtx_src->pos.y; |
||||
vtx_dst->pos.z = 0.0f; |
||||
vtx_dst->col = (vtx_src->col & 0xFF00FF00) | ((vtx_src->col & 0xFF0000)>>16) | ((vtx_src->col & 0xFF) << 16); // RGBA --> ARGB for DirectX9
|
||||
vtx_dst->uv.x = vtx_src->uv.x; |
||||
vtx_dst->uv.y = vtx_src->uv.y; |
||||
vtx_dst++; |
||||
vtx_src++; |
||||
} |
||||
} |
||||
g_pVB->Unlock(); |
||||
|
||||
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) ); |
||||
g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); |
||||
|
||||
// Setup render state: alpha-blending, no face culling, no depth testing
|
||||
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); |
||||
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, false ); |
||||
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, false ); |
||||
g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, true ); |
||||
g_pd3dDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD ); |
||||
g_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, false ); |
||||
g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA ); |
||||
g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA ); |
||||
g_pd3dDevice->SetRenderState( D3DRS_SCISSORTESTENABLE, true ); |
||||
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 ); |
||||
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE ); |
||||
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE ); |
||||
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE ); |
||||
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE ); |
||||
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR ); |
||||
g_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR ); |
||||
|
||||
// Setup orthographic projection matrix
|
||||
D3DXMATRIXA16 mat; |
||||
D3DXMatrixIdentity(&mat); |
||||
g_pd3dDevice->SetTransform(D3DTS_WORLD, &mat); |
||||
g_pd3dDevice->SetTransform(D3DTS_VIEW, &mat); |
||||
D3DXMatrixOrthoOffCenterLH(&mat, 0.5f, ImGui::GetIO().DisplaySize.x+0.5f, ImGui::GetIO().DisplaySize.y+0.5f, 0.5f, -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++) |
||||
{ |
||||
const ImDrawList* cmd_list = cmd_lists[n]; |
||||
for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++) |
||||
{ |
||||
const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i]; |
||||
if (pcmd->user_callback) |
||||
{ |
||||
pcmd->user_callback(cmd_list, pcmd); |
||||
} |
||||
else |
||||
{ |
||||
const RECT r = { (LONG)pcmd->clip_rect.x, (LONG)pcmd->clip_rect.y, (LONG)pcmd->clip_rect.z, (LONG)pcmd->clip_rect.w }; |
||||
g_pd3dDevice->SetTexture( 0, (LPDIRECT3DTEXTURE9)pcmd->texture_id ); |
||||
g_pd3dDevice->SetScissorRect(&r); |
||||
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, vtx_offset, pcmd->vtx_count/3); |
||||
} |
||||
vtx_offset += pcmd->vtx_count; |
||||
} |
||||
} |
||||
} |
||||
|
||||
LRESULT ImGui_ImplDX9_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) |
||||
{ |
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
switch (msg) |
||||
{ |
||||
case WM_LBUTTONDOWN: |
||||
io.MouseDown[0] = true; |
||||
return true; |
||||
case WM_LBUTTONUP: |
||||
io.MouseDown[0] = false;
|
||||
return true; |
||||
case WM_RBUTTONDOWN: |
||||
io.MouseDown[1] = true;
|
||||
return true; |
||||
case WM_RBUTTONUP: |
||||
io.MouseDown[1] = false;
|
||||
return true; |
||||
case WM_MOUSEWHEEL: |
||||
io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.0f : -1.0f; |
||||
return true; |
||||
case WM_MOUSEMOVE: |
||||
io.MousePos.x = (signed short)(lParam); |
||||
io.MousePos.y = (signed short)(lParam >> 16);
|
||||
return true; |
||||
case WM_KEYDOWN: |
||||
if (wParam >= 0 && wParam < 256) |
||||
io.KeysDown[wParam] = 1; |
||||
return true; |
||||
case WM_KEYUP: |
||||
if (wParam >= 0 && wParam < 256) |
||||
io.KeysDown[wParam] = 0; |
||||
return true; |
||||
case WM_CHAR: |
||||
// You can also use ToAscii()+GetKeyboardState() to retrieve characters.
|
||||
if (wParam > 0 && wParam < 0x10000) |
||||
io.AddInputCharacter((unsigned short)wParam); |
||||
return true; |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
bool ImGui_ImplDX9_Init(void* hwnd, IDirect3DDevice9* device) |
||||
{ |
||||
g_hWnd = (HWND)hwnd; |
||||
g_pd3dDevice = device; |
||||
|
||||
if (!QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))
|
||||
return false; |
||||
if (!QueryPerformanceCounter((LARGE_INTEGER *)&g_Time)) |
||||
return false; |
||||
|
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
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_RightArrow] = VK_RIGHT; |
||||
io.KeyMap[ImGuiKey_UpArrow] = VK_UP; |
||||
io.KeyMap[ImGuiKey_DownArrow] = VK_UP; |
||||
io.KeyMap[ImGuiKey_Home] = VK_HOME; |
||||
io.KeyMap[ImGuiKey_End] = VK_END; |
||||
io.KeyMap[ImGuiKey_Delete] = VK_DELETE; |
||||
io.KeyMap[ImGuiKey_Backspace] = VK_BACK; |
||||
io.KeyMap[ImGuiKey_Enter] = VK_RETURN; |
||||
io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE; |
||||
io.KeyMap[ImGuiKey_A] = 'A'; |
||||
io.KeyMap[ImGuiKey_C] = 'C'; |
||||
io.KeyMap[ImGuiKey_V] = 'V'; |
||||
io.KeyMap[ImGuiKey_X] = 'X'; |
||||
io.KeyMap[ImGuiKey_Y] = 'Y'; |
||||
io.KeyMap[ImGuiKey_Z] = 'Z'; |
||||
|
||||
io.RenderDrawListsFn = ImGui_ImplDX9_RenderDrawLists; |
||||
io.ImeWindowHandle = g_hWnd; |
||||
|
||||
if (g_pd3dDevice->CreateVertexBuffer(10000 * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL) < 0) |
||||
return false; |
||||
|
||||
return true; |
||||
} |
||||
|
||||
void ImGui_ImplDX9_Shutdown() |
||||
{ |
||||
ImGui_ImplDX9_InvalidateDeviceObjects(); |
||||
ImGui::Shutdown(); |
||||
g_pd3dDevice = NULL; |
||||
g_hWnd = 0; |
||||
} |
||||
|
||||
static void ImGui_ImplDX9_CreateFontsTexture() |
||||
{ |
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
|
||||
// Build
|
||||
unsigned char* pixels; |
||||
int width, height, bytes_per_pixel; |
||||
io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height, &bytes_per_pixel); |
||||
|
||||
// Create DX9 texture
|
||||
LPDIRECT3DTEXTURE9 pTexture = NULL; |
||||
if (D3DXCreateTexture(g_pd3dDevice, width, height, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8, D3DPOOL_DEFAULT, &pTexture) < 0) |
||||
{ |
||||
IM_ASSERT(0); |
||||
return; |
||||
} |
||||
D3DLOCKED_RECT tex_locked_rect; |
||||
if (pTexture->LockRect(0, &tex_locked_rect, NULL, 0) != D3D_OK)
|
||||
{
|
||||
IM_ASSERT(0);
|
||||
return;
|
||||
} |
||||
for (int y = 0; y < height; y++) |
||||
memcpy((unsigned char *)tex_locked_rect.pBits + tex_locked_rect.Pitch * y, pixels + (width * bytes_per_pixel) * y, (width * bytes_per_pixel)); |
||||
pTexture->UnlockRect(0); |
||||
|
||||
// Store our identifier
|
||||
io.Fonts->TexID = (void *)pTexture; |
||||
} |
||||
|
||||
bool ImGui_ImplDX9_CreateDeviceObjects() |
||||
{ |
||||
if (!g_pd3dDevice) |
||||
return false; |
||||
|
||||
if (g_pd3dDevice->CreateVertexBuffer(10000 * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL) < 0) |
||||
return false; |
||||
|
||||
ImGui_ImplDX9_CreateFontsTexture(); |
||||
return true; |
||||
} |
||||
|
||||
void ImGui_ImplDX9_InvalidateDeviceObjects() |
||||
{ |
||||
if (!g_pd3dDevice) |
||||
return; |
||||
if (g_pVB) |
||||
{ |
||||
g_pVB->Release(); |
||||
g_pVB = NULL; |
||||
} |
||||
if (LPDIRECT3DTEXTURE9 tex = (LPDIRECT3DTEXTURE9)ImGui::GetIO().Fonts->TexID) |
||||
{ |
||||
tex->Release(); |
||||
ImGui::GetIO().Fonts->TexID = 0; |
||||
} |
||||
} |
||||
|
||||
void ImGui_ImplDX9_NewFrame() |
||||
{ |
||||
if (!g_pVB) |
||||
ImGui_ImplDX9_CreateDeviceObjects(); |
||||
|
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
|
||||
// Setup display size (every frame to accommodate for window resizing)
|
||||
RECT rect; |
||||
GetClientRect(g_hWnd, &rect); |
||||
io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top)); |
||||
|
||||
// Setup time step
|
||||
INT64 current_time; |
||||
QueryPerformanceCounter((LARGE_INTEGER *)¤t_time);
|
||||
io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond; |
||||
g_Time = current_time; |
||||
|
||||
// Read keyboard modifiers inputs
|
||||
io.KeyCtrl = (GetKeyState(VK_CONTROL) & 0x8000) != 0; |
||||
io.KeyShift = (GetKeyState(VK_SHIFT) & 0x8000) != 0; |
||||
// io.KeysDown : filled by WM_KEYDOWN/WM_KEYUP events
|
||||
// io.MousePos : filled by WM_MOUSEMOVE events
|
||||
// io.MouseDown : filled by WM_*BUTTON* events
|
||||
// io.MouseWheel : filled by WM_MOUSEWHEEL events
|
||||
|
||||
// Start the frame
|
||||
ImGui::NewFrame(); |
||||
} |
@ -0,0 +1,19 @@ |
||||
// ImGui Win32 + DirectX9 binding
|
||||
// https://github.com/ocornut/imgui
|
||||
|
||||
struct IDirect3DDevice9; |
||||
|
||||
bool ImGui_ImplDX9_Init(void* hwnd, IDirect3DDevice9* device); |
||||
void ImGui_ImplDX9_Shutdown(); |
||||
void ImGui_ImplDX9_NewFrame(); |
||||
|
||||
// Use if you want to reset your rendering device without losing ImGui state.
|
||||
void ImGui_ImplDX9_InvalidateDeviceObjects(); |
||||
bool ImGui_ImplDX9_CreateDeviceObjects(); |
||||
|
||||
// Handler for Win32 messages, update mouse/keyboard data.
|
||||
// You may or not need this for your implementation, but it can serve as reference for handling inputs.
|
||||
// Commented out to avoid dragging dependencies on <windows.h> types. You can copy the extern declaration in your code.
|
||||
/*
|
||||
extern LRESULT ImGui_ImplDX9_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); |
||||
*/ |
@ -0,0 +1,348 @@ |
||||
// ImGui GLFW binding with OpenGL3 + shaders
|
||||
// https://github.com/ocornut/imgui
|
||||
|
||||
#include <imgui.h> |
||||
#include "imgui_impl_glfw_gl3.h" |
||||
|
||||
// GL3W/GLFW
|
||||
#include <GL/gl3w.h> |
||||
#include <GLFW/glfw3.h> |
||||
#ifdef _MSC_VER |
||||
#undef APIENTRY |
||||
#define GLFW_EXPOSE_NATIVE_WIN32 |
||||
#define GLFW_EXPOSE_NATIVE_WGL |
||||
#include <GLFW/glfw3native.h> |
||||
#endif |
||||
|
||||
// Data
|
||||
static GLFWwindow* g_Window = NULL; |
||||
static double g_Time = 0.0f; |
||||
static bool g_MousePressed[3] = { false, false, false }; |
||||
static float g_MouseWheel = 0.0f; |
||||
static GLuint g_FontTexture = 0; |
||||
static int g_ShaderHandle = 0, g_VertHandle = 0, g_FragHandle = 0; |
||||
static int g_AttribLocationTex = 0, g_AttribLocationProjMtx = 0; |
||||
static int g_AttribLocationPosition = 0, g_AttribLocationUV = 0, g_AttribLocationColor = 0; |
||||
static size_t g_VboMaxSize = 20000; |
||||
static unsigned int g_VboHandle = 0, g_VaoHandle = 0; |
||||
|
||||
// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
|
||||
// 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)
|
||||
static void ImGui_ImplGlfwGL3_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) |
||||
{ |
||||
if (cmd_lists_count == 0) |
||||
return; |
||||
|
||||
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled
|
||||
glEnable(GL_BLEND); |
||||
glBlendEquation(GL_FUNC_ADD); |
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
||||
glDisable(GL_CULL_FACE); |
||||
glDisable(GL_DEPTH_TEST); |
||||
glEnable(GL_SCISSOR_TEST); |
||||
glActiveTexture(GL_TEXTURE0); |
||||
|
||||
// Setup orthographic projection matrix
|
||||
const float width = ImGui::GetIO().DisplaySize.x; |
||||
const float height = ImGui::GetIO().DisplaySize.y; |
||||
const float ortho_projection[4][4] = |
||||
{ |
||||
{ 2.0f/width, 0.0f, 0.0f, 0.0f }, |
||||
{ 0.0f, 2.0f/-height, 0.0f, 0.0f }, |
||||
{ 0.0f, 0.0f, -1.0f, 0.0f }, |
||||
{ -1.0f, 1.0f, 0.0f, 1.0f }, |
||||
}; |
||||
glUseProgram(g_ShaderHandle); |
||||
glUniform1i(g_AttribLocationTex, 0); |
||||
glUniformMatrix4fv(g_AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]); |
||||
|
||||
// Grow our buffer according to what we need
|
||||
size_t total_vtx_count = 0; |
||||
for (int n = 0; n < cmd_lists_count; n++) |
||||
total_vtx_count += cmd_lists[n]->vtx_buffer.size(); |
||||
glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle); |
||||
size_t neededBufferSize = total_vtx_count * sizeof(ImDrawVert); |
||||
if (neededBufferSize > g_VboMaxSize) |
||||
{ |
||||
g_VboMaxSize = neededBufferSize + 5000; // Grow buffer
|
||||
glBufferData(GL_ARRAY_BUFFER, g_VboMaxSize, NULL, GL_STREAM_DRAW); |
||||
} |
||||
|
||||
// Copy and convert all vertices into a single contiguous buffer
|
||||
unsigned char* buffer_data = (unsigned char*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); |
||||
if (!buffer_data) |
||||
return; |
||||
for (int n = 0; n < cmd_lists_count; n++) |
||||
{ |
||||
const ImDrawList* cmd_list = cmd_lists[n]; |
||||
memcpy(buffer_data, &cmd_list->vtx_buffer[0], cmd_list->vtx_buffer.size() * sizeof(ImDrawVert)); |
||||
buffer_data += cmd_list->vtx_buffer.size() * sizeof(ImDrawVert); |
||||
} |
||||
glUnmapBuffer(GL_ARRAY_BUFFER); |
||||
glBindBuffer(GL_ARRAY_BUFFER, 0); |
||||
glBindVertexArray(g_VaoHandle); |
||||
|
||||
int cmd_offset = 0; |
||||
for (int n = 0; n < cmd_lists_count; n++) |
||||
{ |
||||
const ImDrawList* cmd_list = cmd_lists[n]; |
||||
int vtx_offset = cmd_offset; |
||||
const ImDrawCmd* pcmd_end = cmd_list->commands.end(); |
||||
for (const ImDrawCmd* pcmd = cmd_list->commands.begin(); pcmd != pcmd_end; pcmd++) |
||||
{ |
||||
if (pcmd->user_callback) |
||||
{ |
||||
pcmd->user_callback(cmd_list, pcmd); |
||||
} |
||||
else |
||||
{ |
||||
glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->texture_id); |
||||
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)); |
||||
glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count); |
||||
} |
||||
vtx_offset += pcmd->vtx_count; |
||||
} |
||||
cmd_offset = vtx_offset; |
||||
} |
||||
|
||||
// Restore modified state
|
||||
glBindVertexArray(0); |
||||
glUseProgram(0); |
||||
glDisable(GL_SCISSOR_TEST); |
||||
glBindTexture(GL_TEXTURE_2D, 0); |
||||
} |
||||
|
||||
static const char* ImGui_ImplGlfwGL3_GetClipboardText() |
||||
{ |
||||
return glfwGetClipboardString(g_Window); |
||||
} |
||||
|
||||
static void ImGui_ImplGlfwGL3_SetClipboardText(const char* text) |
||||
{ |
||||
glfwSetClipboardString(g_Window, text); |
||||
} |
||||
|
||||
void ImGui_ImplGlfwGL3_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods) |
||||
{ |
||||
if (action == GLFW_PRESS && button >= 0 && button < 3) |
||||
g_MousePressed[button] = true; |
||||
} |
||||
|
||||
void ImGui_ImplGlfwGL3_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset) |
||||
{ |
||||
g_MouseWheel += (float)yoffset; // Use fractional mouse wheel, 1.0 unit 5 lines.
|
||||
} |
||||
|
||||
void ImGui_ImplGlfwGL3_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) |
||||
{ |
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
if (action == GLFW_PRESS) |
||||
io.KeysDown[key] = true; |
||||
if (action == GLFW_RELEASE) |
||||
io.KeysDown[key] = false; |
||||
io.KeyCtrl = (mods & GLFW_MOD_CONTROL) != 0; |
||||
io.KeyShift = (mods & GLFW_MOD_SHIFT) != 0; |
||||
} |
||||
|
||||
void ImGui_ImplGlfwGL3_CharCallback(GLFWwindow* window, unsigned int c) |
||||
{ |
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
if (c > 0 && c < 0x10000) |
||||
io.AddInputCharacter((unsigned short)c); |
||||
} |
||||
|
||||
void ImGui_ImplGlfwGL3_CreateFontsTexture() |
||||
{ |
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
|
||||
unsigned char* pixels; |
||||
int width, height; |
||||
io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bits for OpenGL3 demo because it is more likely to be compatible with user's existing shader.
|
||||
|
||||
glGenTextures(1, &g_FontTexture); |
||||
glBindTexture(GL_TEXTURE_2D, g_FontTexture); |
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
||||
|
||||
// Store our identifier
|
||||
io.Fonts->TexID = (void *)(intptr_t)g_FontTexture; |
||||
} |
||||
|
||||
bool ImGui_ImplGlfwGL3_CreateDeviceObjects() |
||||
{ |
||||
const GLchar *vertex_shader = |
||||
"#version 330\n" |
||||
"uniform mat4 ProjMtx;\n" |
||||
"in vec2 Position;\n" |
||||
"in vec2 UV;\n" |
||||
"in vec4 Color;\n" |
||||
"out vec2 Frag_UV;\n" |
||||
"out vec4 Frag_Color;\n" |
||||
"void main()\n" |
||||
"{\n" |
||||
" Frag_UV = UV;\n" |
||||
" Frag_Color = Color;\n" |
||||
" gl_Position = ProjMtx * vec4(Position.xy,0,1);\n" |
||||
"}\n"; |
||||
|
||||
const GLchar* fragment_shader = |
||||
"#version 330\n" |
||||
"uniform sampler2D Texture;\n" |
||||
"in vec2 Frag_UV;\n" |
||||
"in vec4 Frag_Color;\n" |
||||
"out vec4 Out_Color;\n" |
||||
"void main()\n" |
||||
"{\n" |
||||
" Out_Color = Frag_Color * texture( Texture, Frag_UV.st);\n" |
||||
"}\n"; |
||||
|
||||
g_ShaderHandle = glCreateProgram(); |
||||
g_VertHandle = glCreateShader(GL_VERTEX_SHADER); |
||||
g_FragHandle = glCreateShader(GL_FRAGMENT_SHADER); |
||||
glShaderSource(g_VertHandle, 1, &vertex_shader, 0); |
||||
glShaderSource(g_FragHandle, 1, &fragment_shader, 0); |
||||
glCompileShader(g_VertHandle); |
||||
glCompileShader(g_FragHandle); |
||||
glAttachShader(g_ShaderHandle, g_VertHandle); |
||||
glAttachShader(g_ShaderHandle, g_FragHandle); |
||||
glLinkProgram(g_ShaderHandle); |
||||
|
||||
g_AttribLocationTex = glGetUniformLocation(g_ShaderHandle, "Texture"); |
||||
g_AttribLocationProjMtx = glGetUniformLocation(g_ShaderHandle, "ProjMtx"); |
||||
g_AttribLocationPosition = glGetAttribLocation(g_ShaderHandle, "Position"); |
||||
g_AttribLocationUV = glGetAttribLocation(g_ShaderHandle, "UV"); |
||||
g_AttribLocationColor = glGetAttribLocation(g_ShaderHandle, "Color"); |
||||
|
||||
glGenBuffers(1, &g_VboHandle); |
||||
glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle); |
||||
glBufferData(GL_ARRAY_BUFFER, g_VboMaxSize, NULL, GL_DYNAMIC_DRAW); |
||||
|
||||
glGenVertexArrays(1, &g_VaoHandle); |
||||
glBindVertexArray(g_VaoHandle); |
||||
glBindBuffer(GL_ARRAY_BUFFER, g_VboHandle); |
||||
glEnableVertexAttribArray(g_AttribLocationPosition); |
||||
glEnableVertexAttribArray(g_AttribLocationUV); |
||||
glEnableVertexAttribArray(g_AttribLocationColor); |
||||
|
||||
#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT)) |
||||
glVertexAttribPointer(g_AttribLocationPosition, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, pos)); |
||||
glVertexAttribPointer(g_AttribLocationUV, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, uv)); |
||||
glVertexAttribPointer(g_AttribLocationColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (GLvoid*)OFFSETOF(ImDrawVert, col)); |
||||
#undef OFFSETOF |
||||
glBindVertexArray(0); |
||||
glBindBuffer(GL_ARRAY_BUFFER, 0); |
||||
|
||||
ImGui_ImplGlfwGL3_CreateFontsTexture(); |
||||
|
||||
return true; |
||||
} |
||||
|
||||
bool ImGui_ImplGlfwGL3_Init(GLFWwindow* window, bool install_callbacks) |
||||
{ |
||||
g_Window = window; |
||||
|
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
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_RightArrow] = GLFW_KEY_RIGHT; |
||||
io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP; |
||||
io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN; |
||||
io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME; |
||||
io.KeyMap[ImGuiKey_End] = GLFW_KEY_END; |
||||
io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE; |
||||
io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE; |
||||
io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER; |
||||
io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE; |
||||
io.KeyMap[ImGuiKey_A] = GLFW_KEY_A; |
||||
io.KeyMap[ImGuiKey_C] = GLFW_KEY_C; |
||||
io.KeyMap[ImGuiKey_V] = GLFW_KEY_V; |
||||
io.KeyMap[ImGuiKey_X] = GLFW_KEY_X; |
||||
io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y; |
||||
io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z; |
||||
|
||||
io.RenderDrawListsFn = ImGui_ImplGlfwGL3_RenderDrawLists; |
||||
io.SetClipboardTextFn = ImGui_ImplGlfwGL3_SetClipboardText; |
||||
io.GetClipboardTextFn = ImGui_ImplGlfwGL3_GetClipboardText; |
||||
#ifdef _MSC_VER |
||||
io.ImeWindowHandle = glfwGetWin32Window(g_Window); |
||||
#endif |
||||
|
||||
if (install_callbacks) |
||||
{ |
||||
glfwSetMouseButtonCallback(window, ImGui_ImplGlfwGL3_MouseButtonCallback); |
||||
glfwSetScrollCallback(window, ImGui_ImplGlfwGL3_ScrollCallback); |
||||
glfwSetKeyCallback(window, ImGui_ImplGlfwGL3_KeyCallback); |
||||
glfwSetCharCallback(window, ImGui_ImplGlfwGL3_CharCallback); |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
void ImGui_ImplGlfwGL3_Shutdown() |
||||
{ |
||||
if (g_VaoHandle) glDeleteVertexArrays(1, &g_VaoHandle); |
||||
if (g_VboHandle) glDeleteBuffers(1, &g_VboHandle); |
||||
g_VaoHandle = 0; |
||||
g_VboHandle = 0; |
||||
|
||||
glDetachShader(g_ShaderHandle, g_VertHandle); |
||||
glDeleteShader(g_VertHandle); |
||||
g_VertHandle = 0; |
||||
|
||||
glDetachShader(g_ShaderHandle, g_FragHandle); |
||||
glDeleteShader(g_FragHandle); |
||||
g_FragHandle = 0; |
||||
|
||||
glDeleteProgram(g_ShaderHandle); |
||||
g_ShaderHandle = 0; |
||||
|
||||
if (g_FontTexture) |
||||
{ |
||||
glDeleteTextures(1, &g_FontTexture); |
||||
ImGui::GetIO().Fonts->TexID = 0; |
||||
g_FontTexture = 0; |
||||
} |
||||
ImGui::Shutdown(); |
||||
} |
||||
|
||||
void ImGui_ImplGlfwGL3_NewFrame() |
||||
{ |
||||
if (!g_FontTexture) |
||||
ImGui_ImplGlfwGL3_CreateDeviceObjects(); |
||||
|
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
|
||||
// Setup display size (every frame to accommodate for window resizing)
|
||||
int w, h; |
||||
int display_w, display_h; |
||||
glfwGetWindowSize(g_Window, &w, &h); |
||||
glfwGetFramebufferSize(g_Window, &display_w, &display_h); |
||||
io.DisplaySize = ImVec2((float)display_w, (float)display_h); |
||||
|
||||
// Setup time step
|
||||
double current_time = glfwGetTime(); |
||||
io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f); |
||||
g_Time = current_time; |
||||
|
||||
// Setup inputs
|
||||
// (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
|
||||
double mouse_x, mouse_y; |
||||
glfwGetCursorPos(g_Window, &mouse_x, &mouse_y); |
||||
mouse_x *= (float)display_w / w; // Convert mouse coordinates to pixels
|
||||
mouse_y *= (float)display_h / h; |
||||
io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
|
||||
|
||||
for (int i = 0; i < 3; i++) |
||||
{ |
||||
io.MouseDown[i] = g_MousePressed[i] || glfwGetMouseButton(g_Window, i) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
|
||||
g_MousePressed[i] = false; |
||||
} |
||||
|
||||
io.MouseWheel = g_MouseWheel; |
||||
g_MouseWheel = 0.0f; |
||||
|
||||
// Start the frame
|
||||
ImGui::NewFrame(); |
||||
} |
@ -0,0 +1,20 @@ |
||||
// ImGui GLFW binding with OpenGL3 + shaders
|
||||
// https://github.com/ocornut/imgui
|
||||
|
||||
struct GLFWwindow; |
||||
|
||||
bool ImGui_ImplGlfwGL3_Init(GLFWwindow* window, bool install_callbacks); |
||||
void ImGui_ImplGlfwGL3_Shutdown(); |
||||
void ImGui_ImplGlfwGL3_NewFrame(); |
||||
|
||||
// Use if you want to reset your rendering device without losing ImGui state.
|
||||
void ImGui_ImplGlfwGL3_InvalidateDeviceObjects(); |
||||
bool ImGui_ImplGlfwGL3_CreateDeviceObjects(); |
||||
|
||||
// GLFW callbacks (installed by default if you enable 'install_callbacks' during initialization)
|
||||
// Provided here if you want to chain callbacks.
|
||||
// You can also handle inputs yourself and use those as a reference.
|
||||
void ImGui_ImplGlfwGL3_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods); |
||||
void ImGui_ImplGlfwGL3_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset); |
||||
void ImGui_ImplGlFwGL3_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); |
||||
void ImGui_ImplGlfwGL3_CharCallback(GLFWwindow* window, unsigned int c); |
@ -0,0 +1,252 @@ |
||||
// ImGui GLFW binding with OpenGL
|
||||
// https://github.com/ocornut/imgui
|
||||
|
||||
#include <imgui.h> |
||||
#include "imgui_impl_glfw.h" |
||||
|
||||
// GLFW
|
||||
#include <GLFW/glfw3.h> |
||||
#ifdef _MSC_VER |
||||
#undef APIENTRY |
||||
#define GLFW_EXPOSE_NATIVE_WIN32 |
||||
#define GLFW_EXPOSE_NATIVE_WGL |
||||
#include <GLFW/glfw3native.h> |
||||
#endif |
||||
|
||||
// Data
|
||||
static GLFWwindow* g_Window = NULL; |
||||
static double g_Time = 0.0f; |
||||
static bool g_MousePressed[3] = { false, false, false }; |
||||
static float g_MouseWheel = 0.0f; |
||||
static GLuint g_FontTexture = 0; |
||||
|
||||
// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
|
||||
// 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)
|
||||
static void ImGui_ImplGlfw_RenderDrawLists(ImDrawList** const cmd_lists, int cmd_lists_count) |
||||
{ |
||||
if (cmd_lists_count == 0) |
||||
return; |
||||
|
||||
// We are using the OpenGL fixed pipeline to make the example code simpler to read!
|
||||
// A probable faster way to render would be to collate all vertices from all cmd_lists into a single vertex buffer.
|
||||
// Setup render state: alpha-blending enabled, no face culling, no depth testing, scissor enabled, vertex/texcoord/color pointers.
|
||||
glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_TRANSFORM_BIT); |
||||
glEnable(GL_BLEND); |
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
||||
glDisable(GL_CULL_FACE); |
||||
glDisable(GL_DEPTH_TEST); |
||||
glEnable(GL_SCISSOR_TEST); |
||||
glEnableClientState(GL_VERTEX_ARRAY); |
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
||||
glEnableClientState(GL_COLOR_ARRAY); |
||||
glEnable(GL_TEXTURE_2D); |
||||
|
||||
// Setup orthographic projection matrix
|
||||
const float width = ImGui::GetIO().DisplaySize.x; |
||||
const float height = ImGui::GetIO().DisplaySize.y; |
||||
glMatrixMode(GL_PROJECTION); |
||||
glPushMatrix(); |
||||
glLoadIdentity(); |
||||
glOrtho(0.0f, width, height, 0.0f, -1.0f, +1.0f); |
||||
glMatrixMode(GL_MODELVIEW); |
||||
glPushMatrix(); |
||||
glLoadIdentity(); |
||||
|
||||
// Render command lists
|
||||
#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT)) |
||||
for (int n = 0; n < cmd_lists_count; n++) |
||||
{ |
||||
const ImDrawList* cmd_list = cmd_lists[n]; |
||||
const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->vtx_buffer.front(); |
||||
glVertexPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, pos))); |
||||
glTexCoordPointer(2, GL_FLOAT, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, uv))); |
||||
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ImDrawVert), (void*)(vtx_buffer + OFFSETOF(ImDrawVert, col))); |
||||
|
||||
int vtx_offset = 0; |
||||
for (size_t cmd_i = 0; cmd_i < cmd_list->commands.size(); cmd_i++) |
||||
{ |
||||
const ImDrawCmd* pcmd = &cmd_list->commands[cmd_i]; |
||||
if (pcmd->user_callback) |
||||
{ |
||||
pcmd->user_callback(cmd_list, pcmd); |
||||
} |
||||
else |
||||
{ |
||||
glBindTexture(GL_TEXTURE_2D, (GLuint)(intptr_t)pcmd->texture_id); |
||||
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)); |
||||
glDrawArrays(GL_TRIANGLES, vtx_offset, pcmd->vtx_count); |
||||
} |
||||
vtx_offset += pcmd->vtx_count; |
||||
} |
||||
} |
||||
#undef OFFSETOF |
||||
|
||||
// Restore modified state
|
||||
glDisableClientState(GL_COLOR_ARRAY); |
||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
||||
glDisableClientState(GL_VERTEX_ARRAY); |
||||
glMatrixMode(GL_MODELVIEW); |
||||
glPopMatrix(); |
||||
glMatrixMode(GL_PROJECTION); |
||||
glPopMatrix(); |
||||
glPopAttrib(); |
||||
} |
||||
|
||||
static const char* ImGui_ImplGlfw_GetClipboardText() |
||||
{ |
||||
return glfwGetClipboardString(g_Window); |
||||
} |
||||
|
||||
static void ImGui_ImplGlfw_SetClipboardText(const char* text) |
||||
{ |
||||
glfwSetClipboardString(g_Window, text); |
||||
} |
||||
|
||||
void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods) |
||||
{ |
||||
if (action == GLFW_PRESS && button >= 0 && button < 3) |
||||
g_MousePressed[button] = true; |
||||
} |
||||
|
||||
void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset) |
||||
{ |
||||
g_MouseWheel += (float)yoffset; // Use fractional mouse wheel, 1.0 unit 5 lines.
|
||||
} |
||||
|
||||
void ImGui_ImplGlFw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) |
||||
{ |
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
if (action == GLFW_PRESS) |
||||
io.KeysDown[key] = true; |
||||
if (action == GLFW_RELEASE) |
||||
io.KeysDown[key] = false; |
||||
io.KeyCtrl = (mods & GLFW_MOD_CONTROL) != 0; |
||||
io.KeyShift = (mods & GLFW_MOD_SHIFT) != 0; |
||||
} |
||||
|
||||
void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c) |
||||
{ |
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
if (c > 0 && c < 0x10000) |
||||
io.AddInputCharacter((unsigned short)c); |
||||
} |
||||
|
||||
bool ImGui_ImplGlfw_CreateDeviceObjects() |
||||
{ |
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
|
||||
// Build texture
|
||||
unsigned char* pixels; |
||||
int width, height; |
||||
io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height); |
||||
|
||||
// Create texture
|
||||
glGenTextures(1, &g_FontTexture); |
||||
glBindTexture(GL_TEXTURE_2D, g_FontTexture); |
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels); |
||||
|
||||
// Store our identifier
|
||||
io.Fonts->TexID = (void *)(intptr_t)g_FontTexture; |
||||
|
||||
return true; |
||||
} |
||||
|
||||
void ImGui_ImplGlfw_InvalidateDeviceObjects() |
||||
{ |
||||
if (g_FontTexture) |
||||
{ |
||||
glDeleteTextures(1, &g_FontTexture); |
||||
ImGui::GetIO().Fonts->TexID = 0; |
||||
g_FontTexture = 0; |
||||
} |
||||
} |
||||
|
||||
bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks) |
||||
{ |
||||
g_Window = window; |
||||
|
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
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_RightArrow] = GLFW_KEY_RIGHT; |
||||
io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP; |
||||
io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN; |
||||
io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME; |
||||
io.KeyMap[ImGuiKey_End] = GLFW_KEY_END; |
||||
io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE; |
||||
io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE; |
||||
io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER; |
||||
io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE; |
||||
io.KeyMap[ImGuiKey_A] = GLFW_KEY_A; |
||||
io.KeyMap[ImGuiKey_C] = GLFW_KEY_C; |
||||
io.KeyMap[ImGuiKey_V] = GLFW_KEY_V; |
||||
io.KeyMap[ImGuiKey_X] = GLFW_KEY_X; |
||||
io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y; |
||||
io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z; |
||||
|
||||
io.RenderDrawListsFn = ImGui_ImplGlfw_RenderDrawLists; |
||||
io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText; |
||||
io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText; |
||||
#ifdef _MSC_VER |
||||
io.ImeWindowHandle = glfwGetWin32Window(g_Window); |
||||
#endif |
||||
|
||||
if (install_callbacks) |
||||
{ |
||||
glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback); |
||||
glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback); |
||||
glfwSetKeyCallback(window, ImGui_ImplGlFw_KeyCallback); |
||||
glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback); |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
void ImGui_ImplGlfw_Shutdown() |
||||
{ |
||||
ImGui_ImplGlfw_InvalidateDeviceObjects(); |
||||
ImGui::Shutdown(); |
||||
} |
||||
|
||||
void ImGui_ImplGlfw_NewFrame() |
||||
{ |
||||
if (!g_FontTexture) |
||||
ImGui_ImplGlfw_CreateDeviceObjects(); |
||||
|
||||
ImGuiIO& io = ImGui::GetIO(); |
||||
|
||||
// Setup display size (every frame to accommodate for window resizing)
|
||||
int w, h; |
||||
int display_w, display_h; |
||||
glfwGetWindowSize(g_Window, &w, &h); |
||||
glfwGetFramebufferSize(g_Window, &display_w, &display_h); |
||||
io.DisplaySize = ImVec2((float)display_w, (float)display_h); |
||||
|
||||
// Setup time step
|
||||
double current_time = glfwGetTime(); |
||||
io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f); |
||||
g_Time = current_time; |
||||
|
||||
// Setup inputs
|
||||
// (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents())
|
||||
double mouse_x, mouse_y; |
||||
glfwGetCursorPos(g_Window, &mouse_x, &mouse_y); |
||||
mouse_x *= (float)display_w / w; // Convert mouse coordinates to pixels
|
||||
mouse_y *= (float)display_h / h; |
||||
io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
|
||||
|
||||
for (int i = 0; i < 3; i++) |
||||
{ |
||||
io.MouseDown[i] = g_MousePressed[i] || glfwGetMouseButton(g_Window, i) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
|
||||
g_MousePressed[i] = false; |
||||
} |
||||
|
||||
io.MouseWheel = g_MouseWheel; |
||||
g_MouseWheel = 0.0f; |
||||
|
||||
// Start the frame
|
||||
ImGui::NewFrame(); |
||||
} |
@ -0,0 +1,20 @@ |
||||
// ImGui GLFW binding with OpenGL
|
||||
// https://github.com/ocornut/imgui
|
||||
|
||||
struct GLFWwindow; |
||||
|
||||
bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks); |
||||
void ImGui_ImplGlfw_Shutdown(); |
||||
void ImGui_ImplGlfw_NewFrame(); |
||||
|
||||
// Use if you want to reset your rendering device without losing ImGui state.
|
||||
void ImGui_ImplGlfw_InvalidateDeviceObjects(); |
||||
bool ImGui_ImplGlfw_CreateDeviceObjects(); |
||||
|
||||
// GLFW callbacks (installed by default if you enable 'install_callbacks' during initialization)
|
||||
// Provided here if you want to chain callbacks.
|
||||
// You can also handle inputs yourself and use those as a reference.
|
||||
void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods); |
||||
void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset); |
||||
void ImGui_ImplGlFw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); |
||||
void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c); |
Loading…
Reference in New Issue