imgui和osg相结合

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();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值