Describing the textured 3D polygon.
Let points_in_poly = The number of points that define the 3D polygon.
Let Pn = Point n in 3D polygon.
Let Tn = Texture point corresponding to point n in 3D polygon.
Let tex_per_world_units = The size of a pixel per units.
Let xoff = X offset of the texture map.
Let yoff = Y offset of the texture map.
struct TexInfo
{
TexImage image;
Vector3D u;
Vector3D v;
Vector3D n;
Ce2dPoints[points_in_poly]; // Equivalent of Tn
}
u' = P2 - P1
u'' = u' / ||u'|| // Normalized u vector
u = tex_per_world_units * u'' // Scaled u vector
n' = (P1 - P3) X u
n = n' / ||n'|| // Normalized n vector
v' = n X u''
v'' = v' / ||v'|| // Normalized v vector
v = tex_per_world_units * v'' // Scaled v vector
Tn = ((u X (Pn - P0)) - xoff, (v X (Pn - P0)) - yoff)
In my code, I also rotate u by some angle, but that's done using a simple rotation matrix. After its rotated, I again make sure that v is still orthogonal to both n and u.
Setup for drawing the polygon.
struct TexDrawInfo
{
TexImage image; // The image to use in texture mapping.
Vector3D u; // Vector on the polygon's plane that points in the
// direction of increasing x in the texture's space.
Vector3D v; // Vector on the polygon's plane that points in the
// direction of increasing y in the texture's space.
Vector3D n; // A vector that is orthogonal to the polygon's plane.
// Let distance = The distance of the focus to the viewport.
real uz_distance; // A precalculated value for Uz * distance.
real vz_distance; // A precalculated value for Vz * distance.
real nz_distance; // A precalculated value for Nz * distance.
real u_off; // The [U,C] constant offset as shown in the discussion
// on texture mapping.
real v_off; // The [V,C] constant offset as shown in the discussion
// on texture mapping.
real scalar; // The [N,P] constant scalar as shown in the discussion
// on texture mapping.
}
Let (x', y') = The screen coordinates.
Let (x, y, z) = The coordinates for the position of the texture point.
Let P = Any point on the polygon's plane.
Let T = Texture point in texture coordinates.
Let C = Point on the polygon to be the texture map's origin.
Let N = Normalized vector that is orthogonal to the polygon's plane
Let U = Normalized vector on the polygon's plane that points in the
direction of increasing x in the texture space.
Let V = Normalized vector on the polygon's plane that points in the
direction of increasing y in the texture space.
Tx = [U,(x, y, z)]
Ty = [V,(x, y, z)]
Tx = (([N,P] / [N,(x', y', d)]) * [U,(x', y', d)]) - [U,C]
Ty = (([N,P] / [N,(x', y', d)]) * [V,(x', y', d)]) - [V,C]
Let n = The starting point number.
Let (Txn, Tyn) = Texture point corresponding to point n in 3D polygon.
Let (xn', yn') = Screen coordinates of 3D polygon point n.
Let (xn, yn, zn) = 3D polygon coordinates of point n.
Let uz_distance = A precalculated value for Uz * distance.
Let vz_distance = A precalculated value for Vz * distance.
Let nz_distance = A precalculated value for Nz * distance.
Let u_off = The [U,C] constant offset.
Let v_off = The [V,C] constant offset.
Let scalar = The [N,P] constant scalar.
uz_distance = Uz * d
vz_distance = Vz * d
nz_distance = Nz * d
scalar = [N,(xn, yn, zn)]
u_off = ((scalar / [N,(xn', yn', d)]) * [U,(xn', yn', d)]) - Txn
v_off = ((scalar / [N,(xn', yn', d)]) * [V,(xn', yn', d)]) - Tyn
Finding the pixel in the texture given a point on the screen.
Let temp = Intermediate value used for both answers. temp = scalar / [N,(x', y', d)] Tx = (temp * [U,(x', y', d)]) - u_off Ty = (temp * [V,(x', y', d)]) - v_off
Where is this technique used?