imgui是一个轻量的界面库,其是由纯C++编写,opengl渲染,其中编译方式已经语法网上资料丰富,此处不再讨论,记录一下Osg与imgui的结合。
经过简单的学习可知,需要使用相对于的Imgui关键代码,其中在osg的结合中使用如下代码。imgui开头的文件
此代码由gitee下载
自定义一个类,继承osgGA::GUIEventHandler,在构造函数中对Imgui进行初始化,创建Imgui上下文,再在创建两个回调
//
/ setPreDrawCallback、setPostDrawCallback、
setFinalDrawCallback相机在绘制中有三次的回调,回之前,绘制后,最后,在RenderStage::draw函数中被调用。
/
//
我们在绘制前调用imgui自绘制
//ImGui开始自绘
ImGui_ImplOpenGL3_NewFrame();
ImGuiIO& io = ImGui::GetIO();
osg::Viewport* viewport = renderInfo.getCurrentCamera()->getViewport();
io.DisplaySize = ImVec2(viewport->width(), viewport->height());
double currentTime = renderInfo.getView()->getFrameStamp()->getSimulationTime();
io.DeltaTime = currentTime - time_ + 0.0000001;
time_ = currentTime;
for (int i = 0; i < 3; i++)
{
io.MouseDown[i] = mousePressed_[i];
}
io.MouseWheel = mouseWheel_;
mouseWheel_ = 0.0f;
ImGui::NewFrame();,
将窗口创建处理,在绘制后通过
drawUi();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
绘制到界面。
完整的Imgui绘制流程
// Setup Dear ImGui context
IMGUI_CHECKVERSION();//检查版本
ImGui::CreateContext();//创建上下文
ImGuiIO& io = ImGui::GetIO();
(void)io;
ImGui::StyleColorsDark();//设置样式类型
//ImGui::StyleColorsClassic();
// Setup Platform/Renderer backends
ImGui_ImplWin32_Init(hwnd);//初始化imgui
// 开始绘制
{
// 启动IMGUI自绘
ImGui_ImplDX9_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
static float f = 0.0f;
static int counter = 0;
static char sz[256] = { 0 };
ImGui::Begin("LyShark 辅助GUI主菜单");
ImGui::Text("这是一段测试字符串");
ImGui::Checkbox("弹出子窗口", &show_another_window);
ImGui::SliderFloat("浮点条", &f, 0.0f, 1.0f);
ImGui::InputText("输入内容", sz, 256, 0, 0, 0);
if (ImGui::Button("点我触发"))
counter++;
ImGui::SameLine();
ImGui::Text("触发次数 = %d", counter);
ImGui::Text("当前FPS: %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
ImGui::End();
if (show_another_window)
{
ImGui::Begin("我是子窗体", &show_another_window);
ImGui::Text(" 您好,LyShark !");
if (ImGui::Button("关闭窗体"))
show_another_window = false;
ImGui::End();
}
ImGui::EndFrame();
}
ImGui::Render();//根据数据渲染
ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
g_pd3dDevice->EndScene();