From 86d7c06f1ebca9a3a0ea35ff3c01b308e7a892c2 Mon Sep 17 00:00:00 2001 From: mwlasiuk Date: Fri, 30 Oct 2026 20:13:22 +0100 Subject: [PATCH] Add more colormaps --- .../RendererShaders.h | 4 +- .../TrajectoryViewer.cpp | 2 +- .../TrajectoryViewerShaders.h | 2 +- .../multi_view_tls_registration_gui.cpp | 17 ++- core/include/Core/raylib_render.hpp | 17 ++- core/src/raylib_render.cpp | 3 + core/src/raylib_render_shaders.hpp | 9 +- .../include/RaylibWidgets/Shaders.h | 136 +++++++++++++++++- 8 files changed, 169 insertions(+), 21 deletions(-) diff --git a/apps/camera_lidar_calibration/RendererShaders.h b/apps/camera_lidar_calibration/RendererShaders.h index 69e077451..f0ac0ff9c 100644 --- a/apps/camera_lidar_calibration/RendererShaders.h +++ b/apps/camera_lidar_calibration/RendererShaders.h @@ -98,7 +98,7 @@ uniform float maxDist; uniform float opacity; uniform sampler2D imageTex; out vec4 finalColor; -)") + raylib_widgets::kJetColormapGLSL + +)") + raylib_widgets::kColormapsGLSL + R"( void main() { if (colorMode == 3) { @@ -213,7 +213,7 @@ uniform vec2 depthRange; uniform float opacity; uniform int colorMode; out vec4 finalColor; -)") + raylib_widgets::kJetColormapGLSL + +)") + raylib_widgets::kColormapsGLSL + R"( void main() { if (fragDepth < depthRange.x || fragDepth > depthRange.y) discard; diff --git a/apps/camera_lidar_trajectory_viewer/TrajectoryViewer.cpp b/apps/camera_lidar_trajectory_viewer/TrajectoryViewer.cpp index e9f532553..319f7924e 100644 --- a/apps/camera_lidar_trajectory_viewer/TrajectoryViewer.cpp +++ b/apps/camera_lidar_trajectory_viewer/TrajectoryViewer.cpp @@ -1318,7 +1318,7 @@ static void loadCloud(AppState& s) } //! Small CPU jet colormap approximation, matching the GLSL one used by the -//! GPU point renderer's Intensity color mode (raylib_widgets::kJetColormapGLSL) +//! GPU point renderer's Intensity color mode (raylib_widgets::kColormapsGLSL) //! closely enough for a visual reference image. Returns BGR (OpenCV order). static cv::Vec3b jetColorBGR(float t) { diff --git a/apps/camera_lidar_trajectory_viewer/TrajectoryViewerShaders.h b/apps/camera_lidar_trajectory_viewer/TrajectoryViewerShaders.h index f0cace564..da122c628 100644 --- a/apps/camera_lidar_trajectory_viewer/TrajectoryViewerShaders.h +++ b/apps/camera_lidar_trajectory_viewer/TrajectoryViewerShaders.h @@ -70,7 +70,7 @@ uniform int colorMode; uniform int selectedCamera; // -1 = show all, else keep only points from this image uniform float depthColorMax; // m: colorMode 4 maps 0..this onto the jet colormap out vec4 finalColor; -)") + raylib_widgets::kJetColormapGLSL + +)") + raylib_widgets::kColormapsGLSL + R"( vec3 hsv2rgb(vec3 c) { vec4 K = vec4(1.0, 2.0/3.0, 1.0/3.0, 3.0); diff --git a/apps/multi_view_tls_registration/multi_view_tls_registration_gui.cpp b/apps/multi_view_tls_registration/multi_view_tls_registration_gui.cpp index 49ec9c1be..40dff10b7 100644 --- a/apps/multi_view_tls_registration/multi_view_tls_registration_gui.cpp +++ b/apps/multi_view_tls_registration/multi_view_tls_registration_gui.cpp @@ -119,6 +119,7 @@ #include #include #include +#include #include #ifdef _WIN32 @@ -1084,11 +1085,12 @@ int index_end = 0; ColorScheme csPointCloud = CS_GRAD_INTENS; ColorScheme csTrajectory = CS_SOLID; +int pointCloudColormap = 0; // New (not in the original GLUT app): CS_GRAD_INTENS/CS_GRAD_ELEV/CS_GRAD_DIST // were declared in the original's ColorScheme enum but never actually wired // to a menu item or the renderer -- this hooks them up to scan_renderer's -// per-point jet-colormap shader modes (see Core/raylib_render.hpp's +// per-point colormap shader modes (see Core/raylib_render.hpp's // ScanColorMode), alongside the two modes (Solid/Random) the original did // implement. ScanColorMode scanColorModeFromScheme(ColorScheme cs) @@ -6278,21 +6280,26 @@ void display() // Gradient modes -- declared in ColorScheme since the // original, but never actually wired to a menu item or // the renderer there; hooked up here to - // scan_renderer's per-point jet-colormap shader. + // scan_renderer's per-point colormap shader. if (ImGui::MenuItem("> By intensity (gradient)", nullptr, (csPointCloud == CS_GRAD_INTENS))) csPointCloud = CS_GRAD_INTENS; if (ImGui::IsItemHovered()) - ImGui::SetTooltip("Per-point jet colormap from LAS/LAZ intensity"); + ImGui::SetTooltip("Per-point colormap from LAS/LAZ intensity"); if (ImGui::MenuItem("> By height (gradient)", nullptr, (csPointCloud == CS_GRAD_ELEV))) csPointCloud = CS_GRAD_ELEV; if (ImGui::IsItemHovered()) - ImGui::SetTooltip("Per-point jet colormap from world Z, over the session's [z_min, z_max]"); + ImGui::SetTooltip("Per-point colormap from world Z, over the session's [z_min, z_max]"); if (ImGui::MenuItem("> By distance (gradient)", nullptr, (csPointCloud == CS_GRAD_DIST))) csPointCloud = CS_GRAD_DIST; if (ImGui::IsItemHovered()) - ImGui::SetTooltip("Per-point jet colormap from distance to the rotation center"); + ImGui::SetTooltip("Per-point colormap from distance to the rotation center"); + + ImGui::SetNextItemWidth(ImGuiNumberWidth); + if (ImGui::Combo( + "Colormap", &pointCloudColormap, raylib_widgets::kColormapNames, IM_ARRAYSIZE(raylib_widgets::kColormapNames))) + scan_renderer.setColormap(pointCloudColormap); ImGui::EndMenu(); } diff --git a/core/include/Core/raylib_render.hpp b/core/include/Core/raylib_render.hpp index 8e2da0beb..bb1ed431c 100644 --- a/core/include/Core/raylib_render.hpp +++ b/core/include/Core/raylib_render.hpp @@ -38,16 +38,16 @@ // ============================================================================ // ScanRenderer -- GPU (rlgl-based) rendering of Core::PointCloud scans. // ============================================================================ -// Per-point GPU color modes for ScanRenderer::draw() -- jet-colormap +// Per-point GPU color modes for ScanRenderer::draw() -- colormap // gradients computed in the shader from data already on the GPU (per-point // intensity, or world-space position for Elevation/Distance), rather than // per-scan flat colors. enum class ScanColorMode { Flat, // pc.render_color, uniform per scan (the original app's only mode) - Intensity, // jet colormap by normalized LAS/LAZ intensity - Elevation, // jet colormap by world-space Z, normalized over [elevationMin, elevationMax] - Distance, // jet colormap by distance from distanceCenter, normalized over [0, distanceMax] + Intensity, // colormap by normalized LAS/LAZ intensity + Elevation, // colormap by world-space Z, normalized over [elevationMin, elevationMax] + Distance, // colormap by distance from distanceCenter, normalized over [0, distanceMax] FlatIntensity, // pc.render_color shaded by normalized intensity (keeps per-scan colors distinguishable) }; @@ -85,7 +85,7 @@ class ScanRenderer void syncPoses(const std::vector& pointClouds); // colorMode switches every scan from its flat render_color to a - // per-point jet-colormap gradient (see ScanColorMode). Intensity is + // per-point colormap gradient (see ScanColorMode). Intensity is // normalized per scan at rebuild() time (raw intensity range varies by // sensor); Elevation/Distance are normalized using the elevationMin/Max // and distanceCenter/distanceMax parameters, which the caller computes @@ -134,6 +134,11 @@ class ScanRenderer static constexpr int kMaxClipPlanes = 6; void setClipPlanes(const Eigen::Vector4d* planes, int count); + void setColormap(int colormap) + { + colormap_ = colormap; + } + // Number of glDrawArrays calls draw() issued the last time it ran (one // per visible scan) -- raylib/rlgl don't expose a draw-call counter for // custom, non-batched GL calls like these (rlgl's own internal @@ -303,6 +308,7 @@ class ScanRenderer int locPointSize_ = -1; int locColor_ = -1; int locColorMode_ = -1; + int locColormap_ = -1; int locElevMin_ = -1; int locElevMax_ = -1; int locDistCenter_ = -1; @@ -315,6 +321,7 @@ class ScanRenderer int locClipPlanes_ = -1; float clipPlanes_[kMaxClipPlanes * 4] = {}; int clipPlaneCount_ = 0; + int colormap_ = 0; mutable int lastDrawCallCount_ = 0; mutable int lastVertexCount_ = 0; }; diff --git a/core/src/raylib_render.cpp b/core/src/raylib_render.cpp index 7e3746f20..56664f100 100644 --- a/core/src/raylib_render.cpp +++ b/core/src/raylib_render.cpp @@ -53,6 +53,7 @@ void ScanRenderer::init() locPointSize_ = rlGetLocationUniform(shader_.id, "pointSize"); locColor_ = rlGetLocationUniform(shader_.id, "pointColor"); locColorMode_ = rlGetLocationUniform(shader_.id, "colorMode"); + locColormap_ = rlGetLocationUniform(shader_.id, "colormap"); locElevMin_ = rlGetLocationUniform(shader_.id, "elevMin"); locElevMax_ = rlGetLocationUniform(shader_.id, "elevMax"); locDistCenter_ = rlGetLocationUniform(shader_.id, "distCenter"); @@ -237,6 +238,7 @@ void ScanRenderer::draw( rlSetUniformMatrix(locMVP_, mvp); rlSetUniformMatrix(locModel_, MatrixIdentity()); rlSetUniform(locPointSize_, &pointSize, RL_SHADER_UNIFORM_FLOAT, 1); + rlSetUniform(locColormap_, &colormap_, RL_SHADER_UNIFORM_INT, 1); rlSetUniform(locElevMin_, &elevationMin, RL_SHADER_UNIFORM_FLOAT, 1); rlSetUniform(locElevMax_, &elevationMax, RL_SHADER_UNIFORM_FLOAT, 1); float distCenterF[3] = { static_cast(distanceCenter.x()), @@ -447,6 +449,7 @@ void ScanRenderer::drawCachedWithTransform( rlSetUniform(locPointSize_, &pointSize, RL_SHADER_UNIFORM_FLOAT, 1); rlSetUniform(locColor_, colorF, RL_SHADER_UNIFORM_VEC4, 1); rlSetUniform(locColorMode_, &colorModeInt, RL_SHADER_UNIFORM_INT, 1); + rlSetUniform(locColormap_, &colormap_, RL_SHADER_UNIFORM_INT, 1); rlSetUniform(locElevMin_, &elevationMin, RL_SHADER_UNIFORM_FLOAT, 1); rlSetUniform(locElevMax_, &elevationMax, RL_SHADER_UNIFORM_FLOAT, 1); float distCenterF[3] = { static_cast(distanceCenter.x()), diff --git a/core/src/raylib_render_shaders.hpp b/core/src/raylib_render_shaders.hpp index eb4ab81f2..b0722a732 100644 --- a/core/src/raylib_render_shaders.hpp +++ b/core/src/raylib_render_shaders.hpp @@ -47,6 +47,7 @@ void main() #version 330 uniform vec4 pointColor; uniform int colorMode; +uniform int colormap; uniform float elevMin; uniform float elevMax; uniform vec3 distCenter; @@ -67,7 +68,7 @@ uniform vec4 clipPlanes[6]; in float fragIntensity; in vec3 fragWorldPos; out vec4 finalColor; -)") + raylib_widgets::kJetColormapGLSL + +)") + raylib_widgets::kColormapsGLSL + R"( void main() { @@ -87,17 +88,17 @@ void main() if (colorMode == 1) { - finalColor = vec4(jet(fragIntensity), pointColor.a); + finalColor = vec4(applyColormap(colormap, fragIntensity), pointColor.a); } else if (colorMode == 2) { float range = max(elevMax - elevMin, 1e-6); - finalColor = vec4(jet((fragWorldPos.z - elevMin) / range), pointColor.a); + finalColor = vec4(applyColormap(colormap, (fragWorldPos.z - elevMin) / range), pointColor.a); } else if (colorMode == 3) { float d = length(fragWorldPos - distCenter); - finalColor = vec4(jet(d / max(distMax, 1e-6)), pointColor.a); + finalColor = vec4(applyColormap(colormap, d / max(distMax, 1e-6)), pointColor.a); } else if (colorMode == 4) { diff --git a/raylib_widgets/include/RaylibWidgets/Shaders.h b/raylib_widgets/include/RaylibWidgets/Shaders.h index 6708774bf..9d64ec6a8 100644 --- a/raylib_widgets/include/RaylibWidgets/Shaders.h +++ b/raylib_widgets/include/RaylibWidgets/Shaders.h @@ -9,23 +9,153 @@ namespace raylib_widgets { - // Standard "jet" colormap (blue -> cyan -> yellow -> red), t in [0,1]. Was + inline constexpr const char* kColormapNames[] = { "Jet", "Viridis", "Turbo", "Inferno", "Plasma", "Cividis", + "Magma", "Cubehelix", "Hot", "Blues", "Blue-White-Red" }; + + // Colormaps, t in [0,1]. "jet" (blue -> cyan -> yellow -> red) was // byte-for-byte duplicated in core/src/raylib_render.cpp's ScanRenderer, // apps/camera_lidar_calibration/Renderer.cpp's two point shaders, and // apps/camera_lidar_trajectory_viewer/TrajectoryViewer.cpp's point shader. // Splice into a fragment shader's source via string concatenation, e.g.: // static const std::string kMyFS = std::string(R"(#version 330 // ...uniforms/varyings... - // )") + raylib_widgets::kJetColormapGLSL + R"( + // )") + raylib_widgets::kColormapsGLSL + R"( // void main() { ... } // )"; - inline constexpr const char* kJetColormapGLSL = R"( + inline constexpr const char* kColormapsGLSL = R"( vec3 jet(float t) { t = clamp(t, 0.0, 1.0); return clamp(vec3(1.5 - abs(4.0*t - 3.0), 1.5 - abs(4.0*t - 2.0), 1.5 - abs(4.0*t - 1.0)), 0.0, 1.0); } +vec3 poly6(float t, vec3 c0, vec3 c1, vec3 c2, vec3 c3, vec3 c4, vec3 c5, vec3 c6) { + return c0 + t * (c1 + t * (c2 + t * (c3 + t * (c4 + t * (c5 + t * c6))))); +} +vec3 viridis(float t) { + return poly6(t, + vec3(0.2777273272234177, 0.005407344544966578, 0.3340998053353061), + vec3(0.1050930431085774, 1.404613529898575, 1.384590162594685), + vec3(-0.3308618287255563, 0.214847559468213, 0.09509516302823659), + vec3(-4.634230498983486, -5.799100973351585, -19.33244095627987), + vec3(6.228269936347081, 14.17993336680509, 56.69055260068105), + vec3(4.776384997670288, -13.74514537774601, -65.35303263337234), + vec3(-5.435455855934631, 4.645852612178535, 26.3124352495832)); +} +vec3 turbo(float t) { + const vec4 redLow = vec4(0.13572138, 4.61539260, -42.66032258, 132.13108234); + const vec4 greenLow = vec4(0.09140261, 2.19418839, 4.84296658, -14.18503333); + const vec4 blueLow = vec4(0.10667330, 12.64194608, -60.58204836, 110.36276771); + const vec2 redHigh = vec2(-152.94239396, 59.28637943); + const vec2 greenHigh = vec2(4.27729857, 2.82956604); + const vec2 blueHigh = vec2(-89.90310912, 27.34824973); + vec4 powersLow = vec4(1.0, t, t * t, t * t * t); + vec2 powersHigh = powersLow.zw * powersLow.z; + return vec3(dot(powersLow, redLow) + dot(powersHigh, redHigh), + dot(powersLow, greenLow) + dot(powersHigh, greenHigh), + dot(powersLow, blueLow) + dot(powersHigh, blueHigh)); +} +vec3 inferno(float t) { + return poly6(t, + vec3(0.0002189403691192265, 0.001651004631001012, -0.01948089843709184), + vec3(0.1065134194856116, 0.5639564367884091, 3.932712388889277), + vec3(11.60249308247187, -3.972853965665698, -15.9423941062914), + vec3(-41.70399613139459, 17.43639888205313, 44.35414519872813), + vec3(77.162935699427, -33.40235894210092, -81.80730925738993), + vec3(-71.31942824499214, 32.62606426397723, 73.20951985803202), + vec3(25.13112622477341, -12.24266895238567, -23.07032500287172)); +} +vec3 plasma(float t) { + return poly6(t, + vec3(0.05873234392399702, 0.02333670892565664, 0.5433401826748754), + vec3(2.176514634195958, 0.2383834171260182, 0.7539604599784036), + vec3(-2.689460476458034, -7.455851135738909, 3.110799939717086), + vec3(6.130348345893603, 42.3461881477227, -28.51885465332158), + vec3(-11.10743619062271, -82.66631109428045, 60.13984767418263), + vec3(10.02306557647065, 71.41361770095349, -54.07218655560067), + vec3(-3.658713842777788, -22.93153465461149, 18.19190778539828)); +} +vec3 cividis(float t) { + const vec3 stops[10] = vec3[10]( + vec3(0.0000, 0.1333, 0.3059), + vec3(0.0706, 0.2078, 0.4392), + vec3(0.2314, 0.2863, 0.4235), + vec3(0.3412, 0.3647, 0.4275), + vec3(0.4392, 0.4431, 0.4510), + vec3(0.5412, 0.5255, 0.4706), + vec3(0.6471, 0.6118, 0.4549), + vec3(0.7647, 0.7020, 0.4118), + vec3(0.8824, 0.8000, 0.3333), + vec3(0.9961, 0.9098, 0.2196)); + float scaled = t * 9.0; + int index = min(int(scaled), 8); + return mix(stops[index], stops[index + 1], scaled - float(index)); +} +vec3 magma(float t) { + return poly6(t, + vec3(-0.002136485053939582, -0.000749655052795221, -0.005386127855323933), + vec3(0.2516605407371642, 0.6775232436837668, 2.494026599312351), + vec3(8.353717279216625, -3.577719514958484, 0.3144679030132573), + vec3(-27.66873308576866, 14.26473078096533, -13.64921318813922), + vec3(52.17613981234068, -27.94360607168351, 12.94416944238394), + vec3(-50.76852536473588, 29.04658282127291, 4.23415299384598), + vec3(18.65570506591883, -11.48977351997711, -5.601961508734096)); +} +vec3 cubehelix(float t) { + const float start = 0.5; + const float rotations = -1.5; + const float hue = 1.0; + const float twoPi = 6.283185307179586; + float amplitude = hue * t * (1.0 - t) * 0.5; + float angle = twoPi * (start / 3.0 + rotations * t); + float cosine = cos(angle); + float sine = sin(angle); + return t + amplitude * vec3(-0.14861 * cosine + 1.78277 * sine, + -0.29227 * cosine - 0.90649 * sine, + 1.97294 * cosine); +} +vec3 hot(float t) { + const vec3 rampBegin = vec3(0.0, 0.365079, 0.746032); + const vec3 rampEnd = vec3(0.365079, 0.746032, 1.0); + vec3 color = clamp((vec3(t) - rampBegin) / (rampEnd - rampBegin), 0.0, 1.0); + color.r = mix(0.0416, 1.0, color.r); + return color; +} +vec3 blues(float t) { + const vec3 stops[9] = vec3[9]( + vec3(0.9686, 0.9843, 1.0000), + vec3(0.8706, 0.9216, 0.9686), + vec3(0.7765, 0.8588, 0.9373), + vec3(0.6196, 0.7922, 0.8824), + vec3(0.4196, 0.6824, 0.8392), + vec3(0.2588, 0.5725, 0.7765), + vec3(0.1294, 0.4431, 0.7098), + vec3(0.0314, 0.3176, 0.6118), + vec3(0.0314, 0.1882, 0.4196)); + float scaled = t * 8.0; + int index = min(int(scaled), 7); + return mix(stops[index], stops[index + 1], scaled - float(index)); +} +vec3 blueWhiteRed(float t) { + if (t < 0.5) return mix(vec3(0.0, 0.0, 1.0), vec3(1.0), t * 2.0); + return mix(vec3(1.0), vec3(1.0, 0.0, 0.0), t * 2.0 - 1.0); +} +vec3 applyColormap(int index, float t) { + t = clamp(t, 0.0, 1.0); + switch (index) { + case 1: return viridis(t); + case 2: return turbo(t); + case 3: return inferno(t); + case 4: return plasma(t); + case 5: return cividis(t); + case 6: return magma(t); + case 7: return cubehelix(t); + case 8: return hot(t); + case 9: return blues(t); + case 10: return blueWhiteRed(t); + default: return jet(t); + } +} )"; } // namespace raylib_widgets