I’ve been teaching myself OpenGL by developing my own software implementation of the API. Currently I am stuck on a problem related to non-perspective correct texture mapping.
The problem occurs in my clipper when I am trying to interpolate texture coordinates along an edge that stretches across the near frustum clip plane and into -w coordinate space. What I require of the clipper is to perform a hyperbolic interpolation to determine the texture and color values of the point clipped against the near frustum clip plane.
I developed the following algorithm to calculate the hyperbolic interpolation factor ftHyper that can be used for interpolating texture and color values for non-perspective correct rendering. It works in all circumstances except for edges crossing into the -w coordinate space where ftHyper is no longer correct:
/////////////////////////////////////////////////////////////////////
/// pDest is the vertex we will be interpolating to
/// pStart is the vertex we will be interpolating from
/// pEnd is the vertex at the end point of the edge
/// ftLinear is the linear interpolation factor
/// ftHyper is the hyperbolic interpolation factor that we must find
void DetermineHyperbolic_t(PVERTEX pDest, const PVERTEX pStart, const PVERTEX pEnd, float ftLinear, float & ftHyper)
{
// This function operates in eye-space coordinates
float numerator, denominator;
denominator = pDest->position.v[3] * (pEnd->position.v[3] - pStart->position.v[3]);
// If the denominator is 0.0 exactly then the linear factor t and hyperbolic factor t
// are equal. If the denominator is very near 0.0 then to avoid precision issues we
// can also approximate the hyperbolic factor t with the linear factor.
if (fabsf(denominator) < 0.0001f) {
ftHyper = ftLinear;
}
else {
numerator = pEnd->position.v[3] * (pDest->position.v[3] - pStart->position.v[3]);
ftHyper = numerator / denominator;
}
}
Unfortunately my mathematical understanding of 3D homogenous coordinate spaces is severely lacking right now (up until 4 months ago I had never even heard of homogenous coordinates). I simply can’t see how to fix this algorithm from breaking when there is -w coordinate. I have been considering trying to determine ftHyper from the x,y, and z coordinates of the vertex instead of w but I see this solution as ugly and would like to keep my calculations to simply the w coordinate.
If anyone could provide some insight on why my algorithm is failing and to perhaps suggest a solution it would be greatly appreciated.
Thank you,
-Toasty