Skip to content

Commit

Permalink
leaf nav mesh pathfinding and octree speedup
Browse files Browse the repository at this point in the history
roughly 100x faster generation using the octree. Pathfinding needs to work to avoid sending the player into areas that are impossible to reach normally. Either that or the walkable nav mesh should be used instead, which has the opposite problem of not reaching enough areas.
  • Loading branch information
wootguy committed Jul 3, 2024
1 parent a4fd61a commit 23231e3
Show file tree
Hide file tree
Showing 17 changed files with 573 additions and 58 deletions.
13 changes: 8 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ set(SOURCE_FILES
src/util/mat4x4.h src/util/mat4x4.cpp
src/util/Polygon3D.h src/util/Polygon3D.cpp
src/util/Line2D.h src/util/Line2D.cpp
src/util/PolyOctree.h src/util/PolyOctree.cpp
src/globals.h src/globals.cpp

# Navigation meshes
src/nav/NavMesh.h src/nav/NavMesh.cpp
src/nav/NavMeshGenerator.h src/nav/NavMeshGenerator.cpp
src/nav/LeafNavMeshGenerator.h src/nav/LeafNavMeshGenerator.cpp
src/nav/LeafNavMesh.h src/nav/LeafNavMesh.cpp
src/nav/PolyOctree.h src/nav/PolyOctree.cpp
src/nav/LeafOctree.h src/nav/LeafOctree.cpp

# OpenGL rendering
src/gl/shaders.h src/gl/shaders.cpp
Expand Down Expand Up @@ -182,25 +183,27 @@ if(MSVC)
src/util/vectors.h
src/util/Polygon3D.h
src/util/Line2D.h
src/util/PolyOctree.h
src/util/mat4x4.h)

source_group("Source Files\\util" FILES src/util/util.cpp
src/util/vectors.cpp
src/util/Polygon3D.cpp
src/util/Line2D.cpp
src/util/PolyOctree.cpp
src/util/mat4x4.cpp)

source_group("Header Files\\nav" FILES src/nav/NavMesh.h
src/nav/NavMeshGenerator.h
src/nav/LeafNavMeshGenerator.h
src/nav/LeafNavMesh.h)
src/nav/LeafNavMesh.h
src/nav/PolyOctree.h
src/nav/LeafOctree.h)

source_group("Source Files\\nav" FILES src/nav/NavMesh.cpp
src/nav/NavMeshGenerator.cpp
src/nav/LeafNavMeshGenerator.cpp
src/nav/LeafNavMesh.cpp)
src/nav/LeafNavMesh.cpp
src/nav/PolyOctree.cpp
src/nav/LeafOctree.cpp)

source_group("Header Files\\util\\lib" FILES src/util/lodepng.h)

Expand Down
16 changes: 16 additions & 0 deletions src/bsp/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <set>
#include <algorithm>
#include <sstream>
#include "Bsp.h"

using namespace std;

Expand Down Expand Up @@ -149,6 +150,21 @@ vec3 Entity::getOrigin() {
return hasKey("origin") ? parseVector(keyvalues["origin"]) : vec3(0, 0, 0);
}

vec3 Entity::getHullOrigin(Bsp* map) {
vec3 ori = getOrigin();
int modelIdx = getBspModelIdx();

if (modelIdx != -1) {
BSPMODEL& model = map->models[modelIdx];

vec3 mins, maxs;
map->get_model_vertex_bounds(modelIdx, mins, maxs);
ori += (maxs + mins) * 0.5f;
}

return ori;
}

// TODO: maybe store this in a text file or something
#define TOTAL_TARGETNAME_KEYS 134
const char* potential_tergetname_keys[TOTAL_TARGETNAME_KEYS] = {
Expand Down
4 changes: 4 additions & 0 deletions src/bsp/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <map>
#include <vector>

class Bsp;

class Entity
{
public:
Expand Down Expand Up @@ -34,6 +36,8 @@ class Entity

vec3 getOrigin();

vec3 getHullOrigin(Bsp* map);

bool hasKey(const std::string& key);

vector<string> getTargets();
Expand Down
4 changes: 1 addition & 3 deletions src/editor/BspRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1940,8 +1940,7 @@ bool BspRenderer::pickModelPoly(vec3 start, vec3 dir, vec3 offset, int modelIdx,
pickInfo.faceIdx = -1;

// Nav mesh WIP code
/*
if (modelIdx == 0 && hullIdx == 3) {
if (g_app->debugNavMesh && modelIdx == 0 && hullIdx == 3) {
static int lastPick = 0;

g_app->debugPoly = debugFaces[i];
Expand All @@ -1956,7 +1955,6 @@ bool BspRenderer::pickModelPoly(vec3 start, vec3 dir, vec3 offset, int modelIdx,
lastPick = i;
logf("Picked hull %d, face %d, verts %d, area %.1f\nNav links %d\n", hullIdx, i, debugFaces[i].verts.size(), debugFaces[i].area, node.numLinks());
}
*/
}
}
}
Expand Down
92 changes: 62 additions & 30 deletions src/editor/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,45 +457,86 @@ void Renderer::renderLoop() {
if (debugLeafNavMesh) {
glLineWidth(1);

int leafIdx = mapRenderers[0]->map->get_leaf(cameraOrigin, 3);
Bsp* map = mapRenderers[0]->map;
int leafIdx = map->get_leaf(cameraOrigin, 3);
int leafNavIdx = MAX_NAV_LEAVES;

if (leafIdx >= 0 && leafIdx < MAX_MAP_CLIPNODE_LEAVES) {
leafNavIdx = debugLeafNavMesh->leafMap[leafIdx];
}

if (leafNavIdx < MAX_NAV_LEAVES) {
LeafNavNode& node = debugLeafNavMesh->nodes[leafNavIdx];
LeafMesh& leaf = debugLeafNavMesh->leaves[leafNavIdx];

drawBox(leaf.center, 2, COLOR4(0, 255, 0, 255));
if (pickInfo.valid && pickInfo.ent && pickInfo.entIdx != 0) {
glDisable(GL_DEPTH_TEST);

int endNode = debugLeafNavMesh->getNodeIdx(map, pickInfo.ent);
vector<int> route = debugLeafNavMesh->AStarRoute(map, leafNavIdx, endNode);

if (route.size()) {
LeafMesh& firstNode = debugLeafNavMesh->leaves[route[route.size() - 1]];

vec3 lastPos = firstNode.center;
drawBox(firstNode.center, 2, COLOR4(0, 255, 255, 255));

for (int i = route.size() - 2; i >= 0; i--) {
LeafNavNode& node = debugLeafNavMesh->nodes[route[i]];
LeafMesh& mesh = debugLeafNavMesh->leaves[route[i]];

vec3 nodeCenter = mesh.center;

for (int k = 0; k < MAX_NAV_LEAF_LINKS; k++) {
LeafNavLink& link = node.links[k];

std::string linkStr;
if (link.node == route[i + 1]) {
vec3 linkPoint = link.linkArea.center;

for (int i = 0; i < MAX_NAV_LEAF_LINKS; i++) {
LeafNavLink& link = node.links[i];
if (link.node == -1) {
break;
drawLine(lastPos, linkPoint, COLOR4(0, 255, 255, 255));
drawLine(linkPoint, mesh.center, COLOR4(0, 255, 255, 255));
drawBox(nodeCenter, 2, COLOR4(0, 255, 255, 255));
lastPos = nodeCenter;
break;
}
}
}

drawLine(lastPos, pickInfo.ent->getHullOrigin(map), COLOR4(0, 255, 255, 255));
}
LeafMesh& linkLeaf = debugLeafNavMesh->leaves[link.node];
Polygon3D& linkArea = link.linkArea;
}
else {
LeafNavNode& node = debugLeafNavMesh->nodes[leafNavIdx];
LeafMesh& leaf = debugLeafNavMesh->leaves[leafNavIdx];

drawLine(leaf.center, linkArea.center, COLOR4(0, 255, 255, 255));
drawLine(linkArea.center, linkLeaf.center, COLOR4(0, 255, 255, 255));
drawBox(leaf.center, 2, COLOR4(0, 255, 0, 255));

for (int k = 0; k < linkArea.verts.size(); k++) {
drawBox(linkArea.verts[k], 1, COLOR4(255, 255, 0, 255));
std::string linkStr;

for (int i = 0; i < MAX_NAV_LEAF_LINKS; i++) {
LeafNavLink& link = node.links[i];
if (link.node == -1) {
break;
}
LeafMesh& linkLeaf = debugLeafNavMesh->leaves[link.node];
Polygon3D& linkArea = link.linkArea;

drawLine(leaf.center, linkArea.center, COLOR4(0, 255, 255, 255));
drawLine(linkArea.center, linkLeaf.center, COLOR4(0, 255, 255, 255));

for (int k = 0; k < linkArea.verts.size(); k++) {
drawBox(linkArea.verts[k], 1, COLOR4(255, 255, 0, 255));
}
drawBox(linkArea.center, 1, COLOR4(0, 255, 0, 255));
drawBox(linkLeaf.center, 2, COLOR4(0, 255, 255, 255));
linkStr += to_string(link.node) + " (" + to_string(linkArea.verts.size()) + "v), ";
}
drawBox(linkArea.center, 1, COLOR4(0, 255, 0, 255));
drawBox(linkLeaf.center, 2, COLOR4(0, 255, 255, 255));
linkStr += to_string(link.node) + " (" + to_string(linkArea.verts.size()) + "v), ";

//logf("Leaf node idx: %d, links: %s\n", leafNavIdx, linkStr.c_str());
}

//logf("Leaf node idx: %d, links: %s\n", leafNavIdx, linkStr.c_str());
}

glDisable(GL_DEPTH_TEST);

/*
colorShader->pushMatrix(MAT_PROJECTION);
colorShader->pushMatrix(MAT_VIEW);
projection.ortho(0, windowWidth, windowHeight, 0, -1.0f, 1.0f);
Expand All @@ -505,18 +546,9 @@ void Renderer::renderLoop() {
Line2D edge(vec2(1000, 400), vec2(1400, 630));
drawLine2D(edge.start, edge.end, COLOR4(255, 0, 0, 255));
/*
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
vec2 mousepos = vec2(xpos, ypos);
drawBox2D(mousepos, 8, COLOR4(255, 0, 0, 255));
drawBox2D(edge.project(mousepos), 8, COLOR4(255, 0, 0, 255));
float dist = edge.distance(mousepos);
logf("dist: %f\n", edge.distance(mousepos));
*/

colorShader->popMatrix(MAT_PROJECTION);
colorShader->popMatrix(MAT_VIEW);
*/
}

if (debugPoly.isValid) {
Expand Down
1 change: 1 addition & 0 deletions src/editor/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Renderer {
friend class FixSurfaceExtentsCommand;
friend class DeduplicateModelsCommand;
friend class MoveMapCommand;
friend class LeafNavMesh;

public:
vector<BspRenderer*> mapRenderers;
Expand Down
1 change: 1 addition & 0 deletions src/gl/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct cVert
cVert() {}
cVert(float x, float y, float z, COLOR4 c) : c(c), x(x), y(y), z(z) {}
cVert(vec3 p, COLOR4 c) : c(c), x(p.x), y(p.y), z(p.z) {}
vec3 pos() { return vec3(x, y, z); }
};

struct tTri
Expand Down
Loading

0 comments on commit 23231e3

Please sign in to comment.