PS2 Linux Programming
Perspective Correct Texturing
Introduction
In this tutorial STQ
texture coordinates are used in order to provide perspective correct texturing
of a quad. In addition, the graphics pipeline has been wrapped up into a class
and the information needed to specify each vertex has been given a more
manageable structure.
Texture perspective
distortion is illustrated in figure 1 below.
Figure 1
The basic problem is that
uniform steps in screen space do not equate to uniform steps in world
coordinates. Since texturing is performed in screen space, the texture (which
should be correct in world space) becomes distorted.
The GS has the ability to
correct texture perspective distortion. This is done by giving the GS texture
coordinates (S, T, Q) for each vertex. S, T and Q are defined below:
S = s/W
T = t/W
Q = 1/W
The s and t values are the
texture coordinates in texture space measured from the top left hand corner of
the texture (similar to the use of u and v texture coordinates) where both s
and t are floating point numbers which range from 0.0 to 1.0. (0.0, 0.0) is the
top left hand corner of the texture and (1.0, 1.0) is the bottom right hand
corner of the texture. W is the homogeneous vertex coordinate resulting from
the application of the perspective transformation matrix to a vertex.
A number of modifications
have been incorporated in order to provide perspective correct texturing. The
S, T, Q values are obtained in the graphics pipeline after the application of
the perspective transformation matrix with the following code.
// Get Perspective correct
texture coordinates
for(int i = 0; i < 4;
i++)
{
pSprite->Vertex[i].Q =
1.0f/pSprite->Vertex[i].TVertex.v[3];
pSprite->Vertex[i].S = pSprite->Vertex[i].s *
pSprite->Vertex[i].Q;
pSprite->Vertex[i].T = pSprite->Vertex[i].t *
pSprite->Vertex[i].Q;
}
pSprite->Vertex[i].TVertex.v[3]
is effectively the homogeneous W coordinate for the vertex being processed.
When building the GS primitive,
the texture coordinate method being used is STQ (PRIM_FST_STQ). In the
primitive data, each vertex now has three quad word of data associated with it:
Texture coordinates (STQ), Colour (RGBAQ) and position (XYZ2). Note that it is
important to send the information in this order and to also send the colour
information with each vertex. When the STQ data is sent to the GIF, the Q value
is stored in an internal GIF register and is not sent to the GS. When the
colour data is sent to the GIF, the currently stored Q value within the GIF is
sent to the GS as well as the RGBA values. Note also that the Q value is
initialised to 1.0 whenever a GifTag is read by the GIF.
The functional operation
of the tutorial code is identical to that of the previous tutorial.
In this tutorial a wrapper
class has been introduced for the graphics pipeline calculations and correct
perspective texture mapping of a quad has been performed. Once again, the code
produced is neither efficient nor optimal, but is written in a manner to
illustrate the techniques being described.
Dr Henry S Fortuna
University of Abertay
Dundee