-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
552 lines (446 loc) · 14.9 KB
/
main.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#ifdef WIN32
#include <windows.h>
#endif
#include "GL/glut.h"
#include <stdlib.h>
#include <math.h>
#include <ctime>
#include <thread>
#include <algorithm> //random_shuffle
#include <assert.h>
#include "config.h"
#include "raytracing.h"
#include "mesh.h"
#include "traqueboule.h"
#include "imageWriter.h"
#include "settings.h"
//This is the main application
//Most of the code in here, does not need to be modified.
//It is enough to take a look at the function "drawFrame",
//in case you want to provide your own different drawing functions
Vec3Df MyCameraPosition;
//MyLightPositions stores all the light positions to use
//for the ray tracing. Please notice, the light that is
//used for the real-time rendering is NOT one of these,
//but following the camera instead.
std::vector<Vec3Df> MyLightPositions;
//Main mesh
Mesh MyMesh;
class RayTracer {
public:
RayTracer(void) {
Image result(WindowSize_X, WindowSize_Y);
for (int i = 0; i < 16; ++i)
pixelsdone[i] = 0;
for (int i = 0; i < WindowSize_Y; ++i)
linedone[i] = false;
produceRay(0, 0, &origin00, &dest00);
produceRay(0, WindowSize_Y - 1, &origin01, &dest01);
produceRay(WindowSize_X - 1, 0, &origin10, &dest10);
produceRay(WindowSize_X - 1, WindowSize_Y - 1, &origin11, &dest11);
};
static void produceRay(int x_I, int y_I, Vec3Df * origin, Vec3Df * dest);
Vec3Df raytrace(double x, double y, float &depth);
Vec3Df raytraceMSAA(int x, int y, float &depth);
void threadmethod(int threadID);
double doDaRayTracingShizz();
private:
clock_t begin;
int pixelsdone[16];
std::thread t[16];
unsigned int tstart[16];
unsigned int tend[16];
unsigned int tcurrent[16];
bool linedone[3000];
//Setup an image with the size of the current image.
Image result;
//produce the rays for each pixel, by first computing
//the rays for the corners of the frustum.
Vec3Df origin00, dest00;
Vec3Df origin01, dest01;
Vec3Df origin10, dest10;
Vec3Df origin11, dest11;
};
/**
* Main function, which is drawing an image (frame) on the screen
*/
void drawFrame( )
{
yourDebugDraw();
}
//animation is called for every image on the screen once
void animate()
{
MyCameraPosition=getCameraPosition();
glutPostRedisplay();
}
void display(void);
void reshape(int w, int h);
void keyboard(unsigned char key, int x, int y);
/**
* Main Programme
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
//framebuffer setup
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH );
// positioning and size of window
glutInitWindowPosition(WindowPos_X, WindowPos_Y);
glutInitWindowSize(WindowSize_X,WindowSize_Y);
glutCreateWindow(argv[0]);
//initialize viewpoint
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-4);
tbInitTransform(); // This is for the trackball, please ignore
tbHelp(); // idem
MyCameraPosition=getCameraPosition();
//activate the light following the camera
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable(GL_COLOR_MATERIAL);
int LightPos[4] = {0,0,2,0};
int MatSpec [4] = {1,1,1,1};
glLightiv(GL_LIGHT0,GL_POSITION,LightPos);
//normals will be normalized in the graphics pipeline
glEnable(GL_NORMALIZE);
//clear color of the background is black.
glClearColor (backgroundColor[0], backgroundColor[1], backgroundColor[2], 0.0);
// Activate rendering modes
//activate depth test
glEnable( GL_DEPTH_TEST );
//draw front-facing triangles filled
//and back-facing triangles as wires
glPolygonMode(GL_FRONT,GL_FILL);
glPolygonMode(GL_BACK,GL_LINE);
//interpolate vertex colors over the triangles
glShadeModel(GL_SMOOTH);
// glut setup... to ignore
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMouseFunc(tbMouseFunc); // trackball
glutMotionFunc(tbMotionFunc); // uses mouse
glutIdleFunc( animate);
init();
//main loop for glut... this just runs your application
glutMainLoop();
return 0; // execution never reaches this point
}
/**
* OpenGL setup - functions do not need to be changed!
* you can SKIP AHEAD TO THE KEYBOARD FUNCTION
*/
//what to do before drawing an image
void display(void)
{
glPushAttrib(GL_ALL_ATTRIB_BITS);//store GL state
// Effacer tout
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear image
glLoadIdentity();
tbVisuTransform(); // init trackball
drawFrame( ); //actually draw
glutSwapBuffers();//glut internal switch
glPopAttrib();//return to old GL state
}
//Window changes size
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho (-1.1, 1.1, -1.1,1.1, -1000.0, 1000.0);
gluPerspective (50, (float)w/h, 0.01, 10);
glMatrixMode(GL_MODELVIEW);
}
// react to keyboard input
void keyboard(unsigned char key, int x, int y)
{
Image img = Image();
double times = 0;
int lights = 0;
float invalue = 0;
printf("key %d '%c' pressed at %d,%d\n", key, key, x, y);
fflush(stdout);
switch (key)
{
//add/update a light based on the camera position.
case 't':
img.printGauseanMap(0.001f);
img.printGauseanMap(0.01f);
img.printGauseanMap(0.1f);
img.printGauseanMap(0.3f);
img.printGauseanMap(0.7f);
img.printGauseanMap(0.9f);
img.printGauseanMap(1.0f);
break;
case 'L':
MyLightPositions.push_back(getCameraPosition());
break;
case 'l':
MyLightPositions[MyLightPositions.size()-1]=getCameraPosition();
break;
case 'r':
{
//Pressing r will launch the raytracing.
RayTracer r;
r.doDaRayTracingShizz();
break;
}
case 'R':
for (int i = 0; i < 5; ++i) {
RayTracer r;
times += r.doDaRayTracingShizz() / 5;
}
printf("\nAverage RayTracing time: %f", times);
break;
case 's':
FILE* file;
file = fopen("camera.sav", "wb");
for (int i = 0; i < 16; ++i)
fprintf(file, "%f ", tb_matrix[i]);
fprintf(file, "\r\n%d\r\n", MyLightPositions.size());
for (int i = 0; i < MyLightPositions.size(); ++i)
fprintf(file, "%f %f %f\r\n", MyLightPositions[i][0], MyLightPositions[i][1], MyLightPositions[i][2]);
fclose(file);
printf("Camera and light positions saved\n\n");
break;
case 'S':
FILE* in;
in = fopen("camera.sav", "r");
for (int i = 0; i < 16; ++i) {
fscanf(in, "%f ", &invalue);
tb_matrix[i] = invalue;
}
fscanf(in, "\r\n%d\r\n", &lights);
MyLightPositions.clear();
MyLightPositions.reserve(lights);
for (int i = 0; i < lights; ++i) {
float inlight[3];
fscanf(in, "%f ", &invalue);
inlight[0] = invalue;
fscanf(in, "%f ", &invalue);
inlight[1] = invalue;
fscanf(in, "%f", &invalue);
inlight[2] = invalue;
fscanf(in, "\r\n");
MyLightPositions.push_back(Vec3Df(inlight));
}
fclose(in);
glutPostRedisplay();
break;
case 27: // touche ESC
exit(0);
}
//produce the ray for the current mouse position
Vec3Df testRayOrigin, testRayDestination;
RayTracer::produceRay(x, y, &testRayOrigin, &testRayDestination);
yourKeyboardFunc(key,x,y, testRayOrigin, testRayDestination);
}
/* @return amount of seconds the raytracing took */
double RayTracer::doDaRayTracingShizz() {
printf("\nRaytracing a complete %i x %i image \n", WindowSize_X, WindowSize_Y);
// Without the following line, changing sizes will mess up the preview.
result.writeImageBMP("result.bmp");
begin = clock();
unsigned n = Thread_Amount;
n = (n < 1 ? 1 : n); //not less than 1, duh. xD
n = (n > 16 ? 16 : n); //not more than 16, as thread array is hardcoded length 16
printf("There are %d threads", n);
float linesperthread = float(WindowSize_Y) / float(n);
for (int j = 0; j < n; ++j) {
tcurrent[j] = tstart[j] = j * linesperthread;
tend[j] = (j + 1) * linesperthread;
t[j] = std::thread(&RayTracer::threadmethod, this, j);
}
// make sure the last thread won't get out of bounds
tend[n - 1] = WindowSize_Y;
linedone[WindowSize_Y] = true;
int currpx = 0;
int prevpx = 0;
int pxpersec = 0;
int totalsize = WindowSize_X * WindowSize_Y;
do {
//print progress once per second
Sleep(1000);
prevpx = currpx;
currpx = 0;
for (int j = 0; j < n; ++j)
currpx += pixelsdone[j];
int newspeed = (currpx - prevpx);
pxpersec = 0.75 * pxpersec + 0.25 * newspeed;
double remaining = (double)(totalsize - currpx) / (double)(pxpersec);
int r = (int)round(remaining);
printf("\n%3d%%\tPixel %8d/%8d %5d s remaining", 100 * currpx / totalsize, currpx, totalsize, r);
} while (currpx < result._width * result._height);
for (int j = 0; j < n; ++j)
t[j].join();
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
printf("\nRaytracing comlete in %g seconds, storing result...\n", elapsed_secs);
result.writeImagePPM("result.ppm");
printf("Stored result in result.ppm");
result.writeImageBMP("result.bmp");
printf(", result.bmp");
result.writeDepthBMP("depth.bmp");
printf(", depth.bmp");
result.writeBlurredBMP("blurred.bmp");
printf(" and blurred.bmp\n");
return elapsed_secs;
}
void depthIncrease(float &depth, float &depthHolder, int &hitNumber) {
if (depthHolder != std::numeric_limits<float>::max()) {
depth += depthHolder;
++hitNumber;
}
}
void RayTracer::threadmethod(int threadID)
{
bool done = false;
unsigned y = tcurrent[threadID];
linedone[y] = true;
float depth;
while (!done)
{
// Perform raytracing on the line
for (unsigned int x = 0; x < WindowSize_X; ++x)
{
Vec3Df rgb = raytraceMSAA(x, y, depth);
//store the result in an image
result.setPixel(x, y, RGBValue(rgb[0], rgb[1], rgb[2]), depth);
++pixelsdone[threadID];
}
result.writeImageBMP("result.bmp", 0, y, result._width, 1);
// search for next line to do
if (!linedone[tcurrent[threadID] + 1]) {
linedone[++tcurrent[threadID]] = true;
++y;
} else {
done = true;
std::vector<int> randomlist;
for (int i = 0; i < Thread_Amount; ++i)
randomlist.push_back(i); // fill list with threadIDs
std::random_shuffle(randomlist.begin(), randomlist.end());
for (int i = 0; i < randomlist.size(); ++i) {
int tid = randomlist[i];
if (done && tcurrent[tid]+1 < tend[tid] && !linedone[tend[tid]-1]) {
linedone[tend[tid]-1] = true;
y = tend[tid]-1;
--tend[tid];
done = false;
}
}
}
}
printf(" %d done", threadID + 1);
}
Vec3Df RayTracer::raytraceMSAA(int x, int y, float &depth) {
int hitNumber = 0;
float depthHolder;
depth = 0;
Vec3Df rgb = raytrace(x, y, depthHolder);
depthIncrease(depth, depthHolder, hitNumber);
if (hitNumber == 0)
depth = std::numeric_limits<float>::max();
if (hitNumber > 0) {
// MSAA is done using a rotated (square) grid,
// and can therefore only be 4x or 16x.
switch (MSAA) {
case 4:
depth = 0;
hitNumber = 0;
rgb = raytrace(x - 1. / 8., y - 3. / 8., depthHolder); // +#++
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x + 3. / 8., y - 1. / 8., depthHolder); // +++#
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x - 3. / 8., y + 1. / 8., depthHolder); // #+++
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x + 1. / 8., y + 3. / 8., depthHolder); // ++#+
depthIncrease(depth, depthHolder, hitNumber);
rgb /= 4;
if (hitNumber > 0)
depth /= hitNumber;
else
depth = std::numeric_limits<float>::max();
break;
case 16:
depth = 0;
hitNumber = 0;
rgb = raytrace(x - 9. / 32., y - 15. / 32., depthHolder); // +++#++++ ++++++++
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x - 1. / 32., y - 13. / 32., depthHolder); // +++++++# ++++++++
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x + 7. / 32., y - 11. / 32., depthHolder); // ++++++++ +++#++++
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x + 15. / 32., y - 9. / 32., depthHolder); // ++++++++ +++++++#
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x - 11. / 32., y - 7. / 32., depthHolder); // ++#+++++ ++++++++
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x - 3. / 32., y - 5. / 32., depthHolder); // ++++++#+ ++++++++
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x + 5. / 32., y - 3. / 32., depthHolder); // ++++++++ ++#+++++
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x + 13. / 32., y - 1. / 32., depthHolder); // ++++++++ ++++++#+
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x - 13. / 32., y + 1. / 32., depthHolder); // +#++++++ ++++++++
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x - 5. / 32., y + 3. / 32., depthHolder); // +++++#++ ++++++++
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x + 3. / 32., y + 5. / 32., depthHolder); // ++++++++ +#++++++
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x + 11. / 32., y + 7. / 32., depthHolder); // ++++++++ +++++#++
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x - 15. / 32., y + 9. / 32., depthHolder); // #+++++++ ++++++++
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x - 7. / 32., y + 11. / 32., depthHolder); // ++++#+++ ++++++++
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x + 1. / 32., y + 13. / 32., depthHolder); // ++++++++ #+++++++
depthIncrease(depth, depthHolder, hitNumber);
rgb += raytrace(x + 9. / 32., y + 15. / 32., depthHolder); // ++++++++ ++++#+++
depthIncrease(depth, depthHolder, hitNumber);
rgb /= 16.;
if (hitNumber > 0)
depth /= hitNumber;
else
depth = std::numeric_limits<float>::max();
break;
}
}
return rgb;
}
//perform the raytracing for one x/y position
Vec3Df RayTracer::raytrace(double x, double y, float &depth)
{
Vec3Df origin, dest;
//produce the rays for each pixel, by interpolating
//the four rays of the frustum corners.
double xscale = 1.0 - x / (double)(WindowSize_X - 1);
double yscale = 1.0 - y / (double)(WindowSize_Y - 1);
origin = yscale*(xscale*origin00 + (1 - xscale)*origin10) +
(1 - yscale)*(xscale*origin01 + (1 - xscale)*origin11);
dest = yscale*(xscale*dest00 + (1 - xscale)*dest10) +
(1 - yscale)*(xscale*dest01 + (1 - xscale)*dest11);
//launch raytracing for the given ray.
return performRayTracing(origin, dest, 0, 1.0f, depth);
}
//transform the x, y position on the screen into the corresponding 3D world position
void RayTracer::produceRay(int x_I, int y_I, Vec3Df * origin, Vec3Df * dest)
{
int viewport[4];
double modelview[16];
double projection[16];
glGetDoublev(GL_MODELVIEW_MATRIX, modelview); //recuperer matrices
glGetDoublev(GL_PROJECTION_MATRIX, projection); //recuperer matrices
glGetIntegerv(GL_VIEWPORT, viewport);//viewport
int y_new = viewport[3] - y_I;
double x, y, z;
gluUnProject(x_I, y_new, 0, modelview, projection, viewport, &x, &y, &z);
origin->p[0] = float(x);
origin->p[1] = float(y);
origin->p[2] = float(z);
gluUnProject(x_I, y_new, 1, modelview, projection, viewport, &x, &y, &z);
dest->p[0] = float(x);
dest->p[1] = float(y);
dest->p[2] = float(z);
}