77#include " os.h"
88#include < imgui_internal.h>
99#include < realsense_imgui.h>
10+ #ifdef BUILD_VIEWPORT_GRID_OVERLAY
11+ #include < iomanip>
12+ #include < sstream>
13+ #endif
1014
1115struct attribute
1216{
@@ -28,6 +32,23 @@ namespace rs2
2832 configurations::viewer::show_stream_details, false );
2933 show_safety_zones_2d = config_file::instance ().get_or_default (
3034 configurations::viewer::show_safety_zones_2d, true );
35+ #ifdef BUILD_VIEWPORT_GRID_OVERLAY
36+ {
37+ namespace cfg = configurations::viewer::viewport_grid;
38+ auto & cf = config_file::instance ();
39+
40+ auto valid_lines = []( int v ) { return ( v >= 1 && v <= 5 ) ? v : 1 ; };
41+ grid_h_lines = valid_lines ( cf.get_nested <int >( cfg::horizontal_lines, 1 ) );
42+ grid_v_lines = valid_lines ( cf.get_nested <int >( cfg::vertical_lines, 1 ) );
43+ int w = cf.get_nested <int >( cfg::line_width, 1 );
44+ if ( w >= 1 ) grid_line_width = w;
45+ int r = cf.get_nested <int >( cfg::line_color_r, 255 );
46+ int g = cf.get_nested <int >( cfg::line_color_g, 255 );
47+ int b = cf.get_nested <int >( cfg::line_color_b, 255 );
48+ if ( r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255 )
49+ { grid_color_r = r; grid_color_g = g; grid_color_b = b; }
50+ }
51+ #endif
3152 }
3253
3354 std::shared_ptr<texture_buffer> stream_model::upload_frame (frame&& f)
@@ -131,6 +152,34 @@ namespace rs2
131152 glPopAttrib ();
132153 }
133154
155+ #ifdef BUILD_VIEWPORT_GRID_OVERLAY
156+ static void draw_2d_grid (const rect& r, int h_lines, int v_lines, int line_width,
157+ int cr, int cg, int cb)
158+ {
159+ glPushAttrib (GL_ENABLE_BIT | GL_LINE_BIT | GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT );
160+ glLineWidth (static_cast <GLfloat>(line_width));
161+ glEnable (GL_BLEND );
162+ glBlendFunc (GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );
163+ glColor4f (cr / 255 .f , cg / 255 .f , cb / 255 .f , 0 .7f );
164+ glBegin (GL_LINES );
165+ for (int c = 1 ; c <= v_lines; ++c)
166+ {
167+ float x = r.x + r.w * c / static_cast <float >(v_lines + 1 );
168+ glVertex2f (x, r.y );
169+ glVertex2f (x, r.y + r.h );
170+ }
171+ for (int row = 1 ; row <= h_lines; ++row)
172+ {
173+ float y = r.y + r.h * row / static_cast <float >(h_lines + 1 );
174+ glVertex2f (r.x , y);
175+ glVertex2f (r.x + r.w , y);
176+ }
177+ glEnd ();
178+ glPopAttrib ();
179+ }
180+
181+ #endif
182+
134183 bool stream_model::is_stream_visible () const
135184 {
136185 if (dev &&
@@ -413,6 +462,9 @@ namespace rs2
413462 if (RS2_STREAM_DEPTH == profile.stream_type ()) ++num_of_buttons; // Color map ruler button
414463 if (RS2_FORMAT_MOTION_XYZ32F == profile.format ()) ++num_of_buttons; // Motion graph button
415464 if (RS2_STREAM_OCCUPANCY == profile.stream_type () && _normalized_zoom.w == 1 ) ++num_of_buttons; // Safety zones button
465+ #ifdef BUILD_VIEWPORT_GRID_OVERLAY
466+ ++num_of_buttons; // Grid overlay button
467+ #endif
416468
417469 RsImGui_ScopePushFont (font);
418470 ImGui::PushStyleColor (ImGuiCol_Text, light_grey);
@@ -548,6 +600,33 @@ namespace rs2
548600 }
549601 ImGui::SameLine ();
550602
603+ #ifdef BUILD_VIEWPORT_GRID_OVERLAY
604+ label = rsutils::string::from () << textual_icons::grid << " ##Grid " << profile.unique_id ();
605+ if (show_grid)
606+ {
607+ ImGui::PushStyleColor (ImGuiCol_Text, light_blue);
608+ ImGui::PushStyleColor (ImGuiCol_TextSelectedBg, light_blue);
609+ if (ImGui::Button (label.c_str (), { 24 , top_bar_height }))
610+ {
611+ show_grid = false ;
612+ }
613+ if (ImGui::IsItemHovered ())
614+ RsImGui::CustomTooltip (" Hide grid overlay" );
615+ ImGui::PopStyleColor (2 );
616+ }
617+ else
618+ {
619+ if (ImGui::Button (label.c_str (), { 24 , top_bar_height }))
620+ {
621+ show_grid = true ;
622+ }
623+ if (ImGui::IsItemHovered ())
624+ RsImGui::CustomTooltip (" Show grid overlay" );
625+ }
626+ ImGui::SameLine ();
627+ #endif
628+
629+
551630 if (RS2_STREAM_DEPTH == profile.stream_type ())
552631 {
553632 label = rsutils::string::from () << textual_icons::bar_chart << " ##Color map" ;
@@ -2036,6 +2115,13 @@ namespace rs2
20362115 }
20372116
20382117 update_ae_roi_rect (stream_rect, g, error_message);
2118+
2119+ #ifdef BUILD_VIEWPORT_GRID_OVERLAY
2120+ if (show_grid)
2121+ draw_2d_grid (stream_rect, grid_h_lines, grid_v_lines, grid_line_width,
2122+ grid_color_r, grid_color_g, grid_color_b);
2123+ #endif
2124+
20392125 }
20402126 texture->show_preview (stream_rect, _normalized_zoom);
20412127
0 commit comments