Skip to content

Commit

Permalink
[render] first imgui demo
Browse files Browse the repository at this point in the history
  • Loading branch information
alemuntoni committed Jan 9, 2025
1 parent 5ff2d13 commit 194adc8
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 10 deletions.
30 changes: 21 additions & 9 deletions examples/render/910-viewer-imgui/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,40 @@

#include "get_drawable_mesh.h"

#include <vclib/imgui/imgui_drawer.h>
#include <vclib/render/drawers/viewer_drawer.h>
#include <vclib/render/canvas.h>
#include <vclib/glfw/window_manager.h>
#include <vclib/render/renderer.h>

#include <imgui.h>
#include <vclib/glfw_imgui/viewer_window_imgui.h>

class ImguiDemo : public vcl::glfw::ViewerWindowImgui
template<typename DerivedRenderer>
class DemoImguiDrawer : public vcl::imgui::ImguiDrawer<DerivedRenderer>
{
using ParentDrawer = vcl::imgui::ImguiDrawer<DerivedRenderer>;

public:
ImguiDemo(const std::string& windowTitle) : ViewerWindowImgui(windowTitle)
{
}
using ParentDrawer::ParentDrawer;

void draw() override
virtual void onDraw(uint viewId) override
{
// draw the scene
ParentDrawer::onDraw(viewId);

// imgui demo window
ImGui::ShowDemoWindow();

// draw the scene
ViewerWindowImgui::draw();
}
};

int main(int argc, char** argv)
{
using ImguiDemo = vcl::Renderer<
vcl::glfw::WindowManager,
vcl::Canvas,
DemoImguiDrawer,
vcl::ViewerDrawer>;

ImguiDemo tw("Viewer GLFW");

// load and set up a drawable mesh
Expand Down
2 changes: 1 addition & 1 deletion examples/render/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ if (TARGET vclib-processing)
endif()

if(TARGET vclib-3rd-imgui AND TARGET vclib-3rd-glfw)
#add_subdirectory(910-viewer-imgui)
add_subdirectory(910-viewer-imgui)
endif()

add_subdirectory(999-misc)
Expand Down
119 changes: 119 additions & 0 deletions vclib/render/include/vclib/imgui/imgui_drawer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*****************************************************************************
* VCLib *
* Visual Computing Library *
* *
* Copyright(C) 2021-2024 *
* Visual Computing Lab *
* ISTI - Italian National Research Council *
* *
* All rights reserved. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the Mozilla Public License Version 2.0 as published *
* by the Mozilla Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* Mozilla Public License Version 2.0 *
* (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
****************************************************************************/

#ifndef IMGUI_DRAWER_H
#define IMGUI_DRAWER_H

#include <vclib/render/drawers/event_drawer.h>
#include <vclib/render/window_managers.h>

// Include the render backand imgui implementation
#ifdef VCLIB_RENDER_BACKEND_OPENGL2
#include <imgui_impl_opengl2.h>
#elif defined(VCLIB_RENDER_BACKEND_BGFX)
#include <vclib/bgfx_imgui/imgui_impl_bgfx.h>
#endif

// Include the event backend imgui implementations
#ifdef VCLIB_WITH_GLFW
#include <imgui_impl_glfw.h>
#endif

namespace vcl::imgui {

template<typename DerivedRenderer>
class ImguiDrawer : public EventDrawer<DerivedRenderer>
{
protected:
using DRT = DerivedRenderer;

public:
ImguiDrawer()
{
static_assert(
DRT::WINDOW_MANAGER_ID == WindowManagerId::GLFW_WINDOW,
"ImGuiDrawer supports only GLFW window manager.");
}

ImguiDrawer(uint, uint) : ImguiDrawer() {}

virtual void onInit(uint viewId)
{
// setup ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
// Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
// Enable Gamepad Controls
// io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
// setup ImGui style
ImGui::StyleColorsDark();

if constexpr (DRT::WINDOW_MANAGER_ID == WindowManagerId::GLFW_WINDOW) {
GLFWwindow* mWindow =
reinterpret_cast<GLFWwindow*>(DRT::DRW::windowPtr(derived()));
// setup platform/renderer backends (GLFW and ImGui)
#ifdef VCLIB_RENDER_BACKEND_OPENGL2
ImGui_ImplGlfw_InitForOpenGL(mWindow, true);
ImGui_ImplOpenGL2_Init();
#elif defined(VCLIB_RENDER_BACKEND_BGFX)
ImGui_ImplGlfw_InitForOther(mWindow, true);
ImGui_ImplBgfx_Init();
#endif
}
}

virtual void onDraw(uint)
{
// imgui frame
#ifdef VCLIB_RENDER_BACKEND_OPENGL2
ImGui_ImplOpenGL2_NewFrame();
#elif defined(VCLIB_RENDER_BACKEND_BGFX)
ImGui_ImplBgfx_NewFrame();
#endif
if constexpr (DRT::WINDOW_MANAGER_ID == WindowManagerId::GLFW_WINDOW) {
ImGui_ImplGlfw_NewFrame();
}
ImGui::NewFrame();
}

virtual void onPostDraw()
{
// imgui rendering
ImGui::Render();
#ifdef VCLIB_RENDER_BACKEND_OPENGL2
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData());
#elif defined(VCLIB_RENDER_BACKEND_BGFX)
ImGui_ImplBgfx_RenderDrawData(ImGui::GetDrawData());
#endif
}

private:
auto* derived() { return static_cast<DRT*>(this); }

const auto* derived() const { return static_cast<const DRT*>(this); }
};

} // namespace vcl::imgui

#endif // IMGUI_DRAWER_H

0 comments on commit 194adc8

Please sign in to comment.