Barycentric coordinates: Difference between revisions

From vegard.wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
(add link)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
Barycentric coordinates let you represent a coordinate as a weighted sum of three points. This can be very useful for interpolating values across a triangle, including world coordinates or texture coordinates.

=== Conversion from cartesian ===
=== Conversion from cartesian ===


Line 5: Line 7:
// Compute barycentric coordinates (u, v, w) for
// Compute barycentric coordinates (u, v, w) for
// point p with respect to triangle (a, b, c)
// point p with respect to triangle (a, b, c)
glm::vec3 barycentric(glm::vec3 a, glm::vec3 b, glm::vec3 c, glm::vec3 p)
void Barycentric(Point p, Point a, Point b, Point c, float &u, float &v, float &w)
{
{
Vector v0 = b - a, v1 = c - a, v2 = p - a;
auto v0 = b - a;
float d00 = Dot(v0, v0);
auto v1 = c - a;
float d01 = Dot(v0, v1);
auto v2 = p - a;

float d11 = Dot(v1, v1);
float d20 = Dot(v2, v0);
float d00 = glm::dot(v0, v0);
float d21 = Dot(v2, v1);
float d01 = glm::dot(v0, v1);
float denom = d00 * d11 - d01 * d01;
float d11 = glm::dot(v1, v1);
v = (d11 * d20 - d01 * d21) / denom;
float d20 = glm::dot(v2, v0);
w = (d00 * d21 - d01 * d20) / denom;
float d21 = glm::dot(v2, v1);
u = 1.0f - v - w;
float denom = d00 * d11 - d01 * d01;

float v = (d11 * d20 - d01 * d21) / denom;
float w = (d00 * d21 - d01 * d20) / denom;
float u = 1.0f - v - w;

return glm::vec3(u, v, w);
}
}
</source>
</source>
Line 24: Line 32:
<source lang="C++">
<source lang="C++">
// https://stackoverflow.com/a/11262425
// https://stackoverflow.com/a/11262425
glm::vec3 cartesian(glm::vec3 a, glm::vec3 b, glm::vec3 c, glm::vec3 p)
Vector3d Tri::cartesian(const Vector3d& barycentric) const
{
{
return barycentric.x * p0 + barycentric.y * p1 + barycentric.z * p2;
return a * p.x + b * p.y + c * p.z;
}
}
</source>
</source>

=== See also ===

* https://asawicki.info/news_1721_how_to_correctly_interpolate_vertex_attributes_on_a_parallelogram_using_modern_gpus


[[Category:Graphics programming]]
[[Category:Graphics programming]]

Latest revision as of 15:51, 17 February 2020

Barycentric coordinates let you represent a coordinate as a weighted sum of three points. This can be very useful for interpolating values across a triangle, including world coordinates or texture coordinates.

Conversion from cartesian

// https://gamedev.stackexchange.com/a/23745
// Compute barycentric coordinates (u, v, w) for
// point p with respect to triangle (a, b, c)
glm::vec3 barycentric(glm::vec3 a, glm::vec3 b, glm::vec3 c, glm::vec3 p)
{
        auto v0 = b - a;
        auto v1 = c - a;
        auto v2 = p - a;

        float d00 = glm::dot(v0, v0);
        float d01 = glm::dot(v0, v1);
        float d11 = glm::dot(v1, v1);
        float d20 = glm::dot(v2, v0);
        float d21 = glm::dot(v2, v1);
        float denom = d00 * d11 - d01 * d01;

        float v = (d11 * d20 - d01 * d21) / denom;
        float w = (d00 * d21 - d01 * d20) / denom;
        float u = 1.0f - v - w;

        return glm::vec3(u, v, w);
}

Conversion to cartesian

// https://stackoverflow.com/a/11262425
glm::vec3 cartesian(glm::vec3 a, glm::vec3 b, glm::vec3 c, glm::vec3 p) 
{
        return a * p.x + b * p.y + c * p.z;
}

See also