Skip to main content

PlottingOpenGL::BatchLineRenderer

PlottingOpenGL::BatchLineRenderer

#include <BatchLineRenderer.hpp>

Inherits from PlottingOpenGL::IBatchRenderer

Public Functions

Name
BatchLineRenderer(BatchLineStore & store, std::string shader_base_path =““)
Construct a BatchLineRenderer backed by the given store.
~BatchLineRenderer() 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 setLineWidth(float width)
void setGlobalColor(glm::vec4 const & color)
void setSelectedColor(glm::vec4 const & color)
void setHoverColor(glm::vec4 const & color)
void setCanvasSize(glm::vec2 const & size)
void setViewportSize(glm::vec2 const & size)
void setHoverLine(std::optional< std::uint32_t > line_index)
Set the line index to render with hover highlighting.
void updateLineColorOverrides(std::vector< glm::vec4 > const & per_line_colors)
Upload per-line color overrides.
bool isUsingSSBOs() const
Whether GPU SSBOs are used (GL 4.3) vs CPU-side filtering (GL 4.1).
void syncFromStore()
Upload vertex data from the store’s CPU mirror to the VBO/VAO.
void renderWithCache(glm::mat4 const & view, glm::mat4 const & proj, glm::mat4 const & model =glm::mat4{1.0f}, bool force_redraw =false)
Render to an offscreen FBO, then blit to the current framebuffer.
void markViewDirty()
Mark the cached FBO as needing re-render.

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

Public Functions Documentation

function BatchLineRenderer

explicit BatchLineRenderer(
    BatchLineStore & store,
    std::string shader_base_path =""
)

Construct a BatchLineRenderer backed by the given store.

Parameters:

  • store The BatchLineStore that owns the segment data. Must outlive this renderer.
  • shader_base_path Base path to the shaders directory (e.g., “:/shaders/”). If empty, uses embedded fallback.

function ~BatchLineRenderer

~BatchLineRenderer() 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 setLineWidth

inline void setLineWidth(
    float width
)

function setGlobalColor

inline void setGlobalColor(
    glm::vec4 const & color
)

function setSelectedColor

inline void setSelectedColor(
    glm::vec4 const & color
)

function setHoverColor

inline void setHoverColor(
    glm::vec4 const & color
)

function setCanvasSize

inline void setCanvasSize(
    glm::vec2 const & size
)

function setViewportSize

inline void setViewportSize(
    glm::vec2 const & size
)

function setHoverLine

void setHoverLine(
    std::optional< std::uint32_t > line_index
)

Set the line index to render with hover highlighting.

Parameters:

  • line_index 0-based index into LineBatchData::lines, or std::nullopt to clear hover.

function updateLineColorOverrides

void updateLineColorOverrides(
    std::vector< glm::vec4 > const & per_line_colors
)

Upload per-line color overrides.

Each entry corresponds to a line in LineBatchData::lines (0-based). A color with alpha > 0 overrides the global color for that line. Alpha == 0 means “use global color” (no override).

Must be called after syncFromStore() — the number of entries must equal the number of lines in the current LineBatchData.

function isUsingSSBOs

inline bool isUsingSSBOs() const

Whether GPU SSBOs are used (GL 4.3) vs CPU-side filtering (GL 4.1).

function syncFromStore

void syncFromStore()

Upload vertex data from the store’s CPU mirror to the VBO/VAO.

Precondition:

  • initialize() must have been called and returned true (enforcement: runtime_check — returns immediately if !m_initialized)
  • A valid OpenGL context must be current on the calling thread (enforcement: none) [CRITICAL] — m_vao.bind() and all VBO allocate() calls issue raw GL immediately.
  • BatchLineStore::upload() must have been called on m_store at least once before syncFromStore() (enforcement: runtime_check — silent) — if no upload() has been called, cpu.empty() fires, m_total_vertices is set to 0, and the function returns. Caller cannot distinguish “store empty” from “store never uploaded”.
  • m_store.cpuData().selection_mask.size() == m_store.numLines() (enforcement: none) [CRITICAL] — the selection flag loop accesses cpu.selection_mask[id - 1] with a bounds check against cpu.numLines() (== cpu.lines.size()), but NOT against cpu.selection_mask.size(). If selection_mask is shorter than lines, an out-of-bounds vector read occurs for line_ids whose 1-based id exceeds selection_mask.size().
  • m_store.cpuData().line_ids.size() == m_store.numSegments() (enforcement: none) [IMPORTANT] — line_ids is iterated once per segment to build both the line_id VBO and the selection flag VBO. A shorter line_ids would produce VBOs misaligned with the vertex buffer, causing incorrect per-vertex attribute reads in the shader.
  • cpu.segments.size() * sizeof(float) <= INT_MAX (narrowing cast to int for m_vertex_vbo.allocate()) (enforcement: none) [LOW]

Postcondition:

  • m_total_vertices == m_store.numSegments() * 2.
  • m_canvas_size is updated from cpu.canvas_width / canvas_height.
  • m_view_dirty == true (render cache invalidated).

Call after BatchLineStore::upload() to sync the render buffers. Separate from IBatchRenderer::uploadData because the data source is the store, not a RenderablePrimitive.

Uploads to four VBOs:

  1. m_vertex_vbo — xy positions from cpu.segments (2 floats per vertex)
  2. m_line_id_vbo — line IDs duplicated for both vertices of each segment
  3. m_selection_vbo — per-vertex selection flags (GL 4.1 fallback path)
  4. m_color_override_vbo — transparent overrides (defaults; call updateLineColorOverrides() afterwards if group coloring is needed)

An empty CPU mirror (cpu.empty()) is handled gracefully — clears m_total_vertices and returns without touching the GPU.

function renderWithCache

void renderWithCache(
    glm::mat4 const & view,
    glm::mat4 const & proj,
    glm::mat4 const & model =glm::mat4{1.0f},
    bool force_redraw =false
)

Render to an offscreen FBO, then blit to the current framebuffer.

Parameters:

  • view View matrix
  • proj Projection matrix
  • model Model matrix (typically identity for batch lines)
  • force_redraw Currently ignored — present for future FBO interface compatibility only.

Note: FBO caching is not yet implemented. This function currently renders directly to the active framebuffer on every call, ignoring both the force_redraw parameter and the m_view_dirty flag. The documented FBO/blit pattern (render-to-texture + cache) is planned but not present in the current implementation. Only m_view_dirty is written at the end of this call (set to false).

Precondition:

  • initialize() must have been called and returned true (enforcement: runtime_check — returns immediately if !m_initialized)
  • syncFromStore() must have been called at least once after the last BatchLineStore::upload() to populate vertex buffers and m_total_vertices (enforcement: runtime_check — silent via m_total_vertices == 0 guard; caller cannot distinguish “no data” from “sync not called”)
  • A valid OpenGL context must be current on the calling thread (enforcement: none) [CRITICAL] — renderLines() and renderHoverLine() issue GL draw calls with no context guard beyond the m_initialized check at entry.
  • setViewportSize() must have been called with a positive x component (m_viewport_size.x > 0) (enforcement: none) [IMPORTANT] — the geometry shader computes half_width_ndc = (u_line_width / u_viewport_size.x) * 2.0; a zero x value causes shader division by zero, producing Inf/NaN in all vertex positions.
  • m_line_width > 0 (enforcement: none) [LOW] — a negative line width produces geometrically flipped quad strips in the geometry shader without any error or clamp.

Postcondition: m_view_dirty == false (regardless of whether anything was rendered).

Returns without rendering if !isInitialized() or hasData() == false.

function markViewDirty

inline void markViewDirty()

Mark the cached FBO as needing re-render.


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