Skip to main content

PlottingOpenGL::RectangleRenderer

PlottingOpenGL::RectangleRenderer

Renders RenderableRectangleBatch data using instanced rendering. More…

#include <RectangleRenderer.hpp>

Inherits from PlottingOpenGL::IBatchRenderer

Public Classes

Name
struct SelectionStyle
Visual style for selected rectangles.

Public Functions

Name
RectangleRenderer(std::string shader_base_path =““)
Construct a RectangleRenderer with optional shader paths.
~RectangleRenderer() override
virtual bool initialize() override
Initialize GPU resources (VAOs, VBOs, shaders).
virtual void cleanup() override
Release all GPU resources.
virtual bool isInitialized() const override
Check if the renderer has been successfully initialized.
virtual void render(glm::mat4 const & view_matrix, glm::mat4 const & projection_matrix) override
Render the current batch with the given View and Projection matrices.
virtual bool hasData() const override
Check if there is any data to render.
virtual void clearData() override
Clear all uploaded data (but keep GPU resources allocated).
void uploadData(CorePlotting::RenderableRectangleBatch const & batch)
Upload a rectangle batch to GPU memory.
void setBorderEnabled(bool enabled)
Enable or disable border rendering.
void setBorderColor(glm::vec4 const & color)
Set the border color for all rectangles.
void setBorderWidth(float width)
Set the border width in pixels.
bool isUsingShaderManager() const
Check if using ShaderManager (vs embedded fallback).
void setSelectionStyle(SelectionStyle const & style)
Set the visual style for selected rectangles.
SelectionStyle const & getSelectionStyle() const
Get the current selection style.

Additional inherited members

Public Functions inherited from PlottingOpenGL::IBatchRenderer

Name
virtual ~IBatchRenderer() =default

Protected Functions inherited from PlottingOpenGL::IBatchRenderer

Name
IBatchRenderer() =default
IBatchRenderer(IBatchRenderer const & ) =delete
IBatchRenderer & operator=(IBatchRenderer const & ) =delete
IBatchRenderer(IBatchRenderer && ) =delete
IBatchRenderer & operator=(IBatchRenderer && ) =delete

Detailed Description

class PlottingOpenGL::RectangleRenderer;

Renders RenderableRectangleBatch data using instanced rendering.

See: CorePlotting::RenderableRectangleBatch

This renderer is designed for DigitalIntervalSeries visualization, rendering filled rectangles (epochs, intervals, regions) efficiently.

Rendering Strategy:

  • Uses instanced rendering with a unit quad as base geometry
  • Each instance specifies position (x, y) and size (width, height)
  • Per-instance colors supported for different interval types
  • Optional border/outline rendering

Shader Loading:

  • By default, uses embedded shaders (no external shader file equivalent)
  • Can optionally use ShaderManager if shader_base_path is provided
  • Shader program names: “rectangle_fill_renderer”, “rectangle_border_renderer”

The rectangle bounds are stored as glm::vec4(x, y, width, height) where (x, y) is the bottom-left corner in world space.

Public Functions Documentation

function RectangleRenderer

explicit RectangleRenderer(
    std::string shader_base_path =""
)

Construct a RectangleRenderer with optional shader paths.

Parameters:

  • shader_base_path Base path to shader directory (e.g., “src/WhiskerToolbox/shaders/”) If empty, uses embedded fallback shaders.

function ~RectangleRenderer

~RectangleRenderer() override

function initialize

virtual bool initialize() override

Initialize GPU resources (VAOs, VBOs, shaders).

Return: true if initialization succeeded

Reimplements: PlottingOpenGL::IBatchRenderer::initialize

Must be called with a valid, current OpenGL context. Can be called multiple times (will reinitialize if needed).

function cleanup

virtual void cleanup() override

Release all GPU resources.

Reimplements: PlottingOpenGL::IBatchRenderer::cleanup

Should be called before the OpenGL context is destroyed. Safe to call even if initialize() was never called.

function isInitialized

virtual bool isInitialized() const override

Check if the renderer has been successfully initialized.

Reimplements: PlottingOpenGL::IBatchRenderer::isInitialized

function render

virtual void render(
    glm::mat4 const & view_matrix,
    glm::mat4 const & projection_matrix
) override

Render the current batch with the given View and Projection matrices.

Parameters:

  • view_matrix Global camera/view transformation
  • projection_matrix Projection to NDC

Reimplements: PlottingOpenGL::IBatchRenderer::render

The Model matrix is stored per-batch in the renderable data structure and will be combined with the provided VP matrices.

function hasData

virtual bool hasData() const override

Check if there is any data to render.

Return: true if the renderer has data uploaded and ready to draw

Reimplements: PlottingOpenGL::IBatchRenderer::hasData

function clearData

virtual void clearData() override

Clear all uploaded data (but keep GPU resources allocated).

Reimplements: PlottingOpenGL::IBatchRenderer::clearData

function uploadData

void uploadData(
    CorePlotting::RenderableRectangleBatch const & batch
)

Upload a rectangle batch to GPU memory.

Parameters:

  • batch The rectangle batch to upload

Precondition:

  • initialize() must have been called and returned true before the first call (enforcement: runtime_check — early return if !m_initialized)
  • No GL context is required at call time — GPU upload is deferred to render() (enforcement: N/A — noted here to distinguish from other renderers)
  • If batch.colors is non-empty, batch.colors.size() >= batch.bounds.size() (enforcement: none) [IMPORTANT] — if colors has fewer elements than bounds, render() uploads only the provided colors to the color VBO, but glDrawArraysInstanced draws instance_count = batch.bounds.size() instances, causing the GPU to read uninitialized instance color data for the excess rectangles.
  • If batch.selection_flags is non-empty, batch.selection_flags.size() >= batch.bounds.size() (enforcement: none) [LOW] — render() guards selection_flags access with i < selection_flags.size(), so excess rectangles are silently treated as unselected (no crash, but selection state silently wrong for those instances).

Stores the batch into an internal CPU-side list. The actual GPU upload (VBO allocation and data transfer) is deferred and occurs during render(). Each call appends a new batch — call clearData() between frames to avoid accumulating stale batches across renders.

An empty batch.bounds is handled gracefully (early return, no batch stored). If batch.colors is empty, all rectangles default to semi-transparent white. If batch.selection_flags is empty, all rectangles default to unselected.

function setBorderEnabled

void setBorderEnabled(
    bool enabled
)

Enable or disable border rendering.

Parameters:

  • enabled Whether to draw borders

When enabled, rectangles are drawn with an outline in addition to the fill color.

function setBorderColor

void setBorderColor(
    glm::vec4 const & color
)

Set the border color for all rectangles.

Parameters:

  • color Border color (RGBA)

function setBorderWidth

void setBorderWidth(
    float width
)

Set the border width in pixels.

Parameters:

  • width Border width

function isUsingShaderManager

inline bool isUsingShaderManager() const

Check if using ShaderManager (vs embedded fallback).

function setSelectionStyle

void setSelectionStyle(
    SelectionStyle const & style
)

Set the visual style for selected rectangles.

Parameters:

  • style Selection style configuration

function getSelectionStyle

inline SelectionStyle const & getSelectionStyle() const

Get the current selection style.


Updated on 2026-11-13 at 20:25:41 +0000