Calculate UV at intersectionpoint

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
r-type
Posts: 41
Joined: Sun Feb 12, 2006 1:54 am

Calculate UV at intersectionpoint

Post by r-type »

Tried some things but all ending up incorrectly. I got a triangle with 3 vertices and their UV's, does anyone know how to calculate the UV at the intersection point? Many thanks in advance!
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

A triangle does not have an intersection point :shock:
r-type
Posts: 41
Joined: Sun Feb 12, 2006 1:54 am

Post by r-type »

sorry for that :) i mean i have a triangle , vertices and their according uv's AND a point inside that triangle (the intersection with a ray i shot)
inet
Posts: 29
Joined: Wed Nov 08, 2006 9:25 pm
Location: Berlin, Germany
Contact:

Post by inet »

Actually the direct results from a ray-triangle intersection are the u,v coordinates.

Refer to this algorithm.
http://www.graphics.cornell.edu/pubs/1997/MT97.html
r-type
Posts: 41
Joined: Sun Feb 12, 2006 1:54 am

Post by r-type »

hi , came across this picture on the internet on my quest too, But I don't quite understand how -texture- UV's can be used for ray intersection in a 3D scene. Perhaps you could provide me with some pointers ?
inet
Posts: 29
Joined: Wed Nov 08, 2006 9:25 pm
Location: Berlin, Germany
Contact:

Post by inet »

u, v are just names. Texture coord can use them. Barycentric coord can also use them.

I suggest you download the paper on the webpage I referred to and read the paper. Everything is described quite clear.
r-type
Posts: 41
Joined: Sun Feb 12, 2006 1:54 am

Post by r-type »

Ok I'll try! hope it's not too much out of my league
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Here's the algorithm from that paper (It's listed at the end of the paper but sometimes you can't copy properly from a pdf), i've been trying to use it in one of my projects with limited luck so far, not quite working properly!

http://jgt.akpeters.com/papers/MollerTrumbore97/

#define EPSILON 0.000001
#define CROSS(dest,v1,v2) \
dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
#define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
#define SUB(dest,v1,v2) \
dest[0]=v1[0]-v2[0]; \
dest[1]=v1[1]-v2[1]; \
dest[2]=v1[2]-v2[2];

int
intersect_triangle(double orig[3], double dir[3],
double vert0[3], double vert1[3], double vert2[3],
double *t, double *u, double *v)
{
double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
double det,inv_det;

/* find vectors for two edges sharing vert0 */
SUB(edge1, vert1, vert0);
SUB(edge2, vert2, vert0);

/* begin calculating determinant - also used to calculate U parameter */
CROSS(pvec, dir, edge2);

/* if determinant is near zero, ray lies in plane of triangle */
det = DOT(edge1, pvec);

#ifdef TEST_CULL /* define TEST_CULL if culling is desired */
if (det < EPSILON)
return 0;

/* calculate distance from vert0 to ray origin */
SUB(tvec, orig, vert0);

/* calculate U parameter and test bounds */
*u = DOT(tvec, pvec);
if (*u < 0.0 || *u > det)
return 0;

/* prepare to test V parameter */
CROSS(qvec, tvec, edge1);

/* calculate V parameter and test bounds */
*v = DOT(dir, qvec);
if (*v < 0.0 || *u + *v > det)
return 0;

/* calculate t, scale parameters, ray intersects triangle */
*t = DOT(edge2, qvec);
inv_det = 1.0 / det;
*t *= inv_det;
*u *= inv_det;
*v *= inv_det;
#else /* the non-culling branch */
if (det > -EPSILON && det < EPSILON)
return 0;
inv_det = 1.0 / det;

/* calculate distance from vert0 to ray origin */
SUB(tvec, orig, vert0);

/* calculate U parameter and test bounds */
*u = DOT(tvec, pvec) * inv_det;
if (*u < 0.0 || *u > 1.0)
return 0;

/* prepare to test V parameter */
CROSS(qvec, tvec, edge1);

/* calculate V parameter and test bounds */
*v = DOT(dir, qvec) * inv_det;
if (*v < 0.0 || *u + *v > 1.0)
return 0;

/* calculate t, ray intersects triangle */
*t = DOT(edge2, qvec) * inv_det;
#endif
return 1;
}
Image Image Image
r-type
Posts: 41
Joined: Sun Feb 12, 2006 1:54 am

Post by r-type »

Got the math working to get the UV, but now i need to get that translated to the texture coordinate. Found some article about it but it doesnt seem to work. This stuff is way above me , but I really want it to work as it is the last step to reaching my goal.

It's usually below me to ask someone else to do the work but could you provide me with the translation formula from the tuv coordinate as gotten from the article's algorithm to the texture coordinates ? I would be forever in your debt.
Post Reply