Repository navigation
Expand file tree
/
Copy pathRaytracer.cpp
More file actions
350 lines (296 loc) · 9.94 KB
/
Copy pathRaytracer.cpp
File metadata and controls
350 lines (296 loc) · 9.94 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "Raytracer.h"
#include <algorithm>
#include <cmath>
#include <iostream>
#include <thread>
#include <fstream>
#include <tuple>
#include <chrono>
#include <boost/lexical_cast.hpp>
#include <boost/tuple/tuple.hpp>
//fundamental methods:
std::vector<PrimaryRayBundle> Raytracer::generatePrimaryRays() const{
std::vector<PrimaryRayBundle> primRays;
const Image& img = _scene.getImage();
for(unsigned int i= 0; i< img.getWidth(); ++i)
for(unsigned int j= 0; j< img.getHeight(); ++j){
PrimaryRayBundle pr;
pr.x=i; pr.y=j;
Ray ray;
ray.setRaydepth(_scene.getMaxRayDepth());
ray.setOrigin(_scene.getEye());
Vector3 dir = _scene.getUpperLeftCorner()
+ (_scene.getRight() * (_scene.getWidth()*i/img.getWidth()) )
+ (_scene.getDown() * (_scene.getHeight()*j/img.getHeight()) )
- _scene.getEye();
dir.normalize();
ray.setDirection( dir );
pr.r = ray;
pr.sampling = 1;
primRays.push_back(pr);
}
return primRays;
}
std::vector<PrimaryRayBundle> Raytracer::generatePrimaryRays(int x1, int x2, int y1, int y2) const{
std::vector<PrimaryRayBundle> primRays;
const Image& img = _scene.getImage();
for(int i = x1; i< x2;++i)
for(int j= y1; j < y2; ++j){
PrimaryRayBundle pr;
pr.x=i; pr.y=j;
Ray ray;
ray.setRaydepth(_scene.getMaxRayDepth());
ray.setOrigin(_scene.getEye());
Vector3 dir = _scene.getUpperLeftCorner()
+ (_scene.getRight() * (_scene.getWidth()*i/img.getWidth()) )
+ (_scene.getDown() * (_scene.getHeight()*j/img.getHeight()) )
- _scene.getEye();
dir.normalize();
ray.setDirection( dir );
pr.r = ray;
pr.sampling = 1;
primRays.push_back(pr);
}
return primRays;
}
//replace by recursive ray calculation and color calculation, has to return a color
Color Raytracer::traceRay(const Ray& ray) const{
Color col = Color(0,0,0);
//intersection Informations
IntersectionCompound intersection;
intersection.t = -1;
intersection.mat = 0;
//TODO: delete this part, use only the
//test for nearest intersections
for(unsigned int i=0; i< _scene.getNumObjects(); ++i){
IntersectionCompound inter = _scene.getObject(i)->getIntersection(ray);
if(inter.t>0 && ( inter.t < intersection.t or intersection.t==-1))
intersection = inter;
}
//+ kd-tree testen
IntersectionCompound inter = _scene.getIntersectionWithKDTree(ray);
if(inter.t>0 && ( inter.t < intersection.t or intersection.t==-1))
intersection = inter;
//calculate color and recursive call
if( intersection.t > eps and intersection.mat!=0){//something hit?
col = localColor(intersection,ray);
if(intersection.mat->getReflection()>eps and ray.getDepth()>0){
Ray reflectedRay = Ray::getReflectedRay(ray,intersection.t,intersection.normal);
reflectedRay.multiplyStrength(intersection.mat->getReflection());
if(reflectedRay.getStrength() > 0.01)//more than 1% influence on the color
col += intersection.mat->getReflection()*traceRay(reflectedRay);
//TODO: tranparenz erweitern
}
}
return col;
}
void Raytracer::traceRays(std::vector<PrimaryRayBundle>& rays){
Raytracer *rt = this;
TracingFunctor t_func( rt );
for(uint i=0; i<rays.size();++i)
t_func(rays[i]);
}
Color Raytracer::localColor(const IntersectionCompound& inter,const Ray& ray) const{//TODO: init scene here
Material &m = *inter.mat;
Vector3 hitpoint = ray.getOrigin()+inter.t * ray.getDirection();
//get all shadow percentages ONCE:
std::vector<real> shadows;
int shadow_sampling = 7;//TODO: better position to set up light sampling param
for(unsigned int i=0; i<_scene.getNumLights() ; ++i)
shadows.push_back(inShadow(hitpoint,_scene.getLight(i),shadow_sampling));
//ambient term
Color local_color = m.getColor(inter);
Color c = _scene.getAmbientIntensity()*_scene.getAmbient()*local_color *m.getAmbient();
//diffuse term
for(unsigned int i=0; i<_scene.getNumLights() ; ++i){
const Vector3 &pos = _scene.getLight(i).getPosition();
if(shadows[i] > 1-eps)
continue;
Vector3 lightdir = pos - hitpoint;
lightdir.normalize();
//TODO get shadow percentage
c += (1-shadows[i])*_scene.getLight(i).getIntensity()*
_scene.getLight(i).getColor()*m.getDiffuse()*local_color
*std::max(real(0),inter.normal.dot(lightdir));
}
//specular term
for(unsigned int i=0; i<_scene.getNumLights() ; ++i){
std::vector<Vector3> lightdirs;
_scene.getLight(i).getDirectionsToLight( hitpoint, shadow_sampling, lightdirs);
real max_spec = 0;
foreach(Vector3 &lightdir, lightdirs){
//blinn-phong
/*
lightdir.normalize();
Vector3 toEye = _scene.getEye() - hitpoint;
toEye.normalize();
Vector3 h = lightdir + toEye;
h.normalize();
max_spec = std::max(max_spec,inter.normal.dot(h));
*/
//only phong:
///*
lightdir.normalize();
Vector3 toEye = _scene.getEye() - hitpoint;
toEye.normalize();
Vector3 reflected = 2*(lightdir.dot(inter.normal))*inter.normal-lightdir;
max_spec = std::max(max_spec,reflected.dot(toEye));
//*/
}
c += (1-shadows[i])*_scene.getLight(i).getIntensity()*_scene.getLight(i).getColor()
*m.getSpecular()* std::pow(max_spec,m.getShininess());
}
return c;
}
std::vector<PrimaryRayBundle> Raytracer::generateAliasedRays(int x1, int x2, int y1, int y2,
const std::vector<std::vector<bool> > *edges, bool debug) const{
//find pixel to trace
std::vector<PrimaryRayBundle> aliasedRays;
for(int i= x1; i< x2; ++i)
for(int j= y1; j< y2; ++j)
if( (*edges)[i][j]){
PrimaryRayBundle pr;
pr.x=i; pr.y=j;
if(debug){
pr.color = Color(1,0,0);
continue;
}
Ray ray;
ray.setRaydepth(_scene.getMaxRayDepth());
ray.setOrigin(_scene.getEye());
Vector3 dir = _scene.getUpperLeftCorner()
+ (_scene.getRight()
* (_scene.getWidth()*i/_scene.getImage().getWidth()) )
+ (_scene.getDown()
* (_scene.getHeight()*j/_scene.getImage().getHeight()) )
- _scene.getEye();
dir.normalize();
ray.setDirection( dir );
pr.r = ray;
pr.sampling = _scene.getSampling();
aliasedRays.push_back(pr);
}
return aliasedRays;
}
//regiona based version
std::vector<PrimaryRayBundle> Raytracer::generateAliasedRays(bool debug) const{
//find pixel to trace
const Image &img = _scene.getImage();
std::vector<std::vector<bool> > edges = img.findEdges();
std::vector<PrimaryRayBundle> aliasedRays;
for(unsigned int i= 0; i< edges.size(); ++i)
for(unsigned int j= 0; j< edges[i].size(); ++j)
if(edges[i][j]){
PrimaryRayBundle pr;
pr.x=i; pr.y=j;
if(debug){
pr.color = Color(1,0,0);
continue;
}
Ray ray;
ray.setRaydepth(_scene.getMaxRayDepth());
ray.setOrigin(_scene.getEye());
Vector3 dir = _scene.getUpperLeftCorner()
+ (_scene.getRight()
* (_scene.getWidth()*i/_scene.getImage().getWidth()) )
+ (_scene.getDown()
* (_scene.getHeight()*j/_scene.getImage().getHeight()) )
- _scene.getEye();
dir.normalize();
ray.setDirection( dir );
pr.r = ray;
pr.sampling = _scene.getSampling();
aliasedRays.push_back(pr);
}
return aliasedRays;
}
void Raytracer::trace(int t){
if (t>0) num_cores = t;
std::cout<<"starting tracing with "<< num_cores <<" threads"<<std::endl;
//time stuff
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::duration<double> sec;
Clock::time_point start, stop;
start = Clock::now();
//create num core threads
//and create enough chunks for them to work on
//at the end: join
std::vector<std::thread> threads;
tilesToTrace_.clear();
curr_ = 0;
uint x = 0,y = 0, x_step = 32, y_step = 32;//TODO fix stepsize
while( x < _scene.getImage().getWidth()){
uint x2 = std::min(x+x_step,_scene.getImage().getWidth());
y = 0;
while( y < _scene.getImage().getHeight() ){
uint y2 = std::min(y+y_step,_scene.getImage().getHeight());
tilesToTrace_.push_back(std::make_tuple(x,x2,y,y2));
y = y2;
}
x = x2;
}
//create new thread here
for(uint i =0; i<num_cores; ++i){
threads.push_back(
std::thread{TracingFunctor(this)}
);
}
foreach(auto &t, threads)
t.join();
//TODO: delete old call:
//std::vector<PrimaryRayBundle> primRays = generatePrimaryRays();//generate rays
//traceRays(primRays);
stop = Clock::now();
auto time1 = sec(stop-start).count();
std::cout<<"Runtime: "<<time1<<" seconds"<<std::endl;
start = Clock::now();
//remove ugly edges
if( _scene.getSampling()>0){
const std::vector<std::vector<bool> > edges = _scene.getImage().findEdges();
curr_ = 0;
threads.clear();
for(uint i =0; i<num_cores; ++i){
threads.push_back(
std::thread{TracingFunctor(this,_debug,&edges)}
);
}
foreach(auto &t, threads)
t.join();
//traceRays( aliasRays );
}
stop = Clock::now();
auto time2 = sec(stop-start).count();
std::cout<<"Runtime(antialias): "<<time2<<" seconds"<<std::endl;
std::cout<<"Total running time: "<<time1+time2<<" seconds"<<std::endl;
}
real Raytracer::inShadow(const Vector3& coord, const Lightsource &light, int num_samples) const{
real shadow_hits = 0;
//Vector3 lightdir = light - coord;
std::vector<Vector3> lightdirs;
light.getDirectionsToLight( coord, num_samples, lightdirs);
foreach(Vector3& lightdir, lightdirs){
real distance = lightdir.length();
lightdir.normalize();
Ray ray(coord+eps*lightdir,lightdir);
//TODO: do something better here AND work only with the kd_tree
bool hit = false;
for(unsigned int i=0; i<_scene.getNumObjects(); ++i){
IntersectionCompound inter;
//prevent jumps on uninitialised values
inter.t = -1;
inter = _scene.getObject(i)->getIntersection(ray);
if(inter.t > eps and inter.t < distance){
shadow_hits+=1;
hit = true;
break; //no need to find out how much objects block the view
}
}
if(not hit){
IntersectionCompound inter = _scene.getIntersectionWithKDTree(ray);
if(inter.t > eps and inter.t < distance)
shadow_hits+=1;
}
}
//1 => totally in shadow, 0 => no shadow at all
return shadow_hits/lightdirs.size();
}