PBRT_V2 总结记录 <18> OrthoCamera 和 EnvironmentCamera

OrthoCamera 类


// OrthographicCamera Declarations
class OrthoCamera : public ProjectiveCamera {
public:
    // OrthoCamera Public Methods
    OrthoCamera(const AnimatedTransform &cam2world, const float screenWindow[4],
        float sopen, float sclose, float lensr, float focald, Film *film);
    float GenerateRay(const CameraSample &sample, Ray *) const;
    float GenerateRayDifferential(const CameraSample &sample, RayDifferential *) const;
private:
    // OrthoCamera Private Data
    Vector dxCamera, dyCamera;
};

类的作用: 正交相机

1. 构造函数

OrthoCamera::OrthoCamera(const AnimatedTransform &cam2world,
        const float screenWindow[4], float sopen, float sclose,
        float lensr, float focald, Film *f)
    : ProjectiveCamera(cam2world, Orthographic(0., 1.), screenWindow,
                       sopen, sclose, lensr, focald, f) {
    // Compute differential changes in origin for ortho camera rays
    dxCamera = RasterToCamera(Vector(1, 0, 0));
    dyCamera = RasterToCamera(Vector(0, 1, 0));
}

细节:

(主要在 Orthographic() 函数中生成正交投影矩阵)

The orthographic camera constructor generates the orthographic transformation matrix
with the Orthographic() function, which will be defined shortly.

Orthographic() 函数:

Transform Orthographic(float znear, float zfar) {
	return Scale(1.f, 1.f, 1.f / (zfar - znear)) *
		Translate(Vector(0.f, 0.f, -znear));
}

Figure 6.2: The orthographic view volume is an axis-aligned box in camera space, defined such that
objects inside the region are projected onto the z = hither face of the box.

The orthographic viewing transformation leaves x and y coordinates unchanged, but
maps z values at the hither plane to 0 and z values at the yon plane to 1. To do this, the
scene is first translated along the z axis so that the hither plane is aligned with z = 0.
Then, the scene is scaled in z so that the yon plane maps to z = 1. The composition of
these two transformations gives the overall transformation.
(For a ray tracer like pbrt,
we’d like the hither plane to be at 0 so that rays start at the plane that goes through the
camera’s position; the yon plane offset doesn’t particularly matter.)

总结: 正交投影矩阵 在PBRT中的生成思路:

a. 保持x,y 坐标不变

b. z : 【0, 1】, 所以,第一步就是进行z偏移,把近平面的 移到 z = 0 ,之后再缩放 z值,把远平面变成 z = 1,所以就涉及到了 平移和缩放。

2. GenerateRay


float OrthoCamera::GenerateRay(const CameraSample &sample, Ray *ray) const {
    // Generate raster and camera samples
    Point Pras(sample.imageX, sample.imageY, 0);
    Point Pcamera;
    RasterToCamera(Pras, &Pcamera);
    *ray = Ray(Pcamera, Vector(0,0,1), 0.f, INFINITY);
    // Modify ray for depth of field
    if (lensRadius > 0.) {
        // Sample point on lens
        float lensU, lensV;
        ConcentricSampleDisk(sample.lensU, sample.lensV, &lensU, &lensV);
        lensU *= lensRadius;
        lensV *= lensRadius;

        // Compute point on plane of focus
        float ft = focalDistance / ray->d.z;
        Point Pfocus = (*ray)(ft);

        // Update ray for effect of lens
        ray->o = Point(lensU, lensV, 0.f);
        ray->d = Normalize(Pfocus - ray->o);
    }
    ray->time = sample.time;
    CameraToWorld(*ray, ray);
    return 1.f;
}

思路:

(需要注意的是,正交相机,发出的射线的方向是 (0,0,1))

To create a ray with the orthographic camera, a raster space position on the image plane
is transformed to camera space, giving the ray’s origin on the hither plane. The ray’s direction in
camera space is (0, 0, 1),
down the z axis.

EnvironmentCamera 类

class EnvironmentCamera : public Camera {
public:
    // EnvironmentCamera Public Methods
    EnvironmentCamera(const AnimatedTransform &cam2world, float sopen,
                      float sclose, Film *film)
        : Camera(cam2world, sopen, sclose, film) {
    }
    float GenerateRay(const CameraSample &sample, Ray *) const;
};

类的作用:

(环境相机,围绕一个点 发出 360 度的 射线,形成一张环境图)

A camera model that traces rays in all directions around a
point in the scene, giving a two-dimensional view of everything that is visible from that
point.
Consider a sphere around the camera position in the scene; choosing points on
that sphere gives directions to trace rays in. If we parameterize the sphere with spherical
coordinates, each point on the sphere is associated with a (θ , φ) pair, where θ ∈ [0, π]
and φ ∈ [0, 2π].

This type of image is particularly useful because it represents all of the incident light at a point
on the scene. It will be useful later when we discuss environment lighting
—a rendering
technique that uses image-based representations of light in a scene.

1. float GenerateRay(const CameraSample &sample, Ray *) const;

float EnvironmentCamera::GenerateRay(const CameraSample &sample,
                                     Ray *ray) const {
    // Compute environment camera ray direction
    float theta = M_PI * sample.imageY / film->yResolution;
    float phi = 2 * M_PI * sample.imageX / film->xResolution;
    Vector dir(sinf(theta) * cosf(phi), cosf(theta),
               sinf(theta) * sinf(phi));
    *ray = Ray(Point(0,0,0), dir, 0.f, INFINITY, sample.time);
    CameraToWorld(*ray, ray);
    return 1.f;
}

细节:

To compute the (θ , φ) coordinates for this ray, NDC coordinates are computed from the
raster image sample position and then scaled to cover the (θ , φ) range.

Next, the spherical coordinate formula is used to compute the ray direction, and finally the direction is
converted to world space. (Note that because the y direction is “up” in camera space, here
the x and y coordinates in the spherical coordinate formula are exchanged(交换) in comparison(比较)
to usage elsewhere in the system.)

解析:

(PBRT使用的是,左手坐标系)

OP 在 XZ平面上的投影 H.

θ 是 OP 与 Y 轴的 夹角, θ ∈ [0, π]  (theta)

φ 是 H 与 X 轴的夹角, φ ∈ [0, 2π].  (phi)

得到:

H = r * sin θ

x = r sin θ cos φ
y = r cos θ
z = r sin θ sin φ

1.  先计算出  (θ , φ) 坐标,主要就是,

θ  = M_PI * sample.imageY / film->yResolution    其实就是:  y = [0, yResolution] => [0, π]

φ = 2 * M_PI * sample.imageX / film->xResolution  其实就是 : x = [0, xResolution] => [0, 2π]

2. 计算dir,计算dir其实就是用上面的 

x = r sin θ cos φ
y = r cos θ
z = r sin θ sin φ

猜你喜欢

转载自blog.csdn.net/aa20274270/article/details/83056486
今日推荐