diff --git a/DebugUtils/Include/RecastDebugDraw.h b/DebugUtils/Include/RecastDebugDraw.h index 0a9e7e6..21ce6d0 100644 --- a/DebugUtils/Include/RecastDebugDraw.h +++ b/DebugUtils/Include/RecastDebugDraw.h @@ -31,6 +31,8 @@ void duDebugDrawCompactHeightfieldDistance(struct duDebugDraw* dd, const struct void duDebugDrawLeanHeightfieldSolid(duDebugDraw* dd, const struct rcLeanHeightfield& lhf); +void duDebugDrawHeightfieldLayers(duDebugDraw* dd, const struct rcHeightfieldLayerSet& lset); + void duDebugDrawRegionConnections(struct duDebugDraw* dd, const struct rcContourSet& cset, const float alpha = 1.0f); void duDebugDrawRawContours(struct duDebugDraw* dd, const struct rcContourSet& cset, const float alpha = 1.0f); void duDebugDrawContours(struct duDebugDraw* dd, const struct rcContourSet& cset, const float alpha = 1.0f); diff --git a/DebugUtils/Source/RecastDebugDraw.cpp b/DebugUtils/Source/RecastDebugDraw.cpp index 77fc9d6..af1be3b 100644 --- a/DebugUtils/Source/RecastDebugDraw.cpp +++ b/DebugUtils/Source/RecastDebugDraw.cpp @@ -300,7 +300,7 @@ void duDebugDrawLeanHeightfieldSolid(duDebugDraw* dd, const rcLeanHeightfield& l for (int i = idx, ni = idx+count; i < ni; ++i) { - const int y = floors[i] & mask; + const int h = floors[i] & mask; unsigned char area = areas[i]; unsigned int color; @@ -311,7 +311,7 @@ void duDebugDrawLeanHeightfieldSolid(duDebugDraw* dd, const rcLeanHeightfield& l else color = duIntToCol(area, 255); - const float fy = lhf.bmin[1] + (y+1)*ch; + const float fy = lhf.bmin[1] + (h+1)*ch; dd->vertex(fx, fy, fz, color); dd->vertex(fx, fy, fz+cs, color); dd->vertex(fx+cs, fy, fz+cs, color); @@ -323,6 +323,98 @@ void duDebugDrawLeanHeightfieldSolid(duDebugDraw* dd, const rcLeanHeightfield& l dd->end(); } +void duDebugDrawHeightfieldLayers(duDebugDraw* dd, const struct rcHeightfieldLayerSet& lset) +{ + if (!dd) return; + + const float cs = lset.cs; + const float ch = lset.ch; + const int w = lset.width; + const int h = lset.height; + + for (int i = 0; i < lset.nlayers; ++i) + { + const rcHeightfieldLayer* layer = &lset.layers[i]; + + unsigned int color = duIntToCol(i+1, 255); + + // Layer bounds + float bmin[3], bmax[3]; + rcVcopy(bmin, lset.bmin); + rcVcopy(bmax, lset.bmax); + bmin[1] = lset.bmin[1] + (layer->ymin-1)*ch; + bmax[1] = lset.bmin[1] + (layer->ymax+1)*ch; + duDebugDrawBoxWire(dd, bmin[0],bmin[1],bmin[2], bmax[0],bmax[1],bmax[2], duTransCol(color,128), 2.0f); + + // Layer height + dd->begin(DU_DRAW_QUADS); + for (int y = 0; y < h; ++y) + { + for (int x = 0; x < w; ++x) + { + const int idx = x+y*w; + const int h = (int)layer->heights[idx]; + if (h == 0xffff) continue; + const unsigned char area = layer->areas[idx]; + + unsigned int col; + if (area == RC_WALKABLE_AREA) + col = duLerpCol(color, duRGBA(0,192,255,64), 32); + else if (area == RC_NULL_AREA) + col = duLerpCol(color, duRGBA(0,0,0,64), 32); + else + col = duLerpCol(color, duIntToCol(area, 255), 32); + + const float fx = lset.bmin[0] + x*cs; + const float fy = lset.bmin[1] + (h+1)*ch; + const float fz = lset.bmin[2] + y*cs; + + dd->vertex(fx, fy, fz, col); + dd->vertex(fx, fy, fz+cs, col); + dd->vertex(fx+cs, fy, fz+cs, col); + dd->vertex(fx+cs, fy, fz, col); + } + } + dd->end(); + + // Portals + unsigned int pcol = duLerpCol(color,duRGBA(255,255,255,255),128); + dd->begin(DU_DRAW_LINES, 2.0f); + for (int j = 0; j < layer->nportals; ++j) + { + const rcHeightfieldLayerPortal* portal = &layer->portals[j]; + if (portal->dir == 0 || portal->dir == 2) + { + const int ha = (int)layer->heights[portal->pos + portal->smin*w]; + const int hb = (int)layer->heights[portal->pos + (portal->smax-1)*w]; + const int xx = (portal->dir == 0) ? portal->pos : portal->pos+1; + const float fx = lset.bmin[0] + xx*cs; + const float fya = lset.bmin[1] + (ha+4)*ch; + const float fyb = lset.bmin[1] + (hb+4)*ch; + const float fza = lset.bmin[2] + portal->smin*cs; + const float fzb = lset.bmin[2] + portal->smax*cs; + dd->vertex(fx, fya, fza, pcol); + dd->vertex(fx, fyb, fzb, pcol); + } + else if (portal->dir == 3 || portal->dir == 1) + { + const int ha = (int)layer->heights[portal->smin + portal->pos*w]; + const int hb = (int)layer->heights[(portal->smax-1) + portal->pos*w]; + const int yy = (portal->dir == 3) ? portal->pos : portal->pos+1; + const float fxa = lset.bmin[0] + portal->smin*cs; + const float fxb = lset.bmin[0] + portal->smax*cs; + const float fya = lset.bmin[1] + (ha+3)*ch; + const float fyb = lset.bmin[1] + (hb+3)*ch; + const float fz = lset.bmin[2] + yy*cs; + dd->vertex(fxa, fya, fz, pcol); + dd->vertex(fxb, fyb, fz, pcol); + } + } + dd->end(); + } + +} + static void getContourCenter(const rcContour* cont, const float* orig, float cs, float ch, float* center) { center[0] = 0; diff --git a/DebugUtils/Source/RecastDump.cpp b/DebugUtils/Source/RecastDump.cpp index 4660d03..4ca3056 100644 --- a/DebugUtils/Source/RecastDump.cpp +++ b/DebugUtils/Source/RecastDump.cpp @@ -421,7 +421,7 @@ void duLogBuildTimes(rcContext& ctx, const int totalTimeUsec) logLine(ctx, RC_TIMER_MARK_BOX_AREA, "- Mark Box Area", pc); logLine(ctx, RC_TIMER_MARK_CONVEXPOLY_AREA, "- Mark Convex Area", pc); logLine(ctx, RC_TIMER_MARK_CYLINDER_AREA, "- Mark Cylinder Area", pc); - logLine(ctx, RC_TIMER_BUILD_DISTANCEFIELD, "- Build Disntace Field", pc); + logLine(ctx, RC_TIMER_BUILD_DISTANCEFIELD, "- Build Distance Field", pc); logLine(ctx, RC_TIMER_BUILD_DISTANCEFIELD_DIST, " - Distance", pc); logLine(ctx, RC_TIMER_BUILD_DISTANCEFIELD_BLUR, " - Blur", pc); logLine(ctx, RC_TIMER_BUILD_REGIONS, "- Build Regions", pc); @@ -429,6 +429,7 @@ void duLogBuildTimes(rcContext& ctx, const int totalTimeUsec) logLine(ctx, RC_TIMER_BUILD_REGIONS_EXPAND, " - Expand", pc); logLine(ctx, RC_TIMER_BUILD_REGIONS_FLOOD, " - Find Basins", pc); logLine(ctx, RC_TIMER_BUILD_REGIONS_FILTER, " - Filter", pc); + logLine(ctx, RC_TIMER_BUILD_LAYERS, "- Build Layers", pc); logLine(ctx, RC_TIMER_BUILD_CONTOURS, "- Build Contours", pc); logLine(ctx, RC_TIMER_BUILD_CONTOURS_TRACE, " - Trace", pc); logLine(ctx, RC_TIMER_BUILD_CONTOURS_SIMPLIFY, " - Simplify", pc); diff --git a/Recast/Include/Recast.h b/Recast/Include/Recast.h index dadfd67..6b41cb2 100644 --- a/Recast/Include/Recast.h +++ b/Recast/Include/Recast.h @@ -57,6 +57,7 @@ enum rcTimerLabel RC_TIMER_BUILD_REGIONS_EXPAND, RC_TIMER_BUILD_REGIONS_FLOOD, RC_TIMER_BUILD_REGIONS_FILTER, + RC_TIMER_BUILD_LAYERS, RC_TIMER_BUILD_POLYMESHDETAIL, RC_TIMER_MERGE_POLYMESHDETAIL, RC_MAX_TIMERS @@ -738,4 +739,41 @@ bool rcBuildPolyMeshDetail(rcContext* ctx, const rcPolyMesh& mesh, const rcCompa bool rcMergePolyMeshDetails(rcContext* ctx, rcPolyMeshDetail** meshes, const int nmeshes, rcPolyMeshDetail& mesh); + +// TODO: Put in right place! +struct rcHeightfieldLayerPortal +{ + unsigned short pos, smin, smax; + unsigned char dir; +}; + +struct rcHeightfieldLayer +{ + unsigned short ymin, ymax; + unsigned short* heights; + unsigned char* areas; + rcHeightfieldLayerPortal* portals; + int nportals; +}; + +struct rcHeightfieldLayerSet +{ + rcHeightfieldLayer* layers; + int nlayers; + int width, height; + int borderSize; + float bmin[3], bmax[3]; // Bounding box of the heightfield. + float cs, ch; // Cell size and height. +}; + +rcHeightfieldLayerSet* rcAllocHeightfieldLayerSet(); +void rcFreeHeightfieldLayerSet(rcHeightfieldLayerSet* lset); + + +bool rcBuildHeightfieldLayers(rcContext* ctx, rcCompactHeightfield& chf, + const int borderSize, const int walkableHeight, + rcHeightfieldLayerSet& lset); + + + #endif // RECAST_H diff --git a/Recast/Source/Recast.cpp b/Recast/Source/Recast.cpp index 05e1b5b..bcb8ce4 100644 --- a/Recast/Source/Recast.cpp +++ b/Recast/Source/Recast.cpp @@ -90,6 +90,26 @@ void rcFreeCompactHeightfield(rcCompactHeightfield* chf) rcFree(chf); } + +rcHeightfieldLayerSet* rcAllocHeightfieldLayerSet() +{ + rcHeightfieldLayerSet* lset = (rcHeightfieldLayerSet*)rcAlloc(sizeof(rcHeightfieldLayerSet), RC_ALLOC_PERM); + memset(lset, 0, sizeof(rcHeightfieldLayerSet)); + return lset; +} + +void rcFreeHeightfieldLayerSet(rcHeightfieldLayerSet* lset) +{ + if (!lset) return; + for (int i = 0; i < lset->nlayers; ++i) + { + rcFree(lset->layers[i].heights); + rcFree(lset->layers[i].areas); + } + rcFree(lset->layers); + rcFree(lset); +} + rcContourSet* rcAllocContourSet() { rcContourSet* cset = (rcContourSet*)rcAlloc(sizeof(rcContourSet), RC_ALLOC_PERM); diff --git a/Recast/Source/RecastLayers.cpp b/Recast/Source/RecastLayers.cpp new file mode 100644 index 0000000..7ed815f --- /dev/null +++ b/Recast/Source/RecastLayers.cpp @@ -0,0 +1,675 @@ +// +// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would be +// appreciated but is not required. +// 2. Altered source versions must be plainly marked as such, and must not be +// misrepresented as being the original software. +// 3. This notice may not be removed or altered from any source distribution. +// + +#include +#define _USE_MATH_DEFINES +#include +#include +#include +#include +#include "Recast.h" +#include "RecastAlloc.h" +#include "RecastAssert.h" + + +static const int RC_MAX_LAYERS = RC_NOT_CONNECTED; +static const int RC_MAX_NEIS = 16; + +struct rcLayerRegion +{ + unsigned char layers[RC_MAX_LAYERS]; + unsigned char neis[RC_MAX_NEIS]; + + unsigned short ymin, ymax; + + unsigned short count; + + unsigned char layerId; + unsigned char nlayers; + unsigned char nneis; + + unsigned char start; +}; + + +static void addUnique(unsigned char* a, unsigned char& an, unsigned char v) +{ + const int n = (int)an; + for (int i = 0; i < n; ++i) + if (a[i] == v) + return; + a[an] = v; + an++; +} + +static bool contains(const unsigned char* a, const unsigned char an, const unsigned char v) +{ + const int n = (int)an; + for (int i = 0; i < n; ++i) + if (a[i] == v) + return true; + return false; +} + +inline bool overlapRange(const unsigned short amin, const unsigned short amax, + const unsigned short bmin, const unsigned short bmax) +{ + return (amin > bmax || amax < bmin) ? false : true; +} + + +struct rcLayerSweepSpan +{ + unsigned short ns; // number samples + unsigned char id; // region id + unsigned char nei; // neighbour id +}; + + +rcHeightfieldLayerPortal* allocPortal(rcHeightfieldLayerPortal** portals, int& nportals, int& cportals) +{ + if (nportals+1 >= cportals) + { + cportals *= 2; + rcHeightfieldLayerPortal* np = (rcHeightfieldLayerPortal*)rcAlloc(sizeof(rcHeightfieldLayerPortal)*cportals,RC_ALLOC_PERM); + if (!np) + return 0; + if (nportals > 0) + memcpy(np,*portals,sizeof(rcHeightfieldLayerPortal)*nportals); + rcFree(*portals); + *portals = np; + } + nportals++; + return &(*portals)[nportals-1]; +} + + +bool rcBuildHeightfieldLayers(rcContext* ctx, rcCompactHeightfield& chf, + const int borderSize, const int walkableHeight, + rcHeightfieldLayerSet& lset) +{ + rcAssert(ctx); + + ctx->startTimer(RC_TIMER_BUILD_LAYERS); + + const int w = chf.width; + const int h = chf.height; + + rcScopedDelete srcReg = (unsigned char*)rcAlloc(sizeof(unsigned char)*chf.spanCount, RC_ALLOC_TEMP); + if (!srcReg) + { + ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'srcReg' (%d).", chf.spanCount); + return false; + } + memset(srcReg,0xff,sizeof(unsigned char)*chf.spanCount); + + const int nsweeps = chf.width; + rcScopedDelete sweeps = (rcLayerSweepSpan*)rcAlloc(sizeof(rcLayerSweepSpan)*nsweeps, RC_ALLOC_TEMP); + if (!sweeps) + { + ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'sweeps' (%d).", nsweeps); + return false; + } + + + // Partition walkable area into monotone regions. + int prevCount[256]; + unsigned char regId = 0; + +// for (int y = 0; y < h; ++y) + for (int y = borderSize; y < h-borderSize; ++y) + { + memset(prevCount,0,sizeof(int)*regId); + unsigned char sweepId = 0; + +// for (int x = 0; x < w; ++x) + for (int x = borderSize; x < w-borderSize; ++x) + { + const rcCompactCell& c = chf.cells[x+y*w]; + + for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) + { + const rcCompactSpan& s = chf.spans[i]; + if (chf.areas[i] == RC_NULL_AREA) continue; + + unsigned char sid = 0xff; + + // -x + if (rcGetCon(s, 0) != RC_NOT_CONNECTED) + { + const int ax = x + rcGetDirOffsetX(0); + const int ay = y + rcGetDirOffsetY(0); + const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 0); + if (chf.areas[ai] != RC_NULL_AREA && srcReg[ai] != 0xff) + sid = srcReg[ai]; + } + + if (sid == 0xff) + { + sid = sweepId++; + sweeps[sid].nei = 0xff; + sweeps[sid].ns = 0; + } + + // -y + if (rcGetCon(s,3) != RC_NOT_CONNECTED) + { + const int ax = x + rcGetDirOffsetX(3); + const int ay = y + rcGetDirOffsetY(3); + const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 3); + const unsigned char nr = srcReg[ai]; + if (nr != 0xff) + { + // Set neighbour when first valid neighbour is encoutered. + if (sweeps[sid].ns == 0) + sweeps[sid].nei = nr; + + if (sweeps[sid].nei == nr) + { + // Update existing neighbour + sweeps[sid].ns++; + prevCount[nr]++; + } + else + { + // This is hit if there is nore than one neighbour. + // Invalidate the neighbour. + sweeps[sid].nei = 0xff; + } + } + } + + srcReg[i] = sid; + } + } + + // Create unique ID. + for (int i = 0; i < sweepId; ++i) + { + // If the neighbour is set and there is only one continuous connection to it, + // the sweep will be merged with the previous one, else new region is created. + if (sweeps[i].nei != 0xff && prevCount[sweeps[i].nei] == (int)sweeps[i].ns) + { + sweeps[i].id = sweeps[i].nei; + } + else + { + if (regId == 255) + { + ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Region ID overflow."); + return false; + } + sweeps[i].id = regId++; + } + } + + // Remap local sweep ids to region ids. +// for (int x = 0; x < w; ++x) + for (int x = borderSize; x < w-borderSize; ++x) + { + const rcCompactCell& c = chf.cells[x+y*w]; + for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) + { + if (srcReg[i] != 0xff) + srcReg[i] = sweeps[srcReg[i]].id; + } + } + } + + // Allocate and init layer regions. + const int nregs = (int)regId; + rcScopedDelete regs = (rcLayerRegion*)rcAlloc(sizeof(rcLayerRegion)*nregs, RC_ALLOC_TEMP); + if (!regs) + { + ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'regs' (%d).", nregs); + return false; + } + memset(regs, 0, sizeof(rcLayerRegion)*nregs); + for (int i = 0; i < nregs; ++i) + { + regs[i].layerId = 0xff; + regs[i].ymin = 0xffff; + regs[i].ymax = 0; + } + + // Find region neighbours and overlapping regions. + for (int y = 0; y < h; ++y) + { + for (int x = 0; x < w; ++x) + { + const rcCompactCell& c = chf.cells[x+y*w]; + + unsigned char lregs[RC_MAX_LAYERS]; + int nlregs = 0; + + for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) + { + const rcCompactSpan& s = chf.spans[i]; + const unsigned char ri = srcReg[i]; + if (ri == 0xff) continue; + + regs[ri].ymin = rcMin(regs[ri].ymin, s.y); + regs[ri].ymax = rcMax(regs[ri].ymax, s.y); + + // Collect all region layers. + if (nlregs < RC_MAX_LAYERS) + lregs[nlregs++] = ri; + + // Update neighbours + for (int dir = 0; dir < 4; ++dir) + { + if (rcGetCon(s, dir) != RC_NOT_CONNECTED) + { + 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); + const unsigned char rai = srcReg[ai]; + if (rai != 0xff && rai != ri) + addUnique(regs[ri].neis, regs[ri].nneis, rai); + } + } + + } + + // Update overlapping regions. + for (int i = 0; i < nlregs-1; ++i) + { + for (int j = i+1; j < nlregs; ++j) + { + if (lregs[i] != lregs[j]) + { + rcLayerRegion& ri = regs[lregs[i]]; + rcLayerRegion& rj = regs[lregs[j]]; + addUnique(ri.layers, ri.nlayers, lregs[j]); + addUnique(rj.layers, rj.nlayers, lregs[i]); + } + } + } + + } + } + + // Create 2D layers from regions. + unsigned char layerId = 0; + + static const int MAX_STACK = 64; + unsigned char stack[MAX_STACK]; + int nstack = 0; + + for (int i = 0; i < nregs; ++i) + { + rcLayerRegion& root = regs[i]; + // Skip alreadu visited. + if (root.layerId != 0xff) + continue; + + // Start search. + root.layerId = layerId; + root.start = 1; + + nstack = 0; + stack[nstack++] = (unsigned char)i; + + while (nstack) + { + // Pop front + rcLayerRegion& reg = regs[stack[0]]; + nstack--; + for (int j = 0; j < nstack; ++j) + stack[j] = stack[j+1]; + + const int nneis = (int)reg.nneis; + for (int j = 0; j < nneis; ++j) + { + const unsigned char nei = reg.neis[j]; + // Skip already visited. + if (regs[nei].layerId != 0xff) + continue; + // Skip if the neighbour is overlapping root region. + if (contains(root.layers, root.nlayers, nei)) + continue; + + if (nstack < MAX_STACK) + { + // Deepen + stack[nstack++] = (unsigned char)nei; + + rcLayerRegion& regn = regs[nei]; + // Mark layer id + regn.layerId = layerId; + // Merge current layers to root. + for (int k = 0; k < regn.nlayers; ++k) + addUnique(root.layers, root.nlayers, regn.layers[k]); + root.ymin = rcMin(root.ymin, regn.ymin); + root.ymax = rcMax(root.ymax, regn.ymax); + } + } + } + + layerId++; + } + + // Merge non-overlapping regions that are close in height. + const int mergeHeight = walkableHeight * 4; + + for (int i = 0; i < nregs; ++i) + { + rcLayerRegion& ri = regs[i]; + if (!ri.start) continue; + + unsigned char newId = ri.layerId; + + for (;;) + { + unsigned char oldId = 0xff; + + for (int j = 0; j < nregs; ++j) + { + if (i == j) continue; + rcLayerRegion& rj = regs[j]; + if (!rj.start) continue; + + // Skip if teh regions are not close to each other. + if (!overlapRange(ri.ymin,ri.ymax+mergeHeight, rj.ymin,rj.ymax+mergeHeight)) + continue; + + // Make sure that there is no overlap when mergin 'ri' and 'rj'. + bool overlap = false; + // Iterate over all regions which have the same layerId as 'rj' + for (int k = 0; k < nregs; ++k) + { + if (regs[k].layerId != rj.layerId) + continue; + // Check if region 'k' is overlapping region 'ri' + // Index to 'regs' is the same as region id. + if (contains(ri.layers,ri.nlayers, (unsigned char)k)) + { + overlap = true; + break; + } + } + // Cannot merge of regions overlap. + if (overlap) + continue; + + // Can merge i and j. + oldId = rj.layerId; + break; + } + + // Could not find anything to merge with, stop. + if (oldId == 0xff) + break; + + // Merge + for (int j = 0; j < nregs; ++j) + { + rcLayerRegion& rj = regs[j]; + if (rj.layerId == oldId) + { + rj.start = 0; + // Remap layerIds. + rj.layerId = newId; + // Add overlaid layers from 'rj' to 'ri'. + for (int k = 0; k < rj.nlayers; ++k) + addUnique(ri.layers, ri.nlayers, rj.layers[k]); + // Update heigh bounds. + ri.ymin = rcMin(ri.ymin, rj.ymin); + ri.ymax = rcMax(ri.ymax, rj.ymax); + } + } + } + } + + // Compact layerIds + unsigned char remap[256]; + memset(remap, 0, 256); + + // Find number of unique layers. + layerId = 0; + for (int i = 0; i < nregs; ++i) + remap[regs[i].layerId] = 1; + for (int i = 0; i < 256; ++i) + { + if (remap[i]) + remap[i] = layerId++; + else + remap[i] = 0xff; + } + // Remap ids. + for (int i = 0; i < nregs; ++i) + regs[i].layerId = remap[regs[i].layerId]; + + // No layers, return empty. + if (layerId == 0) + { + ctx->stopTimer(RC_TIMER_BUILD_REGIONS); + return true; + } + + // Create layers. + rcAssert(lset.layers == 0); + + const int lw = w - borderSize*2; + const int lh = h - borderSize*2; + + lset.nlayers = (int)layerId; + lset.width = lw; + lset.height = lh; + lset.borderSize = borderSize; + rcVcopy(lset.bmin, chf.bmin); + rcVcopy(lset.bmax, chf.bmax); + lset.bmin[0] += borderSize*chf.cs; + lset.bmin[2] += borderSize*chf.cs; + lset.bmax[0] -= borderSize*chf.cs; + lset.bmax[2] -= borderSize*chf.cs; + lset.cs = chf.cs; + lset.ch = chf.ch; + + lset.layers = (rcHeightfieldLayer*)rcAlloc(sizeof(rcHeightfieldLayer)*lset.nlayers, RC_ALLOC_PERM); + if (!lset.layers) + { + ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'layers' (%d).", lset.nlayers); + return false; + } + memset(lset.layers, 0, sizeof(rcHeightfieldLayer)*lset.nlayers); + + rcScopedDelete cons = (unsigned char*)rcAlloc(sizeof(unsigned char)*lw*lh, RC_ALLOC_TEMP); + if (!cons) + { + ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'con' (%d).", lw*lh); + return false; + } + + + // Store layers. + for (int i = 0; i < lset.nlayers; ++i) + { + unsigned char curId = (unsigned char)i; + + // Allocate memory for the current layer. + rcHeightfieldLayer* layer = &lset.layers[i]; + layer->heights = (unsigned short*)rcAlloc(sizeof(unsigned short)*lw*lh, RC_ALLOC_PERM); + if (!layer->heights) + { + ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'heights' (%d).", w*h); + return false; + } + memset(layer->heights, 0xff, sizeof(unsigned short)*lw*lh); + + layer->areas = (unsigned char*)rcAlloc(sizeof(unsigned char)*lw*lh, RC_ALLOC_PERM); + if (!layer->areas) + { + ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'areas' (%d).", w*h); + return false; + } + memset(layer->areas, RC_NULL_AREA, sizeof(unsigned char)*lw*lh); + + memset(cons, 0, sizeof(unsigned char)*lw*lh); + + // Find layer height bounds. + for (int j = 0; j < nregs; ++j) + { + if (regs[j].start && regs[j].layerId == curId) + { + layer->ymin = regs[j].ymin; + layer->ymax = regs[j].ymax; + } + } + + // Copy height and area from compact heighfield. + for (int y = 0; y < lh; ++y) + { + for (int x = 0; x < lw; ++x) + { + const int cx = borderSize+x; + const int cy = borderSize+y; + const rcCompactCell& c = chf.cells[cx+cy*w]; + for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i) + { + const rcCompactSpan& s = chf.spans[i]; + if (srcReg[i] == 0xff) continue; + unsigned char lid = regs[srcReg[i]].layerId; + if (lid != curId) + continue; + const int idx = x+y*lw; + layer->heights[idx] = s.y; + layer->areas[idx] = chf.areas[i]; + // Check connection. + unsigned char con = 0; + for (int dir = 0; dir < 4; ++dir) + { + if (rcGetCon(s, dir) != RC_NOT_CONNECTED) + { + const int ax = cx + rcGetDirOffsetX(dir); + const int ay = cy + rcGetDirOffsetY(dir); + const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir); + unsigned char alid = srcReg[ai] != 0xff ? regs[srcReg[ai]].layerId : 0xff; + if (chf.areas[ai] != RC_NULL_AREA && lid != alid) + con |= (unsigned char)(1<portals = (rcHeightfieldLayerPortal*)rcAlloc(sizeof(rcHeightfieldLayerPortal)*cportals,RC_ALLOC_PERM); + if (!layer->portals) + { + ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'portals' (%d).", cportals); + return false; + } + layer->nportals = 0; + + // Directions same as rcGetCon() + const unsigned char XM = 1<<0; // x- + const unsigned char YP = 1<<1; // y+ + const unsigned char XP = 1<<2; // x+ + const unsigned char YM = 1<<3; // y- + + // Portals along x-axis + for (int y = 0; y < lh; ++y) + { + const unsigned char dir[2] = {3,1}; + const unsigned char mask[2] = {YM,YP}; + int start[2] = { -1, -1}; + + for (int x = 0; x < lw+1; ++x) + { + const int idx = x+y*lw; + for (int j = 0; j < 2; ++j) + { + unsigned char set = xportals,layer->nportals,cportals); + if (!portal) + { + ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'portals' (%d).", cportals); + return false; + } + portal->pos = (unsigned short)y; + portal->smin = (unsigned short)start[j]; + portal->smax = (unsigned short)x; + portal->dir = dir[j]; + + start[j] = -1; + } + } + } + } + } + + // Portals along y-axis + for (int x = 0; x < lw; ++x) + { + const unsigned char dir[2] = {0,2}; + const unsigned char mask[2] = {XM,XP}; + int start[2] = { -1, -1}; + + for (int y = 0; y < lh+1; ++y) + { + const int idx = x+y*lw; + for (int j = 0; j < 2; ++j) + { + unsigned char set = yportals,layer->nportals,cportals); + if (!portal) + { + ctx->log(RC_LOG_ERROR, "rcBuildHeightfieldLayers: Out of memory 'portals' (%d).", cportals); + return false; + } + portal->pos = (unsigned short)x; + portal->smin = (unsigned short)start[j]; + portal->smax = (unsigned short)y; + portal->dir = dir[j]; + + start[j] = -1; + } + } + } + } + } + + } + + ctx->stopTimer(RC_TIMER_BUILD_LAYERS); + + return true; +} diff --git a/RecastDemo/Bin/Recast.app/Contents/MacOS/Recast b/RecastDemo/Bin/Recast.app/Contents/MacOS/Recast index 5679463..6b61005 100755 Binary files a/RecastDemo/Bin/Recast.app/Contents/MacOS/Recast and b/RecastDemo/Bin/Recast.app/Contents/MacOS/Recast differ diff --git a/RecastDemo/Build/Xcode/Recast.xcodeproj/project.pbxproj b/RecastDemo/Build/Xcode/Recast.xcodeproj/project.pbxproj index a813ef7..df6d092 100644 --- a/RecastDemo/Build/Xcode/Recast.xcodeproj/project.pbxproj +++ b/RecastDemo/Build/Xcode/Recast.xcodeproj/project.pbxproj @@ -24,6 +24,7 @@ 6B25B61D0FFA62BE004F1BC4 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B25B6180FFA62BE004F1BC4 /* main.cpp */; }; 6B2AEC530FFB8958005BE9CC /* Sample_TileMesh.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B2AEC520FFB8958005BE9CC /* Sample_TileMesh.cpp */; }; 6B324C66111C5D9A00EBD2FD /* ConvexVolumeTool.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B324C65111C5D9A00EBD2FD /* ConvexVolumeTool.cpp */; }; + 6B3F9D6D13179EFC000B33D9 /* RecastLayers.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B3F9D6C13179EFC000B33D9 /* RecastLayers.cpp */; }; 6B555DB1100B212E00247EA3 /* imguiRenderGL.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B555DB0100B212E00247EA3 /* imguiRenderGL.cpp */; }; 6B5683B812D9E7D3000B9960 /* Sample_TempObstacles.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B5683B712D9E7D3000B9960 /* Sample_TempObstacles.cpp */; }; 6B62416A103434880002E346 /* RecastMeshDetail.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6B624169103434880002E346 /* RecastMeshDetail.cpp */; }; @@ -97,6 +98,7 @@ 6B2AEC520FFB8958005BE9CC /* Sample_TileMesh.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Sample_TileMesh.cpp; path = ../../Source/Sample_TileMesh.cpp; sourceTree = SOURCE_ROOT; }; 6B324C64111C5D9A00EBD2FD /* ConvexVolumeTool.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ConvexVolumeTool.h; path = ../../Include/ConvexVolumeTool.h; sourceTree = SOURCE_ROOT; }; 6B324C65111C5D9A00EBD2FD /* ConvexVolumeTool.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = ConvexVolumeTool.cpp; path = ../../Source/ConvexVolumeTool.cpp; sourceTree = SOURCE_ROOT; }; + 6B3F9D6C13179EFC000B33D9 /* RecastLayers.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = RecastLayers.cpp; path = ../../../Recast/Source/RecastLayers.cpp; sourceTree = SOURCE_ROOT; }; 6B555DAE100B211D00247EA3 /* imguiRenderGL.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = imguiRenderGL.h; path = ../../Include/imguiRenderGL.h; sourceTree = SOURCE_ROOT; }; 6B555DB0100B212E00247EA3 /* imguiRenderGL.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = imguiRenderGL.cpp; path = ../../Source/imguiRenderGL.cpp; sourceTree = SOURCE_ROOT; }; 6B555DF6100B273500247EA3 /* stb_truetype.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = stb_truetype.h; path = ../../Contrib/stb_truetype.h; sourceTree = SOURCE_ROOT; }; @@ -303,6 +305,7 @@ 6B137C880F7FCC1100459200 /* RecastRasterization.cpp */, 6B137C850F7FCC1100459200 /* RecastFilter.cpp */, 6BF7C4531115C277002B3F46 /* RecastArea.cpp */, + 6B3F9D6C13179EFC000B33D9 /* RecastLayers.cpp */, 6B137C890F7FCC1100459200 /* RecastRegion.cpp */, 6B137C830F7FCC1100459200 /* RecastContour.cpp */, 6B137C870F7FCC1100459200 /* RecastMesh.cpp */, @@ -517,6 +520,7 @@ 6BB5013A12F458CB001B1957 /* DetourProximityGrid.cpp in Sources */, 6BB501E312F46B6A001B1957 /* DetourObstacleAvoidance.cpp in Sources */, 6B86C9AA12F69DD500C92D2E /* fastlz.c in Sources */, + 6B3F9D6D13179EFC000B33D9 /* RecastLayers.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/RecastDemo/Include/Sample_TileMesh.h b/RecastDemo/Include/Sample_TileMesh.h index c010d2a..4e2d9a0 100644 --- a/RecastDemo/Include/Sample_TileMesh.h +++ b/RecastDemo/Include/Sample_TileMesh.h @@ -35,6 +35,7 @@ protected: unsigned char* m_triareas; rcHeightfield* m_solid; rcCompactHeightfield* m_chf; + rcHeightfieldLayerSet* m_lset; rcContourSet* m_cset; rcPolyMesh* m_pmesh; rcPolyMeshDetail* m_dmesh; @@ -60,6 +61,7 @@ protected: DRAWMODE_CONTOURS, DRAWMODE_POLYMESH, DRAWMODE_POLYMESH_DETAIL, + DRAWMODE_HEIGHFIELD_LAYERS, MAX_DRAWMODE }; diff --git a/RecastDemo/Source/Sample_TileMesh.cpp b/RecastDemo/Source/Sample_TileMesh.cpp index c3db8f6..57804a5 100644 --- a/RecastDemo/Source/Sample_TileMesh.cpp +++ b/RecastDemo/Source/Sample_TileMesh.cpp @@ -178,6 +178,7 @@ Sample_TileMesh::Sample_TileMesh() : m_triareas(0), m_solid(0), m_chf(0), + m_lset(0), m_cset(0), m_pmesh(0), m_dmesh(0), @@ -212,6 +213,8 @@ void Sample_TileMesh::cleanup() m_solid = 0; rcFreeCompactHeightfield(m_chf); m_chf = 0; + rcFreeHeightfieldLayerSet(m_lset); + m_lset = 0; rcFreeContourSet(m_cset); m_cset = 0; rcFreePolyMesh(m_pmesh); @@ -466,6 +469,7 @@ void Sample_TileMesh::handleDebugMode() valid[DRAWMODE_CONTOURS] = m_cset != 0; valid[DRAWMODE_POLYMESH] = m_pmesh != 0; valid[DRAWMODE_POLYMESH_DETAIL] = m_dmesh != 0; + valid[DRAWMODE_HEIGHFIELD_LAYERS] = m_lset != 0; } int unavail = 0; @@ -512,6 +516,8 @@ void Sample_TileMesh::handleDebugMode() m_drawMode = DRAWMODE_POLYMESH; if (imguiCheck("Poly Mesh Detail", m_drawMode == DRAWMODE_POLYMESH_DETAIL, valid[DRAWMODE_POLYMESH_DETAIL])) m_drawMode = DRAWMODE_POLYMESH_DETAIL; + if (imguiCheck("Heighfield Layers", m_drawMode == DRAWMODE_HEIGHFIELD_LAYERS, valid[DRAWMODE_HEIGHFIELD_LAYERS])) + m_drawMode = DRAWMODE_HEIGHFIELD_LAYERS; if (unavail) { @@ -565,7 +571,7 @@ void Sample_TileMesh::handleRender() duDebugDrawGridXZ(&dd, bmin[0],bmin[1],bmin[2], tw,th, s, duRGBA(0,0,0,64), 1.0f); // Draw active tile - duDebugDrawBoxWire(&dd, m_tileBmin[0],m_tileBmin[1],m_tileBmin[2], m_tileBmax[0],m_tileBmax[1],m_tileBmax[2], m_tileCol, 2.0f); + duDebugDrawBoxWire(&dd, m_tileBmin[0],m_tileBmin[1],m_tileBmin[2], m_tileBmax[0],m_tileBmax[1],m_tileBmax[2], m_tileCol, 1.0f); /* if (m_navMesh) { @@ -614,12 +620,14 @@ void Sample_TileMesh::handleRender() duDebugDrawHeightfieldWalkable(&dd, *m_solid); glDisable(GL_FOG); } + if (m_cset && m_drawMode == DRAWMODE_RAW_CONTOURS) { glDepthMask(GL_FALSE); duDebugDrawRawContours(&dd, *m_cset); glDepthMask(GL_TRUE); } + if (m_cset && m_drawMode == DRAWMODE_BOTH_CONTOURS) { glDepthMask(GL_FALSE); @@ -654,6 +662,13 @@ void Sample_TileMesh::handleRender() glDepthMask(GL_TRUE); } + if (m_lset && m_drawMode == DRAWMODE_HEIGHFIELD_LAYERS) + { + glDepthMask(GL_FALSE); + duDebugDrawHeightfieldLayers(&dd, *m_lset); + glDepthMask(GL_TRUE); + } + m_geom->drawConvexVolumes(&dd); if (m_tool) @@ -667,12 +682,32 @@ void Sample_TileMesh::handleRenderOverlay(double* proj, double* model, int* view GLdouble x, y, z; // Draw start and end point labels - if (m_tileBuildTime > 0.0f && gluProject((GLdouble)(m_tileBmin[0]+m_tileBmax[0])/2, (GLdouble)(m_tileBmin[1]+m_tileBmax[1])/2, (GLdouble)(m_tileBmin[2]+m_tileBmax[2])/2, +/* if (m_tileBuildTime > 0.0f && gluProject((GLdouble)(m_tileBmin[0]+m_tileBmax[0])/2, (GLdouble)(m_tileBmin[1]+m_tileBmax[1])/2, (GLdouble)(m_tileBmin[2]+m_tileBmax[2])/2, model, proj, view, &x, &y, &z)) { char text[32]; snprintf(text,32,"%.3fms / %dTris / %.1fkB", m_tileBuildTime, m_tileTriCount, m_tileMemUsage); imguiDrawText((int)x, (int)y-25, IMGUI_ALIGN_CENTER, text, imguiRGBA(0,0,0,220)); + }*/ + + if (m_lset && m_drawMode == DRAWMODE_HEIGHFIELD_LAYERS) + { + for (int i = 0; i < m_lset->nlayers; ++i) + { + const rcHeightfieldLayer* layer = &m_lset->layers[i]; + unsigned int color = duIntToCol(i+1, 255); + float pos[3]; + rcVcopy(pos, m_lset->bmin); + pos[1] = m_lset->bmin[1] + ((layer->ymin+layer->ymax)/2)*m_lset->ch; + if (gluProject((GLdouble)pos[0], (GLdouble)pos[1], (GLdouble)pos[2], model, proj, view, &x, &y, &z)) + { + char text[32]; + snprintf(text,32,"Layer %d", i+1); + imguiDrawText((int)x+10+1, (int)y-1, IMGUI_ALIGN_LEFT, text, imguiRGBA(0,0,0,128)); + imguiDrawText((int)x+10, (int)y, IMGUI_ALIGN_LEFT, text, color); + } + + } } if (m_tool) @@ -764,7 +799,7 @@ void Sample_TileMesh::buildTile(const float* pos) m_tileBmax[1] = bmax[1]; m_tileBmax[2] = bmin[2] + (ty+1)*ts; - m_tileCol = duRGBA(77,204,0,255); + m_tileCol = duRGBA(255,255,255,64); m_ctx->resetLog(); @@ -816,7 +851,7 @@ void Sample_TileMesh::removeTile(const float* pos) m_tileBmax[1] = bmax[1]; m_tileBmax[2] = bmin[2] + (ty+1)*ts; - m_tileCol = duRGBA(204,25,0,255); + m_tileCol = duRGBA(128,32,16,64); m_navMesh->removeTile(m_navMesh->getTileRefAt(tx,ty),0,0); } @@ -1063,6 +1098,24 @@ unsigned char* Sample_TileMesh::buildTileMesh(const int tx, const int ty, const } } + + // TODO: Remove + // NOTE! This is for heighfield layer testing only, not needed for general build process! + { + m_lset = rcAllocHeightfieldLayerSet(); + if (!m_lset) + { + m_ctx->log(RC_LOG_ERROR, "buildNavigation: Out of memory 'lset'."); + return 0; + } + if (!rcBuildHeightfieldLayers(m_ctx, *m_chf, m_cfg.borderSize, m_cfg.walkableHeight, *m_lset)) + { + m_ctx->log(RC_LOG_ERROR, "buildNavigation: Could not build heighfield layers."); + return 0; + } + } + + // Create contours. m_cset = rcAllocContourSet(); if (!m_cset)