当前位置:   article > 正文

【一】使用imgui(win32+opengl+imgui)_vs2022 imgui

vs2022 imgui

1. 下载源码

git@github.com:ocornut/imgui.git

这里我下载的事 docking分支

git clone -b docking --recurse-submodules git@github.com:ocornut/imgui.git

2. 目录结构

3. 配置项目(win32+opengl)

  1. VS2022 创建一个空项目、新建imgui文件夹(放置imgui拷贝过来的文件)
  2. 拷贝源码根目录下所有头文件和源文件
  3. 拷贝backends目录下的imgui_impl_opengl3.cppimgui_impl_opengl3.h imgui_impl_opengl3_loader.h imgui_impl_win32.cpp imgui_impl_win32.h
  4. 拷贝misc目录下的cppdebuggers文件夹

  1. 设置include包含目录

$(SolutionDir)\imgui

$(SolutionDir)\imgui\backends

  1. 设置附加依赖项 opengl32.lib
  2. 添加头文件

#include "imgui.h"

#include "imgui_impl_opengl3.h"

#include "imgui_impl_win32.h"

#include <windows.h>

#include <GL/GL.h>

4. main.cpp

  1. //Windows 窗口数据
  2. struct WGL_WindowData { HDC hDC; };
  3. static HGLRC g_hRC; //opengl 句柄
  4. static WGL_WindowData g_MainWindow; //窗口句柄
  5. static int g_Width;
  6. static int g_Height;
  7. //初始化opengl
  8. bool CreateDeviceWGL(HWND hWnd, WGL_WindowData* data)
  9. {
  10. HDC hDc = ::GetDC(hWnd);
  11. PIXELFORMATDESCRIPTOR pfd = { 0 };
  12. pfd.nSize = sizeof(pfd);
  13. pfd.nVersion = 1;
  14. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  15. pfd.iPixelType = PFD_TYPE_RGBA;
  16. pfd.cColorBits = 32;
  17. const int pf = ::ChoosePixelFormat(hDc, &pfd);
  18. if (pf == 0)
  19. return false;
  20. if (::SetPixelFormat(hDc, pf, &pfd) == FALSE)
  21. return false;
  22. ::ReleaseDC(hWnd, hDc);
  23. data->hDC = ::GetDC(hWnd);
  24. if (!g_hRC)
  25. g_hRC = wglCreateContext(data->hDC);
  26. return true;
  27. }
  28. //释放opengl
  29. void CleanupDeviceWGL(HWND hWnd, WGL_WindowData* data)
  30. {
  31. wglMakeCurrent(nullptr, nullptr);
  32. ::ReleaseDC(hWnd, data->hDC);
  33. }
  34. static void Hook_Renderer_CreateWindow(ImGuiViewport* viewport)
  35. {
  36. assert(viewport->RendererUserData == nullptr);
  37. WGL_WindowData* data = IM_NEW(WGL_WindowData);
  38. CreateDeviceWGL((HWND)viewport->PlatformHandle, data);
  39. viewport->RendererUserData = data;
  40. }
  41. static void Hook_Renderer_DestroyWindow(ImGuiViewport* viewport)
  42. {
  43. if (viewport->RendererUserData != nullptr)
  44. {
  45. WGL_WindowData* data = (WGL_WindowData*)viewport->RendererUserData;
  46. CleanupDeviceWGL((HWND)viewport->PlatformHandle, data);
  47. IM_DELETE(data);
  48. viewport->RendererUserData = nullptr;
  49. }
  50. }
  51. static void Hook_Platform_RenderWindow(ImGuiViewport* viewport, void*)
  52. {
  53. // Activate the platform window DC in the OpenGL rendering context
  54. if (WGL_WindowData* data = (WGL_WindowData*)viewport->RendererUserData)
  55. wglMakeCurrent(data->hDC, g_hRC);
  56. }
  57. static void Hook_Renderer_SwapBuffers(ImGuiViewport* viewport, void*)
  58. {
  59. if (WGL_WindowData* data = (WGL_WindowData*)viewport->RendererUserData)
  60. ::SwapBuffers(data->hDC);
  61. }
  62. extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  63. //窗口处理函数(自定义、处理消息)
  64. LRESULT APIENTRY WndProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM lParam)
  65. {
  66. if (ImGui_ImplWin32_WndProcHandler(hWnd, msgID, wParam, lParam))
  67. return true;
  68. //处理消息
  69. switch (msgID)
  70. {
  71. case WM_SIZE:
  72. if (wParam != SIZE_MINIMIZED)
  73. {
  74. g_Width = LOWORD(lParam);
  75. g_Height = HIWORD(lParam);
  76. }
  77. return 0;
  78. case WM_SYSCOMMAND:
  79. if ((wParam & 0xfff0) == SC_KEYMENU) //禁用ALT应用程序菜单
  80. return 0;
  81. break;
  82. case WM_CREATE:
  83. break;
  84. case WM_DESTROY:
  85. PostQuitMessage(0);
  86. break;
  87. default:
  88. break;
  89. }
  90. //给各种消息默认处理
  91. return DefWindowProc(hWnd, msgID, wParam, lParam);
  92. }
  93. int main(int argc, char* argv[])
  94. {
  95. //注册窗口类
  96. WNDCLASS wc = { 0 };
  97. wc.style = CS_OWNDC;//窗口类的风格
  98. wc.lpfnWndProc = WndProc;//窗口处理函数
  99. wc.cbClsExtra = 0;//窗口类的附加数据buff的大小
  100. wc.cbWndExtra = 0;//窗口的附加数据buff的大小
  101. wc.hInstance = GetModuleHandle(nullptr);//当前模块实例句柄
  102. wc.hIcon = nullptr;//窗口图标句柄
  103. wc.hCursor = nullptr;//鼠标句柄
  104. wc.hbrBackground = nullptr;//绘制窗口背景的画刷句柄
  105. wc.lpszMenuName = nullptr; //窗口菜单的资源id字符串
  106. wc.lpszClassName = TEXT("mainWindow");//窗口类的名称
  107. RegisterClass(&wc);
  108. //创建窗口
  109. HWND hWnd = CreateWindowEx(0, TEXT("mainWindow"), TEXT("window"), WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, nullptr, nullptr, wc.hInstance, nullptr);
  110. //初始化OpenGL
  111. if (!CreateDeviceWGL(hWnd, &g_MainWindow))
  112. {
  113. CleanupDeviceWGL(hWnd, &g_MainWindow);
  114. ::DestroyWindow(hWnd);
  115. ::UnregisterClassW(wc.lpszClassName, wc.hInstance);
  116. return 1;
  117. }
  118. wglMakeCurrent(g_MainWindow.hDC, g_hRC);
  119. //显示窗口
  120. ShowWindow(hWnd, SW_SHOWDEFAULT);
  121. UpdateWindow(hWnd);
  122. //设置 imgui上下文
  123. IMGUI_CHECKVERSION();
  124. ImGui::CreateContext();
  125. ImGuiIO& io = ImGui::GetIO(); (void)io;
  126. io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // 键盘控制
  127. io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Gamepad
  128. io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Docking
  129. io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Multi-Viewport / Platform Windows
  130. //设置窗口风格
  131. ImGui::StyleColorsDark();
  132. //ImGuiConfigFlags_ViewportsEnable 启用后,调整WindowRounding/WindowBg 使平台窗口看起来与常规窗口相同
  133. ImGuiStyle& style = ImGui::GetStyle();
  134. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  135. {
  136. style.WindowRounding = 0.0f;
  137. style.Colors[ImGuiCol_WindowBg].w = 1.0f;
  138. }
  139. //设置平台/渲染器后端-opengl
  140. ImGui_ImplWin32_InitForOpenGL(hWnd);
  141. ImGui_ImplOpenGL3_Init();
  142. //通过hook连接Win32和GLapi。
  143. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  144. {
  145. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  146. IM_ASSERT(platform_io.Renderer_CreateWindow == nullptr);
  147. IM_ASSERT(platform_io.Renderer_DestroyWindow == nullptr);
  148. IM_ASSERT(platform_io.Renderer_SwapBuffers == nullptr);
  149. IM_ASSERT(platform_io.Platform_RenderWindow == nullptr);
  150. platform_io.Renderer_CreateWindow = Hook_Renderer_CreateWindow;
  151. platform_io.Renderer_DestroyWindow = Hook_Renderer_DestroyWindow;
  152. platform_io.Renderer_SwapBuffers = Hook_Renderer_SwapBuffers;
  153. platform_io.Platform_RenderWindow = Hook_Platform_RenderWindow;
  154. }
  155. ImVec4 clear_color = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);
  156. //消息循环
  157. bool done = false;
  158. while (!done)
  159. {
  160. MSG nMsg = { 0 };
  161. //轮询和处理消息
  162. while (::PeekMessage(&nMsg, nullptr, 0U, 0U, PM_REMOVE))
  163. {
  164. TranslateMessage(&nMsg); //翻译键盘消息
  165. DispatchMessage(&nMsg); //派发消息,将消息交给窗口处理函数
  166. if (nMsg.message == WM_QUIT)
  167. done = true;
  168. }
  169. if (done)
  170. break;
  171. //imgui
  172. ImGui_ImplOpenGL3_NewFrame();
  173. ImGui_ImplWin32_NewFrame();
  174. ImGui::NewFrame();
  175. //显示窗口
  176. static int counter = 0;
  177. ImGui::Begin("window!");
  178. ImGui::Text("helloworld.");
  179. if (ImGui::Button("Button"))
  180. counter++;
  181. ImGui::Text("counter = %d", counter);
  182. /* bool bShowDemoWindow = true;
  183. ImGui::ShowDemoWindow(&bShowDemoWindow);*/
  184. ImGui::End();
  185. //渲染
  186. ImGui::Render();
  187. glViewport(0, 0, g_Width, g_Height);
  188. glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
  189. glClear(GL_COLOR_BUFFER_BIT);
  190. ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  191. //更新渲染 windows平台
  192. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  193. {
  194. ImGui::UpdatePlatformWindows();
  195. ImGui::RenderPlatformWindowsDefault();
  196. //将OpenGL渲染上下文恢复到主窗口DC,因为平台窗口可能已经更改了它。
  197. wglMakeCurrent(g_MainWindow.hDC, g_hRC);
  198. }
  199. ::SwapBuffers(g_MainWindow.hDC);
  200. }
  201. ImGui_ImplOpenGL3_Shutdown();
  202. ImGui_ImplWin32_Shutdown();
  203. ImGui::DestroyContext();
  204. CleanupDeviceWGL(hWnd, &g_MainWindow);
  205. wglDeleteContext(g_hRC);
  206. ::DestroyWindow(hWnd);
  207. ::UnregisterClassW(wc.lpszClassName, wc.hInstance);
  208. return 0;
  209. }

完整源码 GitHub - jermydu/LearnImgui

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号