238 lines
7.3 KiB
C++
238 lines
7.3 KiB
C++
#ifndef NAVMESHRENDERER_H
|
|
#define NAVMESHRENDERER_H
|
|
|
|
|
|
|
|
#include <unordered_set>
|
|
#include <Indoor/navMesh/NavMesh.h>
|
|
#include <Indoor/navMesh/NavMeshTriangle.h>
|
|
#include <Indoor/navMesh/NavMeshType.h>
|
|
|
|
#include <QPainter>
|
|
#include <QOpenGLWidget>
|
|
|
|
#include "../3D/misc/Renderable3D.h"
|
|
#include "../3D/misc/Shader.h"
|
|
|
|
class NavMeshRenderer {
|
|
|
|
private:
|
|
|
|
// settings
|
|
//GridRendererColorMode colorMode = GridRendererColorMode::SHOW_NODE_IMPORTANCE;
|
|
bool showEdges = false;
|
|
|
|
struct Color {
|
|
float r,g,b;
|
|
Color() : r(1), g(0), b(0) {;}
|
|
Color(float r, float g, float b) : r(r), g(g), b(b) {;}
|
|
};
|
|
|
|
/** node color depending on the node's type. see ctor */
|
|
Color colors[200];
|
|
|
|
public:
|
|
|
|
/** ctor */
|
|
NavMeshRenderer() {
|
|
|
|
colors[GridNode::TYPE_FLOOR] = Color(0.4, 0.4, 0.4);
|
|
colors[GridNode::TYPE_STAIR] = Color(0,0,1);
|
|
colors[GridNode::TYPE_ELEVATOR] = Color(0,0,1);
|
|
colors[GridNode::TYPE_DOOR] = Color(0.0, 0.0, 0.0);
|
|
|
|
colors[GridNode::TYPE_OUTDOOR] = Color(0.0, 0.5, 0.0);
|
|
|
|
}
|
|
|
|
|
|
// void setNodeColorMode(const GridRendererColorMode mode) {this->colorMode = mode;}
|
|
void setShowEdges(const bool show) {this->showEdges = show;}
|
|
|
|
|
|
/** render the given grid using GL commands */
|
|
void render(const RenderSettings& rs, NM::NavMesh<NM::NavMeshTriangle>* navMesh, QOpenGLWidget* dst) {
|
|
|
|
if (navMesh == nullptr) {return;}
|
|
|
|
|
|
rs.shader->bind();
|
|
rs.shader->setModelMatrix(QMatrix4x4());
|
|
|
|
std::vector<float> vertices;
|
|
std::vector<float> colors;
|
|
|
|
for (const NM::NavMeshTriangle* tria : *navMesh) {
|
|
|
|
Point3 color;
|
|
|
|
switch (tria->getType()) {
|
|
case (int) NM::NavMeshType::FLOOR_INDOOR: color = Point3(0.8, 0.8, 0.8); break;
|
|
case (int) NM::NavMeshType::FLOOR_OUTDOOR: color = Point3(0.1, 0.8, 0.1); break;
|
|
case (int) NM::NavMeshType::DOOR: color = Point3(0.7, 0.7, 0.8); break;
|
|
case (int) NM::NavMeshType::STAIR_LEVELED: color = Point3(0.5, 0.5, 0.5); break;
|
|
case (int) NM::NavMeshType::STAIR_SKEWED: color = Point3(0.6, 0.6, 0.6); break;
|
|
}
|
|
|
|
vertices.insert(vertices.end(), {tria->getP1().x, tria->getP1().y, tria->getP1().z});
|
|
vertices.insert(vertices.end(), {tria->getP3().x, tria->getP3().y, tria->getP3().z});
|
|
vertices.insert(vertices.end(), {tria->getP2().x, tria->getP2().y, tria->getP2().z});
|
|
|
|
colors.insert(colors.end(), {color.x, color.y, color.z, 1});
|
|
colors.insert(colors.end(), {color.x, color.y, color.z, 1});
|
|
colors.insert(colors.end(), {color.x, color.y, color.z, 1});
|
|
|
|
}
|
|
|
|
rs.shader->setVertices(vertices.data());
|
|
rs.shader->setVertexColor(colors.data());
|
|
glDrawArrays(GL_TRIANGLES, 0, vertices.size()/3);
|
|
rs.shader->unsetVertices();
|
|
rs.shader->unsetVertexColor();
|
|
|
|
vertices.clear();
|
|
colors.clear();
|
|
|
|
for (const NM::NavMeshTriangle* tria : *navMesh) {
|
|
|
|
Point3 color;
|
|
|
|
switch (tria->getType()) {
|
|
case (int) NM::NavMeshType::FLOOR_INDOOR: color = Point3(0.6, 0.6, 0.6); break;
|
|
case (int) NM::NavMeshType::FLOOR_OUTDOOR: color = Point3(0.0, 0.6, 0.0); break;
|
|
case (int) NM::NavMeshType::DOOR: color = Point3(0.5, 0.5, 0.6); break;
|
|
case (int) NM::NavMeshType::STAIR_LEVELED: color = Point3(0.4, 0.4, 0.4); break;
|
|
case (int) NM::NavMeshType::STAIR_SKEWED: color = Point3(0.4, 0.4, 0.4); break;
|
|
}
|
|
|
|
const float o = 0.001f;
|
|
vertices.insert(vertices.end(), {tria->getP1().x, tria->getP1().y, tria->getP1().z+o});
|
|
vertices.insert(vertices.end(), {tria->getP2().x, tria->getP2().y, tria->getP2().z+o});
|
|
|
|
vertices.insert(vertices.end(), {tria->getP2().x, tria->getP2().y, tria->getP2().z+o});
|
|
vertices.insert(vertices.end(), {tria->getP3().x, tria->getP3().y, tria->getP3().z+o});
|
|
|
|
vertices.insert(vertices.end(), {tria->getP3().x, tria->getP3().y, tria->getP3().z+o});
|
|
vertices.insert(vertices.end(), {tria->getP1().x, tria->getP1().y, tria->getP1().z+o});
|
|
|
|
colors.insert(colors.end(), {color.x, color.y, color.z, 1});
|
|
colors.insert(colors.end(), {color.x, color.y, color.z, 1});
|
|
colors.insert(colors.end(), {color.x, color.y, color.z, 1});
|
|
colors.insert(colors.end(), {color.x, color.y, color.z, 1});
|
|
colors.insert(colors.end(), {color.x, color.y, color.z, 1});
|
|
colors.insert(colors.end(), {color.x, color.y, color.z, 1});
|
|
|
|
}
|
|
|
|
rs.shader->setVertices(vertices.data());
|
|
rs.shader->setVertexColor(colors.data());
|
|
glDrawArrays(GL_LINES, 0, vertices.size()/3);
|
|
rs.shader->unsetVertices();
|
|
rs.shader->unsetVertexColor();
|
|
|
|
rs.shader->release();
|
|
|
|
|
|
/*
|
|
|
|
TODO_GL
|
|
|
|
glDisable(GL_LIGHTING);
|
|
|
|
const float s = 2;
|
|
|
|
glBegin(GL_TRIANGLES);
|
|
for (const NM::NavMeshTriangle* tria : *navMesh) {
|
|
|
|
|
|
|
|
switch (tria->getType()) {
|
|
case (int) NM::NavMeshType::FLOOR_INDOOR: glColor3f(0.8, 0.8, 0.8); break;
|
|
case (int) NM::NavMeshType::FLOOR_OUTDOOR: glColor3f(0.1, 0.8, 0.1); break;
|
|
case (int) NM::NavMeshType::DOOR: glColor3f(0.7, 0.7, 0.8); break;
|
|
case (int) NM::NavMeshType::STAIR_LEVELED: glColor3f(0.5, 0.5, 0.5); break;
|
|
case (int) NM::NavMeshType::STAIR_SKEWED: glColor3f(0.6, 0.6, 0.6); break;
|
|
}
|
|
|
|
glVertex3f(tria->getP1().x, tria->getP1().z, tria->getP1().y);
|
|
glVertex3f(tria->getP2().x, tria->getP2().z, tria->getP2().y);
|
|
glVertex3f(tria->getP3().x, tria->getP3().z, tria->getP3().y);
|
|
|
|
}
|
|
glEnd();
|
|
|
|
|
|
glEnable(GL_MULTISAMPLE);
|
|
//glEnable(GL_LINE_SMOOTH);
|
|
|
|
glBegin(GL_LINES);
|
|
//glColor3f(0.2,0.2,0.2);
|
|
for (const NM::NavMeshTriangle* tria : *navMesh) {
|
|
|
|
switch (tria->getType()) {
|
|
case (int) NM::NavMeshType::FLOOR_INDOOR: glColor3f(0.6, 0.6, 0.6); break;
|
|
case (int) NM::NavMeshType::FLOOR_OUTDOOR: glColor3f(0.0, 0.6, 0.0); break;
|
|
case (int) NM::NavMeshType::DOOR: glColor3f(0.5, 0.5, 0.6); break;
|
|
case (int) NM::NavMeshType::STAIR_LEVELED: glColor3f(0.4, 0.4, 0.4); break;
|
|
case (int) NM::NavMeshType::STAIR_SKEWED: glColor3f(0.4, 0.4, 0.4); break;
|
|
}
|
|
|
|
glVertex3f(tria->getP1().x, tria->getP1().z+0.01, tria->getP1().y);
|
|
glVertex3f(tria->getP2().x, tria->getP2().z+0.01, tria->getP2().y);
|
|
|
|
glVertex3f(tria->getP2().x, tria->getP2().z+0.01, tria->getP2().y);
|
|
glVertex3f(tria->getP3().x, tria->getP3().z+0.01, tria->getP3().y);
|
|
|
|
glVertex3f(tria->getP3().x, tria->getP3().z+0.01, tria->getP3().y);
|
|
glVertex3f(tria->getP1().x, tria->getP1().z+0.01, tria->getP1().y);
|
|
|
|
// glBegin(GL_LINE_LOOP);
|
|
// glVertex3f(tria->getP1().x, tria->getP1().z+0.01, tria->getP1().y);
|
|
// glVertex3f(tria->getP2().x, tria->getP2().z+0.01, tria->getP2().y);
|
|
// glVertex3f(tria->getP3().x, tria->getP3().z+0.01, tria->getP3().y);
|
|
// glEnd();
|
|
}
|
|
|
|
glEnd();
|
|
|
|
glEnable(GL_LIGHTING);
|
|
|
|
|
|
// qp.drawText(20,20, "sdfsdkflsdfjasfklsdklfsdklfsdf");
|
|
// qp.drawText(20,-20, "sdfsdkflsdfjasfklsdklfsdklfsdf");
|
|
// qp.drawText(0.5,0.5, "sdfsdkflsdfjasfklsdklfsdklfsdf");
|
|
// qp.end();
|
|
|
|
// renderText(1,1,1, "123");
|
|
|
|
const QString str1 = "Triangles: " + QString::number(navMesh->getNumTriangles());
|
|
const QString str2 = "Size: ~" + QString::number(navMesh->getNumTriangles() * sizeof(NM::NavMeshTriangle) / 1024) + " kB";
|
|
|
|
glColor3f(0.0, 0.0, 0.0);
|
|
dst->renderText(20,20, str1);
|
|
dst->renderText(20,35, str2);
|
|
|
|
// dst->renderText(0,0, "2342342342342423423423423423423432");
|
|
// dst->renderText(0.1, 0.1, 0.1, "lsdfsdfsdfsdfsdfsdfsdfol");
|
|
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
// 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);}
|
|
// }
|
|
// }
|
|
|
|
};
|
|
|
|
#endif // NAVMESHRENDERER_H
|