Skip to main content
GameDev.net gamedev.net
🔒 Locked

Interpolating texture coordinates

Started by Timbatron Nov 22, 2003 at 1:17 PM 5 replies 1.1k views
Original Post
Timbatron
Timbatron
The program that I am working on is eventually going to allow the user to paint directly on a 3d model. I have all the screen unprojection and ray intersection code done, and I can get the triangle that is intersected, along with the point at which it intersects. The question is, given the texture coordinates and position coordinates of a triangle, and the position coordinate of the intersection, what are the texture coordinates of the intersection? I thought about taking the distances to all the points and weighting the tex coords according to their ratios, but that definately seems wrong. Any ideas or pointers in the right direction?
My Page : http://acidbase.computed.net
JuNC
JuNC
It''s easiest if your triangle-intersection code works by a barycentric coordinate test (e.g. here).

You just then use the barycentric coordinates to interpolate the texture coordinates just like you would with the position.
Timbatron
Timbatron
That really doesn''t seem to help very much... I can get the barycentric coordinates pretty easily from my positional data, although that algorithm is definately faster (not really concerned about speed though). If I could even firgure it out for the two dimensional scenario, I could work from there
My Page : http://acidbase.computed.net
JuNC
JuNC
What is the problem then?

Just interpolate the texture coordinates in exactly the same way you would do the position.

dmounty
dmounty
As the previous poster said (almost). You just use bilinear interpolation of the texture coordinate within the triangle. Conceptually.. its like this.

Imagine your triangle of intersection is transformed onto the x z axis (like a piece of paper on the desk). In this new space, look at the projected 2D coords of the triangle, forming a triangle in the new space. Translate the corners of the triangle up according to the individual coords of the texture (ie you have to do this twice). You have now defined a plane that represents the relation of coordinates in the plane of your triangle, and one specific texture coord (ie s or t). You then pic the appropriate point of intersection from this plane, and it gives you that texture coord. Repeat for the other texture coord, and you are done. If you work through the maths, you''ll probably find its pretty easy, and simplifies a lot. This method is essentially bilinear interpolation within a triangle, and I''m sure it will work out very nicely.
Timbatron
Timbatron
No offense dmounty... but i have no clue what you're trying to tell me. JuNC was right. I ended up just plugging the vectors of the triangle into a matrix equation and solved for the weights. IE: [v1 v2 v3] = vI (Of course I had to have an additional row because you have to have the weights sum to 1). Once you have barycentric coordinates, you just use the same coordinates on the texture... I'm not sure what part I didn't understand when I made my second post. (Of course, the algorithm that JuNC pointed me towards would have been ALOT faster i'm sure, but I dont need it to be fast, and understanding the code would have taken longer for me than to write my own function.)


[edited by - timbatron on November 24, 2003 12:35:30 AM]
My Page : http://acidbase.computed.net
dmounty
dmounty
Oh well.. it was a bit confusing reading it back :S. At least you got it working, thats all that counts in the end .

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.