Skip to content

Instantly share code, notes, and snippets.

@dougbinks
Created April 3, 2014 11:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dougbinks/9952415 to your computer and use it in GitHub Desktop.
Save dougbinks/9952415 to your computer and use it in GitHub Desktop.
One way to generate barycentric coordinates from a vertex stream is to use gl_VertexID. This requires that your vertices are ordered in the vertex buffer in a regular fashion as gl_VertexID is the vertex position in the buffer, so vertex optimization using an index buffer will likely break this.
Step 1 is to generate a vec3 in the vertex shader:
//VS
out vec3 ex_BarycentricCoords;
void main(void)
{
//your usual code
vec3 bary;
int i = gl_VertexID % 3;
bary.x = float(i == 0);
bary.y = float(i == 1);
bary.z = float(i == 2);
ex_BarycentricCoords = bary;
}
Step 2 is to use these to get outlining in the fs:
//FS
in vex3 ex_BarycentricCoords;
out vec4 fragColor;
void main(void)
{
vec3 line = 10.0 * (0.1 - clamp( ex_BarycentricCoords, 0.0, 0.1 ));
fragColor = vec4(line,1.0);
}
Step 3 would be to get anti-aliasing by using derivatives (to come...).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment