Skip to content

Instantly share code, notes, and snippets.

@serg06
Last active January 10, 2020 22:42
Show Gist options
  • Save serg06/7c2eea7857f6a3ec2ad806b258022564 to your computer and use it in GitHub Desktop.
Save serg06/7c2eea7857f6a3ec2ad806b258022564 to your computer and use it in GitHub Desktop.
OpenGL shaders for rendering an array of sequential unsigned integers starting at 0
#version 450 core
// get color from geometry shader
in vec4 gs_color;
// output color
out vec4 color;
void main(void)
{
color = gs_color;
}
#version 450 core
layout (location = 0) uniform uint max_height;
layout (points) in; // discarded
layout (triangle_strip, max_vertices = 4) out; // output "bar" in "bar graph"
in uint vs_height[]; // height of an array element
in vec4 vs_color[]; // color of an array element
in uint vs_index[]; // index of an array element
out vec4 gs_color; // pass-through color
void main(void)
{
gs_color = vs_color[0];
float unit_width = 2.0f/max_height;
vec2 bottom_left_corner = vec2(unit_width * vs_index[0] - 1, -1);
vec2 dx = vec2(unit_width, 0);
vec2 dy = vec2(0, vs_height[0] * unit_width);
gl_Position = vec4(bottom_left_corner, 0.0f, 1.0f); EmitVertex();
gl_Position = vec4(bottom_left_corner + dx, 0.0f, 1.0f); EmitVertex();
gl_Position = vec4(bottom_left_corner + dy, 0.0f, 1.0f); EmitVertex();
gl_Position = vec4(bottom_left_corner + dx + dy, 0.0f, 1.0f); EmitVertex();
}
#version 450 core
layout (location = 0) uniform uint max_height; // tallest element in graph (== number of elements)
layout (location = 0) in uint height; // height of element
layout (location = 1) in vec4 color; // color of element
out uint vs_height; // pass-through height
out vec4 vs_color; // pass-through color
out uint vs_index; // index in array
void main(void)
{
// max-height tells us not only the max height, but also how many elements there are.
// gl_VertexID tells us where (horizontally) to draw the element
// geometry shader does all the work
vs_height = height;
vs_color = color;
vs_index = gl_VertexID;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment