[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.12.3.4 Bringing it All Together

Using all this information we have enough to correctly map a texture on screen. Let's disregard clipping for the moment and just explain all the steps from the original object space polygon until the final texture mapped polygon on screen.

We will assume that the polygon (and the texture) has already been transformed from object to world space. So we start with a world space polygon, Pw.

First all vertices of the polygon are transformed to camera space (note that in Crystal Space this is done earlier since vertices are shared for one sector. This text ignores that and just concentrates on one polygon) with the equation:

Vc = Mwc * (Vw - Vwc)

(Also note that at this point you could discard vertices because they are behind the view plane (or Z = 0). We assume here that the polygon is completely visible so this does not matter.)

At this point we do perspective correction on the polygon. This means that we create a new 2-dimensional polygon with vertices Vs (in screen space) using the following equations:

Vs.x = (F * Vc.x) / Vc.z
Vs.y = (F * Vc.y) / Vc.z

Now we create the matrix to transform camera space to texture space starting from the matrix to transform world space to texture space. Given:

Vc = Mwc * (Vw - Vwc)

We calculate (using the inverse matrix of Mwc):

Mcw * Vc + Vwc = Vw

(Equation 1)

Given also:

Vt = Mwt * (Vw - Vwt)

(Equation 2)

We substitute (1) into (2) to get:

Vt = Mwt * (Mcw * Vc + Vwc - Vwt)

This can also be re-written as:

Vt = Mwt * (Mcw * Vc + Mcw * Mwc * (Vwc - Vwt))

Which simplifies to:

Vt = Mwt * Mcw * (Vc + Mwc * (Vwc - Vwt))

If we say that:

Mct = Mwt * Mcw
Vct = Mwc * (Vwt - Vwc)

We then get:

Vt = Mct * (Vc - Vct)

And this is the equation transforming camera space to texture space.

Then we need to transform the world space plane equation to a camera space plane equation. This we do as follows.

The plane vector Nw = (Aw,Bw,Cw) is transformed to Nc = (Ac,Bc,Cc) using the following equation:

Nc = Mwc * Nw

Using the first vertex of the polygon in camera space coordinates (Vc) we then calculate Dc as follows.

Since the plane equation in camera space is equal to:

Ac * Vc.x + Bc * Vc.y + Cc * Vc.z + Dc = 0

For every vertex Vc on the polygon we can calculate the missing Dc as follows:

Dc = -Ac * Vc.x - Bc * Vc.y - Cc * Vc.z

Using this information (the polygon in perspective corrected 2D coordinates, the transformation from camera space to texture space and the plane equation in camera space) we can draw the polygon on the screen and perform correct texture mapping. This happens as follows.

From the perspective correction equations:

Vs.x = (F * Vc.x) / Vc.z
Vs.y = (F * Vc.y) / Vc.z

We can invert them to:

Vc.x = (Vs.x * Vc.z) / F

(Equation 3)

Vc.y = (Vs.y * Vc.z) / F

(Equation 4)

We can now substitute (3) and (4) into the following equation:

Ac * Vc.x + Bc * Vc.y + Cc * Vc.z + Dc = 0

And get:

(Ac * Vs.x * Vc.z) / F + (Bc * Vs.y * Vc.z) / F + (F * Cc * Vc.z) / F = -Dc

Or:

-(Ac * Vs.x) / (F*Dc) - (Bc * Vs.y) / (F*Dc) - Cc / (F*Dc) = 1 / Vc.z

This equation is very important. From this it follows that 1/z linear is in screen space and this can be used for perspective correct texture mapping. Lets define the following three new variables:

M = -Ac / (F * Dc)
N = -Bc / (F * Dc)
O = -Cc / Dc

So the 1/z equation in linear screen space is then written as:

1 / Vc.z = M * Vs.x + N * Vs.y + O

(Equation 5)

So now we can easily calculate 1/z at every point in screen space. But we also need to calculate the texture coordinates (u,v) or Vt. Let's call the individual fields of the transformation matrix Mct as follows:

 
      / m11 m12 m13 \
Mct = | m21 m22 m23 |   Vct = (v1 v2 v3)
      \ m31 m32 m33 /

For simplicity let's use u for Vt.u and v for Vt.v (the u,v texture coordinates). Let us also use x, y, and z for Vc.x, Vc.y, Vc.z respectively.

Then from:

Vt = Mct * (Vc - Vct)

We get:

u = m11 * (x - v1) + m12 * (y - v2) + m13 * (z - v3)
v = m21 * (x - v1) + m22 * (y - v2) + m23 * (z - v3)

This can be rewritten as:

u = m11 * x + m12 * y + m13 * z - (m11 * v1 + m12 * v2 + m13 * v3)
v = m21 * x + m22 * y + m23 * z - (m21 * v1 + m22 * v2 + m23 * v3)

To simplify let's introduce a couple new variables to take the place of complicated expressions from the above equations.

P = - (m11 * v1 + m12 * v2 + m13 * v3)
Q = - (m21 * v1 + m22 * v2 + m23 * v3)

And we have:

u = m11 * x + m12 * y + m13 * z + P
v = m21 * x + m22 * y + m23 * z + Q

As earlier, we substitute the inverse perspective correction equations (3) and (4) into the previous equations and we get:

u = (m11 * Vs.x * z) / F + (m12 * Vs.y * z) / F + m13 * z + P
v = (m21 * Vs.x * z) / F + (m22 * Vs.y * z) / F + m23 * z + Q

And then rewrite as:

u / z = (m11 * Vs.x) / F + (m12 * Vs.y) / F + m13 + P / z
v / z = (m21 * Vs.x) / F + (m22 * Vs.y) / F + m23 + Q / z

Substitute the linear 1/z equation (5) into this to get:

u / z = (m11 * Vs.x) / F + (m12 * Vs.y) / F + m13 + P * (M * Vs.x + N * Vs.y + O)
v / z = (m21 * Vs.x) / F + (m22 * Vs.y) / F + m23 + Q * (M * Vs.x + N * Vs.y + O)

Rewrite as:

u / z = (m11 * Vs.x + m12 * Vs.y + F * (m13 + P * (M*Vs.x + N*Vs.y + O)) / F
v / z = (m21 * Vs.x + m22 * Vs.y + F * (m23 + Q * (M*Vs.x + N*Vs.y + O)) / F

And finally rewrite as:

u / z = (m11 / F + P * M) * Vs.x + (m12 / F + P * N) * Vs.y + (m13 + P * O)
v / z = (m21 / F + Q * M) * Vs.x + (m22 / F + Q * N) * Vs.y + (m23 + Q * O)

These are again two important equations because they state that u/z and v/z are also linear in screen space. Using this we can easily calculate (u,v) at every screen space point.

Now let's define:

J1 = m11 / F + P * M
J2 = m12 / F + P * N
J3 = m13 + P * O
K1 = m21 / F + Q * M
K2 = m22 / F + Q * N
K3 = m23 + Q * O

Then we have the following three equations:

1 / z = M * Vs.x + N * Vs.y + O
u / z = J1 * Vs.x + J2 * Vs.y + J3
v / z = K1 * Vs.x + K2 * Vs.y + K3

With these three important equations we can do all texture mapping we want. With the first equation we can calculate 1/z. This is useful for Z-buffering and also for calculating (u,v) from the two other equations.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated using texi2html 1.76.