
类声明:
#pragma once
#ifndef __CAMERA_HEADER__
#define __CAMERA_HEADER__
#include "../utilities/geometry.h"
class World;
class Camera {
public:
Camera();
Camera(const Camera& cam);
~Camera();
void set_eye(const Point3& p);
void set_lookat(const Point3& p);
void set_up(const Vector3& v);
void set_roll(const ldouble a);
void set_exposure_time(const ldouble t);
void compute_uvw();
virtual Camera* clone() const = 0;
virtual void render_scene(World& w) = 0;
Camera& operator=(const Camera& cam);
protected:
Point3 eye, lookat;
Vector3 up, u, v, w;
ldouble exposure_time, ra;
};
#endif
类实现:
#include "pch.h"
#include "camera.h"
Camera::Camera()
:eye(0, 0, 1), lookat(0), ra(0), up(0, 1, 0), u(1, 0, 0), v(0, 1, 0), w(0, 0, 1), exposure_time(1) {}
Camera::Camera(const Camera& cam)
: eye(cam.eye), lookat(cam.lookat), ra(cam.ra), up(cam.up),
u(cam.u), v(cam.v), w(cam.w), exposure_time(cam.exposure_time) {}
Camera::~Camera() {}
void Camera::set_eye(const Point3& p) {
eye = p;
}
void Camera::set_lookat(const Point3& p) {
lookat = p;
}
void Camera::set_up(const Vector3& v) {
up = v;
}
void Camera::set_roll(const ldouble a) {
ra = a;
}
void Camera::set_exposure_time(const ldouble t) {
exposure_time = t;
}
void Camera::compute_uvw() {
w = eye - lookat;
w.normalize();
u = up ^ w;
u.normalize();
v = w ^ u;
if (eye.x == lookat.x && eye.z == lookat.z && eye.y > lookat.y) { // camera looking vertically down
u = Vector3(0, 0, 1);
v = Vector3(1, 0, 0);
w = Vector3(0, 1, 0);
}
if (eye.x == lookat.x && eye.z == lookat.z && eye.y < lookat.y) { // camera looking vertically up
u = Vector3(1, 0, 0);
v = Vector3(0, 0, 1);
w = Vector3(0, -1, 0);
}
}
Camera& Camera::operator=(const Camera& cam) {
if (this == &cam)
return *this;
eye = cam.eye;
lookat = cam.lookat;
ra = cam.ra;
up = cam.up;
u = cam.u;
v = cam.v;
w = cam.w;
exposure_time = cam.exposure_time;
return *this;
}