122 lines
2.7 KiB
C++
122 lines
2.7 KiB
C++
/*
|
||
* © Copyright 2014 – Urheberrechtshinweis
|
||
* Alle Rechte vorbehalten / All Rights Reserved
|
||
*
|
||
* Programmcode ist urheberrechtlich geschuetzt.
|
||
* Das Urheberrecht liegt, soweit nicht ausdruecklich anders gekennzeichnet, bei Frank Ebner.
|
||
* Keine Verwendung ohne explizite Genehmigung.
|
||
* (vgl. § 106 ff UrhG / § 97 UrhG)
|
||
*/
|
||
|
||
#include "../../../fixC11.h"
|
||
|
||
#include "GridRenderer.h"
|
||
|
||
#include <QOpenGLWidget>
|
||
#include <QOpenGLFunctions>
|
||
|
||
#include "GridModel.h"
|
||
#include <Indoor/grid/Grid.h>
|
||
|
||
/** render the given grid using GL commands */
|
||
void GridRenderer::render(const RenderSettings& rs, GridModel* model) {
|
||
|
||
if (model == nullptr) {return;}
|
||
|
||
rebuildIfNeeded(model);
|
||
|
||
rs.shader->bind();
|
||
rs.shader->setModelMatrix(QMatrix4x4());
|
||
|
||
if (!edges.empty()) {
|
||
rs.shader->setVertices(edges.getVertices().data());
|
||
rs.shader->setVertexColor(edges.getRGBA().data());
|
||
rs.funcs->glDrawArrays(GL_LINES, 0, edges.getVertices().size()/3);
|
||
rs.shader->unsetVertices();
|
||
rs.shader->unsetVertexColor();
|
||
}
|
||
|
||
if (!nodes.empty()) {
|
||
rs.shader->setVertices(nodes.getVertices().data());
|
||
rs.shader->setVertexColor(nodes.getRGBA().data());
|
||
rs.funcs->glDrawArrays(GL_POINTS, 0, nodes.getVertices().size()/3);
|
||
rs.shader->unsetVertices();
|
||
rs.shader->unsetVertexColor();
|
||
}
|
||
|
||
rs.shader->release();
|
||
|
||
}
|
||
|
||
void GridRenderer::rebuildIfNeeded(GridModel* model) {
|
||
|
||
// rebuild needed?
|
||
if (lastBuildID == model->getBuildID()) {return;}
|
||
lastBuildID = model->getBuildID();
|
||
|
||
// reset previous nodes/edges
|
||
edges.clear();
|
||
nodes.clear();
|
||
|
||
// get the grid
|
||
auto grid = model->getGrid();
|
||
|
||
const float sz = 1.0f; // scale-z: more/less z-spacing
|
||
const float oz = 0.1f; // z-offset
|
||
|
||
// build edges
|
||
if (showEdges) {
|
||
|
||
std::unordered_set<uint64_t> used;
|
||
|
||
for (MyNode& n1 : *grid) {
|
||
//glColor3f(0.5, 0.5, 0.5);
|
||
for (MyNode& n2 : grid->neighbors(n1)) {
|
||
uint64_t min = std::min(n1.getIdx(), n2.getIdx());
|
||
uint64_t max = std::max(n1.getIdx(), n2.getIdx());
|
||
uint64_t idx = max << 32 | min;
|
||
if (used.find(idx) == used.end()) {
|
||
Point3 p1(n1.x_cm/100.0f, n1.y_cm/100.0f, n1.z_cm/100.0f*sz+oz);
|
||
Point3 p2(n2.x_cm/100.0f, n2.y_cm/100.0f, n2.z_cm/100.0f*sz+oz);
|
||
edges.addLine(p1, p2, 0,0,0, 0.5);
|
||
used.insert(idx);
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
// build nodes
|
||
if (showNodes) {
|
||
|
||
TriangleData points;
|
||
|
||
for (MyNode& n : *grid) {
|
||
|
||
Color c;
|
||
|
||
// get the color to use
|
||
switch(colorMode) {
|
||
|
||
case GridRendererColorMode::SHOW_NODE_TYPE: {
|
||
c = colors[n.getType()];
|
||
break;
|
||
}
|
||
|
||
case GridRendererColorMode::SHOW_NODE_IMPORTANCE: {
|
||
const float xx = n.navImportance - 0.6;
|
||
c = Color(xx, xx, xx);
|
||
break;
|
||
}
|
||
|
||
}
|
||
|
||
const Point3 p(n.x_cm/100.0f, n.y_cm/100.0f, n.z_cm/100.0f*sz+oz);
|
||
points.addPoint(p, c.r, c.g, c.b, 1);
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|