From 6ec92f212786e886f05babd67f232f4ef5401f93 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Fri, 4 Feb 2011 06:34:32 +0000 Subject: [PATCH] - attempt to add more randomness to pathfinding and faster AI decsions --- source/glest_game/ai/ai.cpp | 5 + source/glest_game/ai/ai_interface.cpp | 27 +++ source/glest_game/ai/ai_interface.h | 1 + source/glest_game/ai/path_finder.cpp | 159 ++++++++-------- source/glest_game/ai/path_finder.h | 8 +- source/glest_game/graphics/renderer.cpp | 170 +++++++++--------- source/glest_game/graphics/renderer.h | 22 ++- .../shared_lib/include/graphics/math_util.h | 10 ++ 8 files changed, 218 insertions(+), 184 deletions(-) diff --git a/source/glest_game/ai/ai.cpp b/source/glest_game/ai/ai.cpp index 1976607d..87f006e0 100644 --- a/source/glest_game/ai/ai.cpp +++ b/source/glest_game/ai/ai.cpp @@ -244,6 +244,10 @@ const ResourceType *Ai::getNeededResource(int unitIndex) { } bool Ai::beingAttacked(Vec2i &pos, Field &field, int radius){ + + const Unit *enemy = aiInterface->getFirstOnSightEnemyUnit(pos, field, radius); + return (enemy != NULL); +/* int count= aiInterface->onSightUnitCount(); const Unit *unit; @@ -259,6 +263,7 @@ bool Ai::beingAttacked(Vec2i &pos, Field &field, int radius){ } } return false; +*/ } bool Ai::isStableBase(){ diff --git a/source/glest_game/ai/ai_interface.cpp b/source/glest_game/ai/ai_interface.cpp index 29a182ae..b90087d4 100644 --- a/source/glest_game/ai/ai_interface.cpp +++ b/source/glest_game/ai/ai_interface.cpp @@ -587,4 +587,31 @@ bool AiInterface::isFreeCells(const Vec2i &pos, int size, Field field){ return world->getMap()->isFreeCells(pos, size, field); } +const Unit *AiInterface::getFirstOnSightEnemyUnit(Vec2i &pos, Field &field, int radius) { + Map *map= world->getMap(); + + for(int i = 0; i < world->getFactionCount(); ++i) { + for(int j = 0; j < world->getFaction(i)->getUnitCount(); ++j) { + Unit * unit= world->getFaction(i)->getUnit(j); + SurfaceCell *sc= map->getSurfaceCell(Map::toSurfCoords(unit->getPos())); + bool cannotSeeUnit = (unit->getType()->hasCellMap() == true && + unit->getType()->getAllowEmptyCellMap() == true && + unit->getType()->hasEmptyCellMap() == true); + + if(sc->isVisible(teamIndex) && cannotSeeUnit == false && + isAlly(unit) == false && unit->isAlive() == true) { + pos= unit->getPos(); + field= unit->getCurrField(); + if(pos.dist(getHomeLocation()) < radius) { + printLog(2, "Being attacked at pos "+intToStr(pos.x)+","+intToStr(pos.y)+"\n"); + + return unit; + } + } + } + } + return NULL; +} + + }}//end namespace diff --git a/source/glest_game/ai/ai_interface.h b/source/glest_game/ai/ai_interface.h index 02002181..0d96201c 100644 --- a/source/glest_game/ai/ai_interface.h +++ b/source/glest_game/ai/ai_interface.h @@ -93,6 +93,7 @@ public: bool reqsOk(const CommandType *ct); bool checkCosts(const ProducibleType *pt); bool isFreeCells(const Vec2i &pos, int size, Field field); + const Unit *getFirstOnSightEnemyUnit(Vec2i &pos, Field &field, int radius); private: string getLogFilename() const {return "ai"+intToStr(factionIndex)+".log";} diff --git a/source/glest_game/ai/path_finder.cpp b/source/glest_game/ai/path_finder.cpp index c4dddbda..e156aa4e 100644 --- a/source/glest_game/ai/path_finder.cpp +++ b/source/glest_game/ai/path_finder.cpp @@ -46,16 +46,11 @@ const int PathFinder::pathFindBailoutRadius = 20; PathFinder::PathFinder() { nodePool.clear(); - //lookupCacheCanMove.clear(); - //moveLookupCacheApproxCanMove.clear(); - map=NULL; } PathFinder::PathFinder(const Map *map) { nodePool.clear(); - //lookupCacheCanMove.clear(); - //moveLookupCacheApproxCanMove.clear(); map=NULL; init(map); @@ -63,30 +58,19 @@ PathFinder::PathFinder(const Map *map) { void PathFinder::init(const Map *map) { nodePool.resize(pathFindNodesMax); - //lookupCacheCanMove.clear(); - //moveLookupCacheApproxCanMove.clear(); - this->map= map; } PathFinder::~PathFinder(){ nodePool.clear(); - //lookupCacheCanMove.clear(); - //moveLookupCacheApproxCanMove.clear(); - map=NULL; } -TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStuck,bool clearLookupCache) { +TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStuck) { if(map == NULL) { throw runtime_error("map == NULL"); } - if(clearLookupCache == true) { - //lookupCacheCanMove.clear(); - //moveLookupCacheApproxCanMove.clear(); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) { char szBuf[4096]=""; sprintf(szBuf,"[findPath] unit->getPos() [%s] finalPos [%s]", @@ -326,6 +310,29 @@ TravelState PathFinder::findPath(Unit *unit, const Vec2i &finalPos, bool *wasStu // ==================== PRIVATE ==================== + +void PathFinder::processNode(Unit *unit, Node *node,const Vec2i finalPos, int i, int j, bool &nodeLimitReached) { + Vec2i sucPos= node->pos + Vec2i(i, j); + + bool canUnitMoveToCell = map->aproxCanMove(unit, node->pos, sucPos); + if(openPos(sucPos) == false && canUnitMoveToCell == true) { + //if node is not open and canMove then generate another node + Node *sucNode= newNode(); + if(sucNode != NULL) { + sucNode->pos= sucPos; + sucNode->heuristic= heuristic(sucNode->pos, finalPos); + sucNode->prev= node; + sucNode->next= NULL; + sucNode->exploredCell= map->getSurfaceCell(Map::toSurfCoords(sucPos))->isExplored(unit->getTeam()); + openNodesList[sucNode->heuristic].push_back(sucNode); + openPosList[sucNode->pos] = true; + } + else { + nodeLimitReached= true; + } + } +} + //route a unit using A* algorithm TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout) { Chrono chrono; @@ -342,7 +349,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout const Vec2i finalPos= computeNearestFreePos(unit, targetPos); - if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); //path find algorithm @@ -366,9 +373,8 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout bool pathFound = true; bool nodeLimitReached = false; Node *node = NULL; - int64 lastPerfTick = 0; - if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); int whileLoopCount = 0; while(nodeLimitReached == false) { @@ -395,64 +401,38 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout closedNodesList[node->heuristic].push_back(node); openPosList[node->pos] = true; - int tryDirection = random.randRange(0,1); - if(tryDirection > 0) { + int tryDirection = random.randRange(0,3); + if(tryDirection == 3) { + for(int i = 1; i >= -1 && nodeLimitReached == false; --i) { + for(int j = -1; j <= 1 && nodeLimitReached == false; ++j) { + processNode(unit, node, finalPos, i, j, nodeLimitReached); + } + } + } + else if(tryDirection == 2) { + for(int i = -1; i <= 1 && nodeLimitReached == false; ++i) { + for(int j = 1; j >= -1 && nodeLimitReached == false; --j) { + processNode(unit, node, finalPos, i, j, nodeLimitReached); + } + } + } + else if(tryDirection == 1) { for(int i = -1; i <= 1 && nodeLimitReached == false; ++i) { for(int j = -1; j <= 1 && nodeLimitReached == false; ++j) { - Vec2i sucPos= node->pos + Vec2i(i, j); - - //bool canUnitMoveToCell = map->aproxCanMove(unit, node->pos, sucPos, &moveLookupCacheApproxCanMove); - bool canUnitMoveToCell = map->aproxCanMove(unit, node->pos, sucPos); - - if(openPos(sucPos) == false && canUnitMoveToCell == true) { - //if node is not open and canMove then generate another node - Node *sucNode= newNode(); - if(sucNode != NULL) { - sucNode->pos= sucPos; - sucNode->heuristic= heuristic(sucNode->pos, finalPos); - sucNode->prev= node; - sucNode->next= NULL; - sucNode->exploredCell= map->getSurfaceCell(Map::toSurfCoords(sucPos))->isExplored(unit->getTeam()); - openNodesList[sucNode->heuristic].push_back(sucNode); - openPosList[sucNode->pos] = true; - } - else { - nodeLimitReached= true; - } - } + processNode(unit, node, finalPos, i, j, nodeLimitReached); } } } else { for(int i = 1; i >= -1 && nodeLimitReached == false; --i) { for(int j = 1; j >= -1 && nodeLimitReached == false; --j) { - Vec2i sucPos= node->pos + Vec2i(i, j); - - //bool canUnitMoveToCell = map->aproxCanMove(unit, node->pos, sucPos, &moveLookupCacheApproxCanMove); - bool canUnitMoveToCell = map->aproxCanMove(unit, node->pos, sucPos); - - if(openPos(sucPos) == false && canUnitMoveToCell == true) { - //if node is not open and canMove then generate another node - Node *sucNode= newNode(); - if(sucNode != NULL) { - sucNode->pos= sucPos; - sucNode->heuristic= heuristic(sucNode->pos, finalPos); - sucNode->prev= node; - sucNode->next= NULL; - sucNode->exploredCell= map->getSurfaceCell(Map::toSurfCoords(sucPos))->isExplored(unit->getTeam()); - openNodesList[sucNode->heuristic].push_back(sucNode); - openPosList[sucNode->pos] = true; - } - else { - nodeLimitReached= true; - } - } + processNode(unit, node, finalPos, i, j, nodeLimitReached); } } } } //while - if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); Node *lastNode= node; @@ -466,7 +446,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout } } - if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); //check results of path finding TravelState ts = tsImpossible; @@ -497,7 +477,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout unit->logSynchData(__FILE__,__LINE__,szBuf); } - if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); } else { //on the way @@ -510,7 +490,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout currNode= currNode->prev; } - if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); //store path path->clear(); @@ -520,7 +500,7 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout path->add(currNode->next->pos); } - if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) { char szBuf[4096]=""; @@ -541,14 +521,14 @@ TravelState PathFinder::aStar(Unit *unit, const Vec2i &targetPos, bool inBailout unit->setCurrentUnitTitle(szBuf); } - if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); } openNodesList.clear(); openPosList.clear(); closedNodesList.clear(); - if(chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] ---------------------------\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled == true && chrono.getMillis() > 4) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] ---------------------------\n",__FILE__,__FUNCTION__,__LINE__,chrono.getMillis()); return ts; } @@ -562,6 +542,25 @@ PathFinder::Node *PathFinder::newNode() { return NULL; } +void PathFinder::processNearestFreePos(const Vec2i &finalPos, int i, int j, int size, Field field, int teamIndex,Vec2i unitPos, Vec2i &nearestPos, float &nearestDist) { + Vec2i currPos= finalPos + Vec2i(i, j); + if(map->isAproxFreeCells(currPos, size, field, teamIndex)) { + float dist= currPos.dist(finalPos); + + //if nearer from finalPos + if(dist < nearestDist){ + nearestPos= currPos; + nearestDist= dist; + } + //if the distance is the same compare distance to unit + else if(dist == nearestDist){ + if(currPos.dist(unitPos) < nearestPos.dist(unitPos)) { + nearestPos= currPos; + } + } + } +} + Vec2i PathFinder::computeNearestFreePos(const Unit *unit, const Vec2i &finalPos) { if(map == NULL) { throw runtime_error("map == NULL"); @@ -581,24 +580,10 @@ Vec2i PathFinder::computeNearestFreePos(const Unit *unit, const Vec2i &finalPos) Vec2i unitPos= unit->getPos(); Vec2i nearestPos= unitPos; float nearestDist= unitPos.dist(finalPos); + for(int i= -maxFreeSearchRadius; i <= maxFreeSearchRadius; ++i) { for(int j= -maxFreeSearchRadius; j <= maxFreeSearchRadius; ++j) { - Vec2i currPos= finalPos + Vec2i(i, j); - if(map->isAproxFreeCells(currPos, size, field, teamIndex)) { - float dist= currPos.dist(finalPos); - - //if nearer from finalPos - if(dist < nearestDist){ - nearestPos= currPos; - nearestDist= dist; - } - //if the distance is the same compare distance to unit - else if(dist == nearestDist){ - if(currPos.dist(unitPos) < nearestPos.dist(unitPos)) { - nearestPos= currPos; - } - } - } + processNearestFreePos(finalPos, i, j, size, field, teamIndex, unitPos, nearestPos, nearestDist); } } return nearestPos; diff --git a/source/glest_game/ai/path_finder.h b/source/glest_game/ai/path_finder.h index fd503476..57caa94a 100644 --- a/source/glest_game/ai/path_finder.h +++ b/source/glest_game/ai/path_finder.h @@ -68,15 +68,12 @@ private: const Map *map; RandomGen random; - //std::map > > > lookupCacheCanMove; - std::map > > > > moveLookupCacheApproxCanMove; - public: PathFinder(); PathFinder(const Map *map); ~PathFinder(); void init(const Map *map); - TravelState findPath(Unit *unit, const Vec2i &finalPos, bool *wasStuck=NULL,bool clearLookupCache=true); + TravelState findPath(Unit *unit, const Vec2i &finalPos, bool *wasStuck=NULL); private: TravelState aStar(Unit *unit, const Vec2i &finalPos, bool inBailout); @@ -86,6 +83,9 @@ private: bool openPos(const Vec2i &sucPos); Node * minHeuristicFastLookup(); + + void processNode(Unit *unit, Node *node,const Vec2i finalPos, int i, int j, bool &nodeLimitReached); + void processNearestFreePos(const Vec2i &finalPos, int i, int j, int size, Field field, int teamIndex,Vec2i unitPos, Vec2i &nearestPos, float &nearestDist); }; }}//end namespace diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 547d5c4a..22a3d454 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -34,6 +34,8 @@ using namespace Shared::Graphics::Gl; using namespace Shared::Util; using namespace Shared::Graphics; +//#define ENABLE_VBO_CODE 1 + namespace Glest { namespace Game{ // ===================================================== @@ -1799,17 +1801,6 @@ void Renderer::ReleaseSurfaceVBOs() { #endif -class SurfaceData { -public: - SurfaceData(){}; - int textureHandle; - vector texCoords; - vector texCoordsSurface; - vector vertices; - vector normals; -}; - - void Renderer::renderSurface(const int renderFps) { IF_DEBUG_EDITION( if (getDebugRenderer().willRenderSurface()) { @@ -1865,90 +1856,95 @@ void Renderer::renderSurface(const int renderFps) { #if defined(ENABLE_VBO_CODE) int lastSurfaceDataIndex = -1; - std::vector surface; - //surface.reserve(qCache.visibleScaledCellList.size()); + if(mapSurfaceData.find(visibleQuad) == mapSurfaceData.end()) { + //std::vector surface; + std::vector &surface = mapSurfaceData[visibleQuad]; - for(int visibleIndex = 0; - visibleIndex < qCache.visibleScaledCellList.size(); ++visibleIndex) { - Vec2i &pos = qCache.visibleScaledCellList[visibleIndex]; + for(int visibleIndex = 0; + visibleIndex < qCache.visibleScaledCellList.size(); ++visibleIndex) { + Vec2i &pos = qCache.visibleScaledCellList[visibleIndex]; - SurfaceCell *tc00= map->getSurfaceCell(pos.x, pos.y); - SurfaceCell *tc10= map->getSurfaceCell(pos.x+1, pos.y); - SurfaceCell *tc01= map->getSurfaceCell(pos.x, pos.y+1); - SurfaceCell *tc11= map->getSurfaceCell(pos.x+1, pos.y+1); + SurfaceCell *tc00= map->getSurfaceCell(pos.x, pos.y); + SurfaceCell *tc10= map->getSurfaceCell(pos.x+1, pos.y); + SurfaceCell *tc01= map->getSurfaceCell(pos.x, pos.y+1); + SurfaceCell *tc11= map->getSurfaceCell(pos.x+1, pos.y+1); - if(tc00 == NULL) { - throw runtime_error("tc00 == NULL"); - } - if(tc10 == NULL) { - throw runtime_error("tc10 == NULL"); - } - if(tc01 == NULL) { - throw runtime_error("tc01 == NULL"); - } - if(tc11 == NULL) { - throw runtime_error("tc11 == NULL"); + if(tc00 == NULL) { + throw runtime_error("tc00 == NULL"); + } + if(tc10 == NULL) { + throw runtime_error("tc10 == NULL"); + } + if(tc01 == NULL) { + throw runtime_error("tc01 == NULL"); + } + if(tc11 == NULL) { + throw runtime_error("tc11 == NULL"); + } + + triangleCount+= 2; + pointCount+= 4; + + //set texture + if(tc00->getSurfaceTexture() == NULL) { + throw runtime_error("tc00->getSurfaceTexture() == NULL"); + } + vector &vboCache = GetSurfaceVBOs(pos); + + int surfaceDataIndex = -1; + currTex= static_cast(tc00->getSurfaceTexture())->getHandle(); + if(currTex != lastTex) { + lastTex = currTex; + } + else { + surfaceDataIndex = lastSurfaceDataIndex; + } + + if(surfaceDataIndex < 0) { + SurfaceData newData; + newData.textureHandle = currTex; + surface.push_back(newData); + + surfaceDataIndex = surface.size()-1; + + //surface[surfaceDataIndex].texCoords.reserve(100); + //surface[surfaceDataIndex].texCoordsSurface.reserve(100); + //surface[surfaceDataIndex].vertices.reserve(100); + //surface[surfaceDataIndex].normals.reserve(100); + } + + lastSurfaceDataIndex = surfaceDataIndex; + + const Vec2f &surfCoord= tc00->getSurfTexCoord(); + + //int dataIndex = surface[surfaceDataIndex].texCoords.size(); + surface[surfaceDataIndex].texCoords.push_back(tc01->getFowTexCoord()); + surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x, surfCoord.y + coordStep)); + surface[surfaceDataIndex].vertices.push_back(tc01->getVertex()); + surface[surfaceDataIndex].normals.push_back(tc01->getNormal()); + + surface[surfaceDataIndex].texCoords.push_back(tc00->getFowTexCoord()); + surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x, surfCoord.y)); + surface[surfaceDataIndex].vertices.push_back(tc00->getVertex()); + surface[surfaceDataIndex].normals.push_back(tc00->getNormal()); + + surface[surfaceDataIndex].texCoords.push_back(tc11->getFowTexCoord()); + surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x+coordStep, surfCoord.y+coordStep)); + surface[surfaceDataIndex].vertices.push_back(tc11->getVertex()); + surface[surfaceDataIndex].normals.push_back(tc11->getNormal()); + + surface[surfaceDataIndex].texCoords.push_back(tc10->getFowTexCoord()); + surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x+coordStep, surfCoord.y)); + surface[surfaceDataIndex].vertices.push_back(tc10->getVertex()); + surface[surfaceDataIndex].normals.push_back(tc10->getNormal()); } - triangleCount+= 2; - pointCount+= 4; - - //set texture - if(tc00->getSurfaceTexture() == NULL) { - throw runtime_error("tc00->getSurfaceTexture() == NULL"); - } - vector &vboCache = GetSurfaceVBOs(pos); - - int surfaceDataIndex = -1; - currTex= static_cast(tc00->getSurfaceTexture())->getHandle(); - if(currTex != lastTex) { - lastTex = currTex; - } - else { - surfaceDataIndex = lastSurfaceDataIndex; - } - - if(surfaceDataIndex < 0) { - SurfaceData newData; - newData.textureHandle = currTex; - surface.push_back(newData); - - surfaceDataIndex = surface.size()-1; - - //surface[surfaceDataIndex].texCoords.reserve(100); - //surface[surfaceDataIndex].texCoordsSurface.reserve(100); - //surface[surfaceDataIndex].vertices.reserve(100); - //surface[surfaceDataIndex].normals.reserve(100); - } - - lastSurfaceDataIndex = surfaceDataIndex; - - const Vec2f &surfCoord= tc00->getSurfTexCoord(); - - //int dataIndex = surface[surfaceDataIndex].texCoords.size(); - surface[surfaceDataIndex].texCoords.push_back(tc01->getFowTexCoord()); - surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x, surfCoord.y + coordStep)); - surface[surfaceDataIndex].vertices.push_back(tc01->getVertex()); - surface[surfaceDataIndex].normals.push_back(tc01->getNormal()); - - surface[surfaceDataIndex].texCoords.push_back(tc00->getFowTexCoord()); - surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x, surfCoord.y)); - surface[surfaceDataIndex].vertices.push_back(tc00->getVertex()); - surface[surfaceDataIndex].normals.push_back(tc00->getNormal()); - - surface[surfaceDataIndex].texCoords.push_back(tc11->getFowTexCoord()); - surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x+coordStep, surfCoord.y+coordStep)); - surface[surfaceDataIndex].vertices.push_back(tc11->getVertex()); - surface[surfaceDataIndex].normals.push_back(tc11->getNormal()); - - surface[surfaceDataIndex].texCoords.push_back(tc10->getFowTexCoord()); - surface[surfaceDataIndex].texCoordsSurface.push_back(Vec2f(surfCoord.x+coordStep, surfCoord.y)); - surface[surfaceDataIndex].vertices.push_back(tc10->getVertex()); - surface[surfaceDataIndex].normals.push_back(tc10->getNormal()); + //mapSurfaceData[visibleQuad] = surface; } - //printf("\nsurface.size() = %d vs qCache.visibleScaledCellList.size() = %d \n",surface.size(),qCache.visibleScaledCellList.size()); + std::vector &surface = mapSurfaceData[visibleQuad]; + for(int i = 0; i < surface.size(); ++i) { SurfaceData &data = surface[i]; diff --git a/source/glest_game/graphics/renderer.h b/source/glest_game/graphics/renderer.h index cab14802..504ad951 100644 --- a/source/glest_game/graphics/renderer.h +++ b/source/glest_game/graphics/renderer.h @@ -42,8 +42,6 @@ #include "leak_dumper.h" -//#define ENABLE_VBO_CODE 1 - namespace Glest{ namespace Game{ using namespace Shared::Graphics; @@ -136,7 +134,7 @@ public: std::vector visibleScaledCellList; }; -#if defined(ENABLE_VBO_CODE) +//#if defined(ENABLE_VBO_CODE) class VisibleQuadContainerVBOCache { public: // Vertex Buffer Object Names @@ -147,7 +145,7 @@ public: uint32 m_nVBONormals; // Normal VBO Name //uint32 m_nVBOIndexes; // Indexes VBO Name }; -#endif +//#endif class Renderer : public RendererInterface, public BaseRenderer, public SimpleTaskCallbackInterface { @@ -265,13 +263,25 @@ private: std::map worldToScreenPosCache; -#if defined(ENABLE_VBO_CODE) +//#if defined(ENABLE_VBO_CODE) std::map > mapSurfaceVBOCache; VisibleQuadContainerVBOCache SetupSurfaceVBO(Vec2i &pos, SurfaceCell *cell, Vec2f surfCoord); vector & GetSurfaceVBOs(Vec2i &pos); void ReleaseSurfaceVBOs(); -#endif +//#endif + + class SurfaceData { + public: + SurfaceData(){}; + int textureHandle; + vector texCoords; + vector texCoordsSurface; + vector vertices; + vector normals; + }; + + std::map > mapSurfaceData; private: Renderer(); diff --git a/source/shared_lib/include/graphics/math_util.h b/source/shared_lib/include/graphics/math_util.h index 0df72935..ded57a3b 100644 --- a/source/shared_lib/include/graphics/math_util.h +++ b/source/shared_lib/include/graphics/math_util.h @@ -86,6 +86,16 @@ public: } } } + + std::string getString() const { + std::ostringstream streamOut; + streamOut << "#1: " << this->p[0].getString(); + streamOut << "#2: " << this->p[1].getString(); + std::string result = streamOut.str(); + streamOut.str(std::string()); + return result; + } + }; typedef Rect2 Rect2i;