Skip to content

Instantly share code, notes, and snippets.

@xaxxon
Created April 7, 2016 17:02
Show Gist options
  • Save xaxxon/93a5a95d2b16175f0e4956b53a4fb8ab to your computer and use it in GitHub Desktop.
Save xaxxon/93a5a95d2b16175f0e4956b53a4fb8ab to your computer and use it in GitHub Desktop.
updated line drawing shaders
const char * LineSegmentList::VERTEX_SHADER = R"VERTEXSHADER(#version 410
in vec2 vertex;
in vec2 normal;
in vec4 color;
in float width;
out vec2 normal_g;
out vec4 color_g;
out float width_g;
void main(){
normal_g = normal / length(normal);
color_g = color;
width_g = width;
gl_Position = vec4(vertex, 0, 1);
}
)VERTEXSHADER";
const char * LineSegmentList::GEOMETRY_SHADER = R"GEOMETRYSHADER(#version 410
layout(lines) in;
in vec2 normal_g[];
in float width_g[];
in vec4 color_g[];
uniform mat4 mvp_matrix;
layout(triangle_strip, max_vertices = 4) out;
out vec2 normal_f;
out float width_f;
out vec4 color_f;
out float intensity;
void main()
{
width_f = width_g[0];
color_f = color_g[0];
float segment_length = length(gl_in[0].gl_Position - gl_in[1].gl_Position);
normal_f = -normal_g[0];
intensity = segment_length;
gl_Position = mvp_matrix * (gl_in[0].gl_Position - vec4(((normal_g[0]/length(normal_g[0])) * width_g[0]), 0, 0));
EmitVertex();
normal_f = -normal_g[1];
intensity = 0;
gl_Position = mvp_matrix * (gl_in[1].gl_Position - vec4(((normal_g[0]/length(normal_g[0])) * width_g[0]), 0, 0));
EmitVertex();
normal_f = normal_g[0];
intensity = segment_length;
gl_Position = mvp_matrix * (gl_in[0].gl_Position + vec4(((normal_g[0]/length(normal_g[0])) * width_g[0]), 0, 0));
EmitVertex();
normal_f = normal_g[1];
intensity = 0;
gl_Position = mvp_matrix * (gl_in[1].gl_Position + vec4(((normal_g[0]/length(normal_g[0])) * width_g[0]), 0, 0));
EmitVertex();
}
)GEOMETRYSHADER";
const char * LineSegmentList::FRAGMENT_SHADER = R"FRAGSHADER(#version 410
in vec4 color_f;
in vec2 normal_f;
in float width_f;
in float intensity;
uniform float time;
out vec4 fragment_color;
void main() {
float final_intensity = abs(mod(intensity + time,1) - 0.5);
fragment_color = vec4(color_f.r * final_intensity,color_f.g*final_intensity ,color_f.b * final_intensity, (width_f-length(normal_f)) / width_f );
}
)FRAGSHADER";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment