Uniformly Sampling A Hemisphere
1. Vector UniformSampleHemisphere(float u1, float u2);
Vector UniformSampleHemisphere(float u1, float u2) {
float z = u1;
float r = sqrtf(max(0.f, 1.f - z*z));
float phi = 2 * M_PI * u2;
float x = r * cosf(phi);
float y = r * sinf(phi);
return Vector(x, y, z);
}
作用:
(传入u1,u2,范围都是[0,1],均匀地选择半球上的一个方向,具体的数学原来可以参考《PBRT》P663,这里就不抄出来)
choosing a direction on the hemisphere uniformly with respect to solid angle.
2. float UniformHemispherePdf();
float UniformHemispherePdf() {
return INV_TWOPI;
}
作用:
(采样半球方向的pdf)
For hemispheres (and all other directional sampling), these functions return
values with respect to solid angle. For the hemisphere, the solid angle PDF is a constant
p(ω) = 1/(2π).
Uniformly Sampling Full Sphere
1. Vector UniformSampleSphere(float u1, float u2);
Vector UniformSampleSphere(float u1, float u2) {
float z = 1.f - 2.f * u1;
float r = sqrtf(max(0.f, 1.f - z*z));
float phi = 2.f * M_PI * u2;
float x = r * cosf(phi);
float y = r * sinf(phi);
return Vector(x, y, z);
}
作用:
(传入u1,u2,范围都是[0,1],均匀地选择整一个球上的一个方向)
Sampling the full sphere uniformly over its area follows almost exactly the same derivation,
which we omit here.