Skip to main content

PlottingOpenGL::GlyphShaders

PlottingOpenGL::GlyphShaders

Embedded fallback shader source code for the glyph renderer. More…

Attributes

Name
char const * INSTANCED_VERTEX_SHADER
Vertex shader for instanced glyph rendering (ticks, top lines, squares, crosses).
char const * INSTANCED_FRAGMENT_SHADER
Fragment shader for instanced glyphs.
char const * POINT_VERTEX_SHADER
Vertex shader for GL_POINTS mode (circles).
char const * POINT_FRAGMENT_SHADER

Detailed Description

Embedded fallback shader source code for the glyph renderer.

These shaders support both GL_POINTS mode (for circles) and instanced rendering mode (for ticks, top lines, squares, crosses).

Attribute layout for instanced rendering:

  • location 0: geometry vertex (per-vertex)
  • location 1: instance position (per-instance)
  • location 2: instance color (per-instance)

For GL_POINTS mode, position data is uploaded to location 0 directly.

Attributes Documentation

variable INSTANCED_VERTEX_SHADER

char const  * INSTANCED_VERTEX_SHADER = R"(
#version 410 core

layout (location = 0) in vec2 a_geometry;       // Per-vertex glyph shape (tick vs top line vs …)
layout (location = 1) in vec2 a_instance_pos;   // Per-instance position
layout (location = 2) in vec4 a_instance_color; // Per-instance color

uniform mat4 u_mvp_matrix;
uniform float u_glyph_size;

out vec4 v_color;

void main() {
    // world = normalized_shape * pixel_or_world_size + instance_anchor
    vec2 world_pos = a_geometry * u_glyph_size + a_instance_pos;
    gl_Position = u_mvp_matrix * vec4(world_pos, 0.0, 1.0);
    v_color = a_instance_color;
}
)";

Vertex shader for instanced glyph rendering (ticks, top lines, squares, crosses).

Per-vertex shape a_geometry is authored in normalized [-0.5, 0.5] space on the CPU (GlyphRenderer::createGlyphGeometry): Tick uses a vertical segment (Δx=0), TopLine a horizontal segment (Δy=0). Both multiply by u_glyph_size and add the instance anchor. There is no geometry shader; orientation is entirely encoded in the vertex buffer.

variable INSTANCED_FRAGMENT_SHADER

char const  * INSTANCED_FRAGMENT_SHADER = R"(
#version 410 core

in vec4 v_color;

out vec4 FragColor;

void main() {
    FragColor = v_color;
}
)";

Fragment shader for instanced glyphs.

variable POINT_VERTEX_SHADER

char const  * POINT_VERTEX_SHADER = R"(
#version 410 core

layout (location = 0) in vec2 a_position;
layout (location = 1) in vec4 a_color;

uniform mat4 u_mvp_matrix;
uniform float u_point_size;

out vec4 v_color;

void main() {
    gl_Position = u_mvp_matrix * vec4(a_position, 0.0, 1.0);
    gl_PointSize = u_point_size;
    v_color = a_color;
}
)";

Vertex shader for GL_POINTS mode (circles).

This shader is used when rendering circle glyphs as point sprites.

variable POINT_FRAGMENT_SHADER

char const  * POINT_FRAGMENT_SHADER = R"(
#version 410 core

in vec4 v_color;

out vec4 FragColor;

void main() {
    // Create circular point by discarding fragments outside circle
    vec2 coord = gl_PointCoord - vec2(0.5);
    float dist = length(coord);
    if (dist > 0.5) {
        discard;
    }
    
    // Smooth edge
    float alpha = 1.0 - smoothstep(0.4, 0.5, dist);
    FragColor = vec4(v_color.rgb, v_color.a * alpha);
}
)";

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