-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.cpp
110 lines (76 loc) · 2.33 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// File: camera.cpp
// Author: Samuel McFalls
// Description: Implementation of the Camera class
#include "camera.hpp"
Camera::Camera() {
center = Vec3();
normal = Vec3();
focus = Vec3();
sizeX = 0;
sizeY = 0;
resX = 0;
resY = 0;
startX = 0;
startY = 0;
}
Camera::Camera(const Vec3 &cenCon, const Vec3 &normCon, double focusCon, double sizeXCon, double sizeYCon,
double resXCon, double resYCon) {
center = cenCon;
normal = normCon.normalize();
focus = center - focusCon * normal;
sizeX = sizeXCon;
sizeY = sizeYCon;
resX = resXCon;
resY = resYCon;
// For default space, assume 0, 0, 0 is center
startX = 0 - (sizeX * resX) / 2 + (resX / 2);
startY = 0 - (sizeY * resY) / 2 + (resY / 2);
}
double Camera::getSizeX() const {
return sizeX;
}
double Camera::getSizeY() const {
return sizeY;
}
Vec3 Camera::getCenter() const {
return center;
}
Vec3 Camera::getNormal() const {
return normal;
}
Ray Camera::pixelRay(int x, int y) const {
Vec3 origin = focus;
// Get the pixel location in default space
double defaultX = startX + x * resX;
double defaultY = startY + y * resY;
double defaultZ = 0;
Vec3 defaultNormal = Vec3(0, 0, 1);
// Now we can rotate the points to get the actual point
Vec3 axis = defaultNormal.cross(normal);
double dot = defaultNormal.dot(normal);
double len1 = defaultNormal.magnitude();
double len2 = normal.magnitude();
double theta = acos(dot / (len1 * len2));
double rotateX, rotateY, rotateZ;
double cost = cos(theta);
double sint = sin(theta);
double ux = axis.getX();
double uy = axis.getY();
double uz = axis.getZ();
double rotationMatrix[3][3] = {
cost + pow(ux, 2) * (1 - cost),
ux * uy * (1 - cost) - ux * sint,
ux * uz * (1 - cost) + uy * sint,
uy * ux * (1 - cost) + uz * sint,
cost + pow(uy, 2) * (1 - cost),
uy * uz * (1 - cost) - ux * sint,
uz * ux * (1 - cost) - uy * sint,
uz * uy * (1 - cost) + ux * sint,
cost + pow(uz, 2) * (1 - cost)
};
rotateX = defaultX * rotationMatrix[0][0] + defaultY * rotationMatrix[1][0] + defaultZ * rotationMatrix[2][0];
rotateY = defaultX * rotationMatrix[0][1] + defaultY * rotationMatrix[1][1] + defaultZ * rotationMatrix[2][1];
rotateZ = defaultX * rotationMatrix[0][2] + defaultY * rotationMatrix[1][2] + defaultZ * rotationMatrix[2][2];
Vec3 dest = Vec3(rotateX, rotateY, rotateZ);
return Ray(origin, dest);
}