`Notes: Perspective correct texturing and quaternion interpolation
`Kavita Bala
`
`This is a short note explaining the math behind perspective correct texturing and spherical
`linear interpolation for quaternions.
`
`1 Perspective Correct Texturing
`
`We want to derive the correct equations for perspective correct texturing and z computation. We
`will stay in 2D, but 3D is an easy generalization.
`Consider a 2D line with end points (X0, Z0) and (X1, Z1). These 2 points project (under a
`perspective transformation) to screen space points S0 and S1 respectively. We want to find the
`texture coordinates U that are correct even with the perspective transform.
`
`it’s screen
`Now consider a point (X, Z) that lies on the line between (X0, Z0) and (X1, Z2):
`space projection on the image plane is S, and its texture coordinate is U . Let the screen space
`interpolation parameter by q, and t be the world space interpolation parameter. Our goal is to find
`the way we need to interpolate in screen space to produce the perspective-correct U value.
`Let d be the distance of the image plane from the eye. Using similar triangles for the points,
`we have:
`
`X0
`Z0
`X1
`Z1
`
`=
`
`=
`
`S0
`d
`S1
`d
`
`1
`
`(1)
`
`(2)
`
`MEDIATEK, Ex. 1027, Page 1
`IPR2018-00102
`
`
`
`(3)
`
`(4)
`
`(5)
`
`Given an arbitrary point (X, Z), we have the equation for its projection S to be:
`
`S d
`
`=
`
`X Z
`
`Z =
`
`dX
`S
`
`The point S is computed by screen space interpolation of S0 and S1.
`S = S0 + q(S1 − S0)
`
`X and Z are obtained by world space interpolation of X0 and X1, and Z0 and Z1, respectively.
`[X, Z] = [X0 + t(X1 − X0), Z0 + t(Z1 − Z0)]
`
`(6)
`
`Substituting Equation 5 in Equation 6 we get:
`
`Z =
`
`=
`
`dX
`S
`d(X0 + t(X1 − X0))
`S0 + q(S1 − S0)
`
`(7)
`
`(8)
`
`(9)
`
`(10)
`
`(11)
`
`(12)
`
`Z =
`
`=
`
`d
`
`d + t (S1Z1−S0Z0)
`d( S0Z0
`S0 + q(S1 − S0)
`S0Z0 + t(S1Z1 − S0Z0)
`S0 + q(S1 − S0)
`But Z is Z0 + t(Z1 − Z0) from Equation 6. Thus,
`S0Z0 + t(S1Z1 − S0Z0)
`S0 + q(S1 − S0)
`
`Z0 + t(Z1 − Z0) =
`
`which can be simplified to
`Z0S0 + Z0q(S1 − S0) + t(Z1 − Z0)S0 + tq(Z1 − Z0)(S1 − S0) = S0Z0 + t(S1Z1 − S0Z0)
`t[S0Z1 − S0Z0 + q(Z1 − Z0)(S1 − S0) − S1Z1 + S0Z0] = −Z0q(S1 − S0)
`t(S1 − S0)[Z1 − q(Z1 − Z0)] = Z0q(S1 − S0)
`t[qZ0 + (1 − q)Z1] = Z0q
`
`giving us the value of t in terms of q:
`
`t =
`
`Z0q
`qZ0 + (1 − q)Z1
`
`2
`
`MEDIATEK, Ex. 1027, Page 2
`IPR2018-00102
`
`
`
`Substituting t in Equation 6 we get:
`Z0q(Z1 − Z0)
`Z = Z0 + t(Z1 − Z0) = Z0 +
`qZ0 + (1 − q)Z1
`0 + (1 − q)Z0Z1 + qZ0Z1 − qZ2
`qZ2
`qZ0 + (1 − q)Z1
`Z0Z1
`qZ0 + (1 − q)Z1
`1
`− 1
`+ q( 1
`Z1
`
`1Z
`
`=
`
`=
`
`=
`
`0
`And finally, letting W = 1/Z we get,
`
`0
`
`)
`
`Z0
`
`(13)
`
`(14)
`
`(15)
`
`(16)
`
`(17)
`
`(18)
`
`(19)
`
`(20)
`
`− 1
`+ q(
`)
`=
`Z0
`W = W0 + q(W1 − W0)
`Our goal is to compute the perspective correct Z value. This equation tells us that we can achieve
`that using screen-space interpolation, as long as we linearly interpolate W (which is reciprocal Z)
`in screen space using q, and then compute Z as 1
`W .
`For computing texture coordinates, the same substitution can be done. Except here we are
`interpolating the texture coordinate U . Given
`U = U0 + t(U1 − U0)
`and substituting t from Equation 12 we get:
`U0W0 + q(U1W1 − U0W0)
`W0 + q(W1 − W0)
`Thus the texture coordinates U can be correctly dervived by linearly interpolating in screen
`space (using q) as follows: interpolate U W and W and then compute U as interpolatedU W
`interpolatedW .
`
`1 Z
`
`1
`
`1 Z
`
`0
`
`1 Z
`
`U =
`
`2 Spherical Linear Interpolation
`
`We derive the slerp equations here. We want to rotate from the start quaternion q1 to the end
`quaternion q2. Simple linear interpolation will not move at a constant rate. Instead we need to
`interpolate along the geodesic of the 4D sphere. The quaternions are two points on this 4D sphere.
`The 4D “angle” between the two quaternions is given by the 4D dot product between q1 and
`q2. For q1 = (w1, x1, y1, z1), and q2 = (w2, x2, y2, z2), the angle between the two quaternions α is
`given as cos(α) = w1w2 + x1x2 + y1y2 + z1z2.
`
`3
`
`MEDIATEK, Ex. 1027, Page 3
`IPR2018-00102
`
`
`
`Now we derive the equation for the quaternion q that interpolates q1 and q2. We want q to
`interpolate the angle α, such that q = q1 when t = 0, and q = q2 when t = 1, and q subtends
`angle tα at t. Remember that since we have unit quaternions, the 4D sphere the quaternions lie
`on (shown) has radius 1.
`Consider the axis ˆw perpendicular to q1 as shown, and (cid:126)v, the projection of q parallel to ˆw.
`
`Also,
`
`(cid:126)v = sin(α) ˆw
`(cid:126)v
`sin(α)
`
`ˆw =
`
`(cid:126)v + q1cos(α) = q2
`q2 − q1cos(α)
`sin(α)
`
`ˆw =
`
`q is given as its projection along q1 plus (cid:126)v:
`
`(21)
`
`(22)
`
`(23)
`
`(24)
`
`(cid:126)q = cos(tα)q1 + sin(tα) ˆw
`q2 − q1cos(α)
`= cos(tα)q1 + sin(tα)
`sin(α)
`= cos(tα)q1 − sin(tα)q1cos(α)
`sin(α)
`= [cos(tα) − sin(tα)cos(α)
`]q1 +
`sin(α)
`cos(tα)sin(α) − sin(tα)cos(α)
`sin(α)
`sin(α − tα)
`sin(tα)
`q1 +
`sin(α)
`sin(α)
`sin(α(1 − t))
`sin(tα)
`sin(α)
`sin(α)
`where we use the identity sin(m − n) = sin(m)cos(n) − cos(m)sin(n).
`
`q2
`
`q2
`
`sin(tα)
`sin(α)
`sin(tα)
`sin(α)
`sin(tα)
`sin(α)
`
`q1 +
`
`q2
`
`=
`
`=
`
`(cid:126)q =
`
`q1 +
`
`+
`
`q2
`
`q2
`
`4
`
`MEDIATEK, Ex. 1027, Page 4
`IPR2018-00102
`
`