4
4
#include < chrono>
5
5
#include < random>
6
6
7
+ #include < GL/gl.h>
8
+ #include < GL/glu.h>
9
+
7
10
const std::vector<std::array<unsigned char , 3 >> zgradient = {
8
11
{0 , 0 , 255 },
9
12
{0 , 29 , 252 },
@@ -84,6 +87,30 @@ const std::vector<std::array<unsigned char, 3>> igradient = {
84
87
85
88
Drawer::Drawer (SDL_Window *window, DataFrame df, std::string hnof)
86
89
{
90
+ zNear = 1 ;
91
+ zFar = 100000 ;
92
+ fov = 70 ;
93
+
94
+ SDL_GetWindowSize (window, &width, &height);
95
+
96
+ glViewport (0 , 0 , width, height);
97
+ glMatrixMode (GL_PROJECTION);
98
+ glLoadIdentity ();
99
+ gluPerspective (fov, (float )width/(float )height, zNear, zFar);
100
+ glMatrixMode (GL_MODELVIEW);
101
+ glLoadIdentity ();
102
+
103
+ glEnable (GL_DEPTH_TEST);
104
+ glDepthFunc (GL_LEQUAL);
105
+ glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
106
+
107
+ glEnable (GL_POINT_SMOOTH);
108
+ glEnable (GL_LINE_SMOOTH);
109
+ glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
110
+
111
+ glPixelStorei (GL_PACK_ALIGNMENT, 1 );
112
+ glPixelStorei (GL_UNPACK_ALIGNMENT, 1 );
113
+
87
114
this ->window = window;
88
115
89
116
this ->df = df;
@@ -300,10 +327,7 @@ bool Drawer::draw()
300
327
301
328
glEnd ();
302
329
303
- if (lightning)
304
- {
305
- eyes_dome_lightning ();
306
- }
330
+ if (lightning) edl ();
307
331
308
332
if (draw_index)
309
333
{
@@ -390,24 +414,19 @@ bool Drawer::draw()
390
414
return true ;
391
415
}
392
416
393
- void Drawer::eyes_dome_lightning ()
417
+ void Drawer::edl ()
394
418
{
395
- GLint viewport[4 ];
396
- glGetIntegerv (GL_VIEWPORT, viewport);
397
- int w = viewport[2 ];
398
- int h = viewport[3 ];
399
-
400
- std::vector< GLfloat > depth ( w * h, 0 );
401
- glReadPixels ( 0 , 0 , w, h, GL_DEPTH_COMPONENT, GL_FLOAT, &depth[0 ] );
419
+ std::vector< GLfloat > depth ( width * height, 0 );
420
+ glReadPixels ( 0 , 0 , width, height, GL_DEPTH_COMPONENT, GL_FLOAT, &depth[0 ] );
402
421
403
- std::vector<GLubyte> colorBuffer (w * h * 3 );
404
- glReadPixels (0 , 0 , w, h , GL_RGB, GL_UNSIGNED_BYTE, &colorBuffer[0 ]);
422
+ std::vector<GLubyte> colorBuffer (width * height * 3 );
423
+ glReadPixels (0 , 0 , width, height , GL_RGB, GL_UNSIGNED_BYTE, &colorBuffer[0 ]);
405
424
406
425
const float zNear = 1 ;
407
426
const float zFar = 10000 ;
408
427
409
- std::vector<GLfloat> worldLogDistances (w * h );
410
- for (int i = 0 ; i < w * h ; ++i)
428
+ std::vector<GLfloat> worldLogDistances (width * height );
429
+ for (int i = 0 ; i < width * height ; ++i)
411
430
{
412
431
GLfloat z = depth[i]; // Depth value from the depth buffer
413
432
GLfloat zNDC = 2 .0f * z - 1 .0f ; // Convert depth value to Normalized Device Coordinate (NDC)
@@ -431,11 +450,11 @@ void Drawer::eyes_dome_lightning()
431
450
432
451
// Iterate over each pixel to shade the rendering
433
452
float edlStrength = 10 ;
434
- for (int y = 0 ; y < h ; ++y)
453
+ for (int y = 0 ; y < height ; ++y)
435
454
{
436
- for (int x = 0 ; x < w ; ++x)
455
+ for (int x = 0 ; x < width ; ++x)
437
456
{
438
- int idx = y * w + x;
457
+ int idx = y * width + x;
439
458
440
459
// Find the maximum log depth among neighbors
441
460
GLfloat maxLogDepth = std::max (0 .0f , worldLogDistances[idx]);
@@ -446,9 +465,9 @@ void Drawer::eyes_dome_lightning()
446
465
{
447
466
int nx = x + offset.first ;
448
467
int ny = y + offset.second ;
449
- if (nx >= 0 && nx < w && ny >= 0 && ny < h )
468
+ if (nx >= 0 && nx < width && ny >= 0 && ny < height )
450
469
{
451
- int nIdx = ny * w + nx;
470
+ int nIdx = ny * width + nx;
452
471
sum += maxLogDepth - worldLogDistances[nIdx];
453
472
}
454
473
}
@@ -463,7 +482,21 @@ void Drawer::eyes_dome_lightning()
463
482
}
464
483
}
465
484
466
- glDrawPixels (w, h, GL_RGB, GL_UNSIGNED_BYTE, colorBuffer.data ());
485
+ glDrawPixels (width, height, GL_RGB, GL_UNSIGNED_BYTE, colorBuffer.data ());
486
+ }
487
+
488
+ void Drawer::resize ()
489
+ {
490
+ SDL_GetWindowSize (window, &width, &height);
491
+
492
+ glViewport (0 , 0 , width, height);
493
+ glMatrixMode (GL_PROJECTION);
494
+ glLoadIdentity ();
495
+ gluPerspective (fov, (float )width/(float )height, zNear, zFar);
496
+ glMatrixMode (GL_MODELVIEW);
497
+ glLoadIdentity ();
498
+
499
+ camera.changed = true ;
467
500
}
468
501
469
502
bool Drawer::is_visible (const Node& octant)
0 commit comments