-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglrenderthread.cpp
More file actions
executable file
·448 lines (369 loc) · 12 KB
/
glrenderthread.cpp
File metadata and controls
executable file
·448 lines (369 loc) · 12 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
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
#include "glrenderthread.h"
#include "glframe.h"
#include <math.h>
#include <iostream>
using namespace std;
GLRenderThread::GLRenderThread (GLFrame *_GLFrame) :
QThread (),
GLFrame_var (_GLFrame)
{
doRendering = true;
doResize = false;
FrameCounter = 0;
_camera_dist = -10;
_o = QVector3D (0.0, 0.0, 0.0); // current origin
_current_angles = QPoint (0,0);
// init m with 4x4 identity matrix
for (int i=0; i<16; i++) {_m[i] = 0;}
_m[0]=1; _m[5]=1; _m[10]=1; _m[15]=1;
// added loader's animation
this->addLoaderAnim ();
_point_cloud = new PointCloud(); // create an empty PointCloud instance so that the render thread can render an empty point cloud, this instance will be replaced as soon as the main point cloud has at least one segment to render.
}
void GLRenderThread::resizeViewport (const QSize &size)
{
w = size.width ();
h = size.height ();
doResize = true;
}
void GLRenderThread::stop()
{
doRendering = false;
}
void GLRenderThread::resume()
{
doRendering = true;
this->start();
}
void GLRenderThread::run ()
{
GLFrame_var->makeCurrent ();
GLInit ();
while (doRendering)
{
if (doResize)
{
GLResize (w, h);
doResize = false;
}
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
paintGL(); // render actual frame
FrameCounter++;
GLFrame_var->swapBuffers();
msleep (16); // wait 16ms => about 60 FPS
}
}
void GLRenderThread::GLInit (void)
{
glClearColor (0.05f, 0.05f, 0.2f, 0.0f);
glShadeModel (GL_SMOOTH); //set the shader to smooth shader
}
void GLRenderThread::GLResize (int width, int height)
{
glViewport (0, 0, width, height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (45.,((GLfloat)width)/((GLfloat)height),0.1f,1000.0f);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
gluLookAt (0, 0,-1, // CameraPosition
0, 0 ,0, // CameraLookAt
0, 1 ,0); // CameraUp
}
void GLRenderThread::drawGrid (void)
{
glLineWidth (1.0);
// grid
for (float i = -10; i <= 10; i += 1)
{
glBegin (GL_LINES);
glColor3ub (75, 75, 75);
glVertex3f (-10, 0, i);
glVertex3f (10, 0, i);
glVertex3f (i, 0,-10);
glVertex3f (i, 0, 10);
glEnd ();
}
// contour
glBegin (GL_LINES);
glColor3ub (45, 45, 45);
glVertex3f (-20, -20, 0);
glVertex3f (20, -20, 0);
glVertex3f (20, -20, 0);
glVertex3f (20, 20, 0);
glVertex3f (20, 20, 0);
glVertex3f (-20, 20, 0);
glVertex3f (-20, 20, 0);
glVertex3f (-20, -20, 0);
glEnd ();
glBegin (GL_LINES);
glColor3ub (45, 45, 45);
glVertex3f (0, -20, -20);
glVertex3f (0, 20, -20);
glVertex3f (0, 20, -20);
glVertex3f (0, 20, 20);
glVertex3f (0, 20, 20);
glVertex3f (0, -20, 20);
glVertex3f (0, -20, 20);
glVertex3f (0, -20, -20);
glEnd ();
glBegin (GL_LINES);
glColor3ub (45, 45, 45);
glVertex3f (-20, 0, -20);
glVertex3f (20, 0, -20);
glVertex3f (20, 0, -20);
glVertex3f (20, 0, 20);
glVertex3f (20, 0, 20);
glVertex3f (-20, 0, 20);
glVertex3f (-20, 0, 20);
glVertex3f (-20, 0, -20);
glEnd ();
glBegin (GL_LINES);
glColor3ub (45, 45, 45);
glVertex3f (0, -20, -20);
glVertex3f (0, 20, -20);
glVertex3f (0, 20, -20);
glVertex3f (0, 20, 20);
glVertex3f (0, 20, 20);
glVertex3f (0, -20, 20);
glVertex3f (0, -20, 20);
glVertex3f (0, -20, -20);
glEnd ();
glLineWidth (3.0);
// main axis
glBegin (GL_LINES);
glColor3ub (250, 0, 0);
glVertex3f (0, 0, 0);
glVertex3f (0, 0, 5);
glColor3ub (125, 0, 0);
glVertex3f (0, 0, 5);
glVertex3f (0, 0, 20);
glEnd ();
glBegin (GL_LINES);
glColor3ub (0, 250, 0);
glVertex3f (0, 0, 0);
glVertex3f (0, 5, 0);
glColor3ub (0, 125, 0);
glVertex3f (0, 5, 0);
glVertex3f (0, 20, 0);
glEnd ();
glBegin (GL_LINES);
glColor3ub (0, 0, 250);
glVertex3f (0, 0, 0);
glVertex3f (5, 0, 0);
glColor3ub (0, 0, 125);
glVertex3f (5, 0, 0);
glVertex3f (20, 0, 0);
glEnd ();
}
void GLRenderThread::drawCurrentOriginAxes (void)
{
glPointSize (6.0);
glBegin (GL_POINTS);
glColor3f (0.0,1.0,1.0); glVertex3f (_o.x(), _o.y(), _o.z());
glEnd ();
glLineWidth (2.0);
// main axis
glBegin (GL_LINES);
glColor3ub (250, 0, 0);
glVertex3f (_o.x(), _o.y(), _o.z());
glVertex3f (_o.x(), _o.y(), _o.z()+5);
glEnd ();
glBegin (GL_LINES);
glColor3ub (0, 250, 0);
glVertex3f (_o.x(), _o.y(), _o.z());
glVertex3f (_o.x(), _o.y()+5, _o.z());
glEnd ();
glBegin (GL_LINES);
glColor3ub (0, 0, 250);
glVertex3f (_o.x(), _o.y(), _o.z());
glVertex3f (_o.x()+5, _o.y(), _o.z());
glEnd ();
}
void GLRenderThread::addCameraPyramid (cv::Mat_<double> &camera_matrix, int rows, int cols)
{
double rh = rows / 2000;
double ch = cols / 2500;
double depth = 2.0;
cv::Mat_<double> pp0, pp1, pp2, pp3, pp4;
cv::Mat_<double> p0 = (cv::Mat_<double>(4,1) << 0.0, 0.0, 0.0, 1.0);
cv::Mat_<double> p1 = (cv::Mat_<double>(4,1) << -ch, -rh, depth, 1.0);
cv::Mat_<double> p2 = (cv::Mat_<double>(4,1) << -ch, rh, depth, 1.0);
cv::Mat_<double> p3 = (cv::Mat_<double>(4,1) << ch, rh, depth, 1.0);
cv::Mat_<double> p4 = (cv::Mat_<double>(4,1) << ch, -rh, depth, 1.0);
pp0 = camera_matrix * p0;
pp1 = camera_matrix * p1;
pp2 = camera_matrix * p2;
pp3 = camera_matrix * p3;
pp4 = camera_matrix * p4;
vector<cv::Mat_<double> > camera_pyramid;
camera_pyramid.push_back (pp0);
camera_pyramid.push_back (pp1);
camera_pyramid.push_back (pp2);
camera_pyramid.push_back (pp3);
camera_pyramid.push_back (pp4);
camera_pyramids.push_back (camera_pyramid);
}
void GLRenderThread::displayCameraPyramids (void)
{
unsigned int len = camera_pyramids.size();
for (unsigned int i=0; i<len; i++)
{
vector <cv::Mat_<double> > cp;
cp = camera_pyramids[i];
glLineWidth (1.0);
// camera pyramid
glBegin (GL_LINES);
glColor3ub (150, 150, 0);
glVertex3f (cp[0](0), cp[0](1), cp[0](2));
glVertex3f (cp[1](0), cp[1](1), cp[1](2));
glVertex3f (cp[1](0), cp[1](1), cp[1](2));
glVertex3f (cp[2](0), cp[2](1), cp[2](2));
glVertex3f (cp[2](0), cp[2](1), cp[2](2));
glVertex3f (cp[3](0), cp[3](1), cp[3](2));
glVertex3f (cp[3](0), cp[3](1), cp[3](2));
glVertex3f (cp[4](0), cp[4](1), cp[4](2));
glVertex3f (cp[4](0), cp[4](1), cp[4](2));
glVertex3f (cp[1](0), cp[1](1), cp[1](2));
glVertex3f (cp[0](0), cp[0](1), cp[0](2));
glVertex3f (cp[2](0), cp[2](1), cp[2](2));
glVertex3f (cp[0](0), cp[0](1), cp[0](2));
glVertex3f (cp[3](0), cp[3](1), cp[3](2));
glVertex3f (cp[0](0), cp[0](1), cp[0](2));
glVertex3f (cp[4](0), cp[4](1), cp[4](2));
glEnd ();
}
}
void GLRenderThread::paintGL (void)
{
_delta = QPoint (0,0);
QPoint q;
if (GLFrame_var->getCtrlMetaFlag()==true ^ GLFrame_var->getAltFlag()==true) // xor
{
if (GLFrame_var->getCtrlMetaFlag()==true) // rotation
{
q = _mouse.pos();
_delta = q - _last_pos;
_last_pos = q;
}
else // translation
{
q = _mouse.pos();
// get the mouse deltas and transform them with the camera's local axes so that
// it'll look like as if the camera will translate based on the local axes x and y.
// What happens in reality is that the origin will shift.
// Somehow similar to the Maya Camera Track Tool.
GLfloat delta_x = q.x() - _last_pos.x();
GLfloat delta_y = q.y() - _last_pos.y();
GLfloat vec_x = - _m[0]*delta_x + _m[1]*delta_y + _m[2]*0 ;
GLfloat vec_y = - _m[4]*delta_x + _m[5]*delta_y + _m[6]*0 ;
GLfloat vec_z = - _m[8]*delta_x + _m[9]*delta_y + _m[10]*0 ;
_o.setX (_o.x() + 0.03 * vec_x);
_o.setY (_o.y() + 0.03 * vec_y);
_o.setZ (_o.z() + 0.03 * vec_z);
_last_pos = q;
}
}
else
{
q = _mouse.pos();
_last_pos = q;
}
_current_angles += _delta;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Z−Buffer
glEnable (GL_DEPTH_TEST);
glDepthMask (GL_TRUE);
// Set the camera orientation :
glMatrixMode (GL_MODELVIEW) ;
glLoadIdentity ();
// CameraPos CameraLookAt CameraUp
gluLookAt (_o.x(),_o.y(),_o.z()-40, _o.x(),_o.y(),_o.z(), 0, 1, 0);
// Rotate and move camera forward or backward (Maya-like 3D navigation).
glTranslatef (0, 0, -_camera_dist);
glTranslatef (_o.x(), _o.y(), _o.z()); // rotation around current origin. Part 1/3
glRotatef (-30 - _current_angles.y() * 0.15f, 1, 0, 0); // rotate around axis x. Part 2/3
glRotatef (-30 + _current_angles.x() * 0.1f, 0, 1, 0); // rotate around axis y. Part 2/3
glTranslatef (-_o.x(), -_o.y(), -_o.z()); // rotation around current origin. Part 3/3
// get the camera matrix (or anyway the main matrix) and put it into the GLfloat m[16].
glGetFloatv (GL_MODELVIEW_MATRIX, _m);
// dispaly grid and current origin.
this->drawGrid ();
this->drawCurrentOriginAxes ();
this->displayCameraPyramids ();
vector <float> temp;
temp.resize (3);
// display the loader anim
for (unsigned int i=0; i<_loader_anim.size(); i++)
{
glBegin (GL_POINTS);
temp.clear();
temp = vector<float> (_loader_anim[i]);
glVertex3f (temp[0], temp[1], temp[2]); glColor3f (1., 0., 0.);
glEnd ();
}
// display the point cloud
glEnable (GL_POINT_SMOOTH);
glPointSize (4.0);
vector<cv::Point3d> *meta_temp;
for (unsigned int i=0; i<_point_cloud->size(); i++)
{
PCSegment *pcs = _point_cloud->getPCSegment (i);
meta_temp = pcs->getVectorX ();
for (unsigned int j=0; j<meta_temp->size(); j++)
{
glBegin (GL_POINTS);
temp.clear();
cv::Point3d *temp ;
temp = &(meta_temp->at(j));
if (i==0)
{
glVertex3f (temp->x, temp->y, temp->z); glColor3f (0, 1, 0);
}
else
{
glVertex3f (temp->x, temp->y, temp->z); glColor3f (1., 1. - float(i)/float(_point_cloud->size()), 0.);
}
glEnd ();
}
}
}
void GLRenderThread::updateCameraDistanceFromCenter (float dist)
{
_camera_dist += dist * 0.01;
}
void GLRenderThread::setPointCloud (PointCloud *point_cloud)
{
_point_cloud = point_cloud;
}
////////// additional functions
void GLRenderThread::addLoaderAnim (void)
{
// add three points
vector<float> temp;
temp.resize (3); // fix temp size to 3 floats.
vector<vector<float> > meta_temp;
meta_temp.clear ();
/*
temp.clear ();
temp.push_back (20.0f);
temp.push_back (0.0f);
temp.push_back (-25.0f);
_loader_anim.push_back (temp);
temp.clear ();
temp.push_back (20.0f);
temp.push_back (0.0f);
temp.push_back (-15.0f);
_loader_anim.push_back (temp);
temp.clear ();
temp.push_back (20.0f);
temp.push_back (0.0f);
temp.push_back (-5.0f);
_loader_anim.push_back (temp);
*/
}
void GLRenderThread::removeLoaderAnim (void)
{
// remove old test points.
_loader_anim.clear ();
}