From 08b2e27ed9a4831e94860fa950da86d83a08e22b Mon Sep 17 00:00:00 2001 From: Benau Date: Sat, 19 Dec 2015 16:23:18 +0800 Subject: [PATCH] More clean up --- src/tracks/battle_graph.cpp | 1 - src/tracks/battle_graph.hpp | 29 +++++++++---- src/tracks/graph_structure.cpp | 79 ++++++++++------------------------ src/tracks/graph_structure.hpp | 39 ++++++++--------- src/tracks/quad_graph.cpp | 1 - src/tracks/quad_graph.hpp | 24 ++++++++--- 6 files changed, 78 insertions(+), 95 deletions(-) diff --git a/src/tracks/battle_graph.cpp b/src/tracks/battle_graph.cpp index 06241fd0f..10d737fd7 100644 --- a/src/tracks/battle_graph.cpp +++ b/src/tracks/battle_graph.cpp @@ -40,7 +40,6 @@ BattleGraph::BattleGraph(const std::string &navmesh_file_name) NavMesh::create(navmesh_file_name); m_navmesh_file = navmesh_file_name; buildGraph(NavMesh::get()); - setType(); computeFloydWarshall(); } // BattleGraph diff --git a/src/tracks/battle_graph.hpp b/src/tracks/battle_graph.hpp index 9611ad102..286a08a06 100644 --- a/src/tracks/battle_graph.hpp +++ b/src/tracks/battle_graph.hpp @@ -63,6 +63,23 @@ private: BattleGraph(const std::string &navmesh_file_name); ~BattleGraph(void); + // ------------------------------------------------------------------------ + virtual void set3DVerticesOfGraph(int i, video::S3DVertex *v, + const video::SColor &color) const + { NavMesh::get()->setVertices(i, v, color); } + // ------------------------------------------------------------------------ + virtual void getGraphBoundingBox(Vec3 *min, Vec3 *max) const + { NavMesh::get()->getBoundingBox(min, max); } + // ------------------------------------------------------------------------ + virtual const bool isNodeInvisible(int n) const + { return false; } + // ------------------------------------------------------------------------ + virtual const bool isNodeInvalid(int n) const + { return (NavMesh::get()->getNavPoly(n).getVerticesIndex()).size()!=4; } + // ------------------------------------------------------------------------ + virtual const bool hasLapLine() const + { return false; } + public: static const int UNKNOWN_POLY; @@ -92,12 +109,12 @@ public: /** Returns the number of nodes in the BattleGraph (equal to the number of * polygons in the NavMesh */ virtual const unsigned int getNumNodes() const - { return m_distance_matrix.size(); } + { return m_distance_matrix.size(); } // ---------------------------------------------------------------------- /** Returns the NavPoly corresponding to the i-th node of the BattleGraph */ const NavPoly& getPolyOfNode(int i) const - { return NavMesh::get()->getNavPoly(i); } + { return NavMesh::get()->getNavPoly(i); } // ---------------------------------------------------------------------- /** Returns the next polygon on the shortest path from i to j. @@ -106,15 +123,9 @@ public: const int & getNextShortestPathPoly(int i, int j) const; const std::vector < std::pair >& getItemList() - { return m_items_on_graph; } + { return m_items_on_graph; } void findItemsOnGraphNodes(); - // ------------------------------------------------------------------------ - /** Sets the type of this graph. */ - virtual void setType() { m_graph_type = GraphType::GT_BATTLE; } - // ------------------------------------------------------------------------ - virtual const std::vector getAllNodes() const - {return std::vector();} }; //BattleGraph #endif diff --git a/src/tracks/graph_structure.cpp b/src/tracks/graph_structure.cpp index 7379838ed..523ca0021 100644 --- a/src/tracks/graph_structure.cpp +++ b/src/tracks/graph_structure.cpp @@ -22,21 +22,17 @@ #include #include -#include "config/user_config.hpp" #include "graphics/irr_driver.hpp" #include "graphics/glwrap.hpp" #include "graphics/shaders.hpp" #include "graphics/rtts.hpp" #include "modes/world.hpp" -#include "tracks/quad_set.hpp" -#include "tracks/navmesh.hpp" #include "utils/log.hpp" // ----------------------------------------------------------------------------- GraphStructure::GraphStructure() { - m_graph_type = GT_RACE; m_min_coord = 0; m_scaling = 0; m_node = NULL; @@ -101,8 +97,7 @@ void GraphStructure::createDebugMesh() /** Creates the actual mesh that is used by createDebugMesh() or makeMiniMap() */ void GraphStructure::createMesh(bool show_invisible, bool enable_transparency, - const video::SColor *track_color, - const video::SColor *lap_color) + const video::SColor *track_color) { // The debug track will not be lighted or culled. video::SMaterial m; @@ -115,21 +110,15 @@ void GraphStructure::createMesh(bool show_invisible, m_mesh = irr_driver->createQuadMesh(&m); m_mesh_buffer = m_mesh->getMeshBuffer(0); assert(m_mesh_buffer->getVertexType()==video::EVT_STANDARD); - const std::vector all_nodes = getAllNodes(); - unsigned int n; - if (m_graph_type == GT_RACE) + unsigned int n = 0; + // Count the number of quads to display (some quads might be invisible + + for (unsigned int i = 0; i < getNumNodes(); i++) { - // Count the number of quads to display (some quads might be invisible - n = 0; - for (unsigned int i = 0; i < getNumNodes(); i++) - { - if (show_invisible || !all_nodes[i]->getQuad().isInvisible()) - n++; - } + if (show_invisible || !isNodeInvisible(i)) + n++; } - else - n = getNumNodes(); // Four vertices for each of the n-1 remaining quads video::S3DVertex *new_v = new video::S3DVertex[4*n]; @@ -146,16 +135,12 @@ void GraphStructure::createMesh(bool show_invisible, for (unsigned int count = 0; count < getNumNodes(); count++) { // Ignore invisible quads - if (m_graph_type == GT_RACE) + if (!show_invisible && isNodeInvisible(count)) + continue; + else if (isNodeInvalid(count)) { - if (!show_invisible && all_nodes[count]->getQuad().isInvisible()) - continue; - } - else if ((NavMesh::get()->getNavPoly(count).getVerticesIndex()).size() !=4 && - m_graph_type == GT_BATTLE) - { - // There should not be a poly which isn't made of 4 vertices - Log::warn("Battle Graph", "There is an invalid poly!"); + // There should not be a node which isn't made of 4 vertices + Log::warn("Graph Structure", "There is an invalid node!"); continue; } @@ -167,10 +152,7 @@ void GraphStructure::createMesh(bool show_invisible, } // Transfer the 4 points of the current quad to the list of vertices - if (m_graph_type == GT_RACE) - all_nodes[count]->getQuad().getVertices(new_v+4*i, c); - else - NavMesh::get()->setVertices(count, new_v+4*i, c); + set3DVerticesOfGraph(count, new_v+4*i, c); // Set up the indices for the triangles // (note, afaik with opengl we could use quads directly, but the code @@ -186,17 +168,17 @@ void GraphStructure::createMesh(bool show_invisible, m_mesh_buffer->append(new_v, n*4, ind, n*6); - if (lap_color && m_graph_type == GT_RACE) + if (hasLapLine()) { video::S3DVertex lap_v[4]; irr::u16 lap_ind[6]; - video::SColor c(128, 255, 0, 0); - all_nodes[0]->getQuad().getVertices(lap_v, *lap_color); + video::SColor lap_color(128, 255, 0, 0); + set3DVerticesOfGraph(0, lap_v, lap_color); // Now scale the length (distance between vertix 0 and 3 // and between 1 and 2) to be 'length': Vec3 bb_min, bb_max; - QuadSet::get()->getBoundingBox(&bb_min, &bb_max); + getGraphBoundingBox(&bb_min, &bb_max); // Length of the lap line about 3% of the 'height' // of the track. const float length = (bb_max.getZ()-bb_min.getZ())*0.03f; @@ -278,21 +260,9 @@ void GraphStructure::makeMiniMap(const core::dimension2du &dimension, irr_driver->getSceneManager()->setAmbientLight(video::SColor(255, 255, 255, 255)); - if (m_graph_type == GT_RACE) - { - video::SColor red(128, 255, 0, 0); - createMesh(/*show_invisible part of the track*/ false, - /*enable_transparency*/ false, - /*track_color*/ &fill_color, - /*lap line color*/ &red); - } - else - { - createMesh(/*show_invisible part of the track*/ false, - /*enable_transparency*/ false, - /*track_color*/ &fill_color, - /*lap line color*/ NULL); - } + createMesh(/*show_invisible part of the track*/ false, + /*enable_transparency*/ false, + /*track_color*/ &fill_color); m_node = irr_driver->addMesh(m_mesh, "mini_map"); #ifdef DEBUG @@ -306,10 +276,7 @@ void GraphStructure::makeMiniMap(const core::dimension2du &dimension, // --------------- scene::ICameraSceneNode *camera = irr_driver->addCameraSceneNode(); Vec3 bb_min, bb_max; - if (m_graph_type == GT_RACE) - QuadSet::get()->getBoundingBox(&bb_min, &bb_max); - else - NavMesh::get()->getBoundingBox(&bb_min, &bb_max); + getGraphBoundingBox(&bb_min, &bb_max); Vec3 center = (bb_max+bb_min)*0.5f; float dx = bb_max.getX()-bb_min.getX(); @@ -384,8 +351,8 @@ void GraphStructure::makeMiniMap(const core::dimension2du &dimension, if (texture == NULL && frame_buffer == NULL) { - Log::error("BattleGraph", "[makeMiniMap] WARNING: RTT does not appear to work," - "mini-map will not be available."); + Log::error("Graph Structure", "[makeMiniMap] WARNING: RTT does not" + "appear to work, mini-map will not be available."); } *oldRttMinimap = texture; diff --git a/src/tracks/graph_structure.hpp b/src/tracks/graph_structure.hpp index b99448a66..665f510d7 100644 --- a/src/tracks/graph_structure.hpp +++ b/src/tracks/graph_structure.hpp @@ -23,14 +23,13 @@ #include #include -#include "tracks/graph_node.hpp" #include "utils/vec3.hpp" #include "utils/no_copy.hpp" namespace irr { namespace scene { class ISceneNode; class IMesh; class IMeshBuffer; } - namespace video { class ITexture; } + namespace video { class ITexture; struct S3DVertex; } } using namespace irr; @@ -38,25 +37,17 @@ class FrameBuffer; class RTT; /** - * \brief Virtual base class for a graph structure. + * \brief Virtual base class for a graph structure. + * This is mainly used for drawing minimap in game. * - * A graph structure has a certain type: - * GT_RACE : Graph used by a lap race. - * GT_BATTLE: Graph used by a battle arena. - * - * \ingroup tracks + * \ingroup tracks */ class GraphStructure : public NoCopy { -public: - enum GraphType {GT_RACE, GT_BATTLE}; - protected: - /** The type of this graph. */ - GraphType m_graph_type; - void cleanupDebugMesh(); - void destroyRTT(); + void cleanupDebugMesh(); + void destroyRTT(); private: RTT* m_new_rtt; @@ -76,10 +67,16 @@ private: /** Scaling for mini map. */ float m_scaling; - void createMesh(bool show_invisible=true, - bool enable_transparency=false, - const video::SColor *track_color=NULL, - const video::SColor *lap_color=NULL); + void createMesh(bool show_invisible=true, + bool enable_transparency=false, + const video::SColor *track_color=NULL); + + virtual void set3DVerticesOfGraph(int i, video::S3DVertex *v, + const video::SColor &color) const = 0; + virtual void getGraphBoundingBox(Vec3 *min, Vec3 *max) const = 0; + virtual const bool isNodeInvisible(int n) const = 0; + virtual const bool isNodeInvalid(int n) const = 0; + virtual const bool hasLapLine() const = 0; public: GraphStructure(); @@ -91,9 +88,7 @@ public: video::ITexture** oldRttMinimap, FrameBuffer** newRttMinimap); void mapPoint2MiniMap(const Vec3 &xyz, Vec3 *out) const; - virtual const unsigned int getNumNodes() const = 0; - virtual const std::vector getAllNodes() const = 0; - virtual void setType() = 0; + virtual const unsigned int getNumNodes() const = 0; }; // GraphStructure #endif diff --git a/src/tracks/quad_graph.cpp b/src/tracks/quad_graph.cpp index 579f484a4..4a05842c7 100644 --- a/src/tracks/quad_graph.cpp +++ b/src/tracks/quad_graph.cpp @@ -57,7 +57,6 @@ QuadGraph::QuadGraph(const std::string &quad_file_name, m_quad_filename = quad_file_name; m_quad_graph = this; load(graph_file_name); - setType(); } // QuadGraph // ----------------------------------------------------------------------------- diff --git a/src/tracks/quad_graph.hpp b/src/tracks/quad_graph.hpp index b65b64611..e2688e4af 100644 --- a/src/tracks/quad_graph.hpp +++ b/src/tracks/quad_graph.hpp @@ -73,6 +73,24 @@ private: const std::string &graph_file_name, const bool reverse); ~QuadGraph (); + + // ------------------------------------------------------------------------ + virtual void set3DVerticesOfGraph(int i, video::S3DVertex *v, + const video::SColor &color) const + { m_all_nodes[i]->getQuad().getVertices(v, color); } + // ------------------------------------------------------------------------ + virtual void getGraphBoundingBox(Vec3 *min, Vec3 *max) const + { QuadSet::get()->getBoundingBox(min, max); } + // ------------------------------------------------------------------------ + virtual const bool isNodeInvisible(int n) const + { return m_all_nodes[n]->getQuad().isInvisible(); } + // ------------------------------------------------------------------------ + virtual const bool isNodeInvalid(int n) const + { return false; } + // ------------------------------------------------------------------------ + virtual const bool hasLapLine() const + { return true; } + public: static const int UNKNOWN_SECTOR; @@ -160,12 +178,6 @@ public: float getLapLength() const {return m_lap_length; } // ------------------------------------------------------------------------ bool isReverse() const {return m_reverse; } - // ------------------------------------------------------------------------ - /** Sets the type of this graph. */ - virtual void setType() { m_graph_type = GraphType::GT_RACE; } - // ------------------------------------------------------------------------ - virtual const std::vector getAllNodes() const - { return m_all_nodes; } }; // QuadGraph #endif