-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
42 lines (33 loc) · 1.42 KB
/
camera.cpp
File metadata and controls
42 lines (33 loc) · 1.42 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
#include "camera.h"
Camera::Camera(Vector3f from, Vector3f to, Vector3f up, float fieldOfView, Vector2i imageResolution)
: from(from),
to(to),
up(up),
fieldOfView(fieldOfView),
imageResolution(imageResolution)
{
this->aspect = imageResolution.x / float(imageResolution.y);
// Determine viewport dimensions in 3D
float fovRadians = fieldOfView * M_PI / 180.f;
float h = std::tan(fovRadians / 2.f);
float viewportHeight = 2.f * h * this->focusDistance;
float viewportWidth = viewportHeight * this->aspect;
// Calculate basis vectors of the camera for the given transform
this->w = Normalize(this->from - this->to);
this->u = Normalize(Cross(up, this->w));
this->v = Normalize(Cross(this->w, this->u));
// Pixel delta vectors
Vector3f viewportU = viewportWidth * this->u;
Vector3f viewportV = viewportHeight * (-this->v);
this->pixelDeltaU = viewportU / float(imageResolution.x);
this->pixelDeltaV = viewportV / float(imageResolution.y);
// Upper left
this->upperLeft = from - this->w * this->focusDistance - viewportU / 2.f - viewportV / 2.f;
}
Ray Camera::generateRay(int x, int y)
{
Vector3f pixelCenter = this->upperLeft + 0.5f * (this->pixelDeltaU + this->pixelDeltaV);
pixelCenter = pixelCenter + x * this->pixelDeltaU + y * this->pixelDeltaV;
Vector3f direction = Normalize(pixelCenter - this->from);
return Ray(this->from, direction);
}