-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
51 lines (45 loc) · 1.52 KB
/
Camera.cpp
File metadata and controls
51 lines (45 loc) · 1.52 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
48
49
50
51
/**
* \author Marie DARRIGOL & Anthony LEONARD & Ophélie PELLOUX-PRAYER & Olivier SOLDANO
* \project Ray-Tracing
* \file Camera.cpp
* \brief Camera representation in the scene
*/
#include "Camera.h"
/*
* On construit une caméra à partir d'une position et d'un vecteur permettant de positionner la frame
* par rapport à l'observateur. On génère une matrice de rotation à partir de ces informations.
*/
Camera::Camera(const Vec3& pOrigin, const Vec3& vFrame, const unsigned int picWidth, const unsigned int picHeight) : pOrigin(pOrigin), picWidth(picWidth), picHeight(picHeight) {
Vec3 i(1.0f, 0.0f, 0.0f);
Vec3 k = (i ^ vFrame).unit();
Mat3 K({ { 0.0f, -k.getZ(), k.getY() }, { k.getZ(), 0.0f, -k.getX() }, { -k.getY(), k.getX(), 0.0f } }),
K2 = K*K;
float cos_th = i*vFrame.unit();
float sin_th = (i^vFrame).length()/vFrame.length();
R = I3 + sin_th*K + (1.0f - cos_th)*K2;
distToFrame = vFrame.length();
if (picWidth > picHeight) {
width = 1.0f;
height = ((float)picHeight) / ((float)picWidth);
}
else {
height = 1.0f;
width = ((float)picWidth) / ((float)picHeight);
}
}
/*
* Retourne le rayon correspondant à un pixel donné suivant les caractéristiques de la camera.
*/
Ray Camera::getRay(const unsigned int x, const unsigned int y) const {
float dy = (float)(picHeight - y);
dy /= (float)picHeight;
dy *= height;
dy -= (height / 2.0f);
float dz = (float)(picWidth - x);
dz /= (float)picWidth;
dz *= width;
dz -= (width / 2.0f);
Vec3 v1(distToFrame, dy, dz);
Vec3 v2 = R*v1;
return Ray(pOrigin, v2.unit(), RAY_TTL);
}