re-added grid-rendering to the editor

rebuild grid/navMesh 3D data only if needed
This commit is contained in:
k-a-z-u
2018-07-03 10:53:03 +02:00
parent 37b3011e5d
commit 2f37baf497
9 changed files with 262 additions and 203 deletions

View File

@@ -26,6 +26,8 @@ private:
Grid<MyNode> grid;
Floorplan::IndoorMap* im;
int buildID = 0;
public:
GridModel() : grid(gridSize_cm) {
@@ -78,6 +80,10 @@ public:
};
int getBuildID() const {
return buildID;
}
void rebuild(Floorplan::IndoorMap* im) {
Listener l;
@@ -88,6 +94,8 @@ public:
Importance::addImportance(grid, &l);
//Importance::addImportance(grid, 400);
buildID = rand();
}
};

View File

@@ -0,0 +1,109 @@
#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);
}
}
}

View File

@@ -4,20 +4,26 @@
#include "MyNode.h"
#include <unordered_set>
#include <Indoor/grid/Grid.h>
#include "../misc/Renderable3D.h"
#include "../misc/Shader.h"
#include "../misc/TriangleData.h"
enum class GridRendererColorMode {
SHOW_NODE_TYPE,
SHOW_NODE_IMPORTANCE,
};
class GridModel;
class GridRenderer {
private:
// settings
GridRendererColorMode colorMode = GridRendererColorMode::SHOW_NODE_IMPORTANCE;
bool showEdges = false;
bool showEdges = true;
bool showNodes = true;
struct Color {
float r,g,b;
@@ -28,6 +34,10 @@ private:
/** node color depending on the node's type. see ctor */
Color colors[200];
TriangleData edges;
TriangleData nodes;
int lastBuildID = 0; // to check whether the model has changed
public:
/** ctor */
@@ -42,99 +52,15 @@ public:
}
void setNodeColorMode(const GridRendererColorMode mode) {this->colorMode = mode;}
void setShowEdges(const bool show) {this->showEdges = show;}
/** render the given grid using GL commands */
void paintGL(Grid<MyNode>* grid) {
void render(const RenderSettings& rs, GridModel* model);
/*
private:
TODO_GL
glDisable(GL_LIGHTING);
const float sz = 1.0f; // scale-z: more/less z-spacing
const float oz = 0.1f; // z-offset
if (showEdges) {
std::unordered_set<uint64_t> used;
glBegin(GL_LINES);
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()) {
glVertex3f(n1.x_cm/100.0f, n1.z_cm/100.0f*sz+oz, n1.y_cm/100.0f);
glVertex3f(n2.x_cm/100.0f, n2.z_cm/100.0f*sz+oz, n2.y_cm/100.0f);
used.insert(idx);
}
}
}
glEnd();
}
glPointSize(3.0f);
glBegin(GL_POINTS);
for (MyNode& n : *grid) {
// get the color to use
switch(colorMode) {
case GridRendererColorMode::SHOW_NODE_TYPE: {
const Color c = colors[n.getType()];
glColor3f(c.r, c.g, c.b);
break;
}
case GridRendererColorMode::SHOW_NODE_IMPORTANCE: {
const float xx = n.navImportance - 0.6;
glColor3f(xx, xx, xx);
break;
}
}
glVertex3f(n.x_cm/100.0f, n.z_cm/100.0f*sz+oz, n.y_cm/100.0f);
}
glEnd();
// std::vector<MyNode> vec = lint();
// glPointSize(4.0f);
// glBegin(GL_POINTS);
// glColor3f(1,0,0);
// for (MyNode& n : vec) {
// glVertex3f(n.x_cm/100.0f, n.z_cm/100.0f*s, n.y_cm/100.0f);
// }
// glEnd();
//glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
*/
}
// std::vector<MyNode> lint() {
// std::vector<MyNode> vec;
// lintStair(vec);
// return vec;
// }
// void lintStair(std::vector<MyNode>& vec) {
// for (MyNode& n1 : *grid) {
// if (n1.getNumNeighbors() <= 5) { vec.push_back(n1);}
// }
// }
void rebuildIfNeeded(GridModel* model);
};