1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 15:02:34 +02:00

Fix AI Graph discombobulation and debug rendering

This commit is contained in:
Daniel Evans 2015-04-20 02:19:30 +01:00
parent e04f9a8d40
commit 4b2fbbd3d8
3 changed files with 46 additions and 10 deletions

View File

@ -14,8 +14,8 @@ AIGraph::~AIGraph()
void AIGraph::createPathNodes(const glm::vec3& position, const glm::quat& rotation, PathData& path) void AIGraph::createPathNodes(const glm::vec3& position, const glm::quat& rotation, PathData& path)
{ {
size_t startIndex = nodes.size(); size_t startIndex = nodes.size();
std::vector<size_t> realNodes; std::vector<AIGraphNode*> pathNodes;
realNodes.reserve(path.nodes.size()); pathNodes.reserve(path.nodes.size());
for( size_t n = 0; n < path.nodes.size(); ++n ) { for( size_t n = 0; n < path.nodes.size(); ++n ) {
auto& node = path.nodes[n]; auto& node = path.nodes[n];
@ -27,7 +27,7 @@ void AIGraph::createPathNodes(const glm::vec3& position, const glm::quat& rotati
auto& realNode = externalNodes[rn]; auto& realNode = externalNodes[rn];
auto d = glm::distance2(realNode->position, nodePosition); auto d = glm::distance2(realNode->position, nodePosition);
if( d < 1.f ) { if( d < 1.f ) {
realNodes.push_back(rn); pathNodes.push_back(realNode);
ainode = realNode; ainode = realNode;
break; break;
} }
@ -46,7 +46,7 @@ void AIGraph::createPathNodes(const glm::vec3& position, const glm::quat& rotati
ainode->external = node.type == PathNode::EXTERNAL; ainode->external = node.type == PathNode::EXTERNAL;
ainode->disabled = false; ainode->disabled = false;
realNodes.push_back(nodes.size()); pathNodes.push_back(ainode);
nodes.push_back(ainode); nodes.push_back(ainode);
if( ainode->external ) if( ainode->external )
@ -68,10 +68,12 @@ void AIGraph::createPathNodes(const glm::vec3& position, const glm::quat& rotati
} }
for(size_t pn = 0; pn < path.nodes.size(); ++pn) { for(size_t pn = 0; pn < path.nodes.size(); ++pn) {
if(path.nodes[pn].next >= 0 && (unsigned) path.nodes[pn].next < realNodes.size()) { if(path.nodes[pn].next >= 0 && (unsigned) path.nodes[pn].next < pathNodes.size()) {
auto node = nodes[realNodes[pn]]; auto node = pathNodes[pn];
node->connections.push_back(nodes[realNodes[path.nodes[pn].next]]); auto next = pathNodes[path.nodes[pn].next];
nodes[realNodes[path.nodes[pn].next]]->connections.push_back(node);
node->connections.push_back(next);
next->connections.push_back(node);
} }
} }
} }

View File

@ -26,8 +26,9 @@ DebugDraw* debug;
StdOutReciever logPrinter; StdOutReciever logPrinter;
RWGame::RWGame(const std::string& gamepath, int argc, char* argv[]) RWGame::RWGame(const std::string& gamepath, int argc, char* argv[])
: engine(nullptr), renderer(nullptr), script(nullptr), inFocus(true), showDebugStats(false), : engine(nullptr), renderer(nullptr), script(nullptr), inFocus(true),
accum(0.f), timescale(1.f) showDebugStats(false), showDebugPaths(false),
accum(0.f), timescale(1.f)
{ {
size_t w = GAME_WINDOW_WIDTH, h = GAME_WINDOW_HEIGHT; size_t w = GAME_WINDOW_WIDTH, h = GAME_WINDOW_HEIGHT;
bool fullscreen = false; bool fullscreen = false;
@ -440,6 +441,11 @@ void RWGame::render(float alpha, float time)
debug->flush(&engine->renderer); debug->flush(&engine->renderer);
#endif #endif
if( showDebugPaths )
{
renderDebugPaths(time);
}
if ( showDebugStats ) if ( showDebugStats )
{ {
renderDebugStats(time, rendertime); renderDebugStats(time, rendertime);
@ -549,6 +555,29 @@ void RWGame::renderDebugStats(float time, Renderer::ProfileInfo& worldRenderTime
}*/ }*/
} }
void RWGame::renderDebugPaths(float time)
{
btVector3 roadColour(1.f, 0.f, 0.f);
btVector3 pedColour(0.f, 0.f, 1.f);
for( AIGraphNode* n : engine->aigraph.nodes )
{
btVector3 p( n->position.x, n->position.y, n->position.z );
auto& col = n->type == AIGraphNode::Pedestrian ? pedColour : roadColour;
debug->drawLine( p - btVector3(0.f, 0.f, 1.f), p + btVector3(0.f, 0.f, 1.f), col);
debug->drawLine( p - btVector3(1.f, 0.f, 0.f), p + btVector3(1.f, 0.f, 0.f), col);
debug->drawLine( p - btVector3(0.f, 1.f, 0.f), p + btVector3(0.f, 1.f, 0.f), col);
for( AIGraphNode* c : n->connections )
{
btVector3 f( c->position.x, c->position.y, c->position.z );
debug->drawLine( p, f, col);
}
}
debug->flush(renderer);
}
void RWGame::globalKeyEvent(const sf::Event& event) void RWGame::globalKeyEvent(const sf::Event& event)
{ {
switch (event.key.code) { switch (event.key.code) {
@ -567,6 +596,9 @@ void RWGame::globalKeyEvent(const sf::Event& event)
case sf::Keyboard::F1: case sf::Keyboard::F1:
showDebugStats = ! showDebugStats; showDebugStats = ! showDebugStats;
break; break;
case sf::Keyboard::F2:
showDebugPaths = ! showDebugPaths;
break;
default: break; default: break;
} }
} }

View File

@ -23,6 +23,7 @@ class RWGame
bool inFocus; bool inFocus;
ViewCamera lastCam, nextCam; ViewCamera lastCam, nextCam;
bool showDebugStats; bool showDebugStats;
bool showDebugPaths;
int lastDraws; /// Number of draws issued for the last frame. int lastDraws; /// Number of draws issued for the last frame.
float accum; float accum;
@ -91,6 +92,7 @@ private:
void render(float alpha, float dt); void render(float alpha, float dt);
void renderDebugStats(float time, Renderer::ProfileInfo& worldRenderTime); void renderDebugStats(float time, Renderer::ProfileInfo& worldRenderTime);
void renderDebugPaths(float time);
void globalKeyEvent(const sf::Event& event); void globalKeyEvent(const sf::Event& event);
}; };