Skip to content

Instantly share code, notes, and snippets.

@vermie
Created June 21, 2010 23:43
Show Gist options
  • Save vermie/447708 to your computer and use it in GitHub Desktop.
Save vermie/447708 to your computer and use it in GitHub Desktop.
5ad40ed5dc17ab9a7797491bb4c8e7251ccde1a8
src/shared/pathfinding/Recast/Recast.cpp | 3 +-
src/shared/pathfinding/Recast/Recast.h | 29 +++++++++--
src/shared/pathfinding/Recast/RecastArea.cpp | 4 +-
.../pathfinding/Recast/RecastRasterization.cpp | 53 ++++++++++++++-----
src/shared/pathfinding/Recast/RecastRegion.cpp | 2 +-
5 files changed, 68 insertions(+), 23 deletions(-)
diff --git a/src/shared/pathfinding/Recast/Recast.cpp b/src/shared/pathfinding/Recast/Recast.cpp
index 9fed737..d492b3f 100644
--- a/src/shared/pathfinding/Recast/Recast.cpp
+++ b/src/shared/pathfinding/Recast/Recast.cpp
@@ -169,7 +169,7 @@ bool rcBuildCompactHeightfield(const int walkableHeight, const int walkableClimb
rcGetLog()->log(RC_LOG_ERROR, "rcBuildCompactHeightfield: Out of memory 'chf.areas' (%d)", spanCount);
return false;
}
- memset(chf.areas, RC_WALKABLE_AREA, sizeof(unsigned char)*spanCount);
+ memset(chf.areas, 0, sizeof(unsigned char)*spanCount);
const int MAX_HEIGHT = 0xffff;
@@ -193,6 +193,7 @@ bool rcBuildCompactHeightfield(const int walkableHeight, const int walkableClimb
const int top = s->next ? (int)s->next->smin : MAX_HEIGHT;
chf.spans[idx].y = (unsigned short)rcClamp(bot, 0, 0xffff);
chf.spans[idx].h = (unsigned char)rcClamp(top - bot, 0, 0xff);
+ chf.areas[idx] = s->area;
idx++;
c.count++;
}
diff --git a/src/shared/pathfinding/Recast/Recast.h b/src/shared/pathfinding/Recast/Recast.h
index 7c75d9f..6392b8b 100644
--- a/src/shared/pathfinding/Recast/Recast.h
+++ b/src/shared/pathfinding/Recast/Recast.h
@@ -47,6 +47,7 @@ struct rcSpan
unsigned int smin : 15; // Span min height.
unsigned int smax : 15; // Span max height.
unsigned int flags : 2; // Span flags.
+ unsigned char area; // Span area type.
rcSpan* next; // Next span in column.
};
@@ -439,16 +440,17 @@ void rcMarkWalkableTriangles(const float walkableSlopeAngle,
// flagMergeThr - (in) merge threshold.
void rcAddSpan(rcHeightfield& solid, const int x, const int y,
const unsigned short smin, const unsigned short smax,
- const unsigned short flags, const int flagMergeThr);
+ const unsigned short flags, const unsigned char area, const int flagMergeThr);
// Rasterizes a triangle into heightfield spans.
// Params:
// v0,v1,v2 - (in) the vertices of the triangle.
// flags - (in) triangle flags (uses WALKABLE)
+// areaFlag - (in) area flag to apply to spans
// solid - (in) heighfield where the triangle is rasterized
// flagMergeThr - (in) distance in voxel where walkable flag is favored over non-walkable.
void rcRasterizeTriangle(const float* v0, const float* v1, const float* v2,
- unsigned char flags, rcHeightfield& solid,
+ unsigned char flags, unsigned char areaFlags, rcHeightfield& solid,
const int flagMergeThr = 1);
// Rasterizes indexed triangle mesh into heightfield spans.
@@ -457,11 +459,12 @@ void rcRasterizeTriangle(const float* v0, const float* v1, const float* v2,
// nv - (in) vertex count
// tris - (in) array of triangle vertex indices
// flags - (in) array of triangle flags (uses WALKABLE)
+// areaFlag - (in) area flag to apply to spans
// nt - (in) triangle count
// solid - (in) heighfield where the triangles are rasterized
// flagMergeThr - (in) distance in voxel where walkable flag is favored over non-walkable.
void rcRasterizeTriangles(const float* verts, const int nv,
- const int* tris, const unsigned char* flags, const int nt,
+ const int* tris, const unsigned char* flags, const unsigned char areaFlag, const int nt,
rcHeightfield& solid, const int flagMergeThr = 1);
// Rasterizes indexed triangle mesh into heightfield spans.
@@ -470,20 +473,36 @@ void rcRasterizeTriangles(const float* verts, const int nv,
// nv - (in) vertex count
// tris - (in) array of triangle vertex indices
// flags - (in) array of triangle flags (uses WALKABLE)
+// areaFlags - (in) array of area flags to apply to spans
// nt - (in) triangle count
// solid - (in) heighfield where the triangles are rasterized
// flagMergeThr - (in) distance in voxel where walkable flag is favored over non-walkable.
void rcRasterizeTriangles(const float* verts, const int nv,
- const unsigned short* tris, const unsigned char* flags, const int nt,
+ const int* tris, const unsigned char* flags, const unsigned char* areaFlags, const int nt,
+ rcHeightfield& solid, const int flagMergeThr = 1);
+
+// Rasterizes indexed triangle mesh into heightfield spans.
+// Params:
+// verts - (in) array of vertices
+// nv - (in) vertex count
+// tris - (in) array of triangle vertex indices
+// flags - (in) array of triangle flags (uses WALKABLE)
+// areaFlags - (in) array of area flags to apply to spans
+// nt - (in) triangle count
+// solid - (in) heighfield where the triangles are rasterized
+// flagMergeThr - (in) distance in voxel where walkable flag is favored over non-walkable.
+void rcRasterizeTriangles(const float* verts, const int nv,
+ const unsigned short* tris, const unsigned char* flags, const unsigned char* areaFlags, const int nt,
rcHeightfield& solid, const int flagMergeThr = 1);
// Rasterizes the triangles into heightfield spans.
// Params:
// verts - (in) array of vertices
// flags - (in) array of triangle flags (uses WALKABLE)
+// areaFlags - (in) array of area flags to apply to spans
// nt - (in) triangle count
// solid - (in) heighfield where the triangles are rasterized
-void rcRasterizeTriangles(const float* verts, const unsigned char* flags, const int nt,
+void rcRasterizeTriangles(const float* verts, const unsigned char* flags, const unsigned char* areaFlags, const int nt,
rcHeightfield& solid, const int flagMergeThr = 1);
// Marks non-walkable low obstacles as walkable if they are closer than walkableClimb
diff --git a/src/shared/pathfinding/Recast/RecastArea.cpp b/src/shared/pathfinding/Recast/RecastArea.cpp
index 56a7bb2..a1cf336 100644
--- a/src/shared/pathfinding/Recast/RecastArea.cpp
+++ b/src/shared/pathfinding/Recast/RecastArea.cpp
@@ -60,7 +60,7 @@ bool rcErodeArea(unsigned char areaId, int radius, rcCompactHeightfield& chf)
const int ax = x + rcGetDirOffsetX(dir);
const int ay = y + rcGetDirOffsetY(dir);
const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir);
- if (chf.areas[ai] == areaId)
+ //if (chf.areas[ai] == areaId)
nc++;
}
}
@@ -192,7 +192,7 @@ bool rcErodeArea(unsigned char areaId, int radius, rcCompactHeightfield& chf)
const unsigned char thr = (unsigned char)(radius*2);
for (int i = 0; i < chf.spanCount; ++i)
- if (dist[i] < thr)
+ if (dist[i] < thr && chf.areas[i] == areaId)
chf.areas[i] = 0;
delete [] dist;
diff --git a/src/shared/pathfinding/Recast/RecastRasterization.cpp b/src/shared/pathfinding/Recast/RecastRasterization.cpp
index 05c131f..3326ac4 100644
--- a/src/shared/pathfinding/Recast/RecastRasterization.cpp
+++ b/src/shared/pathfinding/Recast/RecastRasterization.cpp
@@ -85,7 +85,7 @@ static void freeSpan(rcHeightfield& hf, rcSpan* ptr)
void rcAddSpan(rcHeightfield& hf, const int x, const int y,
const unsigned short smin, const unsigned short smax,
- const unsigned short flags, const int flagMergeThr)
+ const unsigned short flags, const unsigned char area, const int flagMergeThr)
{
int idx = x + y*hf.width;
@@ -93,6 +93,7 @@ void rcAddSpan(rcHeightfield& hf, const int x, const int y,
s->smin = smin;
s->smax = smax;
s->flags = flags;
+ s->area = area;
s->next = 0;
// Empty cell, add he first span.
@@ -185,7 +186,7 @@ static int clipPoly(const float* in, int n, float* out, float pnx, float pnz, fl
}
static void rasterizeTri(const float* v0, const float* v1, const float* v2,
- unsigned char flags, rcHeightfield& hf,
+ unsigned char flags, unsigned char areaFlag, rcHeightfield& hf,
const float* bmin, const float* bmax,
const float cs, const float ics, const float ich,
const int flagMergeThr)
@@ -263,20 +264,20 @@ static void rasterizeTri(const float* v0, const float* v1, const float* v2,
unsigned short ismin = (unsigned short)rcClamp((int)floorf(smin * ich), 0, 0x7fff);
unsigned short ismax = (unsigned short)rcClamp((int)ceilf(smax * ich), (int)ismin+1, 0x7fff);
- rcAddSpan(hf, x, y, ismin, ismax, flags, flagMergeThr);
+ rcAddSpan(hf, x, y, ismin, ismax, flags, areaFlag, flagMergeThr);
}
}
}
void rcRasterizeTriangle(const float* v0, const float* v1, const float* v2,
- unsigned char flags, rcHeightfield& solid,
+ unsigned char flags, unsigned char areaFlags, rcHeightfield& solid,
const int flagMergeThr)
{
rcTimeVal startTime = rcGetPerformanceTimer();
const float ics = 1.0f/solid.cs;
const float ich = 1.0f/solid.ch;
- rasterizeTri(v0, v1, v2, flags, solid, solid.bmin, solid.bmax, solid.cs, ics, ich, flagMergeThr);
+ rasterizeTri(v0, v1, v2, flags, areaFlags, solid, solid.bmin, solid.bmax, solid.cs, ics, ich, flagMergeThr);
rcTimeVal endTime = rcGetPerformanceTimer();
@@ -285,8 +286,8 @@ void rcRasterizeTriangle(const float* v0, const float* v1, const float* v2,
}
void rcRasterizeTriangles(const float* verts, const int /*nv*/,
- const int* tris, const unsigned char* flags, const int nt,
- rcHeightfield& solid, const int flagMergeThr)
+ const int* tris, const unsigned char* flags, const unsigned char areaFlag,
+ const int nt, rcHeightfield& solid, const int flagMergeThr)
{
rcTimeVal startTime = rcGetPerformanceTimer();
@@ -299,7 +300,7 @@ void rcRasterizeTriangles(const float* verts, const int /*nv*/,
const float* v1 = &verts[tris[i*3+1]*3];
const float* v2 = &verts[tris[i*3+2]*3];
// Rasterize.
- rasterizeTri(v0, v1, v2, flags[i], solid, solid.bmin, solid.bmax, solid.cs, ics, ich, flagMergeThr);
+ rasterizeTri(v0, v1, v2, flags[i], areaFlag, solid, solid.bmin, solid.bmax, solid.cs, ics, ich, flagMergeThr);
}
rcTimeVal endTime = rcGetPerformanceTimer();
@@ -309,8 +310,8 @@ void rcRasterizeTriangles(const float* verts, const int /*nv*/,
}
void rcRasterizeTriangles(const float* verts, const int /*nv*/,
- const unsigned short* tris, const unsigned char* flags, const int nt,
- rcHeightfield& solid, const int flagMergeThr)
+ const int* tris, const unsigned char* flags, const unsigned char* areaFlags,
+ const int nt, rcHeightfield& solid, const int flagMergeThr)
{
rcTimeVal startTime = rcGetPerformanceTimer();
@@ -323,7 +324,31 @@ void rcRasterizeTriangles(const float* verts, const int /*nv*/,
const float* v1 = &verts[tris[i*3+1]*3];
const float* v2 = &verts[tris[i*3+2]*3];
// Rasterize.
- rasterizeTri(v0, v1, v2, flags[i], solid, solid.bmin, solid.bmax, solid.cs, ics, ich, flagMergeThr);
+ rasterizeTri(v0, v1, v2, flags[i], areaFlags[i], solid, solid.bmin, solid.bmax, solid.cs, ics, ich, flagMergeThr);
+ }
+
+ rcTimeVal endTime = rcGetPerformanceTimer();
+
+ if (rcGetBuildTimes())
+ rcGetBuildTimes()->rasterizeTriangles += rcGetDeltaTimeUsec(startTime, endTime);
+}
+
+void rcRasterizeTriangles(const float* verts, const int /*nv*/,
+ const unsigned short* tris, const unsigned char* flags, const unsigned char* areaFlags,
+ const int nt, rcHeightfield& solid, const int flagMergeThr)
+{
+ rcTimeVal startTime = rcGetPerformanceTimer();
+
+ const float ics = 1.0f/solid.cs;
+ const float ich = 1.0f/solid.ch;
+ // Rasterize triangles.
+ for (int i = 0; i < nt; ++i)
+ {
+ const float* v0 = &verts[tris[i*3+0]*3];
+ const float* v1 = &verts[tris[i*3+1]*3];
+ const float* v2 = &verts[tris[i*3+2]*3];
+ // Rasterize.
+ rasterizeTri(v0, v1, v2, flags[i], areaFlags[i], solid, solid.bmin, solid.bmax, solid.cs, ics, ich, flagMergeThr);
}
rcTimeVal endTime = rcGetPerformanceTimer();
@@ -332,8 +357,8 @@ void rcRasterizeTriangles(const float* verts, const int /*nv*/,
rcGetBuildTimes()->rasterizeTriangles += rcGetDeltaTimeUsec(startTime, endTime);
}
-void rcRasterizeTriangles(const float* verts, const unsigned char* flags, const int nt,
- rcHeightfield& solid, const int flagMergeThr)
+void rcRasterizeTriangles(const float* verts, const unsigned char* flags, const unsigned char* areaFlags,
+ const int nt, rcHeightfield& solid, const int flagMergeThr)
{
rcTimeVal startTime = rcGetPerformanceTimer();
@@ -346,7 +371,7 @@ void rcRasterizeTriangles(const float* verts, const unsigned char* flags, const
const float* v1 = &verts[(i*3+1)*3];
const float* v2 = &verts[(i*3+2)*3];
// Rasterize.
- rasterizeTri(v0, v1, v2, flags[i], solid, solid.bmin, solid.bmax, solid.cs, ics, ich, flagMergeThr);
+ rasterizeTri(v0, v1, v2, flags[i], areaFlags[i], solid, solid.bmin, solid.bmax, solid.cs, ics, ich, flagMergeThr);
}
rcTimeVal endTime = rcGetPerformanceTimer();
diff --git a/src/shared/pathfinding/Recast/RecastRegion.cpp b/src/shared/pathfinding/Recast/RecastRegion.cpp
index 7e0f2c1..c844cea 100644
--- a/src/shared/pathfinding/Recast/RecastRegion.cpp
+++ b/src/shared/pathfinding/Recast/RecastRegion.cpp
@@ -55,7 +55,7 @@ static void calculateDistanceField(rcCompactHeightfield& chf, unsigned short* sr
const int ax = x + rcGetDirOffsetX(dir);
const int ay = y + rcGetDirOffsetY(dir);
const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir);
- if (area == chf.areas[ai])
+ //if (area == chf.areas[ai])
nc++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment