I have a problem that should involve some rather simple mathematics, but I’m just to dumb to calculate it by myself, so I hope someone can help.

I have done an intersection test in my scene, and I have a result intersection point p1. A also have the polygon hit that consists of n vertics coordinates and n texture coordinates, n is usually 3 but can be more. What I need to calcualte is the texture coordinate for the intersection point p1. Can anyone help me with this?

The function I’m developing is supposed to return the alpha value where the hit occured, but now my function only work on simple polygons(like cross trees).

I’ll give a brief overview of the technique i came up with; I think it should work.

you should be able to google the math involved for what needs to be done, if you can’t find it, I’ll help you out.

what you want to do is find the interpolated value of the texture coord at point p1. It’s easy to find the interpolated tex coord on a polygon edge - it’s interpolated between the two vertices that define that edge.

a line is defined by point p1 and a vertex (pick one, it doesn’t matter which).

find the intersection point (px) of the line defined by p1,v1 (v1 is the vertex you chose to form the line) and the edge opposite to v1.

this point px lies on a polygon edge. Find the interpolated value of the texcoord at this point.

now you’ve got the tex coords at v1 and px, and since p1 lies on this line interpolate its value from v1 and px.

I’ve been trying to figure out several ways to work out my problem by now, but none of them worked out as I hoped. The method you(Aeluned) mentioned might work on polygons with 3 vertices, but on complex polygons with more than 3 vertices I cannot think of a way to fin the correct edge opposite of v1. I also have some difficulty calcualting the line p1v1 intersection point with the opposite polygon edge.

I’ve tried out some calculations on paper involving the different distances between the point p1 and all of my vertices. Then I have tried some calculations to find the U,V texture coordinate by giving most weight to the texture coordinates on the closest vertices. I might find a solution involving these thoughts, but I’m not sure.

The method I’m using now generates a point min and max using my vertices, and a texture coordinate min and max, then I linearly interpolate using my point p1. This works pretty well on most polygons, but not allways, so I have to try finding a better method.

I have of course tried to find some examples on this on google, but I’ve not found a good example yet. There are many tutorials on collision and intersections, but none involving textures coordinates.

How about this?
Since you’re using OpenGL, is it safe to assume that the polygon is convex?

If so:
The polygon must have 3 vertices that define a bounding triangle for the point p1.

two vertices - v1 and v2 - will lie on one side of the point p1 (either at x values greater or less than p1). first find these vertices.

the other vertex v3 must lie on the other side.

now, we can form 2 edges - 1 defined by v1,v3 and the other defined by v2,v3.

solve for y on on the line defined by v1,v3 given p1.x. this gives you a point on the line/edge defined by vertex v1 and v3. Linearly interpolate the value of the tex coord at this point on v1,v3.

now, solve for y on the line v2,v3 given p1.x. again, you get a point on the line v2,v3. interpolate this tex coord value.

now you have p1 on the line defined by the two points you found on the two edges.
interpolate the value at p1 now on that line and you should have your texture coordinate.