1
0
forked from 0ad/0ad

Cleaned up TerrainOverlay a little

This was SVN commit r6104.
This commit is contained in:
Ykkrosh 2008-06-24 23:35:46 +00:00
parent 557f08db27
commit b5987f11e8
8 changed files with 191 additions and 259 deletions

View File

@ -2,6 +2,7 @@
#include "TerrainOverlay.h"
#include "ps/Overlay.h"
#include "ps/Game.h"
#include "ps/World.h"
#include "graphics/Terrain.h"
@ -76,7 +77,7 @@ TerrainOverlay::~TerrainOverlay()
#if 0
//initial test to draw out entity boundaries
//it shows how to retrieve object boundary postions for triangulation
//NOTE: it's a test to see how to retrieve bounadry locations for static objects on the terrain.
@ -164,7 +165,7 @@ void TerrainOverlay::RenderEntityEdges()
//----------------------
}
#endif
void TerrainOverlay::RenderOverlays()

View File

@ -10,24 +10,7 @@
#ifndef INCLUDED_TERRAINOVERLAY
#define INCLUDED_TERRAINOVERLAY
//Kai: added for rendering triangulate Terrain Overlay
#include "ps/Overlay.h" // for CColor
#include <math.h>
#include "ps/Vector2D.h"
#include "graphics/Terrain.h"
//Kai: added for line drawing
#include "simulation/Entity.h"
#include "simulation/EntityManager.h"
#include "simulation/BoundingObjects.h"
#include "dcdt/se/se_dcdt.h"
#include "lib/ogl.h"
class CColor;
class CTerrain;
/**
@ -116,11 +99,12 @@ public:
/// Draw all TerrainOverlay objects that exist.
static void RenderOverlays();
#if 0
/**
* Kai: added function to draw out line segments for triangulation
*/
static void RenderEntityEdges();
#endif
private:
@ -140,238 +124,6 @@ private:
};
class TriangulationTerrainOverlay : public TerrainOverlay
{
SrArray<SrPnt2> constr;
SrArray<SrPnt2> unconstr;
//std::vector<CVector2D> path;
SrPolygon CurPath;
public:
void RenderCurrentPath()
{
glColor3f(1,1,1);
for(int i=0; i< CurPath.size()-1; i++)
{
std::vector<CEntity*> results;
g_EntityManager.GetExtant(results);
CEntity* tempHandle = results[0];
glBegin(GL_LINE_LOOP);
float x1 = CurPath[i].x;
float y1 = CurPath[i].y;
float x2 = CurPath[i+1].x;
float y2 = CurPath[i+1].y;
glVertex3f(x1,tempHandle->GetAnchorLevel(x1,y1) + 0.2f,y1);
glVertex3f(x2,tempHandle->GetAnchorLevel(x2,y2) + 0.2f,y2);
glEnd();
}
}
//
//Kai: added function to draw out constrained line segments in triangulation
//
void RenderConstrainedEdges()
{
std::vector<CEntity*> results;
g_EntityManager.GetExtant(results);
CEntity* tempHandle = results[0];
glColor3f( 1, 1, 1 );
for(int i=0; i<constr.size()-2; i=i+2)
{
glBegin( GL_LINE_LOOP );
SrPnt2 p1 = constr[i];
SrPnt2 p2 = constr[i+1];
float x1 = p1.x;
float y1 = p1.y;
float x2 = p2.x;
float y2 = p2.y;
glVertex3f( x1, tempHandle->GetAnchorLevel( x1, y1 ) + 0.2f, y1 );
glVertex3f( x2, tempHandle->GetAnchorLevel( x2, y2 ) + 0.2f, y2 );
glEnd();
}
}
//
// Kai: added function to draw out unconstrained line segments in triangulation
//
void RenderUnconstrainedEdges()
{
std::vector<CEntity*> results;
g_EntityManager.GetExtant(results);
CEntity* tempHandle = results[0];
glColor3f( 0, 1, 0 );
for(int i=0; i<unconstr.size()-2; i=i+2)
{
glBegin( GL_LINE_LOOP );
SrPnt2 p1 = unconstr[i];
SrPnt2 p2 = unconstr[i+1];
float x1 = p1.x;
float y1 = p1.y;
float x2 = p2.x;
float y2 = p2.y;
glVertex3f( x1, tempHandle->GetAnchorLevel( x1, y1 ) + 0.2f, y1 );
glVertex3f( x2, tempHandle->GetAnchorLevel( x2, y2 ) + 0.2f, y2 );
glEnd();
}
}
void setCurrentPath(SrPolygon _CurPath)
{
CurPath = _CurPath;
}
void setConstrainedEdges(SrArray<SrPnt2> _constr)
{
constr = _constr;
}
void setUnconstrainedEdges(SrArray<SrPnt2> _unconstr)
{
unconstr = _unconstr;
}
void Render()
{
}
TriangulationTerrainOverlay()
{
}
virtual void GetTileExtents(
ssize_t& min_i_inclusive, ssize_t& min_j_inclusive,
ssize_t& max_i_inclusive, ssize_t& max_j_inclusive)
{
min_i_inclusive = 1;
min_j_inclusive = 1;
max_i_inclusive = 2;
max_j_inclusive = 2;
}
virtual void ProcessTile(ssize_t UNUSED(i), ssize_t UNUSED(j))
{
RenderConstrainedEdges();
RenderUnconstrainedEdges();
RenderCurrentPath();
}
};
class PathFindingTerrainOverlay : public TerrainOverlay
{
public:
char random[1021];
std::vector<CVector2D> aPath;
void setPath(std::vector<CVector2D> _aPath)
{
aPath =_aPath;
for(size_t k = 0 ; k< aPath.size();k++)
{
aPath[k] = WorldspaceToTilespace( aPath[k] );
}
}
CVector2D WorldspaceToTilespace( const CVector2D &ws )
{
return CVector2D(floor(ws.x/CELL_SIZE), floor(ws.y/CELL_SIZE));
}
bool inPath(ssize_t i, ssize_t j)
{
for(size_t k = 0 ; k<aPath.size();k++)
{
if(aPath[k].x== i && aPath[k].y== j)
return true;
}
return false;
}
PathFindingTerrainOverlay()
{
}
/*virtual void GetTileExtents(
ssize_t& min_i_inclusive, ssize_t& min_j_inclusive,
ssize_t& max_i_inclusive, ssize_t& max_j_inclusive)
{
min_i_inclusive = 1;
min_j_inclusive = 1;
max_i_inclusive = 50;
max_j_inclusive = 50;
}*/
virtual void ProcessTile(ssize_t i, ssize_t j)
{
if ( inPath( i, j))
{
RenderTile(CColor(random[(i*79+j*13) % ARRAY_SIZE(random)]/4.f, 1, 0, 0.3f), false);
RenderTileOutline(CColor(1, 1, 1, 1), 1, true);
}
}
};
/* Example usage:
class ExampleTerrainOverlay : public TerrainOverlay

View File

@ -38,6 +38,49 @@ enum
};
class PathFindingTerrainOverlay : public TerrainOverlay
{
public:
char random[1021];
std::vector<CVector2D> aPath;
void setPath(std::vector<CVector2D> _aPath)
{
aPath =_aPath;
for(size_t k = 0 ; k< aPath.size();k++)
{
aPath[k] = WorldspaceToTilespace( aPath[k] );
}
}
CVector2D WorldspaceToTilespace( const CVector2D &ws )
{
return CVector2D(floor(ws.x/CELL_SIZE), floor(ws.y/CELL_SIZE));
}
bool inPath(ssize_t i, ssize_t j)
{
for(size_t k = 0 ; k<aPath.size();k++)
{
if(aPath[k].x== i && aPath[k].y== j)
return true;
}
return false;
}
virtual void ProcessTile(ssize_t i, ssize_t j)
{
if ( inPath( i, j))
{
RenderTile(CColor(random[(i*79+j*13) % ARRAY_SIZE(random)]/4.f, 1, 0, 0.3f), false);
RenderTileOutline(CColor(1, 1, 1, 1), 1, true);
}
}
};
CAStarEngine::CAStarEngine()
{
mSearchLimit = DEFAULT_SEARCH_LIMIT;
@ -49,9 +92,8 @@ CAStarEngine::CAStarEngine()
mFlags = new AStarNodeFlag[mFlagArraySize*mFlagArraySize];
memset(mFlags, 0, mFlagArraySize*mFlagArraySize*sizeof(AStarNodeFlag));
// TODO: only instantiate this object when it's going to be used
pathfindingOverlay = new PathFindingTerrainOverlay();
}
CAStarEngine::CAStarEngine(AStarGoalBase *goal)
@ -229,7 +271,7 @@ bool CAStarEngine::FindPath(
//the drawing is disable in the render() function in TerraiOverlay.cpp
if(g_ShowPathfindingOverlay)
{
pathfindingOverlay.setPath(mPath);
pathfindingOverlay->setPath(mPath);
}
Cleanup();

View File

@ -61,6 +61,8 @@ public:
typedef unsigned char AStarNodeFlag;
class PathFindingTerrainOverlay;
class CAStarEngine
{
public:
@ -77,7 +79,7 @@ public:
void SetSearchLimit( int limit );
//Kai:added tile overlay for pathfinding
PathFindingTerrainOverlay pathfindingOverlay;
PathFindingTerrainOverlay* pathfindingOverlay;
SrPolygon pol;

View File

@ -18,6 +18,139 @@
#define EPSILON 0.00001f
class TriangulationTerrainOverlay : public TerrainOverlay
{
SrArray<SrPnt2> constr;
SrArray<SrPnt2> unconstr;
//std::vector<CVector2D> path;
SrPolygon CurPath;
public:
void RenderCurrentPath()
{
glColor3f(1,1,1);
for(int i=0; i< CurPath.size()-1; i++)
{
std::vector<CEntity*> results;
g_EntityManager.GetExtant(results);
CEntity* tempHandle = results[0];
glBegin(GL_LINE_LOOP);
float x1 = CurPath[i].x;
float y1 = CurPath[i].y;
float x2 = CurPath[i+1].x;
float y2 = CurPath[i+1].y;
glVertex3f(x1,tempHandle->GetAnchorLevel(x1,y1) + 0.2f,y1);
glVertex3f(x2,tempHandle->GetAnchorLevel(x2,y2) + 0.2f,y2);
glEnd();
}
}
//
//Kai: added function to draw out constrained line segments in triangulation
//
void RenderConstrainedEdges()
{
std::vector<CEntity*> results;
g_EntityManager.GetExtant(results);
CEntity* tempHandle = results[0];
glColor3f( 1, 1, 1 );
for(int i=0; i<constr.size()-2; i=i+2)
{
glBegin( GL_LINE_LOOP );
SrPnt2 p1 = constr[i];
SrPnt2 p2 = constr[i+1];
float x1 = p1.x;
float y1 = p1.y;
float x2 = p2.x;
float y2 = p2.y;
glVertex3f( x1, tempHandle->GetAnchorLevel( x1, y1 ) + 0.2f, y1 );
glVertex3f( x2, tempHandle->GetAnchorLevel( x2, y2 ) + 0.2f, y2 );
glEnd();
}
}
//
// Kai: added function to draw out unconstrained line segments in triangulation
//
void RenderUnconstrainedEdges()
{
std::vector<CEntity*> results;
g_EntityManager.GetExtant(results);
CEntity* tempHandle = results[0];
glColor3f( 0, 1, 0 );
for(int i=0; i<unconstr.size()-2; i=i+2)
{
glBegin( GL_LINE_LOOP );
SrPnt2 p1 = unconstr[i];
SrPnt2 p2 = unconstr[i+1];
float x1 = p1.x;
float y1 = p1.y;
float x2 = p2.x;
float y2 = p2.y;
glVertex3f( x1, tempHandle->GetAnchorLevel( x1, y1 ) + 0.2f, y1 );
glVertex3f( x2, tempHandle->GetAnchorLevel( x2, y2 ) + 0.2f, y2 );
glEnd();
}
}
void setCurrentPath(SrPolygon _CurPath)
{
CurPath = _CurPath;
}
void setConstrainedEdges(SrArray<SrPnt2> _constr)
{
constr = _constr;
}
void setUnconstrainedEdges(SrArray<SrPnt2> _unconstr)
{
unconstr = _unconstr;
}
void Render()
{
}
TriangulationTerrainOverlay()
{
}
virtual void GetTileExtents(
ssize_t& min_i_inclusive, ssize_t& min_j_inclusive,
ssize_t& max_i_inclusive, ssize_t& max_j_inclusive)
{
min_i_inclusive = 1;
min_j_inclusive = 1;
max_i_inclusive = 2;
max_j_inclusive = 2;
}
virtual void ProcessTile(ssize_t UNUSED(i), ssize_t UNUSED(j))
{
RenderConstrainedEdges();
RenderUnconstrainedEdges();
RenderCurrentPath();
}
};
CPathfindEngine::CPathfindEngine() : triangulationOverlay(0),
OABBBOUNDREDUCTION(0.8f),

View File

@ -17,6 +17,7 @@
#include "TRAStarEngine.h"
class TriangulationTerrainOverlay;
#define g_Pathfinder CPathfindEngine::GetSingleton()

View File

@ -14,7 +14,7 @@ Example usage: (SEE SOUNDGROUPMGR.H)
Example SoundGroup.xml
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<?xml version="1.0" encoding="utf-8"?>
<SoundGroup>
<Gain>1.0</Gain>
<Looping>0</Looping>

View File

@ -2,6 +2,7 @@
#include "Brushes.h"
#include "ps/Overlay.h"
#include "ps/Game.h"
#include "ps/World.h"
#include "graphics/Terrain.h"