Jump to content

  • Log In with Google      Sign In   
  • Create Account

Barycentric UV Help


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 wildboar   Members   -  Reputation: 281

Like
0Likes
Like

Posted 03 January 2013 - 07:08 AM

I have a function like this from recast, its a function that tests if a triangle gets intersected by a ray:

 

 

 

static bool intersectSegmentTriangle(const float* sp, const float* sq,
const float* a, const float* b, const float* c,
float &t)
{
float v, w;
float ab[3], ac[3], qp[3], ap[3], norm[3], e[3];
rcVsub(ab, b, a);
rcVsub(ac, c, a);
rcVsub(qp, sp, sq);


// Compute triangle normal. Can be precalculated or cached if
// intersecting multiple segments against the same triangle
rcVcross(norm, ab, ac);


// Compute denominator d. If d <= 0, segment is parallel to or points
// away from triangle, so exit early
float d = rcVdot(qp, norm);
if (d <= 0.0f) return false;


// Compute intersection t value of pq with plane of triangle. A ray
// intersects iff 0 <= t. Segment intersects iff 0 <= t <= 1. Delay
// dividing by d until intersection has been found to pierce triangle
rcVsub(ap, sp, a);
t = rcVdot(ap, norm);
if (t < 0.0f) return false;
if (t > d) return false; // For segment; exclude this code line for a ray test


// Compute barycentric coordinate components and test if within bounds
rcVcross(e, qp, ap);
v = rcVdot(ac, e);
if (v < 0.0f || v > d) return false;
w = -rcVdot(ab, e);
if (w < 0.0f || v + w > d) return false;


// Segment/ray intersects triangle. Perform delayed division
t /= d;


return true;
}

 

The trouble I am having is that I now have attached some uv coords to the vertices, how can I find out the interpolated uv coord from 3 uv coords of those 3 vertices that go into the function. I need to do something with that t value to get the point in the triangle where the ray hits and then use that to calculate my desired uv coord to output. What I want to achieve is to be able to raycast a triangle and get a uv coord at the point where the ray intersects.
 
Thanks in advance.


Sponsor:

#2 Bacterius   Crossbones+   -  Reputation: 8823

Like
1Likes
Like

Posted 03 January 2013 - 07:59 AM

You have your three barycentric coordinates already: v, w, and u = 1 - v - w. At this point, most data can be linearly interpolated from the vertices of the triangle, using the barycentric coordinates. So, if your barycentric coordinates are (v, w, 1 - v - w), and your vertices are (v1, v2, v3), with uv coordinates v1.uv, v2.uv, v3.uv, then:

 

point.uv = v * v1.uv + w * v2.uv + (1 - v - w) * v3.uv

 

You can quickly check it is valid because the barycentric coordinates add up to 1. You can also do the same for the vertex normals to obtain an interpolated triangle normal.


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#3 wildboar   Members   -  Reputation: 281

Like
0Likes
Like

Posted 03 January 2013 - 09:30 AM

Thanks for the help I am trying that line of code you showed me, however after debugging I noticed that v and w are giving me very strange values:

v:  1213134.3

w: 1669297.8

 

Now the raycast itself is actually working so I am really confused? what values should v and w normally be?



#4 Bacterius   Crossbones+   -  Reputation: 8823

Like
1Likes
Like

Posted 03 January 2013 - 09:34 AM

Thanks for the help I am trying that line of code you showed me, however after debugging I noticed that v and w are giving me very strange values:

v:  1213134.3

w: 1669297.8

 

Now the raycast itself is actually working so I am really confused? what values should v and w normally be?

 

If the ray is inside the triangle, they should all be greater than zero, and v + w cannot exceed 1. If the ray does not intersect the triangle, they can take any value. The barycentric coordinates really define a coordinate space over the plane in which the triangle lies, so you can have arbitrarily small or large barycentric coordinates, that map to some 3D point way outside the triangle.


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#5 wildboar   Members   -  Reputation: 281

Like
0Likes
Like

Posted 03 January 2013 - 09:38 AM

Ok so if those values are correct, how am I supposed to get my uv coordinate? it comes out with crazy numbers with that formula you showed me.



#6 Bacterius   Crossbones+   -  Reputation: 8823

Like
1Likes
Like

Posted 03 January 2013 - 09:43 AM

Ok so if those values are correct, how am I supposed to get my uv coordinate? it comes out with crazy numbers with that formula you showed me.

 

If your barycentric coordinates come out wrong while the raycast test is working, it means you are not actually calculating barycentric coordinates but something else. I think you're doing some precomputation here - what value does "d" have usually? What do you get if you divide v and w by d at the end?


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#7 wildboar   Members   -  Reputation: 281

Like
0Likes
Like

Posted 03 January 2013 - 09:50 AM

I actually did not write that function, its in a library called recast (for navmesh generation).

 

I have modified it like this and left some comments can you have a quick look please:

 

 

 

static bool intersectSegmentTriangle(const float* sp, const float* sq,
const float* a, const float* b, const float* c,
float &t, bool GetUV,
const float* ta, const float* tb, const float* tc)
{
float v, w;
float ab[3], ac[3], qp[3], ap[3], norm[3], e[3];
rcVsub(ab, b, a);
rcVsub(ac, c, a);
rcVsub(qp, sp, sq);


// Compute triangle normal. Can be precalculated or cached if
// intersecting multiple segments against the same triangle
rcVcross(norm, ab, ac);


// Compute denominator d. If d <= 0, segment is parallel to or points
// away from triangle, so exit early
float d = rcVdot(qp, norm);
if (d <= 0.0f) return false;


// Compute intersection t value of pq with plane of triangle. A ray
// intersects iff 0 <= t. Segment intersects iff 0 <= t <= 1. Delay
// dividing by d until intersection has been found to pierce triangle
rcVsub(ap, sp, a);
t = rcVdot(ap, norm);
if (t < 0.0f) return false;
if (t > d) return false; // For segment; exclude this code line for a ray test


// Compute barycentric coordinate components and test if within bounds
rcVcross(e, qp, ap);
v = rcVdot(ac, e);
if (v < 0.0f || v > d) return false;
w = -rcVdot(ab, e);
if (w < 0.0f || v + w > d) return false;


// Segment/ray intersects triangle. Perform delayed division
t /= d; //d ends up 3676338.0 here


//It is about to return true so it means its a triangle hit but the v + w end up as 1315396.8 + 1609470.5 = 3676338.0


if(GetUV)
{
Vector2 v1 = Vector2(ta[0], ta[1]);
Vector2 v2 = Vector2(tb[0], tb[1]);
Vector2 v3 = Vector2(tc[0], tc[1]);


Vector2 UV = v * v1 + w * v2 + (1 - v - w) * v3;
}


return true;
}


#8 Bacterius   Crossbones+   -  Reputation: 8823

Like
1Likes
Like

Posted 03 January 2013 - 09:54 AM

Ah, all good then, the code is just saving some divisions by doing them at the end, your barycentric coordinates are just v / d and w / d (and 1 - v / d - w / d). Try using those and see if they are within the required bounds, then try to apply the formula (make sure to associate the correct uv coordinates with the correct barycentric coordinate, otherwise it'll be off). As far as I can see this should be correct now.

 

If you care about performance, you can compute 1 / d first, then you just have to multiply a few times, which is faster. 


The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis


#9 wildboar   Members   -  Reputation: 281

Like
0Likes
Like

Posted 03 January 2013 - 10:21 AM

then try to apply the formula (make sure to associate the correct uv coordinates with the correct barycentric coordinate, otherwise it'll be off)

 

Sorry what do you mean by that? any sample code would be apreciated. I just added w /= d and v /= d before the if(GetUV) and it produces numbers below 1 now how a uv should be.



#10 Bacterius   Crossbones+   -  Reputation: 8823

Like
1Likes
Like

Posted 03 January 2013 - 10:24 AM

then try to apply the formula (make sure to associate the correct uv coordinates with the correct barycentric coordinate, otherwise it'll be off)

 

Sorry what do you mean by that? any sample code would be apreciated. I just added w /= d and v /= d before the if(GetUV) and it produces numbers below 1 now how a uv should be.

 

Now you just use the formula in my first post again, but with the new, corrected v and w coordinates (divided by d, as you just did):

 

 

v /= d;
w /= d;
...
point.uv = v * v1.uv + w * v2.uv + (1 - v - w) * v3.uv;

 

This should work and give you the proper uv coordinates for the intersection point. You may need to shuffle around v1, v2, etc.. depending on how you defined v and w (which vertex are they based on?) but there's only six possible arrangements, try them and see which one looks correct (the bad ones will look like the texture is rotated, I think).


Edited by Bacterius, 03 January 2013 - 10:26 AM.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

 

- Pessimal Algorithms and Simplexity Analysis





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS