Skip to content

Commit 0f38a37

Browse files
committed
clean code + point budget
1 parent ec2208a commit 0f38a37

File tree

3 files changed

+85
-62
lines changed

3 files changed

+85
-62
lines changed

src/drawer.cpp

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include <chrono>
55
#include <random>
66

7+
#include <GL/gl.h>
8+
#include <GL/glu.h>
9+
710
const std::vector<std::array<unsigned char, 3>> zgradient = {
811
{0, 0, 255},
912
{0, 29, 252},
@@ -84,6 +87,30 @@ const std::vector<std::array<unsigned char, 3>> igradient = {
8487

8588
Drawer::Drawer(SDL_Window *window, DataFrame df, std::string hnof)
8689
{
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+
87114
this->window = window;
88115

89116
this->df = df;
@@ -300,10 +327,7 @@ bool Drawer::draw()
300327

301328
glEnd();
302329

303-
if (lightning)
304-
{
305-
eyes_dome_lightning();
306-
}
330+
if (lightning) edl();
307331

308332
if (draw_index)
309333
{
@@ -390,24 +414,19 @@ bool Drawer::draw()
390414
return true;
391415
}
392416

393-
void Drawer::eyes_dome_lightning()
417+
void Drawer::edl()
394418
{
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] );
402421

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]);
405424

406425
const float zNear = 1;
407426
const float zFar = 10000;
408427

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)
411430
{
412431
GLfloat z = depth[i]; // Depth value from the depth buffer
413432
GLfloat zNDC = 2.0f * z - 1.0f; // Convert depth value to Normalized Device Coordinate (NDC)
@@ -431,11 +450,11 @@ void Drawer::eyes_dome_lightning()
431450

432451
// Iterate over each pixel to shade the rendering
433452
float edlStrength = 10;
434-
for (int y = 0; y < h; ++y)
453+
for (int y = 0; y < height; ++y)
435454
{
436-
for (int x = 0; x < w; ++x)
455+
for (int x = 0; x < width; ++x)
437456
{
438-
int idx = y * w + x;
457+
int idx = y * width + x;
439458

440459
// Find the maximum log depth among neighbors
441460
GLfloat maxLogDepth = std::max(0.0f, worldLogDistances[idx]);
@@ -446,9 +465,9 @@ void Drawer::eyes_dome_lightning()
446465
{
447466
int nx = x + offset.first;
448467
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)
450469
{
451-
int nIdx = ny * w + nx;
470+
int nIdx = ny * width + nx;
452471
sum += maxLogDepth - worldLogDistances[nIdx];
453472
}
454473
}
@@ -463,7 +482,21 @@ void Drawer::eyes_dome_lightning()
463482
}
464483
}
465484

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;
467500
}
468501

469502
bool Drawer::is_visible(const Node& octant)

src/drawer.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#ifndef DRAWER_H
22
#define DRAWER_H
33

4-
#include <GL/gl.h>
5-
#include <GL/glu.h>
64
#include <Rcpp.h>
5+
76
#include "Octree.h"
87
#include "camera.h"
9-
//#include "index.h"
108

119
using namespace Rcpp;
1210

@@ -17,20 +15,23 @@ class Drawer
1715
public:
1816
Drawer(SDL_Window*, DataFrame, std::string hnof);
1917
bool draw();
18+
void resize();
2019
void setPointSize(float);
2120
void setAttribute(Attribute x);
2221
void display_hide_spatial_index() { draw_index = !draw_index; camera.changed = true; };
2322
void display_hide_edl() { lightning = !lightning; camera.changed = true; };
2423
void point_size_plus() { point_size++; camera.changed = true; };
2524
void point_size_minus() { point_size--; camera.changed = true; };
25+
void budget_plus() { point_budget += 500000; camera.changed = true; };
26+
void budget_minus() { if (point_budget > 500000) point_budget -= 500000; camera.changed = true; };
2627
Camera camera;
2728
Octree index;
2829

2930
float point_size;
3031
bool lightning;
3132

3233
private:
33-
void eyes_dome_lightning();
34+
void edl();
3435
bool is_visible(const Node& octant);
3536
void compute_cell_visibility();
3637
void query_rendered_point();
@@ -76,6 +77,11 @@ class Drawer
7677
std::vector<Node*> visible_octants;
7778

7879
SDL_Window *window;
80+
float zNear;
81+
float zFar;
82+
float fov;
83+
int width;
84+
int height;
7985
};
8086

8187
#endif //DRAWER_H

src/lidRviewer.cpp

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22

33
#include <SDL2/SDL.h>
44

5-
#include <GL/gl.h>
6-
#include <GL/glu.h>
7-
85
#include <thread>
96
#include <atomic>
107

118
#include "drawer.h"
129

13-
const float zNear = 1;
14-
const float zFar = 100000;
1510
const Uint32 time_per_frame = 1000 / 30;
1611
bool running = false;
1712

@@ -44,21 +39,6 @@ void sdl_loop(DataFrame df, std::string hnof)
4439
SDL_GLContext glContext = SDL_GL_CreateContext(window);
4540
SDL_GL_SetSwapInterval(1); // Enable VSync
4641

47-
glMatrixMode(GL_PROJECTION);
48-
glLoadIdentity();
49-
gluPerspective(70, (double)width / height, zNear, zFar);
50-
51-
glEnable(GL_DEPTH_TEST);
52-
glDepthFunc(GL_LEQUAL);
53-
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
54-
55-
glEnable(GL_POINT_SMOOTH);
56-
glEnable(GL_LINE_SMOOTH);
57-
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
58-
59-
glPixelStorei(GL_PACK_ALIGNMENT, 1);
60-
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
61-
6242
Drawer *drawer = new Drawer(window, df, hnof);
6343
drawer->camera.setRotateSensivity(0.1);
6444
drawer->camera.setZoomSensivity(10);
@@ -67,6 +47,8 @@ void sdl_loop(DataFrame df, std::string hnof)
6747

6848
last_time = SDL_GetTicks();
6949

50+
bool ctrlPressed = false;
51+
7052
while (run)
7153
{
7254
while (SDL_PollEvent(&event))
@@ -76,10 +58,13 @@ void sdl_loop(DataFrame df, std::string hnof)
7658
case SDL_QUIT:
7759
run = false;
7860
break;
79-
8061
case SDL_KEYDOWN:
8162
switch (event.key.keysym.sym)
8263
{
64+
case SDLK_LCTRL:
65+
case SDLK_RCTRL:
66+
ctrlPressed = true;
67+
break;
8368
case SDLK_ESCAPE:
8469
run = false;
8570
break;
@@ -116,33 +101,32 @@ void sdl_loop(DataFrame df, std::string hnof)
116101
break;
117102
}
118103
break;
119-
104+
case SDL_KEYUP:
105+
{
106+
switch (event.key.keysym.sym)
107+
{
108+
case SDLK_LCTRL:
109+
case SDLK_RCTRL:
110+
ctrlPressed = true;
111+
break;
112+
}
113+
}
120114
case SDL_MOUSEMOTION:
121115
drawer->camera.OnMouseMotion(event.motion);
122116
break;
123-
124117
case SDL_MOUSEBUTTONUP:
125118
case SDL_MOUSEBUTTONDOWN:
126119
drawer->camera.OnMouseEvent(event.button, SDL_MouseWheelEvent{}); // Pass an empty SDL_MouseWheelEvent
127120
break;
128121

129122
case SDL_MOUSEWHEEL:
130-
drawer->camera.OnMouseEvent(SDL_MouseButtonEvent{}, event.wheel); // Pass an empty SDL_MouseButtonEvent
123+
if (ctrlPressed && event.wheel.y > 0) drawer->budget_plus();
124+
else if (ctrlPressed && event.wheel.y < 0) drawer->budget_minus();
125+
else drawer->camera.OnMouseEvent(SDL_MouseButtonEvent{}, event.wheel); // Pass an empty SDL_MouseButtonEvent
131126
break;
132127

133128
case SDL_WINDOWEVENT:
134-
if (event.window.event == SDL_WINDOWEVENT_RESIZED)
135-
{
136-
width = event.window.data1;
137-
height = event.window.data2;
138-
glViewport(0, 0, width, height);
139-
glMatrixMode(GL_PROJECTION);
140-
glLoadIdentity();
141-
gluPerspective(70, (float)width / (float)height, zNear, zFar);
142-
glMatrixMode(GL_MODELVIEW);
143-
glLoadIdentity();
144-
drawer->camera.changed = true;
145-
}
129+
drawer->resize();
146130
break;
147131
}
148132
}

0 commit comments

Comments
 (0)