Texture Coordinate Generation
10.2 Texture Coordinate Generation
Almost all the textures in this chapter are functions that take a 2D or
3D coordinate and return a texture value. Sometimes there are obvious ways
to choose these texture coordinates; for parametric surfaces, such as the
quadrics in Chapter 6, there is a natural 2D
parameterization of the surface, and for all types of surfaces the shading
point
is a natural choice for a 3D coordinate.
In other cases, there is no natural parameterization, or the natural
parameterization may be undesirable. For instance, the values near
the poles of spheres are severely distorted. Therefore, this section
introduces classes that provide an interface to different techniques for
generating these parameterizations as well as a number of implementations
of them.
The Texture implementations later in this chapter store a
tagged pointer to a 2D or 3D mapping function as appropriate and use it to compute
the texture coordinates at each point at which they are evaluated. Thus,
it is easy to add new mappings to the system without having to modify all
the Texture implementations, and different mappings can be used for
different textures associated with the same surface. In pbrt, we will use
the convention that 2D texture coordinates are denoted by ; this
helps make clear the distinction between the intrinsic
parameterization of the underlying surface and the possibly different
coordinate values used for texturing.
TextureMapping2D defines the interface for 2D texture coordinate generation. It is defined in the file base/texture.h. The implementations of the texture mapping classes are in textures.h and textures.cpp.
<>=
class TextureMapping2D : public TaggedPointer<UVMapping, SphericalMapping, CylindricalMapping, PlanarMapping> { public: <<TextureMapping2D Interface>>
using TaggedPointer::TaggedPointer; PBRT_CPU_GPU TextureMapping2D(TaggedPointer<UVMapping, SphericalMapping, CylindricalMapping, PlanarMapping> tp) : TaggedPointer(tp) {} static TextureMapping2D Create(const ParameterDictionary ¶meters, const Transform &renderFromTexture, const FileLoc *loc, Allocator alloc); TexCoord2D Map(TextureEvalContext ctx) const;
};
The TextureMapping2D interface consists of a single method,
Map(). It is given a TextureEvalContext that stores relevant
geometric information at the shading point and returns a small structure,
TexCoord2D, that stores the texture coordinates and estimates
for the change in
with respect to pixel
and
coordinates so
that textures that use the mapping can determine the
sampling rate
and filter accordingly.
<>=
TexCoord2D Map(TextureEvalContext ctx) const;
<>=
struct TexCoord2D { Point2f st; Float dsdx, dsdy, dtdx, dtdy; };
In previous versions of pbrt, the Map() interface was defined to take a complete SurfaceInteraction; the TextureEvalContext structure did not exist. For this version, we have tightened up the interface to only include specific values that are useful for texture coordinate generation. This change was largely motivated by the GPU rendering path: with the CPU renderer, all the relevant information is already at hand in the functions that call the Map() methods; most likely the SurfaceInteraction is already in the CPU cache. On the GPU, the necessary values have to be read from off-chip memory. TextureEvalContext makes it possible for the GPU renderer to only read the necessary values from memory, which in turn has measurable performance benefits.
TextureEvalContext provides three constructors, not included here. Two initialize the various fields using corresponding values from either an Interaction or a SurfaceInteraction and the third allows specifying them directly.
<>=
struct TextureEvalContext { <>
TextureEvalContext() = default; PBRT_CPU_GPU TextureEvalContext(const Interaction &intr) : p(intr.p()), uv(intr.uv) {} PBRT_CPU_GPU TextureEvalContext(const SurfaceInteraction &si) : p(si.p()), dpdx(si.dpdx), dpdy(si.dpdy), n(si.n), uv(si.uv), dudx(si.dudx), dudy(si.dudy), dvdx(si.dvdx), dvdy(si.dvdy), faceIndex(si.faceIndex) {} PBRT_CPU_GPU TextureEvalContext(Point3f p, Vector3f dpdx, Vector3f dpdy, Normal3f n, Point2f uv, Float dudx, Float dudy, Float dvdx, Float dvdy, int faceIndex) : p(p), dpdx(dpdx), dpdy(dpdy), n(n), uv(uv), dudx(dudx), dudy(dudy), dvdx(dvdx), dvdy(dvdy), faceIndex(faceIndex) {} std::string ToString() const;
Point3f p; Vector3f dpdx, dpdy; Normal3f n; Point2f uv; Float dudx = 0, dudy = 0, dvdx = 0, dvdy = 0; };
10.2.1
Mapping
UVMapping uses the coordinates in the
TextureEvalContext to compute the texture coordinates, optionally
scaling and offsetting their values in each dimension.
<>=
class UVMapping { public: <<UVMapping Public Methods>>
UVMapping(Float su = 1, Float sv = 1, Float du = 0, Float dv = 0)
: su(su), sv(sv), du(du), dv(dv) {}
std::string ToString() const;
TexCoord2D Map(TextureEvalContext ctx) const {
<<Compute texture differentials for 2D mapping>>
Float dsdx = su * ctx.dudx, dsdy = su * ctx.dudy; Float dtdx = sv * ctx.dvdx, dtdy = sv * ctx.dvdy;
Point2f st(su * ctx.uv[0] + du, sv * ctx.uv[1] + dv); return TexCoord2D{st, dsdx, dsdy, dtdx, dtdy}; }
private: Float su, sv, du, dv; };
<>=
UVMapping(Float su = 1, Float sv = 1, Float du = 0, Float dv = 0) : su(su), sv(sv), du(du), dv(dv) {}
The scale-and-shift computation to compute coordinates is
straightforward:
<>+=
TexCoord2D Map(TextureEvalContext ctx) const {
<<Compute texture differentials for 2D mapping>>
Float dsdx = su * ctx.dudx, dsdy = su * ctx.dudy; Float dtdx = sv * ctx.dvdx, dtdy = sv * ctx.dvdy;
Point2f st(su * ctx.uv[0] + du, sv * ctx.uv[1] + dv); return TexCoord2D{st, dsdx, dsdy, dtdx, dtdy}; }
For a general 2D mapping function , the
screen-space derivatives of
and
are given by the chain rule:
(10.6)
Note that the
TextureEvalContext provides the values .
In this case, and so
(10.7)
We will skip past the straightforward fragment that implements Equation (10.7) to initialize dsdx, dsdy, dtdx, and dtdy.
10.2.2 Spherical Mapping
Figure 10.9: Use of the SphericalMapping in the Kroken Scene.
(a) Visualization of the resulting parameterization.
(b) Effect of using the SphericalMapping to apply a texture.
Note that although the shape is spherical, it is modeled with a triangle
mesh, to which the SphericalMapping is applied.
(Scene courtesy of Angelo Ferretti.)
Another useful mapping effectively wraps a sphere around the object. Each
point is projected along the vector from the sphere’s center through the
point on to the sphere’s surface. Since this mapping is based on
spherical coordinates, Equation (3.8)
can be applied, with the angles it returns remapped to :
(10.8)
Figure 10.9 shows the use of this mapping with an object in the Kroken scene.
The SphericalMapping further stores a transformation that is applied to points before this mapping is performed; this effectively allows the mapping sphere to be arbitrarily positioned and oriented with respect to the object.
<>=
class SphericalMapping { public: <<SphericalMapping Public Methods>>
SphericalMapping(const Transform &textureFromRender)
: textureFromRender(textureFromRender) {}
std::string ToString() const;
TexCoord2D Map(TextureEvalContext ctx) const {
Point3f pt = textureFromRender(ctx.p);
<<Compute and
for spherical mapping>>
Float x2y2 = Sqr(pt.x) + Sqr(pt.y); Float sqrtx2y2 = std::sqrt(x2y2); Vector3f dsdp = Vector3f(-pt.y, pt.x, 0) / (2 * Pi * x2y2); Vector3f dtdp = 1 / (Pi * (x2y2 + Sqr(pt.z))) * Vector3f(pt.x * pt.z / sqrtx2y2, pt.y * pt.z / sqrtx2y2, -sqrtx2y2);
<<Compute texture coordinate differentials for spherical mapping>>
Vector3f dpdx = textureFromRender(ctx.dpdx); Vector3f dpdy = textureFromRender(ctx.dpdy); Float dsdx = Dot(dsdp, dpdx), dsdy = Dot(dsdp, dpdy); Float dtdx = Dot(dtdp, dpdx), dtdy = Dot(dtdp, dpdy);
<<Return texture coordinates and differentials based on spherical mapping>>
Vector3f vec = Normalize(pt - Point3f(0,0,0)); Point2f st(SphericalTheta(vec) * InvPi, SphericalPhi(vec) * Inv2Pi); return TexCoord2D{st, dsdx, dsdy, dtdx, dtdy};
}
private: <<SphericalMapping Private Members>>
Transform textureFromRender;
};
<>=
Transform textureFromRender;
The Map() function starts by computing the texture-space point pt.
<>=
TexCoord2D Map(TextureEvalContext ctx) const {
Point3f pt = textureFromRender(ctx.p);
<<Compute and
for spherical mapping>>
Float x2y2 = Sqr(pt.x) + Sqr(pt.y); Float sqrtx2y2 = std::sqrt(x2y2); Vector3f dsdp = Vector3f(-pt.y, pt.x, 0) / (2 * Pi * x2y2); Vector3f dtdp = 1 / (Pi * (x2y2 + Sqr(pt.z))) * Vector3f(pt.x * pt.z / sqrtx2y2, pt.y * pt.z / sqrtx2y2, -sqrtx2y2);
<<Compute texture coordinate differentials for spherical mapping>>
Vector3f dpdx = textureFromRender(ctx.dpdx); Vector3f dpdy = textureFromRender(ctx.dpdy); Float dsdx = Dot(dsdp, dpdx), dsdy = Dot(dsdp, dpdy); Float dtdx = Dot(dtdp, dpdx), dtdy = Dot(dtdp, dpdy);
<<Return texture coordinates and differentials based on spherical mapping>>
Vector3f vec = Normalize(pt - Point3f(0,0,0)); Point2f st(SphericalTheta(vec) * InvPi, SphericalPhi(vec) * Inv2Pi); return TexCoord2D{st, dsdx, dsdy, dtdx, dtdy};
}
For a mapping function based on a 3D point , the generalization of
Equation (10.6) is
(10.9)
Taking the partial derivatives of the mapping function, Equation (10.8), we can find
(10.10)
These quantities are computed using the texture-space position pt.
<<Compute and
for spherical mapping>>=
Float x2y2 = Sqr(pt.x) + Sqr(pt.y); Float sqrtx2y2 = std::sqrt(x2y2); Vector3f dsdp = Vector3f(-pt.y, pt.x, 0) / (2 * Pi * x2y2); Vector3f dtdp = 1 / (Pi * (x2y2 + Sqr(pt.z))) * Vector3f(pt.x * pt.z / sqrtx2y2, pt.y * pt.z / sqrtx2y2, -sqrtx2y2);
The final differentials are then found using the four dot products from Equation (10.9).
<>=
Vector3f dpdx = textureFromRender(ctx.dpdx); Vector3f dpdy = textureFromRender(ctx.dpdy); Float dsdx = Dot(dsdp, dpdx), dsdy = Dot(dsdp, dpdy); Float dtdx = Dot(dtdp, dpdx), dtdy = Dot(dtdp, dpdy);
Finally, previously defined spherical geometry utility functions compute the mapping of Equation (10.8).
<<Return texture coordinates and differentials based on spherical mapping>>=
Vector3f vec = Normalize(pt - Point3f(0,0,0)); Point2f st(SphericalTheta(vec) * InvPi, SphericalPhi(vec) * Inv2Pi); return TexCoord2D{st, dsdx, dsdy, dtdx, dtdy};
10.2.3 Cylindrical Mapping
Figure 10.10: Use of the Cylindrical Texture Mapping.
(a) Visualization of the mapping from the CylindricalMapping.
(b) Kettle with texture maps applied.
(c) Scratch texture that is applied using the cylindrical texture mapping.
(Scene courtesy of Angelo Ferretti.)
The cylindrical mapping effectively wraps a cylinder around the object and then uses the cylinder’s parameterization.
(10.11)
See Figure 10.10 for an example of its use.
Note that the texture coordinate it returns is not necessarily between 0 and 1;
the mapping should either be scaled in
so that the object being
textured has
or the texture being used should return results
for coordinates outside that range that match the desired result.
<>=
class CylindricalMapping { public: <>
CylindricalMapping(const Transform &textureFromRender)
: textureFromRender(textureFromRender) {}
std::string ToString() const;
TexCoord2D Map(TextureEvalContext ctx) const {
Point3f pt = textureFromRender(ctx.p);
<<Compute texture coordinate differentials for cylinder mapping>>
Float x2y2 = Sqr(pt.x) + Sqr(pt.y); Vector3f dsdp = Vector3f(-pt.y, pt.x, 0) / (2 * Pi * x2y2), dtdp = Vector3f(0, 0, 1); Vector3f dpdx = textureFromRender(ctx.dpdx), dpdy = textureFromRender(ctx.dpdy); Float dsdx = Dot(dsdp, dpdx), dsdy = Dot(dsdp, dpdy); Float dtdx = Dot(dtdp, dpdx), dtdy = Dot(dtdp, dpdy);
Point2f st((Pi + std::atan2(pt.y, pt.x)) * Inv2Pi, pt.z); return TexCoord2D{st, dsdx, dsdy, dtdx, dtdy}; }
private: <<CylindricalMapping Private Members>>
Transform textureFromRender;
};
CylindricalMapping also supports a transformation to orient the mapping cylinder.
<>=
Transform textureFromRender;
Because the texture coordinate is computed in the same way as it is
with the spherical mapping, the cylindrical mapping’s
matches the sphere’s in Equation (10.10).
The partial derivative in
can easily be seen to be
.
Since the cylindrical mapping function and derivative computation are
only slight variations on the spherical mapping’s, we will not include the
implementation of its Map()
function here.
10.2.4 Planar Mapping
Another classic mapping method is planar mapping. The point is
effectively projected onto a plane; a 2D parameterization of the plane then
gives texture coordinates for the point. For example, a point might be
projected onto the
plane to yield texture coordinates given by
and
.
One way to define such a parameterized plane is with two nonparallel
vectors and
and offsets
and
. The
texture coordinates are given by the coordinates of the point with
respect to the plane’s coordinate system, which are computed by taking the dot
product of the vector from the point to the origin with each vector
and
and then adding the corresponding offset:
<>=
class PlanarMapping { public: <<PlanarMapping Public Methods>>
PlanarMapping(const Transform &textureFromRender, Vector3f vs, Vector3f vt, Float ds, Float dt)
: textureFromRender(textureFromRender), vs(vs), vt(vt), ds(ds), dt(dt) {}
TexCoord2D Map(TextureEvalContext ctx) const {
Vector3f vec(textureFromRender(ctx.p));
<<Initialize partial derivatives of planar mapping coordinates>>
Vector3f dpdx = textureFromRender(ctx.dpdx); Vector3f dpdy = textureFromRender(ctx.dpdy); Float dsdx = Dot(vs, dpdx), dsdy = Dot(vs, dpdy); Float dtdx = Dot(vt, dpdx), dtdy = Dot(vt, dpdy);
Point2f st(ds + Dot(vec, vs), dt + Dot(vec, vt)); return TexCoord2D{st, dsdx, dsdy, dtdx, dtdy}; } std::string ToString() const;
private: <<PlanarMapping Private Members>>
Transform textureFromRender; Vector3f vs, vt; Float ds, dt;
};
A straightforward constructor, not included here, initializes the following member variables.
<>=
Transform textureFromRender; Vector3f vs, vt; Float ds, dt;
<>=
TexCoord2D Map(TextureEvalContext ctx) const {
Vector3f vec(textureFromRender(ctx.p));
<<Initialize partial derivatives of planar mapping coordinates>>
Vector3f dpdx = textureFromRender(ctx.dpdx); Vector3f dpdy = textureFromRender(ctx.dpdy); Float dsdx = Dot(vs, dpdx), dsdy = Dot(vs, dpdy); Float dtdx = Dot(vt, dpdx), dtdy = Dot(vt, dpdy);
Point2f st(ds + Dot(vec, vs), dt + Dot(vec, vt)); return TexCoord2D{st, dsdx, dsdy, dtdx, dtdy}; }
The planar mapping differentials can be computed directly using the
partial derivatives of the mapping function, which are easily found. For
example, the partial derivative of the texture coordinate with respect
to screen-space
is just
.
<<Initialize partial derivatives of planar mapping coordinates>>=
Vector3f dpdx = textureFromRender(ctx.dpdx); Vector3f dpdy = textureFromRender(ctx.dpdy); Float dsdx = Dot(vs, dpdx), dsdy = Dot(vs, dpdy); Float dtdx = Dot(vt, dpdx), dtdy = Dot(vt, dpdy);
10.2.5 3D Mapping
We will also define a TextureMapping3D class that defines the interface for generating 3D texture coordinates.
<>=
class TextureMapping3D : public TaggedPointer<PointTransformMapping> { public: <<TextureMapping3D Interface>>
using TaggedPointer::TaggedPointer; PBRT_CPU_GPU TextureMapping3D(TaggedPointer tp) : TaggedPointer(tp) {} static TextureMapping3D Create(const ParameterDictionary ¶meters, const Transform &renderFromTexture, const FileLoc *loc, Allocator alloc); TexCoord3D Map(TextureEvalContext ctx) const;
};
The Map() method it specifies returns a 3D point and partial derivative vectors in the form of a TexCoord3D structure.
<>=
TexCoord3D Map(TextureEvalContext ctx) const;
TexCoord3D parallels TexCoord2D, storing both the point and its screen-space derivatives.
<>=
struct TexCoord3D { Point3f p; Vector3f dpdx, dpdy; };
The natural 3D mapping takes the rendering-space coordinate of the point and applies a linear transformation to it. This will often be a transformation that takes the point back to the primitive’s object space. Such a mapping is implemented by the PointTransformMapping class.
<>=
class PointTransformMapping { public: <<PointTransformMapping Public Methods>>
PointTransformMapping(const Transform &textureFromRender) : textureFromRender(textureFromRender) {} std::string ToString() const; TexCoord3D Map(TextureEvalContext ctx) const { return TexCoord3D{textureFromRender(ctx.p), textureFromRender(ctx.dpdx), textureFromRender(ctx.dpdy)}; }
private: Transform textureFromRender; };
Because it applies a linear transformation, the differential change in texture coordinates can be found by applying the same transformation to the partial derivatives of position.
<>=
TexCoord3D Map(TextureEvalContext ctx) const { return TexCoord3D{textureFromRender(ctx.p), textureFromRender(ctx.dpdx), textureFromRender(ctx.dpdy)}; }