PDA

View Full Version : texture coordinates



miko
01-01-2003, 09:15 AM
i've got a 2d polygon, i've got coordinates of each vertex and texture coordinates too.

and now i want to split polygon into the smaller ones. and the problem is, i don't know how to determine texture coorinates in a new vertex.

so again: a have a polygon, i know texture coordinates of each vertex and i want to determine texture coordinates at some new place inside the polygon... http://www.opengl.org/discussion_boards/ubb/confused.gif

plz heeeelp http://www.opengl.org/discussion_boards/ubb/smile.gif
i've searched all the net, i tried begginer forum and this is my last hope http://www.opengl.org/discussion_boards/ubb/wink.gif

Coriolis
01-01-2003, 09:50 AM
Well, the answer to this could be easy or hard, depending on how well-behaved your polygon is. If your polygon is planar and your texture mapping is linear, the problem is somewhat easy. If your polygon is not planar or if your texture mapping is non-linear, then there is no single "right" solution, since the texture coordinate you want depends on how you triangulate the polygon. You should triangulate polygons that are non-planar or that have non-linear texture coordinates anyway, and any non-degenerate triangle satisfies the planar and linearity requirements, so this isn't that big a deal.

So, I'll proceed with the assumption that you have a planar polygon with linear texture coordinates. What I have done in the past is to find a pair of projection vectors and constant biases that give me the S and T texture coordinates for any world position. So, I have
s = P * svec + sorig
t = P * tvec + torig

where P is the point for which I want to find the texture coordinates, svec and tvec are vectors, * is dot product, and sorig and torig are numbers.

We can find svec and s0 by solving a system of linear equations. The exact same approach can be used to solve for tvec and t0. The system looks like this for the s equations:

s0 = P0_x * svec_x + P0_y * svec_y + P0_z * svec_z + sorig
s1 = P1_x * svec_x + P1_y * svec_y + P1_z * svec_z + sorig
s2 = P2_x * svec_x + P2_y * svec_y + P2_z * svec_z + sorig

There are 3 equations for 4 unknowns, so there are an infinite number of solutions (or no solutions) to this system. We know that all the points lie on a common plane. This means that one of svec_x, svec_y, or svec_z can be zero. Picking the component corresponding to the component of the normal which has largest magnitude is the choice I've always used, since it is the most numerically stable and never has divide-by-zero issues for axial planes. This reduces the system to 3 equations in 3 unknowns. I typically solve it (and detect the case of no solution) using Cramer's rule. I'll frequently also check the results, to make sure the vectors I get do give me the expected coordinates for the other vertices within some small error.

This approach will work for concave polygons as well as convex. It can also give you texture coordinates for any point on the plane, not just those inside the polygon. If you know that your new point is inside the polygon, you could also find a triangle that is a subset of your polygon that contains the new point and do barycentric interpolation of texture coordinates.