-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
47 lines (38 loc) · 1.8 KB
/
Copy pathCamera.cpp
File metadata and controls
47 lines (38 loc) · 1.8 KB
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
39
40
41
42
43
44
45
46
47
#include "Camera.h"
#include <Eigen/Geometry>
#include <numbers>
#include "util/random.h"
Camera::Camera(Eigen::Vector3f position, const Eigen::Vector3f& rotation,
const float focal_length, const float width, const float height,
const unsigned int resolution_x, const unsigned int resolution_y,
const float exposure)
: position(std::move(position)),
focal_length(focal_length),
width(width),
height(height),
resolution_x(resolution_x),
resolution_y(resolution_y),
exposure(exposure) {
const float pitch = rotation.x() * std::numbers::pi_v<float> / 180.F;
const float yaw = rotation.y() * std::numbers::pi_v<float> / 180.F;
const float roll = rotation.z() * std::numbers::pi_v<float> / 180.F;
Eigen::Matrix3f rot_matrix;
rot_matrix = Eigen::AngleAxisf(roll, Eigen::Vector3f::UnitZ()) *
Eigen::AngleAxisf(yaw, Eigen::Vector3f::UnitY()) *
Eigen::AngleAxisf(pitch, Eigen::Vector3f::UnitX());
v = rot_matrix * Eigen::Vector3f::UnitY(); // up
w = rot_matrix * Eigen::Vector3f::UnitZ(); // backward
u = v.cross(w); // right
}
Ray Camera::viewing_ray(const unsigned int i, const unsigned int j) const {
const auto res_w = static_cast<float>(resolution_x);
const auto res_h = static_cast<float>(resolution_y);
std::uniform_real_distribution<float> dist(0.F, 1.F);
const float pixel_x = static_cast<float>(j) + dist(rng);
const float pixel_y = static_cast<float>(i) + dist(rng);
const float pixel_u = (pixel_x / res_w - 0.5F) * width;
const float pixel_v = -(pixel_y / res_h - 0.5F) * height;
const Eigen::Vector3f direction =
(u * pixel_u + v * pixel_v - w * focal_length).normalized();
return {position, direction};
}