Texture Mapping and Perspectively Correct Interpolation in Computer Graphics, Study notes of Computer Graphics

The concept of texture mapping in computer graphics, which involves applying a color pattern defined by an image onto a 3d surface for realistic color variation. The document also discusses the importance of perspectively correct interpolation for texture coordinates to ensure accurate texture application, especially when dealing with objects at different distances or sizes. The document further delves into the mathematical aspects of texture mapping and interpolation using matrices and coordinates.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-w5e
koofers-user-w5e 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1 Texture
The most common form of texture mapping means laying a color pattern defined
by an image onto a 3D surface. It allows to render 3D surfaces with realistic
color variation. From programmer’s perspective, this is done by providing tex-
ture coordinates for each vertex. For each triangle of the 3D object the texture
applied to it is copied from the triangle bounded by points given by the tex-
ture coordinates (see Figure 1). More precisely, texture coorinates are linearly
interpolated when the triangle is scan-converted. Then, on the pixel processing
stage, they are used to look up color from the texture image. Texture coordi-
nates (0,0), (0,1), (1,0) and (1,1) correspond to the corners of the image. There
are several ways to handle the case of one or both coordinates being outside of
the range from 0 to 1. For example, one can use only the fractional part of
each coordinate to do texture lookup or they can both be clamped to [0,1] (see
‘Repeating and Clamping Textures’ in Chapter 9 of the OpenGL programming
guide).
1
1
(0,0)
(u2,v2)
(u3,v3)
(u1,v1)
(u1,v1)
(u3,v3) (u2,v2)
Figure 1: Applying a texture (left) to a 3D triangle (right). (ui, vi) are the
texture coordinates of the vertices. The contents (colors) of the image specified
as the texture are copied to the 3D triangle. Note that this requires stretching
or shrinking the texture, particularly if the two triangles differ a lot in shape or
size.
OpenGL allows to use up to 4 texture coordinates and they can be trans-
formed using a 4 ×4 texture matrix. Eventually (i.e. after this transformation),
only two texture coordinates are used.
Apart from 2D trextures, one can also use one-dimensional textures or three
dimensional textures. In one-dimensional case, only one texture coordinate is
used to look up the color and the texture is one-dimensional. In the three
dimensional case, the texture is 3D (think of it as a colored volume) and three
texture coordinates are used for color lookup. The simplest application of 3D
texture is to use texture that describes color distribution in some material, e.g.
wood, concrete or marble and use vertex coordinates as texture coordinates.
This causes a color of a point on the 3D surface to be looked up from its
location in the volume and produces an object looking as if it was carved from
the volume.
1
pf3
pf4
pf5

Partial preview of the text

Download Texture Mapping and Perspectively Correct Interpolation in Computer Graphics and more Study notes Computer Graphics in PDF only on Docsity!

1 Texture

The most common form of texture mapping means laying a color pattern defined by an image onto a 3D surface. It allows to render 3D surfaces with realistic color variation. From programmer’s perspective, this is done by providing tex- ture coordinates for each vertex. For each triangle of the 3D object the texture applied to it is copied from the triangle bounded by points given by the tex- ture coordinates (see Figure 1). More precisely, texture coorinates are linearly interpolated when the triangle is scan-converted. Then, on the pixel processing stage, they are used to look up color from the texture image. Texture coordi- nates (0, 0), (0, 1), (1, 0) and (1, 1) correspond to the corners of the image. There are several ways to handle the case of one or both coordinates being outside of the range from 0 to 1. For example, one can use only the fractional part of each coordinate to do texture lookup or they can both be clamped to [0, 1] (see ‘Repeating and Clamping Textures’ in Chapter 9 of the OpenGL programming guide).

1

1

(0,0)

(u2,v2)

(u3,v3)

(u1,v1)

(u1,v1)

(u3,v3) (u2,v2)

Figure 1: Applying a texture (left) to a 3D triangle (right). (ui, vi) are the texture coordinates of the vertices. The contents (colors) of the image specified as the texture are copied to the 3D triangle. Note that this requires stretching or shrinking the texture, particularly if the two triangles differ a lot in shape or size.

OpenGL allows to use up to 4 texture coordinates and they can be trans- formed using a 4 × 4 texture matrix. Eventually (i.e. after this transformation), only two texture coordinates are used. Apart from 2D trextures, one can also use one-dimensional textures or three dimensional textures. In one-dimensional case, only one texture coordinate is used to look up the color and the texture is one-dimensional. In the three dimensional case, the texture is 3D (think of it as a colored volume) and three texture coordinates are used for color lookup. The simplest application of 3D texture is to use texture that describes color distribution in some material, e.g. wood, concrete or marble and use vertex coordinates as texture coordinates. This causes a color of a point on the 3D surface to be looked up from its location in the volume and produces an object looking as if it was carved from the volume.

Figure 2: Sizes of windows depend on how far they are.

2 Perspectively correct interpolation

In graphics pipeline setting, texture coodinates enter the pipeline as vertex attributes. They are interpolated in the rasterization stage. Interpolated texture coordinates are passed on as fragment attributes. In the fragment processing stage, the color of each fragment is looked up from the texture image (the 1D or 3D cases work the same way, except for 1D or 3D texture is used instead of image/2D texture).

2.1 Inadequacy of screen space interpolation of texture

coordinates

So far (as a way to compute depth or color of fragments) we were using screen space linear interpolation. The data to be interpolated was sent with vertices in screen coordinates from vertex processor to the rasterizer. The rasterizer interpolated this data from projected vertices to fragments. This happened for depth, color in Gouraud shading and normals in Phong shading.

resulting point by (x′, y′, z′), we can write

 

x′ y′ z′

 ≈ P ∗

xA yA zA 1

where P is in the following simple 3 × 4 matrix:

P =

0 0 kl −l

Putting all of the above together, we can write the entire transformation as  

x′ y′ z′

 ≈ M ∗

x y z 1

where M = P ∗ A is a 3 × 4 matrix. Now, let’s say we have a triangle with texture coordinates in 3D. We want to linearly interpolate texture coordinates to points on this triangle. This process can be described as an assignment of a 3D point to texture coordinates (u, v). The 3D point corresponding to (u, v) should get color the texture image has at (u, v). Since we use linear interpolation, this defines an affine mapping of texture coordinates into 3D (in Figure 1, this mapping is shown as the blue arrow), which can be thought of as a parametrization of the triangle over texture coordinates. This mapping can be represented using a 4 × 3 matrix T :    

x y z 1

 =^ T^ ∗

u w 1

Using this equation with (1) we obtain one that essentially describes how the texture image needs to be mapped onto the screen:  

x′ y′ z′

 ≈ M ∗ T ∗

u w 1

The matrix M depends only on the view, so it can be computed when the view is specified (just once before rendering all triangles). The matrix T has to be computed on per-triangle basis, based on the coordinates and texture coordinates of its vertices. Notice that B = M ∗ T is a 3 × 3 matrix. If B = [bij ]i=1... 3 ,j=1... 3 , we can write  

u w 1

b 22 b 33 − b 23 b 32 b 13 b 32 − b 12 b 33 b 12 b 23 − b 13 b 22 b 23 b 31 − b 21 b 33 b 11 b 33 − b 13 b 31 b 21 b 13 − b 11 b 23 b 21 b 32 − b 22 b 31 b 12 b 31 − b 11 b 32 b 11 b 22 − b 21 b 12

x′ y′ z′

The 3 × 3 matrix in the above equation plays the role of the inverse of B: even though it is not the same as the inverse of B, it is proportional to it (which is enough for the equation hold up to a constant factor; note it makes things possible to carry out also in some degenerate cases in which B is not invertible and is also faster since it does not require division or determinant calculation). In fact, this equation (2) also describes precisely what the rasterizer needs to do to determine the perspectively correct texture coordinates that need to be used with each fragment. Let’s say the rasterizer generates a fragment at (px, py ). It knows how to compute its depth pz. Now, it has all three coordinates of the fragment in screen coordinates. It evaluates the right-hand side of (2) to

compute a vector proportional to

u w 1

. Let’s say this vector has coordinates

[u∗, v∗, w∗]. Then, u can be computed as u∗/w∗ and v - as v∗/w∗. An important thing to note here is that u∗, v∗, w∗ are given by linear ex- pressions in x′^ and y′^ (equation (2) shows they are linear in x′, y′, z′^ and we know that z′, i.e. depth, is linear in x′^ and y′). Thus, to perform perspectivelly correct interpolation of texture coordinates, the rasterizer needs to do three lin- ear interpolations (needeed to compute u∗, v∗, w∗) and do two divisions for each fragment. The coefficients needed for linear interpolation can be precomputed on per-triangle basis for efficient computation in scanline order.