/
CameraPerspective.cpp
38 lines (32 loc) · 1.11 KB
/
CameraPerspective.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include "CameraPerspective.h"
#include "Ray.h"
#include "macroses.h"
namespace rt
{
void CCameraPerspective::InitRay(Ray& ray, int x, int y, const Vec2f& sample)
{
Size resolution = getResolution();
int width = resolution.width;
int height = resolution.height;
// Asserts
RT_IF_WARNING(x >= width, "Argument x = %d exceeds the camera resolution width (%d)", x, width);
RT_IF_WARNING(y >= height, "Argument y = %d exceeds the camera resolution height (%d)", y, height);
// Normalized device coordinates \in [0; 1]
float ndcx = (x + sample.val[0]) / width;
float ndcy = (y + sample.val[1]) / height;
// Point in the screen-space coordinates \in [-1, 1]
float sscx = 2 * ndcx - 1;
float sscy = 2 * ndcy - 1;
if (m_needUpdateAxes) {
m_zAxis = m_dir;
m_xAxis = normalize(m_zAxis.cross(m_up));
m_yAxis = normalize(m_zAxis.cross(m_xAxis));
m_needUpdateAxes = false;
}
ray.org = m_pos;
ray.dir = normalize(getAspectRatio() * sscx * m_xAxis + sscy * m_yAxis + m_focus * m_zAxis);
ray.t = std::numeric_limits<double>::infinity();
ray.hit = nullptr;
ray.ndc = Vec2f(ndcx, ndcy);
}
}