-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCamera.cpp
72 lines (59 loc) · 1.83 KB
/
Camera.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// Created by debowin on 10/8/17.
//
#include <cmath>
#include "Camera.h"
Camera::Camera() {
setXYZ(&position, 0, 0, 0);
setXYZ(&towards, 0, 0, 1);
setXYZ(&up, 0, 1, 0);
ha = 45;
filmWidth = 640;
filmHeight = 480;
// focal length
focalLength = filmHeight/(2 * std::tan(ha*(float)M_PI/180));
//width angle
wa = (float)(atan2(filmWidth/2, focalLength) * 180/M_PI);
// right vector
right = towards ^ up;
}
void Camera::setCamera(Vector3D p, Vector3D d, Vector3D u, float ha_) {
position = p;
towards = d;
// right vector
right = normalize(towards ^ u);
up = normalize(right ^ towards);
ha = ha_;
// focal length
focalLength = filmHeight/(2 * std::tan(ha*(float)M_PI/180));
//width angle
wa = (float)(atan2(filmWidth/2, focalLength) * 180/M_PI);
}
void Camera::setFilm(int width, int height){
filmWidth = width;
filmHeight = height;
// focal length
focalLength = filmHeight/(2 * std::tan(ha*(float)M_PI/180));
//width angle
wa = (float)(atan2(filmWidth/2, focalLength) * 180/M_PI);
}
int Camera::getFilmWidth() const {
return filmWidth;
}
int Camera::getFilmHeight() const {
return filmHeight;
}
Ray Camera::getRay(float ix, float iy) {
Vector3D direction{};
Vector3D u{}, v{};
Vector3D pRight{}, pLeft{}, pTop{}, pBottom{};
pLeft = position + towards * focalLength - right * (filmWidth/2);
pRight = position + towards * focalLength + right * (filmWidth/2);
pBottom = position + towards * focalLength - up * (filmHeight/2);
pTop = position + towards * focalLength + up * (filmHeight/2);
u = pLeft + (pRight - pLeft)*((ix)/filmWidth);
v = pBottom + (pTop - pBottom)*((iy)/filmHeight);
// d = s - p
direction = u + v - towards*focalLength - position;
return {position, normalize(direction)};
}