Dear imgui & igl

2020-07-12  本文已影响0人  坚果jimbowhy
103_Events.cpp 106-ViewMenu.cpp imgui v1.77 example_glfw_opengl2

Introduction

Libigl 是一个 C++ 几何处理库,用于产品三维原型的开发,或科研使用。

IMGUI - Immediate Mode Graphical User interface,简称 Dear imgui 是 github 上 star 最多的 GUI 框架,Blizzard, Google, Nvidia, Ubisoft 等赞助,易学,易用,好用,功能也不弱,能够胜任绝大部分的桌面应用程序开发,甚至商业程序。

对于游戏开发者来说,在开发过程中,加入 UI 的支持是不可或缺的一环,不过想要自己动手敲代码实现 UI 实属一件难事,后来 ImGUI 诞生为开发者们带来直接拿来用般的便利。

GeeXLab 是基于 imgui 开发的一款界面原型编辑器:

GeeXLab is a cross-platform proto-engine. It can be used for 2D/3D programming, game development, creative coding or prototyping. GeeXLab is based on Lua, Python, OpenGL, Vulkan and Direct3D 12.

在 imgui.cpp 提供的文档中,提到此框架的目标:

 MISSION STATEMENT
 =================

 - Easy to use to create code-driven and data-driven tools.
 - Easy to use to create ad hoc short-lived tools and long-lived, more elaborate tools.
 - Easy to hack and improve.
 - Minimize screen real-estate usage.
 - Minimize setup and maintenance.
 - Minimize state storage on user side.
 - Portable, minimize dependencies, run on target (consoles, phones, etc.).
 - Efficient runtime and memory consumption (NB- we do allocate when "growing" content e.g. creating a window,.
   opening a tree node for the first time, etc. but a typical frame should not allocate anything).

 Designed for developers and content-creators, not the typical end-user! Some of the weaknesses includes:
 - Doesn't look fancy, doesn't animate.
 - Limited layout features, intricate layouts are typically crafted in code.

大多数使用ImGUI风格的程序员发现,使用 ImGUI 来创建用户界面比使用传统的保留模式图形用户界面(GUI)要容易得多。而且性能会得到显著地提高。

典型的面向对象的 GUI 框架是一个保留模式系统,在该系统中,你基本上创建了一个 GUI 框架控件(widget)的“场景图”(窗口、网格、滑块、按钮、复选框等等)。你将你的数据复制到这些控件中,等待事件(event)或回调(callback)在控件被编辑时接收到通知。然后查询控件的新值并将其复制回你的数据中。

这种模式几乎应用于所有的 GUI 系统中,Windows、WFP、HTML DOM、Apple UIKit、Qt,你能叫出名字的 99% 的 GUI 框架都属于保留模式的,面向对象的,“场景图”式的GUI。

这种模式的GUI存在的问题是:

与此相反,ImGUI中没有对象,也几乎没有状态。大多数ImGUI的简单做法是像下面这样调用函数:

if (ImGUI::Button("Click Me")) {
  IWasClickedSoDoSomething();
}
// draw slider
ImGUI::SliderFloat("Speed:" &someInstance.speed, 0.0f, 100.0f);

这里的 Button 和 Slider 做了两件事:

所以,这样做有如下优点:

下面两点可能是这样做的缺点:

示例

请按 VCpkg 开源库管理工具 配置 VCpkg 模块管理工具,安装:

vcpkg install opengl:x64-mingw
vcpkg install glew:x64-mingw
vcpkg install glfw3:x64-mingw
vcpkg install eigen:x64-mingw
vcpkg install imgui:x64-mingw
vcpkg install libigl:x64-mingw

由于模块更新不同步,libigl 中的头文件会需要相应的修改,例如 imgui 中提示的符号更新:

字体部分另行下载;

另外,比较怪异的是 libigl 的示例也依赖了 imgui 中示例的文件,需要给编译器设置好,否则找不到 ImGui_ImplGlfw_InitForOpenGL:

官方提供的示例,编译脚本写得过于复杂,编译前还要判断是否要下载模型文件,如果网络不好,很空间就失败了:

https://github.com/libigl/libigl-tutorial-data

libigl、eigen 这些库虽然可以直接使用头文件而不必事先编译,但是对于多例程的编译过程来说,使用预先编译好的静态库会大大提高编译效率。

cmake ../ -DCMAKE_BUILD_TYPE=Release\
      -DLIBIGL_USE_STATIC_LIBRARY=ON\
      -DCMAKE_INSTALL_PREFIX=/path/to/custom/installation

代码见 libigl 的 tutorial 目录。

上一篇 下一篇

猜你喜欢

热点阅读