4

I'm trying to implement proper texturing of convex polygons. I have a polygon with n triangles, for each triangle i'm calculating barycentric coordinates which are uv of each one but in [0..1] of each triangle, not entire polygon. How to interpolate each uv so it stretches (wraps and not repeats as it is now) entire texture ?

Now it looks like this:

enter image description here

//region.triangulatedVectors = List<Vector2> // triangle points in CCW

//foreach triangle 
for (int i = 0;i<region.triangulatedVectors.size();i+=3){
    float aX = region.triangulatedVectors.get(i).x;
    float aY = region.triangulatedVectors.get(i).y;
    float bX = region.triangulatedVectors.get(i+1).x;
    float bY = region.triangulatedVectors.get(i+1).y;
    float cX = region.triangulatedVectors.get(i+2).x;
    float cY = region.triangulatedVectors.get(i+2).y;
    Vector2 bary0 = new Vector2();
    Vector2 bary1 = new Vector2();
    Vector2 bary2 = new Vector2();
    Vector2 a = new Vector2(aX, aY);
    Vector2 b = new Vector2(bX, bY);
    Vector2 c = new Vector2(cX, cY);
    GeometryUtils.barycentric(a, a, b, c, bary0);
    GeometryUtils.barycentric(b, a, b, c, bary1);
    GeometryUtils.barycentric(c, a, b, c, bary2);
    //first point 
    texCoords[k++] = bary0.x;
    texCoords[k++] = bary0.y;
    texCoords[k++] = bary1.x;
    texCoords[k++] = bary1.y;
    texCoords[k++] = bary2.x;
    texCoords[k++] = bary2.y;
    //TODO , interpolate
}

It seems that there are 3 ways of dealing with 2D. Wachspress, Discrete harmonic and Mean value.

Paweł
  • 2,144
  • 1
  • 18
  • 25
  • You are not being clear about what exactly you are trying to make it look like. If you want the polygon to be textured like the square you have to calculate the UV coordinates for each vertex... But maybe you are trying to texture it differently? – Jyro117 Jun 12 '14 at 16:16

1 Answers1

0

To use generalized barycentric coordinates to map between these two polygon (one n-sided and the other square), you can add artificial vertices around the square so you are mapping one $n$-sided polygon to another. For example, this image shows an eight-sided polygon and a square augmented with side midpoints to also have eight vertices.

Then on the original polygon, you define some generalized barycentric coordinates:

x = L1(x) v1 + L2(x) v2 + ... + Ln(x) vn

The specific functions L1, L2, ..., Ln are defined by which ever generalized barycentric coordinate you select, e.g., mean value Wachspress, etc.

Corresponding generalized barycentric coordinates are also defined for the square with the extra matching vertices identified, i.e.,

y = M1(y) w1 + M2(y) w2 + ... + Mn(y) wn

Now, given a point x in the polygon, we compute the associated point y in the square (i.e. the (u,v) that we can look up the texture from) using the barycentric coordinates from the polygon but the vertex positions in the square,

y= L1(x) w1 + L2(x) w2 + ... + Ln(x) wn

A couple notes:

  • Following this approach, you probably don't want to use Wachspress coordinates. They don't behave well when adding these extra vertices in the middle of straight edges. This image (from here) shows how Wachpress coordinates work poorly in this situation.
  • There are still decisions to be made about where exactly to split the sides of the square to get a matching number of vertices, and how to associate the vertices between the polygon and the square. These decisions will definitely impact how the texture gets stretched and skewed.
Alex
  • 1,042
  • 1
  • 8
  • 17