Perspectively Correct Texture Mapping
And Color Interpolation
Andrew G. Zaferakis
UNC Chapel Hill
COMP 236 Spring 2000

Homework 6 - Texture Mapping
    For this assignment we implemented a portion of the OpenGL texture mapping model.  This software is building upon the SoftGL software OpenGL implementation we have been developing all semester.  The various feature are listed below.

Examples of each feature are below

Feature
Effect
Texture Clamping
Texture coordinate values outside the range [0,1) are clamped into the range.
Texture Repeating
Texture coordinate values may be specified outside the range [0,1) and are wrapped around so that the texture repeats itself.
Color Replacement
The color at each pixel is replaced by the color in the texture map, essentially placing a decal of the texture on the triangle.
Color Modulation
The color at each pixel is modulated (multiplied) by the color in the texture map.
Nearest Filtering
The color of the nearest texel is used to color the pixel.
Bilinear Filtering
The weighted average of the four nearest texels is used to color the pixel.  Bilinear is used for 2D textures, which is what is supported.
Perspectively Incorrect
The texture coordinates are not properly interpolated in three dimensions.
Perspectively Correct
The texture coordinates are correctly interpolated in three dimensions, using s/w, t/w (see below)

Understanding Perspective

    The visible differences between perspectively correct and incorrect interpolation is most noticable when looking at the texture mapped quads below.  Look closely at the 45 degree quads, notice in the perspectively incorrect quad, the color is uniformly distributed just like the flat quad.  The correct image shows the colors closer to the viewer dominate more of teh image, just like the elongated squares on the checkerboard.  It is amazing how almost all hardware interpolated colors incorrectly, checkout my hardware evaluation on color interpolation.
    To correctly interpolate colors and texture coordinates, they must first be divided through by homogeneous coordinate w.  The w coordinate becomes 1/w.  These new values are then linearly interpolated in screen space.  At each pixel, the coordinate is then divided by the interpolated 1/w (essentially multiplying by w) to obtain the correct value.  The transformation between spaces is best shown by Paul Heckbert and Henry Moreton "Interpolation for Polygon Texture Mapping and Shading"

The first batch of images below show how perspective effects an image.

(click on images for larger, more clear versions)

Perspectively Correct
Perspectively Incorrect
Perpendicular to Viewer Color Quad
Perpendicular to Viewer Color Quad
45 Degree Angle Color Quad

notice how the red is more dominant across the face of the quad, it is closer to the viewer.
45 Degree Angle Color Quad

notice how the colors are divided evenly throughout the quad, as if there was no depth.
Flat Checkerboard Texture

same as the perspectively incorrect
Flat Checkerboard Texture

no perspective, therefore equal
45 Degree Angle Texture

notice the closer square are larger. (see below)
45 Degree Angle Texture

notice the problem along the edge of the two triangles.
Sharp Perspective Texture

notice how the closer squares are much larger and elongated, and the farther ones are smaller and shrunk.

Sharp Perspective Texture

notice how incorrect interpolation destroys texture mapping the two triangles.  You can see how the squares maintain the same size at all times.

Clamping Textures
    Texture coordinates may be assigned values outside the range [0,1).  Ther are two methods for dealing with this, texture clamping and texture repeating.  When clamping is specified, any coordinate outside the range is clamped to the min, max appropriately.  The actual texel range is:

s = [1/(2*TW), 1-1/(2*TW)]
t = [1/(2*TH), 1-1/(2*TH)]

Where TW is the texture width and TH is the texture height.  (Note: OpenGL currently only supports 2nx2n textures).  This is because the actual texels are in the middle of the square, similar to the way we perform rasterization at the pixel level.  The images below demonstrate texture clamping.

(click on images for larger, more clear versions)

Perspectively Correct Clamping
Perspectively Incorrect Clamping
Perpendicular
Perpendicular
45 Degree Angle
45 Degree Angle
Nearest

notice the jagged lines
Bilinear

notice the smoother (blurry) lines
Perspectively Correct
Perspectively Incorrect

Repeating Textures
    If repeat mode is used, any coordinate outside the range of texels is wrapped around to the opposite side of the texture.  When a coordinate is greater then 1, the floating point fraction is used.  When a coordinate is less then zero, the coordinate - floor(coordinate) is used.  For example, the texture coordinate (-0.1, 0.5) will wrap to (0.9, 0.5) not +0.1.  The general formula for wrapping textures is:

coordinate = coordinate - floor(coordinate)

Note that this is only necessary for values less then zero, if the value is positive a simple mod can be used.  Below are images demonstrating texture wrapping.

(click on images for larger, more clear versions)

Perspectively Correct Repeating
Perspectively Incorrect Repeating
Perpendicular
Perpendicular
45 Degree Angle
45 Degree Angle
Nearest

notice the jagged lines
Bilinear

notice the smoother (blurry) lines
Perspectively Correct
Perspectively Incorrect

Replace and Modulate
    When in replace mode, the color at each pixel is determined by the color obtained from the texture map (nearest or linear filtering).  Any colors specified on the geometry itself is ignored.  When modulation is specified, the color of each pixel is determined by the color obtained from the geometry multiplied by the value obtained from the texture map (nearest or linear filtering).  Below are images of replace (decaling) and modulation.
 

(click on images for larger, more clear versions)

Replace
Modulate
Perpendicular
Perpendicular
45 Degree Angle Nearest
45 Degree Angle Bilinear

Nearest and Bilinear Filtering
    When looking at images of the checkerboard texture, you will notice aliasing (jaggies) on the lines.  This is where the line jumps a pixel.  This type of image uses nearest filtering.  The color of the pixel is determined by the nearest texel in the texture map.
    A way to smooth out the lines (antialias) is to use linear filtering.  The term linear is a generalization, since we can perform 1D (linear) 2D (bilinear) and 3D (trilinear) filtering using the same technique.  In the images here, I have used bilinear interpolation, since I have 2D texture maps.  The weighted average of the four closest texels is used to color each pixel.  The four closest texels are determined depending upon which wrapping mode is used (clamp or repeat).  When clamping is used, if any of the four texels falls out of the texel range, the texel on the boarder is used in the average twice (or more depending on the situation).  If repeating textures, the texel that is on the opposite side (wrapped around) of the texture is used in the average.  Using this method it is easy to understand how this can be applied to 1D (2 texels) and 3D (6 texels).
    There are pro's and con's for using each method.  If speed is desired, then the nearest filter will run the fastest, while the image may appear quite jagged depending on the texture and orientation.  If quality is preferred, then perhaps linear filtering is appropriate, at the cost of performance.  One common consequence of linear filtering is a blurring of the lines, which can easily be seen in the various images displayed.

Putting it all Together
    Now that you have seen the various methods of texture mapping, I have put together a high resolution (1600x1200) image displaying each feature.  Note that the entire image is rendered perspectively correct, I didn't have to do this, but it just looks much better!  Click on the image for the full size.

The Interactive Demo
    To get a feel for how things work, try out the demo program using the SoftGL system.  Notice how the textures are distorted when using perspectively incorrect interpolation.  Here are the controls:
 
 

SoftGL Viewer Controls (Texture Mapping)
Mouse  Used to Rotate
Left Button Locks mouse movements to controls
, Zoom In
. Zoom Out
1 Use Texture Map 1 (Repeat, Nearest)
2 Use Texture Map 2 (Clamp, Bilinear)
3 Replace Mode
4 Modulate
b Toggle Backface Culling
s Toggle Smooth/Flat Color Interpolation
l Toggle Lighting
t Toggle Texture Mapping
p Toggle Test Plane
c Toggle Perspective Correction
\ Toggle Moving/Stationary Lights
` Toggle Deforming/Stationary Cube
; Decrease Spotlight Exponent
' Increase Spotlight Exponent
[ Decrease Spotlight Cutoff
] Increase Spotlight Cutoff
ESC Exit
SPACE Toggle SoftGL vs OpenGL mode
*most* OpenGL supports only perspectively correct texture mapping.

(click on images for larger, more clear versions)

Perspectively Correct
Perspectively Incorrect

notice the difference in the faces.

click to see a larger version
home

346 Sitterson Hall, CB #3175, Chapel Hill, NC 27599-3175, andrewz@cs.unc.edu