Sheet 2: Texture Coordinates & Sampling
Exercise: Texture Coordinates (10 points)
The updated classes from the previous sheet are prepared to support texturing.
TODO markers in the code indicate missing components.
Texture coordinates are necessary to correctly map textures onto primitives such as planes, spheres, or triangles.
The method Plane.intersect already computes texture coordinates for intersection points.
In computer graphics, such coordinates are often visualized as RGB values — for debugging purposes.
Usually, only u and v are displayed as red and green components.
The two leftmost images in Figure coords show a debug image and a scene without texture. You can toggle between both using the space bar.
For this exercise, implement texture coordinate computation for spheres using
polar coordinates.
The Sphere class accepts three parameters to control the mapping:
-
Euler angles (
Sphere.rotation): Rotate the direction vectorraround the x-, y-, and z-axis in that exact order using rotation matrices. -
Compute the polar coordinates
(ϕ, θ)and normalize(u, v)to the interval[0, 1]. -
Scale the texture coordinates using
Sphere.scaleand then offset them usingSphere.bias.
Figure: Debug textures of the plane (left), untextured scene (center), and scene with texture coordinates (right).
Exercise: Procedurally Generating a Texture (5 points)
Implement the method genCheckerboardTexture in the Texture class.
For a correct result, the first texel (data[0]) must be black.
A correct implementation will produce a checkerboard pattern.
Exercise: Texture Sampling (10 points)
To project the textures onto 2D surfaces, implement the corresponding texture sampling methods.
Familiarize yourself with the Texture class and the enumerators BorderMode
and FilterMode.
Complete the methods marked with TODO so that the following combinations are supported:
- Filters:
NEARESTandBILINEAR - Border Modes:
CLAMP_TO_EDGE,CLAMP_TO_BORDER,MIRRORED_REPEAT
Combinations of filter and border modes can be freely chosen. The textures in the scene use these combinations purposefully:
- Earth:
REPEAT+NEAREST - Light Logo:
CLAMP_TO_EDGE(everything outside becomes white) - Dark Logo:
MIRRORED_REPEAT(in u) +CLAMP_TO_BORDER(in v) → creates ring effect - Checkerboard:
REPEAT+NEARESTorBILINEAR
During ray traversal, the sampled texture values are automatically multiplied with the ambient and diffuse values of the Phong shading model. These equations do not need to be changed.
To test bilinear interpolation, simply change the filter mode of the checkerboard
from NEAREST to BILINEAR.
Drag with the left mouse button or tab and drag to rotate the scene. Stop interacting to start the raytracing.