Skip to content

Instantly share code, notes, and snippets.

@dougbinks
Created April 3, 2014 11:01
Show Gist options
  • Select an option

  • Save dougbinks/9952415 to your computer and use it in GitHub Desktop.

Select an option

Save dougbinks/9952415 to your computer and use it in GitHub Desktop.

Revisions

  1. dougbinks created this gist Apr 3, 2014.
    32 changes: 32 additions & 0 deletions gl_VertexID barycentric coords
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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...).