1. 程式人生 > >PBRT_V2 總結記錄 Sampling Shapes

PBRT_V2 總結記錄 Sampling Shapes

Shape


// Shape Declarations
class Shape : public ReferenceCounted {
public:
    // Shape Interface
    Shape(const Transform *o2w, const Transform *w2o, bool ro);
    virtual ~Shape();

    virtual Point Sample(float u1, float u2, Normal *Ns) const {
        Severe("Unimplemented Shape::Sample() method called");
        return Point();
    }
    virtual float Pdf(const Point &Pshape) const {
        return 1.f / Area();
    }
    virtual Point Sample(const Point &P, float u1, float u2,
                         Normal *Ns) const {
        return Sample(u1, u2, Ns);
    }
    virtual float Pdf(const Point &p, const Vector &wi) const;

};

 

1. 

virtual Point Sample(float u1, float u2, Normal *Ns) const {
    Severe("Unimplemented Shape::Sample() method called");
    return Point();
}

作用:

(有兩個sample 函式,這個是第一個,這個sample主要的作用就是利用 引數 u1,u2,在shape的表面上隨機取樣一個點,返回出去,並且 儲存這個點的法線在引數 Ns 上)

There are two shape sampling methods, both named Shape::Sample(). The first chooses
points on the surface using some sampling distribution with respect to surface area on
the shape and returns the position and surface normal of the point chosen.

 

2. 

    virtual float Pdf(const Point &Pshape) const {
        return 1.f / Area();
    }

作用:

(pdf 是 1/Area )

The Shapes that implement this method will almost always sample uniformly by area on
their surface. Therefore, we will provide a default implementation of the Shape::Pdf()
method corresponding to this sampling method that returns the corresponding PDF:
the reciprocal of the surface area.

 

3. 

    virtual Point Sample(const Point &P, float u1, float u2,
                         Normal *Ns) const {
        return Sample(u1, u2, Ns);
    }

作用:

(這個是第二個 sample 函式,這個函式的主要作用就是,傳入一個 點 P,這個P點是要被area light 照亮的,  那麼 這個函式實現的就是,計算出這個area light 的哪一些區域 相對於 點P 是可視的,那麼就在這些區域進行取樣,返回取樣點)

The second shape sampling method also takes the point from which the surface of the
shape is being integrated over. This method is particularly useful for lighting, since the
caller can pass in the point to be lit and allow shape implementations to ensure that
they only sample the portion of the shape that is potentially visible from that point.

The default implementation ignores the additional point and calls the earlier sampling
method.

 

4. 


float Shape::Pdf(const Point &p, const Vector &wi) const {
    // Intersect sample ray with area light geometry
    DifferentialGeometry dgLight;
    Ray ray(p, wi, 1e-3f);
    ray.depth = -1; // temporary hack to ignore alpha mask
    float thit, rayEpsilon;
    if (!Intersect(ray, &thit, &rayEpsilon, &dgLight)) return 0.;

    // Convert light sample weight to solid angle measure
    float pdf = DistanceSquared(p, ray(thit)) /
                (AbsDot(dgLight.nn, -wi) * Area());
    if (isinf(pdf)) pdf = 0.f;
    return pdf;
}

作用:

(這個pdf其實主要的作用就是,用面積表示的的pdf ( 1/ Area ),換成用 立體角來 表示 (1/dw),那麼他們之間的關係就是 , 所有 1/dw 就是 這個公式的倒數,而且,這裡計算的pdf 是會考慮到 點P的可視區域,也就是說,這個pdf 其實可以理解為 可視區域的 pdf)

Unlike the first Shape sampling method, which generates points on the shape according
to a probability density with respect to surface area on the shape, the second one uses a
density with respect to solid angle from the point p
. This difference stems(起源) from the fact
that the area light sampling routines evaluate the direct lighting integral as an integral
over directions from p—expressing these sampling densities with respect to solid angle
at p is more convenient. Therefore, the standard implementation of the second Pdf()

method here transforms the density from one defined over area to one defined over solid
angle from p.

 

細節

a.

    DifferentialGeometry dgLight;
    Ray ray(p, wi, 1e-3f);
    ray.depth = -1; // temporary hack to ignore alpha mask
    float thit, rayEpsilon;
    if (!Intersect(ray, &thit, &rayEpsilon, &dgLight)) return 0.;

作用:

(先發射線來判斷 ray(p, wi) 與 shape 是否相關,如果不是相關的話,就預設pdf 是0,)

Given a point p and direction ωi, the Pdf() method determines if the ray (p, ωi) intersects
the shape. If the ray doesn’t intersect the shape at all, the probability that the shape
would have chosen the direction ωi can be assumed to be zero
(a well-written sampling
algorithm wouldn’t generate such a sample, and in any case the light’s contribution will
be zero for such directions, so using a zero probability density is fine).
Note that this ray intersection test is only between the ray and the single shape that is the
area light source. The rest of the scene geometry is ignored, thus the intersection test is
reasonably efficient.

 

b.

float pdf = DistanceSquared(p, ray(thit)) / (AbsDot(dgLight.nn, -wi) * Area());

作用:

這裡其實就是公式:

,立體角與面積的關係,用立體角來表示 pdf。

 

To compute the value of the PDF with respect to solid angle from p, this method starts by
computing the PDF with respect to surface area. Conversion from a density with respect
to area to a density with respect to solid angle requires division by the factor

where θo is the angle between the ray from the point on the light to the receiving point p
and the light’s surface normal, and r^2 is the distance between the point on the light and
the point being shaded