1. 程式人生 > >PBRT_V2 總結記錄 Texture 和 ConstantTexture 和 ScaleTexture 和 MixTexture 和 BilerpTexture

PBRT_V2 總結記錄 Texture 和 ConstantTexture 和 ScaleTexture 和 MixTexture 和 BilerpTexture

Texture 類

template <typename T> class Texture : public ReferenceCounted {
public:
    // Texture Interface
    virtual T Evaluate(const DifferentialGeometry &) const = 0;
    virtual ~Texture() { }
};

作用:

(Texture 是一個 模板類,evaluation 函式返回的值得型別就可以多樣化,目前 pbrt 中,evaluation 返回的型別主要是 float 和 Spectrum)

Texture is a template class parameterized by the return type of its evaluation function.
This design makes it possible to reuse almost all of the texturing code between textures
that return different types.
pbrt currently uses only float and Spectrum textures.

 

1. virtual T Evaluate(const DifferentialGeometry &) const = 0;

作用:

(Evaluate 函式 主要的就是 利用 1個 DifferentialGeometry  作為引數,計算圖片中 一個 模板型別T的值)

The key to Texture’s interface is its evaluation function; it returns a value of the template
type T.
The only information it has access to in order to evaluate its value is the
DifferentialGeometry at the point being shaded. Different textures in this chapter will
use different parts of this structure to drive their evaluation.

 

 

ConstantTexture 類

template <typename T> class ConstantTexture : public Texture<T> {
public:
    // ConstantTexture Public Methods
    ConstantTexture(const T &v) { value = v; }
    T Evaluate(const DifferentialGeometry &) const {
        return value;
    }
private:
    T value;
};

類的作用:

(Evaluate 計算出來都是同一個值,存在這個 ConstantTexture ,可以把材質的所有的引數都可以當做 Texture來處理)

ConstantTexture returns the same value no matter where it is evaluated. Because it
represents a constant function, it can be accurately reconstructed with any sampling
rate and therefore needs no antialiasing. Although this texture is trivial, it is actually
quite useful. By providing this class, all parameters to all Materials can be represented
as Textures, whether they are spatially varying or not.
For example, a red diffuse object
will have a ConstantTexture that always returns red as the diffuse color of the material.
This way, the shading system always evaluates a texture to get the surface properties at
a point, avoiding the need for separate textured and nontextured versions of materials.

 

ScaleTexture 類

template <typename T1, typename T2>
class ScaleTexture : public Texture<T2> {
public:
    // ScaleTexture Public Methods
    ScaleTexture(Reference<Texture<T1> > t1, Reference<Texture<T2> > t2)
        : tex1(t1), tex2(t2) { }
    T2 Evaluate(const DifferentialGeometry &dg) const {
        return tex1->Evaluate(dg) * tex2->Evaluate(dg);
    }
private:
    Reference<Texture<T1> > tex1;
    Reference<Texture<T2> > tex2;
};

類的作用:

(ScaleTexture 取兩個Texture,計算它們的乘積)

The ScaleTexture takes two textures and returns the product of their values when evaluated.

 

MixTexture類

template <typename T> class MixTexture : public Texture<T> {
public:
    // MixTexture Public Methods
    MixTexture(Reference<Texture<T> > t1, Reference<Texture<T> > t2,
               Reference<Texture<float> > amt)
        : tex1(t1), tex2(t2), amount(amt) { }
    T Evaluate(const DifferentialGeometry &dg) const {
        T t1 = tex1->Evaluate(dg), t2 = tex2->Evaluate(dg);
        float amt = amount->Evaluate(dg);
        return (1.f - amt) * t1 + amt * t2;
    }
private:
    Reference<Texture<T> > tex1, tex2;
    Reference<Texture<float> > amount;
};

類的作用:

(MixTexture 需要3個Texture,主要的作用就是利用第3張Texture來混合前兩張Texture)

The MixTexture class is a more general variation of ScaleTexture. It takes three textures
as input: two may be of any type, and the third must return a floating-point value. The
floating-point texture is then used to linearly interpolate between the two other textures.

Note that a ConstantTexture could be used for the floating-point value to achieve a
uniform blend, or a more complex Texture to blend in a spatially nonuniform way.

 

BilerpTexture 類

template <typename T> class BilerpTexture : public Texture<T> {
public:
    // BilerpTexture Public Methods
    BilerpTexture(TextureMapping2D *m, const T &t00, const T &t01,
                  const T &t10, const T &t11)
        : mapping(m), v00(t00), v01(t01), v10(t10), v11(t11) {
    }
    ~BilerpTexture() {
        delete mapping;
    }
    T Evaluate(const DifferentialGeometry &dg) const {
        float s, t, dsdx, dtdx, dsdy, dtdy;
        mapping->Map(dg, &s, &t, &dsdx, &dtdx, &dsdy, &dtdy);
        return (1-s)*(1-t) * v00 + (1-s)*(  t) * v01 +
               (  s)*(1-t) * v10 + (  s)*(  t) * v11;
    }
private:
    // BilerpTexture Private Data
    TextureMapping2D *mapping;
    T v00, v01, v10, v11;
};

類的作用:

(傳入4個常量值,這4個常量分別是 紋理空間上(0, 0), (1, 0), (0, 1),(1, 1)  位置上的值,那麼 TextureMapping2D 計算出來的紋理座標(s,t) 其實就是 這 4個常量的 插值時候的權重,也就是說,這個 BilerpTexture 利用 紋理座標來 雙線性插值 紋理座標空間4個角的常量)

The BilerpTexture class provides bilinear interpolation between four constant values.
Values are defined at (0, 0), (1, 0), (0, 1), and (1, 1) in (s , t) parameter space. The value
at a particular (s , t) position is found by interpolating between them.

 

1. T Evaluate(const DifferentialGeometry &dg) const

T Evaluate(const DifferentialGeometry &dg) const {
        float s, t, dsdx, dtdx, dsdy, dtdy;
        mapping->Map(dg, &s, &t, &dsdx, &dtdx, &dsdy, &dtdy);
        return (1-s)*(1-t) * v00 + (1-s)*(  t) * v01 +
               (  s)*(1-t) * v10 + (  s)*(  t) * v11;
    }

思路:

(Evaluate 先 利用 s 來插值 v00 和 v10 得到 tmp1,再利用 s 來 插值 v01 和 v11 得到 tmp2,之後 利用 t 來插值 tmp1 和 tmp2, 得到最後的結果 )

The interpolated value of the four values at an (s , t) position can be computed by three
linear interpolations. For example, we can first use s to interpolate between the values
at (0, 0) and (1, 0) and store that in a temporary tmp1. We can then do the same for the
(0, 1) and (1, 1) values and store the result in tmp2. Finally, we use t to interpolate between
tmp1 and tmp2 and obtain the final result. Mathematically, this is

Rather than storing the intermediate values explicitly, some algebraic rearrangement
gives us the same result from an appropriately weighted average of the four corner values: