-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.cpp
150 lines (125 loc) · 4.75 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "camera.h"
#include "math.h"
#include <iostream>
#include <assert.h>
using namespace Math;
Camera::Camera()
{
this->resolutionX = 64;
this->resolutionY = 64;
this->viewPoint = { 0, 0, 0 };
this->u = { 1, 0, 0 };
this->v = { 0, 0, 1 };
this->w = { 0, -1, 0 };
this->leftBound = -1;
this->rightBound = 1;
this->topBound = 1;
this->bottomBound = -1;
};
Camera::Camera(Vector3 viewPoint, Vector3 u, Vector3 v, Vector3 w, int resolutionX, int resolutionY, float leftBound, float rightBound, float topBound, float bottomBound)
{
assert (leftBound < 0 && rightBound > 0);
assert (bottomBound < 0 && topBound > 0);
this->resolutionX = resolutionX;
this->resolutionY = resolutionY;
this->viewPoint = viewPoint;
this->u = u;
this->v = v;
this->w = w;
this->leftBound = leftBound;
this->rightBound = rightBound;
this->topBound = topBound;
this->bottomBound = bottomBound;
};
Camera::Camera(Vector3 viewPoint, Vector3 viewingDirection, int resolutionX, int resolutionY, float leftBound, float rightBound, float topBound, float bottomBound)
{
assert (leftBound < 0 && rightBound > 0);
assert (bottomBound < 0 && topBound > 0);
this->resolutionX = resolutionX;
this->resolutionY = resolutionY;
this->viewPoint = viewPoint;
this->setOrientation(viewingDirection);
this->leftBound = leftBound;
this->rightBound = rightBound;
this->topBound = topBound;
this->bottomBound = bottomBound;
};
int Camera::getResolutionX() const
{
return this->resolutionX;
};
int Camera::getResolutionY() const
{
return this->resolutionY;
};
void Camera::setOrigin(Math::Vector3 origin)
{
this->viewPoint = origin;
};
void Camera::setResolution(int resolutionX, int resolutionY)
{
this->resolutionX = resolutionX;
this->resolutionY = resolutionY;
};
void Camera::setOrientation(Math::Vector3 u, Math::Vector3 v, Math::Vector3 w)
{
this->u = Math::Vector3(u);
this->v = Math::Vector3(v);
this->w = Math::Vector3(w);
};
void Camera::setOrientation(Math::Vector3 viewingDirection)
{
const Vector3 up = { 0, 0, 1 };
const Vector3 w = viewingDirection / (-viewingDirection.norm());
const Vector3 u = cross(up, w) / (cross(up, w)).norm();
const Vector3 v = cross(w, u);
this->u = u;
this->v = v;
this->w = w;
};
void Camera::setBounds(float leftBound, float rightBound, float topBound, float bottomBound)
{
assert (leftBound < 0 && rightBound > 0);
assert (bottomBound < 0 && topBound > 0);
this->leftBound = leftBound;
this->rightBound = rightBound;
this->topBound = topBound;
this->bottomBound = bottomBound;
};
Ray ParallelOrthographicCamera::computeViewingRay(int pixelIndexX, int pixelIndexY) const
{
assert ((pixelIndexX >= 0) && (pixelIndexX < this->resolutionX));
assert ((pixelIndexY >= 0) && (pixelIndexY < this->resolutionY));
const float uCoordinate = this->leftBound + (this->rightBound - this->leftBound) * (pixelIndexX + 0.5) / this->resolutionX;
const float vCoordinate = this->bottomBound + (this->topBound - this->bottomBound) * (pixelIndexY + 0.5) / this->resolutionY;
Ray ray;
ray.direction = -this->w;
ray.origin = this->viewPoint + (uCoordinate * this->u) + (vCoordinate * this->v);
return ray;
};
PerspectiveCamera::PerspectiveCamera(){};
PerspectiveCamera::PerspectiveCamera(Vector3 viewPoint, Vector3 u, Vector3 v, Vector3 w, int resolutionX, int resolutionY, float leftBound, float rightBound, float topBound, float bottomBound, float focalLength)
: Camera(viewPoint, u, v, w, resolutionX, resolutionY, leftBound, rightBound, topBound, bottomBound)
{
this->focalLength = focalLength;
};
PerspectiveCamera::PerspectiveCamera(Vector3 viewPoint, Vector3 viewingDirection, int resolutionX, int resolutionY, float leftBound, float rightBound, float topBound, float bottomBound, float focalLength)
: Camera(viewPoint, viewingDirection, resolutionX, resolutionY, leftBound, rightBound, topBound, bottomBound)
{
this->focalLength = focalLength;
};
void PerspectiveCamera::setFocalLength(float focalLength)
{
this->focalLength = focalLength;
};
Ray PerspectiveCamera::computeViewingRay(int pixelIndexX, int pixelIndexY) const
{
assert ((pixelIndexX >= 0) && (pixelIndexX < this->resolutionX));
assert ((pixelIndexY >= 0) && (pixelIndexY < this->resolutionY));
const float uCoordinate = this->leftBound + (this->rightBound - this->leftBound) * (pixelIndexX + 0.5) / this->resolutionX;
const float vCoordinate = this->bottomBound + (this->topBound - this->bottomBound) * (pixelIndexY + 0.5) / this->resolutionY;
Ray ray;
ray.origin = this->viewPoint;
ray.direction = -(this->focalLength * this->w) + (uCoordinate * this->u) + (vCoordinate * this->v);
return ray;
}