-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.cpp
258 lines (225 loc) · 8.46 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include "camera.h"
#include "scene.h"
#include <Eigen/Dense>
#include <algorithm>
#include <string>
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include "json.h"
using namespace Eigen;
//using namespace cv;
using json = nlohmann::json;
Vector3d hemisphereRandomRay(float u1=-1, float u2=-1){
if (u1<0 || u2<0 ) {
u1 = (float)rand() / RAND_MAX;
u2 = (float)rand() / RAND_MAX;
}
const float r = sqrt(u1);
const float theta = 2 * M_PI * u2;
const float x = r * cos(theta);
const float y = r * sin(theta);
Vector3d res = Vector3d(x, y, sqrt(fmax(0.0f, 1 - u1)));
res.normalize();
return res;
}
Matrix3d NormalToRotation(Vector3d normal) {
// Find a vector in the plane
Vector3d tangent0 = normal.cross(Vector3d(1, 0, 0));
// if result tangent is too small it means normal was roughly colinear with
// chosen vector (1,0,0)
if (tangent0.dot(tangent0) < 0.001)
tangent0 = normal.cross(Vector3d(0, 1, 0));
tangent0.normalize();
// Find another vector in the plane
Vector3d tangent1 = normal.cross(tangent0);
tangent1.normalize();
// Construct a 3x3 matrix by storing three vectors in the columns of the matrix
Matrix3d res;
res.col(0) = tangent0;
res.col(1) = tangent1;
res.col(2) = normal;
//std::cout << res << std::endl;
//std::cout << "==========" << std::endl;
return res;
}
Camera::Camera (Vector3d center, Vector3d up,
Vector3d dir, double f, double w, double h, int subrays) :
_center(center),
_up(up),
_dir(dir),
_f(f), _width(w), _height(h), _subrays(subrays)
{}
Camera::Camera(std::string filename){
std::ifstream ifs(filename);
if (!ifs.is_open()) {
std::cerr << "Could not open file " << filename << std::endl;
return;
}
json dict;
ifs >> dict;
try{
json camdata = dict["camera"];
_f = camdata["focal"];
_width = camdata["width"];
_height = camdata["height"];
_subrays = camdata["subrays"];
_center = Vector3d(camdata["position"][0], camdata["position"][1], camdata["position"][2]);
_dir = Vector3d(camdata["dir"][0], camdata["dir"][1], camdata["dir"][2]);
_up = Vector3d(camdata["up"][0], camdata["up"][1], camdata["up"][2]);
} catch (std::domain_error e) {
std::cerr << "Error while loading camera data from file: " << filename << std::endl;
_center = Vector3d(0,0,0);
_up = Vector3d(0,1,0);
_dir = Vector3d(0,0,1);
_f = 0.035;
_width = _height = 0.025;
}
}
Camera::~Camera(){}
cv::Mat Camera::renderDepth(const Scene &scene, int w, int h){
double dx = _width/w;
double dy = _height/h;
std::vector<double> depth(w*h,0);
IOFormat singleLine(FullPrecision, DontAlignCols, ", ", ", ", "", "", " [ ", " ] ");
Vector3d screen_ori(_center+Vector3d(-_width/2,_height/2, _f));
#pragma omp parallel for
for (size_t i = 0; i < w; i++) {
#pragma omp parallel for
for (size_t j = 0; j < h; j++) {
Vector3d pix = screen_ori + Vector3d(dx*i, -dy*j, 0);
Line3d line = Line3d::Through(_center, pix);
intersection pt = scene.intersect(line, _center, pix, false);
if (pt.valid()) depth[j*w+i] = pt.depth();
else depth[j*w+i] = 1500;
}
}
// remap values between 0 and 1 based on max min depth of the scene (800, 1500)
std::vector<char> buffer(w*h,0);
int i = 0;
for (auto e : depth) buffer[i++] = 255 - 255*(e-800)/(1500-800);
cv::Mat image(cv::Size(w, h), CV_8UC1, buffer.data(), cv::Mat::AUTO_STEP);
return image.clone();
}
std::pair<colorRGB, intersection> singleRay(const Vector3d &p0, const Vector3d &p1, const Scene &scene){
Line3d line = Line3d::Through(p0, p1);
intersection pt = scene.intersect(line, p0, p1, true);
colorRGB resCol(0,0,0);
if (pt.valid()){
if (pt.element() == NULL) { // no physical element associated, means we hit a light
return std::make_pair(pt.col(), pt);
}
resCol = pt.col()*0.2;
// shoot direct shadow rays
for (const auto l : scene._lights){
//Spot* spot = (Spot*)l;
float sees = l->sees(pt, scene);
//double dist = 1-fabs(((pt.point()-spot->position()).norm())/800.0);
resCol = resCol + l->col()*pt.col()*sees;//*dist;
}
double d = scene._lights.size();
resCol = resCol / d;
}
return std::make_pair(resCol, pt);
}
std::pair<colorRGB, intersection> singleRay_rec(const Vector3d &p0, const Vector3d &p1, const Scene &scene, int depth=5){
Line3d line = Line3d::Through(p0, p1);
intersection pt = scene.intersect(line, p0, p1, true);
colorRGB resCol(0,0,0);
if (pt.valid()){
if (pt.element() == NULL) { // no physical element associated, means we hit a light
return std::make_pair(pt.col(), pt);
}
resCol = pt.col()*0.2;
// shoot direct shadow rays
for (const auto l : scene._lights){
//Spot* spot = (Spot*)l;
float sees = l->sees(pt, scene);
//double dist = 1-fabs(((pt.point()-spot->position()).norm())/800.0);
resCol = resCol + l->col()*pt.col()*sees;//*dist;
}
double d = scene._lights.size();
resCol = resCol / d;
if (pt.element()->_reflection > 0 && depth) {
Vector3d v = (p1-p0);
v.normalize();
Vector3d n = pt.element()->normal(pt.point());
Vector3d r = v - 2*(v.dot(n))*n;
std::pair<colorRGB, intersection> reflected = singleRay_rec(pt.point(),pt.point()+r, scene, depth-1);
resCol = resCol * (1-pt.element()->_reflection) + reflected.first*pt.element()->_reflection;
}
}
return std::make_pair(resCol, pt);
}
cv::Mat Camera::renderBounceOnce(const Scene &scene, int w, int h){
double dx = _width/w;
double dy = _height/h;
std::vector<char> colorImg(w*h*3,0);
Vector3d screen_ori(_center+Vector3d(-_width/2,_height/2, _f));
int nbpix = 0;
#pragma omp parallel for
for (size_t i = 0; i < w; i++) {
#pragma omp parallel for
for (size_t j = 0; j < h; j++) {
std::cout << "pix " << nbpix++ / float(w*h) << "\%" << std::endl;
colorRGB resCol(0,0,0);
Vector3d pix = screen_ori + Vector3d(dx*i, -dy*j, 0);
std::pair<colorRGB, intersection> res = singleRay_rec(_center, pix, scene);
colorRGB primaryCol = res.first;
intersection pt = res.second;
if (pt.valid()){
if (pt.element() == NULL) {
resCol = primaryCol;
} else {
// shoot refracted rays in random directions around surface normal
// and flip normal if we're looking at the object from 'the other side'
Vector3d normal = pt.element()->normal(pt.point());
if (!pt.element()->side(_center)){
normal *= -1;
}
Matrix3d rot = NormalToRotation(normal);
colorRGB secondaryCol(0,0,0);
#pragma omp parallel for
for (size_t i = 0; i < (_subrays*_subrays); i++) {
float u = (1 + i/_subrays)/float(_subrays) + (1./_subrays)*rand()/(float)RAND_MAX;
float v = (1+ i%_subrays)/float(_subrays) + (1./_subrays)*rand()/(float)RAND_MAX;
Vector3d randomRay = rot*hemisphereRandomRay(u, v);
randomRay += pt.point();
std::pair<colorRGB, intersection> resRay = singleRay_rec(pt.point(), randomRay, scene);
secondaryCol = secondaryCol + resRay.first;
}
secondaryCol = secondaryCol / (_subrays*_subrays);
resCol = (primaryCol + pt.col()*secondaryCol)/2;
}
}
colorImg[3*(j*w+i)] = fmin(255,255*resCol.r);
colorImg[3*(j*w+i)+1] = fmin(255,255*resCol.g);
colorImg[3*(j*w+i)+2] = fmin(255,255*resCol.b);
}
}
cv::Mat image(cv::Size(w, h), CV_8UC3, colorImg.data(), cv::Mat::AUTO_STEP);
return image.clone();
}
cv::Mat Camera::renderDirect(const Scene &scene, int w, int h){
double dx = _width/w;
double dy = _height/h;
std::vector<char> colorImg(w*h*3,0);
IOFormat singleLine(FullPrecision, DontAlignCols, ", ", ", ", "", "", " [ ", " ] ");
Vector3d screen_ori(_center+Vector3d(-_width/2,_height/2, _f));
#pragma omp parallel for
for (size_t i = 0; i < w; i++) {
#pragma omp parallel for
for (size_t j = 0; j < h; j++) {
Vector3d pix = screen_ori + Vector3d(dx*i, -dy*j, 0);
std::pair<colorRGB, intersection> res = singleRay_rec(_center, pix, scene);
colorRGB resCol = res.first;
colorImg[3*(j*w+i)] = fmin(255,255*resCol.r);
colorImg[3*(j*w+i)+1] = fmin(255,255*resCol.g);
colorImg[3*(j*w+i)+2] = fmin(255,255*resCol.b);
}
}
cv::Mat image(cv::Size(w, h), CV_8UC3, colorImg.data(), cv::Mat::AUTO_STEP);
return image.clone();
}