Looking Up Texture Values in Texture Mapping



Texture mapping is a technique to apply images or textures onto 3D surfaces. This process adds realistic shades and depth to computer-generated 3D scenes. It provides details like colors, patterns, and surface irregularities. However, texture mapping is complex due to potential distortions and the need for accurate mapping functions.

In this chapter, we will discuss the fundamental process of looking up texture values, using a simple example to illustrate the concept.

What is Texture Mapping?

Texture mapping uses images to define the surface details of a 3D object. When a shader computes the color of a surface, it needs to know the corresponding color in the texture image. This color is fetched using a process called a texture lookup. Texture lookup identifies the exact location on the texture image corresponding to a point on the 3D surface.

In the following image, observe that there is no floor texture.

What is Texture Mapping?

After adding floor texture, it will look like −

After Adding Floor Texture

Consider a scene with a wood floor texture applied to the ground. When light interacts with the floor, the shader needs to determine the wood pattern’s color at each point on the surface. This process involves converting the surface point to a corresponding point on the texture image and fetching its color value. Let us see this example image to understand how this is working on a 3D room.

Texture Coordinates

The first step in looking up texture values is to assign texture coordinates to each point on the 3D surface. The concept of texture coordinates is quite unique. This is typically represented as (u, v), correspond to points on the 2D texture image. These coordinates act as a map, defining where on the texture the shader should sample the color for each point on the surface.

For example, if the floor surface is flat and aligned with the x and y axes, we can define the texture coordinates u and v using a simple mapping −

$$\mathrm{u \:=\: ax,\: \quad v \:=\: by}$$

This formula assigns texture coordinates to the floor surface points (x, y, z) based on the X and Y coordinates of the point. Using these coordinates, the shader performs a texture lookup and fetches the color value from the corresponding pixel in the texture image. Follow the following code for simple coordinate assignment.

Color texture_lookup(Texture t, float u, float v) {
    int i = round(u * t.width() - 0.5);
    int j = round(v * t.height() - 0.5);
    return t.get_pixel(i, j);
}

Color shade_surface_point(Surface s, Point p, Texture t) {
    Vector normal = s.get_normal(p);
    (u, v) = s.get_texcoord(p);
    Color diffuse_color = texture_lookup(u, v);
    // compute shading using diffuse_color and normal
    // return shading result
}

In the above code, texture_lookup fetches the color value at (u, v) on the texture image. The shader then uses this value to calculate the final shading result for the surface point.

Mapping the Surface to the Texture

Defining a function that maps the surface points to texture coordinates is important in texture mapping. This function, known as the texture coordinate function, is denoted as −

$$\mathrm{\varphi\: : \:S \:\to\: T \: :\: (x,\: y,\: z)\: \mapsto \:(u,\: v)}$$

Here, S represents the 3D surface and T represents the 2D texture space. The function φ assigns texture coordinates (u, v) to every point (x, y, z) on the surface. This mapping is analogous to the viewing projection that maps points on the object’s surface to points in the image.

However, unlike the viewing projection, which is typically a perspective or orthographic projection, the texture coordinate function can take many forms depending on the surfaces shape and complexity.

Example of Wood Floor Texture Mapping

Let us see the wood flood texturing. In the above example we saw, we have a wood floor in a 3D scene. The floor surface is flat and parallel to the XY plane, we can define a straightforward texture coordinate mapping −

$$\mathrm{u \:=\: ax,\: \quad\: v \:= \:by}$$

With appropriate scaling factors a and b, this mapping assigns texture coordinates (u, v) to the floor points (x, y, z). The shader then uses these coordinates to perform a texture lookup. This is fetching the texture value (color) at (u, v) from the wood texture image.

This basic mapping works well for flat surfaces but becomes problematic if the floor is not aligned with the X and Y axes or if we want to apply the texture to a curved surface like a chair’s backrest, etc. In such cases, we need a more complex mapping function to correctly assign texture coordinates. In the following figure, it is mapped on a sphere.

Example of Wood Floor Texture Mapping

Challenges in Texture Mapping

There are many challenges in texture mapping but here we will discuss on two main of them.

  • Defining Texture Coordinate Functions − Choosing or designing an appropriate texture coordinate function is critical to ensure that the texture maps correctly onto the surface without unwanted distortions or overlaps.
  • Looking Up Texture Values Without Aliasing − Aliasing can occur when resampling the texture image. High contrast textures viewed at grazing angles can produce artifacts like stair-stepping or wavy patterns. These artifacts are especially noticeable in animations, making it crucial to use proper filtering and antialiasing techniques.

Example of Texture Distortion

See the following example, here the texture on cube is not accurate on some faces. Some faces it is showing the wooden pattern and on same faces it is stretched. This is not desirable.

Example of Texture Distortion

Conclusion

In this chapter, we explained the fundamental process of looking up texture values in texture mapping. We started by explaining the basics of texture mapping and how shaders use texture lookups to fetch color values from textures. We then understood texture coordinate functions and how they map surface points to texture coordinates.

Finally, we looked at an example of applying a wood floor texture and highlighted some of the challenges in texture mapping, such as defining coordinate functions and preventing aliasing artifacts.

Advertisements