• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
wildboar

Barycentric UV Help

9 posts in this topic

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.
0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites
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.

1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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?

1

Share this post


Link to post
Share on other sites

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;
}
0

Share this post


Link to post
Share on other sites

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. 

1

Share this post


Link to post
Share on other sites
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.

0

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0