Skip to content

Instantly share code, notes, and snippets.

@catchouli
Created June 4, 2015 18:07
Show Gist options
  • Save catchouli/a90c5815fc74a7188345 to your computer and use it in GitHub Desktop.
Save catchouli/a90c5815fc74a7188345 to your computer and use it in GitHub Desktop.
interpolate
vec3 interpolate(vec3 tri[3], vec3 vals[3], vec3 intersection)
{
// Calculate barycentric coordinates
float one_over_tri_area = 2.0f / length(cross(tri[1] - tri[0], tri[2] - tri[1]));
float u = 0.5f * one_over_tri_area * length(cross(tri[1] - intersection, tri[2] - intersection));
float v = 0.5f * one_over_tri_area * length(cross(tri[0] - intersection, tri[2] - intersection));
float w = 0.5f * one_over_tri_area * length(cross(tri[0] - intersection, tri[1] - intersection));
return vals[0] * u + vals[1] * v + vals[2] * w;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment