Skip to content

Commit

Permalink
add helper function for new imgui frame
Browse files Browse the repository at this point in the history
  • Loading branch information
Noy-Zini committed Nov 20, 2024
1 parent dd72548 commit 844ac13
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 66 deletions.
4 changes: 2 additions & 2 deletions common/ux-alignment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "ux-alignment.h"
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <realsense_imgui.h>
#include <vector>
#include <memory>

Expand Down Expand Up @@ -49,8 +50,7 @@ bool is_gui_aligned(GLFWwindow *win)

glfwSwapBuffers(win);

ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::PushNewFrame();

SetFocus(hwn);

Expand Down
11 changes: 4 additions & 7 deletions common/ux-window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <realsense_imgui.h>

#include "device-model.h"

Expand Down Expand Up @@ -222,9 +223,7 @@ namespace rs2
if (_use_glsl_proc) rs2::gl::shutdown_processing();

ImGui::GetIO().Fonts->ClearFonts(); // To be refactored into Viewer theme object
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
ImGui::PopNewFrame();
glfwDestroyWindow(_win);
glfwDestroyCursor(_hand_cursor);
glfwDestroyCursor(_cross_cursor);
Expand Down Expand Up @@ -671,7 +670,7 @@ namespace rs2
}

ImGui::GetIO().Fonts->ClearFonts(); // To be refactored into Viewer theme object
ImGui_ImplGlfw_Shutdown();
ImGui::PopNewFrame();
glfwDestroyWindow(_win);

glfwDestroyCursor(_hand_cursor);
Expand Down Expand Up @@ -757,9 +756,7 @@ namespace rs2
ImGui::GetIO().MouseWheel = _mouse.ui_wheel;
_mouse.ui_wheel = 0.f;

ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::PushNewFrame();
}

void ux_window::begin_viewport()
Expand Down
8 changes: 2 additions & 6 deletions examples/align-advanced/rs-align-advanced.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,14 @@ int main(int argc, char * argv[]) try
renderer.show(pip_stream);

// Using ImGui library to provide a slide controller to select the depth clipping distance
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::PushNewFrame();
render_slider({ 5.f, 0, w, h }, depth_clipping_distance);
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
ImGui::PopNewFrame();
return EXIT_SUCCESS;
}
catch (const rs2::error & e)
Expand Down
9 changes: 2 additions & 7 deletions examples/align-gl/rs-align-gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,12 @@ int main(int argc, char * argv[]) try
glDisable(GL_BLEND);

// Render the UI:
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::PushNewFrame();
render_slider({ 15.f, app.height() - 60, app.width() - 30, app.height() }, &alpha, &dir);
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
ImGui::PopNewFrame();
return EXIT_SUCCESS;
}
catch (const rs2::error & e)
Expand Down
9 changes: 2 additions & 7 deletions examples/align/rs-align.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,12 @@ int main(int argc, char * argv[]) try
glDisable(GL_BLEND);

// Render the UI:
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::PushNewFrame();
render_slider({ 15.f, app.height() - 60, app.width() - 30, app.height() }, &alpha, &dir);
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
ImGui::PopNewFrame();
return EXIT_SUCCESS;
}
catch (const rs2::error & e)
Expand Down
6 changes: 2 additions & 4 deletions examples/example-imgui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "example.hpp"
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>


//////////////////////////////
Expand Down Expand Up @@ -163,9 +164,6 @@ class hdr_widgets {
//we need slider 2 to be showen before slider 1 (otherwise slider 1 padding is covering slider 2)
void render_widgets() {

//start a new frame of ImGui
ImGui_ImplGlfw_NewFrame();

_exposure_slider_seq_2.show();
_exposure_slider_seq_1.show();
_gain_slider_seq_2.show();
Expand All @@ -183,7 +181,7 @@ class hdr_widgets {

//render the ImGui features: sliders and text
ImGui::Render();

ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}

// return a reference to frames map
Expand Down
11 changes: 2 additions & 9 deletions examples/hdr/rs-hdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,21 +150,14 @@ int main() try

//update frames in frames map in hdr_widgets
hdr_widgets.update_frames_map(infrared_frame, depth_frame, hdr_frame, hdr_seq_id, hdr_seq_size);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::PushNewFrame();
//render hdr widgets sliders and text boxes
hdr_widgets.render_widgets();

//the show method, when applied on frame map, break it to frames and upload each frame into its specific tile
app.show(hdr_widgets.get_frames_map());
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
ImGui::PopNewFrame();
return EXIT_SUCCESS;
}
catch (const rs2::error& e)
Expand Down
9 changes: 2 additions & 7 deletions examples/post-processing/rs-post-processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,7 @@ int main(int argc, char * argv[]) try
// (Not the safest way to join a thread, please wrap your threads in some RAII manner)
stopped = true;
processing_thread.join();
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
ImGui::PopNewFrame();
return EXIT_SUCCESS;
}
catch (const rs2::error & e)
Expand Down Expand Up @@ -265,9 +262,7 @@ void render_ui(float w, float h, std::vector<filter_options>& filters)
| ImGuiWindowFlags_NoResize
| ImGuiWindowFlags_NoMove;

ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::PushNewFrame();
ImGui::SetNextWindowSize({ w, h });
ImGui::GetStyle().Colors[ImGuiCol_FrameBg] = ImVec4(0.2f, 0.2f, 0.2f, 1.0f);
ImGui::GetStyle().Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.3f, 0.3f, 0.3f, 1.0f);
Expand Down
8 changes: 2 additions & 6 deletions examples/record-playback/rs-record-playback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ int main(int argc, char * argv[]) try
| ImGuiWindowFlags_NoResize
| ImGuiWindowFlags_NoMove;

ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::PushNewFrame();
ImGui::SetNextWindowPos(ImVec2(0, 0));
ImGui::SetNextWindowSize({ app.width(), app.height() });
ImGui::Begin("app", nullptr, flags);
Expand Down Expand Up @@ -208,9 +206,7 @@ int main(int argc, char * argv[]) try
depth_image.render(depth, { app.width() * 0.25f, app.height() * 0.25f, app.width() * 0.5f, app.height() * 0.75f });
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
ImGui::PopNewFrame();
return EXIT_SUCCESS;
}
catch (const rs2::error & e)
Expand Down
24 changes: 21 additions & 3 deletions third-party/imgui/realsense_imgui.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* License: Apache 2.0. See LICENSE file in root directory. */
/* Copyright(c) 2024 Intel Corporation. All Rights Reserved. */

//Overloading imgui functions to accommodate realsense GUI
//Overloading and adding on imgui functions to accommodate realsense GUI
#include "realsense_imgui.h"
#include "imgui_impl_glfw.h"
#include <imgui_impl_opengl3.h>

bool ImGui::SliderIntWithSteps(const char* label, int* v, int v_min, int v_max, int v_step)
{
Expand Down Expand Up @@ -69,7 +71,7 @@ bool Items_SingleStringGetter(void* data, int idx, const char** out_text)
return true;
}

IMGUI_API bool ImGui::CustomComboBox(const char* label, int* current_item, const char* const items[], int items_count)
bool ImGui::CustomComboBox(const char* label, int* current_item, const char* const items[], int items_count)
{
bool value_changed = false;

Expand Down Expand Up @@ -116,7 +118,7 @@ IMGUI_API bool ImGui::CustomComboBox(const char* label, int* current_item, const
return value_changed;
}

IMGUI_API bool ImGui::SliderBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v_min, float v_max, float power, int decimal_precision, ImGuiSliderFlags flags, bool render_bg)
bool ImGui::SliderBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v_min, float v_max, float power, int decimal_precision, ImGuiSliderFlags flags, bool render_bg)
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = GetCurrentWindow();
Expand Down Expand Up @@ -358,3 +360,19 @@ bool ImGui::SliderIntTofloat(const char* label, int* v, int v_min, int v_max, co
*v = (int)v_f;
return value_changed;
}

void ImGui::PushNewFrame()
{
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
}

void ImGui::PopNewFrame()
{
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
2 changes: 2 additions & 0 deletions third-party/imgui/realsense_imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace ImGui
IMGUI_API bool SliderBehavior(const ImRect& frame_bb, ImGuiID id, float* v, float v_min, float v_max, float power, int decimal_precision, ImGuiSliderFlags flags, bool render_bg);
IMGUI_API bool VSliderFloat(const char* label, const ImVec2& size, float* v, float v_min, float v_max, const char* display_format, float power, bool render_bg);
IMGUI_API bool SliderIntTofloat(const char* label, int* v, int v_min, int v_max, const char* display_format = "%.0f");
IMGUI_API void PushNewFrame();
IMGUI_API void PopNewFrame();

}
#define CONCAT_(x,y) x##y
Expand Down
2 changes: 0 additions & 2 deletions tools/realsense-viewer/realsense-viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,6 @@ int main(int argc, const char** argv) try
}
}

ImGui::SetNextWindowContentSize(ImVec2(windows_width, 0.0f));

auto pos = ImGui::GetCursorScreenPos();
auto h = ImGui::GetWindowHeight();
if (h > pos.y - viewer_model.panel_y)
Expand Down
9 changes: 3 additions & 6 deletions tools/rosbag-inspector/rs-rosbag-inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <imgui_impl_glfw.h>
#include "imgui_impl_opengl3.h"
#include <imgui_internal.h>
#include <realsense_imgui.h>
#ifdef _MSC_VER
#ifndef NOMINMAX
#define NOMINMAX
Expand Down Expand Up @@ -69,9 +70,7 @@ class gui_window
glfwGetWindowSize(_window, &_w, &_h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::PushNewFrame();
_first_frame = false;
return res;
}
Expand Down Expand Up @@ -442,9 +441,7 @@ int main(int argc, const char** argv) try
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
// Cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
ImGui::PopNewFrame();
glfwTerminate();
return 0;
}
Expand Down

0 comments on commit 844ac13

Please sign in to comment.