00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "pinholecamera.h"
00024 #include "context.h"
00025 #include "ray.h"
00026
00027 #include <cassert>
00028 #include <cmath>
00029
00030 using namespace sheep;
00031 using namespace toxic;
00032
00033 PinholeCamera::PinholeCamera(const Matrix4 &m, Real hfov, Real aspect_ratio) :
00034 ICamera(m, hfov, aspect_ratio)
00035 {
00036 }
00037
00038 Ray PinholeCamera::ComputeRay(const Context &context,
00039 const Point2 &p) const
00040 {
00041 assert(p.m_x >= -0.5 && p.m_x <= 0.5);
00042 assert(p.m_y >= -0.5 && p.m_y <= 0.5);
00043
00044
00045
00046 Ray ray(Ray::PRIMARY_RAY);
00047 ray.m_origin = m_location;
00048 ray.m_direction =
00049 p.m_x * m_u +
00050 p.m_y * m_ey +
00051 m_w;
00052 ray.m_direction.Normalize();
00053
00054 return ray;
00055 }
00056
00057 Point2 PinholeCamera::Project(const Point3 &p) const {
00058 Vector3 xi = p - m_location;
00059 assert(xi * m_ez != 0.0);
00060
00061 xi *= -m_focal_length / (xi * m_ez);
00062
00063 return Point2(xi * m_ex / m_aspect_ratio, xi * m_ey);
00064 }