Commit df2abed2 authored by Daniel Buckmaster's avatar Daniel Buckmaster

Added navmesh wrapper code and module.

parent 95ef5ec2
//-----------------------------------------------------------------------------
// Copyright (c) 2013 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "torqueRecast.h"
#include "duDebugDrawTorque.h"
#include "gfx/gfxDevice.h"
#include "gfx/primBuilder.h"
#include "gfx/gfxStateBlock.h"
/// @class duDebugDrawTorque
/// This class uses the primitive builder (gfx/primBuild.h) to render navmeshes
/// and other Recast data. To facilitate the primbuilder's requirement to know
/// the number of vertices to render beforehand, this class stores all vertices
/// in a buffer of its own, then passes on that known-size buffer.
/// This means that you only need to call the duDebugDraw functions when your
/// data changes. At other times, you can cache the duDebugDrawTorque object
/// and call its render() method, which actually renders its buffered data.
duDebugDrawTorque::duDebugDrawTorque()
{
mOverrideColor = 0;
mOverride = false;
mGroup = 0;
}
duDebugDrawTorque::~duDebugDrawTorque()
{
clear();
}
void duDebugDrawTorque::depthMask(bool state)
{
mDesc.setZReadWrite(state, state);
}
void duDebugDrawTorque::texture(bool state)
{
}
/// Begin drawing primitives.
/// @param prim [in] primitive type to draw, one of rcDebugDrawPrimitives.
/// @param size [in] size of a primitive, applies to point size and line width only.
void duDebugDrawTorque::begin(duDebugDrawPrimitives prim, float size)
{
mCurrColor = -1;
mQuadsMode = false;
mVertCount = 0;
mPrimType = 0;
switch(prim)
{
case DU_DRAW_POINTS: mPrimType = GFXPointList; break;
case DU_DRAW_LINES: mPrimType = GFXLineList; break;
case DU_DRAW_TRIS: mPrimType = GFXTriangleList; break;
case DU_DRAW_QUADS: mPrimType = GFXTriangleList;
mQuadsMode = true; break;
}
mBuffers.push_back(Buffer(mPrimType));
mBuffers.last().group = mGroup;
mDesc.setCullMode(GFXCullNone);
mDesc.setBlend(true);
}
void duDebugDrawTorque::beginGroup(U32 group)
{
mGroup = group;
}
/// Submit a vertex
/// @param pos [in] position of the verts.
/// @param color [in] color of the verts.
void duDebugDrawTorque::vertex(const float* pos, unsigned int color)
{
vertex(pos[0], pos[1], pos[2], color);
}
/// Submit a vertex
/// @param x,y,z [in] position of the verts.
/// @param color [in] color of the verts.
void duDebugDrawTorque::vertex(const float x, const float y, const float z, unsigned int color)
{
if(mQuadsMode)
{
if(mVertCount == 3)
{
_vertex(x, -z, y, color);
_vertex(mStore[0][0], mStore[0][1], mStore[0][2], color);
_vertex(mStore[1][0], mStore[1][1], mStore[1][2], color);
_vertex(mStore[1][0], mStore[1][1], mStore[1][2], color);
_vertex(mStore[2][0], mStore[2][1], mStore[2][2], color);
_vertex(x, -z, y, color);
mVertCount = 0;
}
else
{
mStore[mVertCount][0] = x;
mStore[mVertCount][1] = -z;
mStore[mVertCount][2] = y;
mVertCount++;
}
}
else
{
_vertex(x, -z, y, color);
}
}
/// Submit a vertex
/// @param pos [in] position of the verts.
/// @param color [in] color of the verts.
void duDebugDrawTorque::vertex(const float* pos, unsigned int color, const float* uv)
{
vertex(pos[0], pos[1], pos[2], color);
}
/// Submit a vertex
/// @param x,y,z [in] position of the verts.
/// @param color [in] color of the verts.
void duDebugDrawTorque::vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v)
{
vertex(x, y, z, color);
}
/// Push a vertex onto the buffer.
void duDebugDrawTorque::_vertex(const float x, const float y, const float z, unsigned int color)
{
// Use override color if we must.
//if(mOverride)
//color = mOverrideColor;
if(mCurrColor != color || !mBuffers.last().buffer.size())
{
U8 r, g, b, a;
// Convert color integer to components.
rcCol(color, r, g, b, a);
mBuffers.last().buffer.push_back(Instruction(r, g, b, a));
mCurrColor = color;
}
// Construct vertex data.
mBuffers.last().buffer.push_back(Instruction(x, y, z));
}
/// End drawing primitives.
void duDebugDrawTorque::end()
{
}
void duDebugDrawTorque::overrideColor(unsigned int col)
{
mOverride = true;
mOverrideColor = col;
}
void duDebugDrawTorque::cancelOverride()
{
mOverride = false;
}
void duDebugDrawTorque::renderBuffer(Buffer &b)
{
PrimBuild::begin(b.primType, b.buffer.size());
Vector<Instruction> &buf = b.buffer;
for(U32 i = 0; i < buf.size(); i++)
{
switch(buf[i].type)
{
case Instruction::POINT:
PrimBuild::vertex3f(buf[i].data.point.x,
buf[i].data.point.y,
buf[i].data.point.z);
break;
case Instruction::COLOR:
if(mOverride)
break;
PrimBuild::color4i(buf[i].data.color.r,
buf[i].data.color.g,
buf[i].data.color.b,
buf[i].data.color.a);
break;
}
}
PrimBuild::end();
}
void duDebugDrawTorque::render()
{
GFXStateBlockRef sb = GFX->createStateBlock(mDesc);
GFX->setStateBlock(sb);
// Use override color for all rendering.
if(mOverride)
{
U8 r, g, b, a;
rcCol(mOverrideColor, r, g, b, a);
PrimBuild::color4i(r, g, b, a);
}
for(U32 b = 0; b < mBuffers.size(); b++)
{
renderBuffer(mBuffers[b]);
}
}
void duDebugDrawTorque::renderGroup(U32 group)
{
GFXStateBlockRef sb = GFX->createStateBlock(mDesc);
GFX->setStateBlock(sb);
// Use override color for all rendering.
if(mOverride)
{
U8 r, g, b, a;
rcCol(mOverrideColor, r, g, b, a);
PrimBuild::color4i(r, g, b, a);
}
for(U32 b = 0; b < mBuffers.size(); b++)
{
if(mBuffers[b].group == group)
renderBuffer(mBuffers[b]);
}
}
void duDebugDrawTorque::clear()
{
for(U32 b = 0; b < mBuffers.size(); b++)
mBuffers[b].buffer.clear();
mBuffers.clear();
}
//-----------------------------------------------------------------------------
// Copyright (c) 2013 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _DU_DEBUG_DRAW_TORQUE_H_
#define _DU_DEBUG_DRAW_TORQUE_H_
#include "core/util/tVector.h"
#include <DebugDraw.h>
#include "gfx/gfxStateBlock.h"
/// @brief Implements the duDebugDraw interface in Torque.
class duDebugDrawTorque: public duDebugDraw {
public:
duDebugDrawTorque();
~duDebugDrawTorque();
/// Enable/disable Z read.
void depthMask(bool state);
/// Enable/disable texturing. Not used.
void texture(bool state);
/// Special colour overwrite for when I get picky about the colours Mikko chose.
void overrideColor(unsigned int col);
/// Stop the colour override.
void cancelOverride();
/// Begin drawing primitives.
/// @param prim [in] primitive type to draw, one of rcDebugDrawPrimitives.
/// @param size [in] size of a primitive, applies to point size and line width only.
void begin(duDebugDrawPrimitives prim, float size = 1.0f);
/// All new buffers go into this group.
void beginGroup(U32 group);
/// Submit a vertex
/// @param pos [in] position of the verts.
/// @param color [in] color of the verts.
void vertex(const float* pos, unsigned int color);
/// Submit a vertex
/// @param x,y,z [in] position of the verts.
/// @param color [in] color of the verts.
void vertex(const float x, const float y, const float z, unsigned int color);
/// Submit a vertex
/// @param pos [in] position of the verts.
/// @param color [in] color of the verts.
void vertex(const float* pos, unsigned int color, const float* uv);
/// Submit a vertex
/// @param x,y,z [in] position of the verts.
/// @param color [in] color of the verts.
void vertex(const float x, const float y, const float z, unsigned int color, const float u, const float v);
/// End drawing primitives.
void end();
/// Render buffered primitive.
void render();
/// Render buffered primitives in a group.
void renderGroup(U32 group);
/// Delete buffered primitive.
void clear();
private:
GFXStateBlockDesc mDesc;
U32 mPrimType;
bool mQuadsMode;
U32 mVertCount;
F32 mStore[3][3];
U32 mGroup;
struct Instruction {
// Contain either a point or a color command.
union {
struct {
U8 r, g, b, a;
} color;
struct {
float x, y, z;
} point;
U32 primType;
} data;
// Which type of data do we store?
enum {
COLOR,
POINT,
PRIMTYPE,
} type;
// Construct as color instruction.
Instruction(U8 r, U8 g, U8 b, U8 a) {
type = COLOR;
data.color.r = r;
data.color.g = g;
data.color.b = b;
data.color.a = a;
}
// Construct as point.
Instruction(float x, float y, float z) {
type = POINT;
data.point.x = x;
data.point.y = y;
data.point.z = z;
}
Instruction(U32 t = 0) {
type = PRIMTYPE;
data.primType = t;
}
};
struct Buffer {
U32 group;
Vector<Instruction> buffer;
GFXPrimitiveType primType;
Buffer(U32 type = 0) {
primType = (GFXPrimitiveType)type;
group = 0;
}
};
Vector<Buffer> mBuffers;
U32 mCurrColor;
U32 mOverrideColor;
bool mOverride;
void _vertex(const float x, const float y, const float z, unsigned int color);
void renderBuffer(Buffer &b);
};
#endif
This diff is collapsed.
//-----------------------------------------------------------------------------
// Copyright (c) 2013 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _NAVMESH_H_
#define _NAVMESH_H_
#include <queue>
#include "torqueRecast.h"
#include "scene/sceneObject.h"
#include "recastPolyList.h"
#include "duDebugDrawTorque.h"
#include <Recast.h>
#include <DetourNavMesh.h>
#include <DetourNavMeshBuilder.h>
#include <DebugDraw.h>
/// @class NavMesh
/// Represents a set of bounds within which a Recast navigation mesh is generated.
/// @see NavMeshPolyList
/// @see Trigger
class NavMesh : public SceneObject {
typedef SceneObject Parent;
friend class NavPath;
public:
/// @name NavMesh build
/// @{
/// Initiates the navmesh build process, which includes notifying the
/// clients and posting an event.
bool build(bool background = true, bool saveIntermediates = false);
/// Stop a build in progress.
void cancelBuild();
/// Save the navmesh to a file.
bool save();
/// Load a saved navmesh from a file.
bool load();
/// Instantly rebuild the tiles in the navmesh that overlap the box.
void buildTiles(const Box3F &box);
/// Instantly rebuild a specific tile.
void buildTile(const U32 &tile);
/// Data file to store this nav mesh in. (From engine executable dir.)
StringTableEntry mFileName;
/// Cell width and height.
F32 mCellSize, mCellHeight;
/// @name Actor data
/// @{
F32 mWalkableHeight,
mWalkableClimb,
mWalkableRadius,
mWalkableSlope;
/// @}
/// @name Generation data
/// @{
U32 mBorderSize;
F32 mDetailSampleDist, mDetailSampleMaxError;
U32 mMaxEdgeLen;
F32 mMaxSimplificationError;
static const U32 mMaxVertsPerPoly;
U32 mMinRegionArea;
U32 mMergeRegionArea;
F32 mTileSize;
U32 mMaxPolysPerTile;
/// @}
/// @}
/// Return the index of the tile included by this point.
S32 getTile(Point3F pos);
/// Return the box of a given tile.
Box3F getTileBox(U32 id);
/// @name SimObject
/// @{
virtual void onEditorEnable();
virtual void onEditorDisable();
void write(Stream &stream, U32 tabStop, U32 flags);
/// @}
/// @name SceneObject
/// @{
static void initPersistFields();
bool onAdd();
void onRemove();
enum flags {
BuildFlag = Parent::NextFreeMask << 0,
LoadFlag = Parent::NextFreeMask << 1,
NextFreeMask = Parent::NextFreeMask << 2,
};
U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
void unpackUpdate(NetConnection *conn, BitStream *stream);
void setTransform(const MatrixF &mat);
void setScale(const VectorF &scale);
/// @}
/// @name ProcessObject
/// @{
void processTick(const Move *move);
/// @}
/// @name Rendering
/// @{
void prepRenderImage(SceneRenderState *state);
void render(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat);
bool mAlwaysRender;
/// @}
NavMesh();
~NavMesh();
DECLARE_CONOBJECT(NavMesh);
void inspectPostApply();
protected:
dtNavMesh const* getNavMesh() { return nm; }
private:
/// Generates a navigation mesh for the collection of objects in this
/// mesh. Returns true if successful. Stores the created mesh in tnm.
bool generateMesh();
/// Builds the next tile in the dirty list.
void buildNextTile();
/// @name Tiles
/// @{
struct Tile {
/// Torque-space world box of this tile.
Box3F box;
/// Local coordinates of this box.
U32 x, y;
/// Recast min and max points.
F32 bmin[3], bmax[3];
/// Default constructor.
Tile() : box(Box3F::Invalid), x(0), y(0)
{
bmin[0] = bmin[1] = bmin[2] = bmax[0] = bmax[1] = bmax[2] = 0.0f;
}
/// Value constructor.
Tile(const Box3F &b, U32 _x, U32 _y, const F32 *min, const F32 *max)
: box(b), x(_x), y(_y)
{
rcVcopy(bmin, min);
rcVcopy(bmax, max);
}
};
/// Intermediate data for tile creation.
struct TileData {
RecastPolyList geom;
rcHeightfield *hf;
rcCompactHeightfield *chf;
rcContourSet *cs;
rcPolyMesh *pm;
rcPolyMeshDetail *pmd;
TileData()
{
hf = NULL;
chf = NULL;
cs = NULL;
pm = NULL;
pmd = NULL;
}
void freeAll()
{
geom.clear();
rcFreeHeightField(hf);
rcFreeCompactHeightfield(chf);
rcFreeContourSet(cs);
rcFreePolyMesh(pm);
rcFreePolyMeshDetail(pmd);
}
~TileData()
{
freeAll();
}
};
/// List of tiles.
Vector<Tile> mTiles;
/// List of indices to the tile array which are dirty.
std::queue<U32> mDirtyTiles;
/// Update tile dimensions.
void updateTiles(bool dirty = false);
/// Generates navmesh data for a single tile.
unsigned char *buildTileData(const Tile &tile, TileData &data, U32 &dataSize);
/// @}
/// @name Intermediate data
/// @{
/// Config struct.
rcConfig cfg;
/// Updates our config from console members.
void updateConfig();
dtNavMesh *nm;
/// @}
/// Used to perform non-standard validation. detailSampleDist can be 0, or >= 0.9.
static bool setProtectedDetailSampleDist(void *obj, const char *index, const char *data);
/// Updates the client when we check the alwaysRender option.
static bool setProtectedAlwaysRender(void *obj, const char *index, const char *data);
/// @name Threaded updates
/// @{
/// A simple flag to say we are building.
bool mBuilding;
/// @}
/// @name Rendering
/// @{
duDebugDrawTorque dd;
void renderToDrawer();
/// @}
};
#endif
This diff is collapsed.
//-----------------------------------------------------------------------------
// Copyright (c) 2013 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _NAVPATH_H_
#define _NAVPATH_H_
#include "scene/sceneObject.h"
#include "scene/simPath.h"
#include "navMesh.h"
#include <DetourNavMeshQuery.h>
class NavPath: public SceneObject {
typedef SceneObject Parent;
/// Maximum size of Detour path.
static const U32 MaxPathLen = 1024;
public:
/// @name NavPath
/// Functions for planning and accessing the path.
/// @{
SimObjectPtr<</