-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
executable file
·82 lines (63 loc) · 1.87 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
//
// Created by liwen on 17-12-7.
//
#include "Camera.h"
Camera::Camera(const Vector3& _eye,const Vector3& _front,const Vector3& _up,double angle_x,double angle_y) {
eye=_eye;
frontDirection=_front.normalize();
upDirection = _up.normalize();
rightDirection = frontDirection^upDirection;
angleX = angle_x;
angleY = angle_y;
v_ref_len = tan(angle_x*PI/360);
h_ref_len = tan(angle_y*PI/360);
}
const Vector2 Camera::OUT = Vector2(-10,-10);
void Camera::setCanvas(int width, int height) {
canvas = Canvas(width, height);
dx = 1.0/width;
dy = 1.0/height;
}
Ray3 Camera::generateRay(bool &working, int& x, int& y) {
last_x = scan_x;
last_y = scan_y;
x = last_x;
y = last_y;
double rate_x = 2*(scan_x+0.5)*dx - 1;
double rate_y = 2*(scan_y+0.5)*dy - 1;
Vector3 direction = (rate_x*v_ref_len*rightDirection+rate_y*h_ref_len*upDirection+frontDirection);
scan_x++;
if(scan_x>=canvas.W){
scan_x = 0;
scan_y++;
}
if(scan_y>=canvas.H){
working = false;
canvas.finish();
}
return Ray3(eye, direction, true);
}
Ray3 Camera::generateRay(int i) {
int x = i % canvas.W;
int y = i / canvas.H;
double rate_x = 2*(x+0.5)*dx - 1;
double rate_y = 2*(y+0.5)*dy - 1;
Vector3 direction = (rate_x*v_ref_len*rightDirection+rate_y*h_ref_len*upDirection+frontDirection);
return Ray3(eye, direction, true);
}
Ray3 Camera::generateRay(int x, int y) {
double rate_x = 2*(x+0.5)*dx - 1;
double rate_y = 2*(y+0.5)*dy - 1;
Vector3 direction = (rate_x*v_ref_len*rightDirection+rate_y*h_ref_len*upDirection+frontDirection);
return Ray3(eye, direction, true);
}
int Camera::rayNum() {
return canvas.W * canvas.H;
}
void Camera::resetScan() {
scan_x = 0;
scan_y = 0;
}
void Camera::recieveColor(Color3 &color) {
canvas.light_hit(last_x,last_y,color);
}