forked from rolker/bagViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBagGL.cpp
621 lines (547 loc) · 20.6 KB
/
BagGL.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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#include "BagGL.h"
#include <QOpenGLShaderProgram>
#include <QScreen>
#include <QMouseEvent>
#include <QMessageBox>
#include <QLabel>
#include <QStatusBar>
#include <cmath>
const GLuint BagGL::primitiveReset = 0xffffffff;
BagGL::BagGL(QWidget *parent): QOpenGLWidget(parent),
program(0),
polygonMode(GL_FILL),
#ifndef NDEBUG
gldebug(this),
#endif
currentColormap("omnimap"),
rotating(false),
translating(false),
adjustingHeightExaggeration(false),
lodBias(3),
statusBar(nullptr),
statusLabel(new QLabel()),
m_animating(false)
{
qRegisterMetaType<TilePtr>("TilePtr");
connect(&bag, SIGNAL(metaLoaded()), this, SLOT(resetView()));
connect(&bag, SIGNAL(tileLoaded(TilePtr,bool)), this, SLOT(newTile(TilePtr,bool)));
connect(this, SIGNAL(frameSwapped()), this, SLOT(checkAnimation()));
}
BagGL::~BagGL()
{
closeBag();
}
void BagGL::initializeGL()
{
#ifndef NDEBUG
qDebug() << "GL debug? " << gldebug.initialize();
connect(&gldebug, SIGNAL(messageLogged(const QOpenGLDebugMessage &)),this,SLOT(messageLogged(const QOpenGLDebugMessage &)));
gldebug.startLogging();
#endif
QSurfaceFormat f = format();
qDebug() << "OpenGL version: " << QString::number(f.majorVersion())+"."+QString::number(f.minorVersion());
switch(f.profile())
{
case(QSurfaceFormat::NoProfile):
qDebug() << " No Profile";
break;
case(QSurfaceFormat::CoreProfile):
qDebug() << " Core Profile";
break;
case(QSurfaceFormat::CompatibilityProfile):
qDebug() << " Compatibility Profile";
break;
default:
qDebug() << " Unknown Profile";
}
qDebug() << "samples:" << f.samples();
qDebug() << "depth buffer size:" << f.depthBufferSize();
qDebug() << "valid context?" << context()->isValid();
qDebug() << "initialize functions succesful?" << initializeOpenGLFunctions();
program = new QOpenGLShaderProgram(this);
program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vertex.glsl");
program->addShaderFromSourceFile(QOpenGLShader::TessellationEvaluation, ":/tes.glsl");
program->addShaderFromSourceFile(QOpenGLShader::Geometry, ":/geometry.glsl");
program->addShaderFromSourceFile(QOpenGLShader::Fragment,":/fragment.glsl");
program->link();
matrixUniform = program->uniformLocation("matrix");
normMatrixUniform = program->uniformLocation("normMatrix");
lightDirectionUniform = program->uniformLocation("lightDirection");
minElevationUniform = program->uniformLocation("minElevation");
maxElevationUniform = program->uniformLocation("maxElevation");
elevationMapUniform = program->uniformLocation("elevationMap");
colorMapUniform = program->uniformLocation("colorMap");
normalMapUniform = program->uniformLocation("normalMap");
spacingUniform = program->uniformLocation("spacing");
lowerLeftUniform = program->uniformLocation("lowerLeft");
tileSizeUniform = program->uniformLocation("tileSize");
eastElevationMapUniform = program->uniformLocation("eastElevationMap");
eastNormalMapUniform = program->uniformLocation("eastNormalMap");
eastSpacingUniform = program->uniformLocation("eastSpacing");
eastLowerLeftUniform = program->uniformLocation("eastLowerLeft");
eastTileSizeUniform = program->uniformLocation("eastTileSize");
hasEastUniform = program->uniformLocation("hasEast");
northElevationMapUniform = program->uniformLocation("northElevationMap");
northNormalMapUniform = program->uniformLocation("northNormalMap");
northSpacingUniform = program->uniformLocation("northSpacing");
northLowerLeftUniform = program->uniformLocation("northLowerLeft");
northTileSizeUniform = program->uniformLocation("northTileSize");
hasNorthUniform = program->uniformLocation("hasNorth");
northEastElevationMapUniform = program->uniformLocation("northEastElevationMap");
northEastNormalMapUniform = program->uniformLocation("northEastNormalMap");
northEastSpacingUniform = program->uniformLocation("northEastSpacing");
northEastLowerLeftUniform = program->uniformLocation("northEastLowerLeft");
hasNorthEastUniform = program->uniformLocation("hasNorthEast");
glEnable(GL_DEPTH_TEST);
glEnable(GL_PRIMITIVE_RESTART);
glPrimitiveRestartIndex(primitiveReset);
colormaps["topographic"] = QOpenGLTexturePtr(new QOpenGLTexture(QImage(QString(":/colormaps/topographic.png"))));
colormaps["omnimap"] = QOpenGLTexturePtr(new QOpenGLTexture(QImage(QString(":/colormaps/omnimap.png"))));
glGenVertexArrays(1,&tileVAO);
glBindVertexArray(tileVAO);
glGenBuffers(1,&tileBuffer);
// Verticies get generated and displaced by the Tessellation and Geometry shaders so we only need one token vertex to draw a tile.
GLfloat vert[2] = {0.0,0.0};
glBindBuffer(GL_ARRAY_BUFFER, tileBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*2,vert,GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindAttribLocation(program->programId(),0,"inPosition");
glClearColor(0.0, 0.0, 0.0, 0.0);
program->release();
}
void BagGL::resizeGL(int w, int h)
{
camera.setViewport(w,h);
}
void BagGL::paintGL()
{
render();
}
void BagGL::checkAnimation()
{
if(m_animating)
{
//qDebug() << "animating!";
update();
}
else
{
//qDebug() << "NOT animating!";
}
}
void BagGL::render(bool picking)
{
if(translating)
{
float p = translateStartTime.elapsed()/250.0;
if(p >= 1.0)
{
camera.setCenterPosition(translateEndPosition);
translating = false;
m_animating = rotating||translating||adjustingHeightExaggeration;
}
else
{
float ip = 1.0-p;
camera.setCenterPosition(translateStartPosition*ip+translateEndPosition*p);
}
}
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
int passes = 1;
if(picking && polygonMode != GL_FILL)
passes = 2;
program->bind();
// only one token vertex needed to specify a patch for the tessellation engine.
glPatchParameteri(GL_PATCH_VERTICES,1);
QMatrix4x4 matrix = camera.getMatrix();
QMatrix4x4 normMatrix = camera.genNormalMatrix();
BagIO::MetaData meta = bag.getMeta();
program->setUniformValue(matrixUniform, matrix);
program->setUniformValue(normMatrixUniform, normMatrix.normalMatrix());
program->setUniformValue(minElevationUniform, meta.minElevation);
program->setUniformValue(maxElevationUniform, meta.maxElevation);
program->setUniformValue(colorMapUniform,0);
program->setUniformValue(elevationMapUniform,1);
program->setUniformValue(normalMapUniform,2);
program->setUniformValue(eastElevationMapUniform,3);
program->setUniformValue(eastNormalMapUniform,4);
program->setUniformValue(northElevationMapUniform,5);
program->setUniformValue(northNormalMapUniform,6);
program->setUniformValue(northEastElevationMapUniform,7);
program->setUniformValue(northEastNormalMapUniform,8);
QVector3D lightDirection(0.0,0.0,1.0);
program->setUniformValue(lightDirectionUniform,lightDirection);
colormaps[currentColormap]->bind(0);
glBindVertexArray(tileVAO);
for (auto t: overviewTiles)
updateLOD(t.second);
for(int pass = 0; pass < passes; ++pass)
{
if(pass == 1)
{
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glColorMaski(0,GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
}
else
{
glPolygonMode(GL_FRONT_AND_BACK,polygonMode);
glColorMaski(0,GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
}
//for(TilePtr t: bag.getOverviewTiles())
for (auto t: overviewTiles)
{
drawTile(t.second);
//break;
}
}
program->release();
}
void BagGL::checkGL(TilePtr t)
{
if(!t->gl)
{
t->gl = std::make_shared<TileGL>();
t->gl->elevations.setSize(t->ncols,t->nrows);
t->gl->elevations.setFormat(QOpenGLTexture::R32F);
t->gl->elevations.allocateStorage();
t->gl->elevations.setData(QOpenGLTexture::Red,QOpenGLTexture::Float32,t->data->elevations.data());
t->gl->elevations.setMinMagFilters(QOpenGLTexture::Nearest,QOpenGLTexture::Nearest);
t->gl->elevations.setWrapMode(QOpenGLTexture::ClampToEdge);
t->gl->normals.setData(t->data->normalMap);
t->gl->normals.setMinMagFilters(QOpenGLTexture::Nearest,QOpenGLTexture::Nearest);
t->gl->normals.setWrapMode(QOpenGLTexture::ClampToEdge);
t->gl->maxLod = log2f(t->ncols)-1;
t->gl->lod = t->gl->maxLod;
t->data.reset();
}
}
void BagGL::updateLOD(TilePtr t)
{
checkGL(t);
float sizeInPix = camera.getSizeInPixels(t->bounds);
if(sizeInPix>0.0)
{
t->gl->lod = std::floor(log2f(t->ncols/sizeInPix));
t->gl->lod+=lodBias;
if(t->gl->lod < 0 && !t->subTiles.empty())
{
for (auto tp: t->subTiles)
{
updateLOD(tp.second);
}
}
else
t->gl->lod = std::max(0, std::min(t->gl->lod,t->gl->maxLod-1));
}
else
t->gl->lod = t->gl->maxLod;
}
void BagGL::drawTile(TilePtr t)
{
if(t->gl->lod < t->gl->maxLod)
{
if(t->gl->lod < 0 && !t->subTiles.empty())
{
for (auto tp: t->subTiles)
{
drawTile(tp.second);
//break;
}
}
else
{
t->gl->elevations.bind(1);
t->gl->normals.bind(2);
program->setUniformValue(spacingUniform,QVector2D(t->dx,t->dy));
program->setUniformValue(tileSizeUniform,QSize(t->ncols,t->nrows));
program->setUniformValue(lowerLeftUniform,QVector2D(t->bounds.min().x(),t->bounds.min().y()));
// For a stand alone tile, we would normally have n-1 segments where n is the number of vertices,
// but we must account for the seam so number of segments == number of vertices
GLfloat tessLevel = (t->ncols/pow(2.0,t->gl->lod));
GLfloat tlEast = tessLevel;
GLfloat tlNorth = tessLevel;
if(t->east)
{
checkGL(t->east);
program->setUniformValue(hasEastUniform,true);
t->east->gl->elevations.bind(3);
t->east->gl->normals.bind(4);
program->setUniformValue(eastSpacingUniform,QVector2D(t->east->dx,t->east->dy));
program->setUniformValue(eastTileSizeUniform,QSize(t->east->ncols,t->east->nrows));
program->setUniformValue(eastLowerLeftUniform,QVector2D(t->east->bounds.min().x(),t->east->bounds.min().y()));
tlEast = (t->east->ncols/pow(2.0,t->east->gl->lod));
}
else
program->setUniformValue(hasEastUniform,false);
if(t->north)
{
checkGL(t->north);
program->setUniformValue(hasNorthUniform,true);
t->north->gl->elevations.bind(5);
t->north->gl->normals.bind(6);
program->setUniformValue(northSpacingUniform,QVector2D(t->north->dx,t->north->dy));
program->setUniformValue(northTileSizeUniform,QSize(t->north->ncols,t->north->nrows));
program->setUniformValue(northLowerLeftUniform,QVector2D(t->north->bounds.min().x(),t->north->bounds.min().y()));
tlNorth = (t->north->ncols/pow(2.0,t->north->gl->lod));
}
else
program->setUniformValue(hasNorthUniform,false);
if(t->northEast)
{
checkGL(t->northEast);
program->setUniformValue(hasNorthEastUniform,true);
t->northEast->gl->elevations.bind(7);
t->northEast->gl->normals.bind(8);
program->setUniformValue(northEastSpacingUniform,QVector2D(t->northEast->dx,t->northEast->dy));
program->setUniformValue(northEastLowerLeftUniform,QVector2D(t->northEast->bounds.min().x(),t->northEast->bounds.min().y()));
}
else
program->setUniformValue(hasNorthEastUniform,false);
// Tessellation parameters used to generate quads.
// They specify the number of edges for a specific side of the patch.
// Outer edges can differ from inner ones to help in stitching tiles of different resolution together.
GLfloat tlInner[2];
GLfloat tlOuter[4];
tlInner[0] = tessLevel; // number of inner edges in the y direction.
tlInner[1] = tessLevel; // number of inner edges in the x direction.
tlOuter[0] = tessLevel; // number of segments on the left edge.
tlOuter[1] = tessLevel; // number of segments on the bottom edge.
tlOuter[2] = tlEast; // number of segments on the right edge.
tlOuter[3] = tlNorth; // number of segments on the top edge.
glPatchParameterfv(GL_PATCH_DEFAULT_INNER_LEVEL,tlInner);
glPatchParameterfv(GL_PATCH_DEFAULT_OUTER_LEVEL,tlOuter);
// Drawing this single vertex launches the Tessellation shader and get expanded into the verticies for the tile.
glDrawArrays(GL_PATCHES,0,1);
}
}
}
void BagGL::mousePressEvent(QMouseEvent* event)
{
if(event->button() == Qt::LeftButton)
{
rotating= true;
lastPosition = event->pos();
}
if(event->button() == Qt::MiddleButton)
{
int mx = event->pos().x();
int my = height()-event->pos().y();
float gx = -1.0+mx/(width()/2.0);
float gy = -1.0+my/(height()/2.0);
makeCurrent();
if(polygonMode != GL_FILL)
render(true);
GLfloat gz;
glReadPixels(mx, my, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &gz);
gz = (gz-.5)*2.0;
QMatrix4x4 matrix = camera.getMatrix().inverted();
QVector4D mousePos(gx,gy,gz,1.0);
mousePos = matrix*mousePos;
if (gz < 1.0)
{
translating = true;
translateStartPosition = camera.getCenterPosition();
translateEndPosition = QVector3D(mousePos.x()/mousePos.w(),mousePos.y()/mousePos.w(),mousePos.z()/mousePos.w());
translateStartTime.start();
}
}
if(event->button() == Qt::RightButton)
{
adjustingHeightExaggeration = true;
lastPosition = event->pos();
}
m_animating = rotating||translating||adjustingHeightExaggeration;
checkAnimation();
}
void BagGL::mouseReleaseEvent(QMouseEvent* event)
{
if(event->button() == Qt::LeftButton)
{
rotating = false;
}
if(event->button() == Qt::MiddleButton)
{
//translating = false;
}
if(event->button() == Qt::RightButton)
{
adjustingHeightExaggeration = false;
}
m_animating = rotating||translating||adjustingHeightExaggeration;
checkAnimation();
}
void BagGL::mouseMoveEvent(QMouseEvent* event)
{
QString posText = "Mouse: " + QString::number(event->pos().x())+","+QString::number(event->pos().y());
int mx = event->pos().x();
int my = height()-event->pos().y();
float gx = -1.0+mx/(width()/2.0);
float gy = -1.0+my/(height()/2.0);
makeCurrent();
if(polygonMode != GL_FILL)
render(true);
GLfloat gz;
glReadPixels(mx, my, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &gz);
gz = (gz-.5)*2.0;
QMatrix4x4 matrix = camera.getMatrix().inverted();
QVector4D mousePos(gx,gy,gz,1.0);
mousePos = matrix*mousePos;
if (gz < 1.0)
{
auto swCorner = bag.getMeta().swBottomCorner;
posText += " World: "+QString::number(swCorner.x()+mousePos.x()/mousePos.w(),'f')+","+QString::number(swCorner.y()+mousePos.y()/mousePos.w(),'f')+","+QString::number(mousePos.z()/mousePos.w(),'f');
}
statusLabel->setText(posText);
if(rotating)
{
int dx = event->pos().x() - lastPosition.x();
int dy = event->pos().y()- lastPosition.y();
lastPosition = event->pos();
camera.setPitch(camera.getPitch() + dy);
camera.setYaw(camera.getYaw() + dx);
}
if(adjustingHeightExaggeration)
{
int dy = event->pos().y()- lastPosition.y();
lastPosition = event->pos();
camera.setHeightExaggeration(camera.getHeightExaggeration() - dy/10.0);
}
}
void BagGL::wheelEvent(QWheelEvent* event)
{
if(event->angleDelta().y() > 0)
camera.setZoom(camera.getZoom() * 1.3f);
else
camera.setZoom(camera.getZoom() / 1.3f);
update();
}
void BagGL::keyPressEvent(QKeyEvent* event)
{
switch(event->key())
{
case Qt::Key_R:
resetView();
break;
case Qt::Key_BracketLeft:
lodBias--;
qDebug() << "lod bias:" << lodBias;
update();
break;
case Qt::Key_BracketRight:
lodBias++;
qDebug() << "lod bias:" << lodBias;
update();
break;
default:
event->ignore();
}
}
bool BagGL::openBag(const QString& bagFileName)
{
closeBag();
bool ret = bag.open(bagFileName);
if(ret)
resetView();
return ret;
}
void BagGL::closeBag()
{
// for(TilePtr t: bag.getOverviewTiles())
// {
// t->gl.reset();
// }
bag.close();
}
void BagGL::resetView()
{
BagIO::MetaData meta = bag.getMeta();
QVector3D p = meta.size/2.0;
p.setZ(0.0);
camera.setCenterPosition(p);
camera.setPitch(30.0);
camera.setYaw(0.0);
float maxDim = std::max(meta.size.x(),meta.size.y());
if(maxDim > 0.0)
camera.setZoom(2/maxDim);
else
camera.setZoom(1.0);
camera.setHeightExaggeration(1.0);
update();
}
void BagGL::setColormap(const std::string& cm)
{
currentColormap = cm;
update();
}
void BagGL::setDrawStyle(const std::string& ds)
{
if(ds == "solid")
polygonMode = GL_FILL;
if(ds == "wireframe")
polygonMode = GL_LINE;
if(ds == "points")
polygonMode = GL_POINT;
update();
}
void BagGL::messageLogged(const QOpenGLDebugMessage& debugMessage)
{
qDebug() << debugMessage.message();
}
void BagGL::newTile(TilePtr tile, bool isVR)
{
if(isVR)
{
u32 ts = bag.getTileSize();
vrTiles[tile->lowerLeftIndex] = tile;
TileIndex2D parentIndex(tile->lowerLeftIndex.first/ts,tile->lowerLeftIndex.second/ts);
if(overviewTiles.count(parentIndex))
overviewTiles[parentIndex]->subTiles[tile->index]=tile;
TileIndex2D east(tile->lowerLeftIndex.first+1,tile->lowerLeftIndex.second);
if(vrTiles.count(east))
tile->east = vrTiles[east];
TileIndex2D north(tile->lowerLeftIndex.first,tile->lowerLeftIndex.second+1);
if(vrTiles.count(north))
tile->north = vrTiles[north];
TileIndex2D northEast(tile->lowerLeftIndex.first+1,tile->lowerLeftIndex.second+1);
if(vrTiles.count(northEast))
tile->northEast = vrTiles[northEast];
TileIndex2D west(tile->lowerLeftIndex.first-1,tile->lowerLeftIndex.second);
if(vrTiles.count(west))
vrTiles[west]->east = tile;
TileIndex2D south(tile->lowerLeftIndex.first,tile->lowerLeftIndex.second-1);
if(vrTiles.count(south))
vrTiles[south]->north = tile;
TileIndex2D southWest(tile->lowerLeftIndex.first-1,tile->lowerLeftIndex.second-1);
if(vrTiles.count(southWest))
vrTiles[southWest]->northEast = tile;
}
else
{
overviewTiles[tile->index] = tile;
TileIndex2D east(tile->index.first+1,tile->index.second);
if(overviewTiles.count(east))
tile->east = overviewTiles[east];
TileIndex2D north(tile->index.first,tile->index.second+1);
if(overviewTiles.count(north))
tile->north = overviewTiles[north];
TileIndex2D northEast(tile->index.first+1,tile->index.second+1);
if(overviewTiles.count(northEast))
tile->northEast = overviewTiles[northEast];
TileIndex2D west(tile->index.first-1,tile->index.second);
if(overviewTiles.count(west))
overviewTiles[west]->east = tile;
TileIndex2D south(tile->index.first,tile->index.second-1);
if(overviewTiles.count(south))
overviewTiles[south]->north = tile;
TileIndex2D southWest(tile->index.first-1,tile->index.second-1);
if(overviewTiles.count(southWest))
overviewTiles[southWest]->northEast = tile;
}
update();
}
void BagGL::setStatusBar(QStatusBar* sb)
{
statusBar = sb;
statusBar->addWidget(statusLabel);
}