Mercator

Terrain.h

00001 // This file may be redistributed and modified only under the terms of
00002 // the GNU General Public License (See COPYING for details).
00003 // Copyright (C) 2003 Alistair Riddoch, Damien McGinnes
00004 
00005 #ifndef MERCATOR_TERRAIN_H
00006 #define MERCATOR_TERRAIN_H
00007 
00008 #include <Mercator/Mercator.h>
00009 #include <Mercator/BasePoint.h>
00010 
00011 #include <wfmath/vector.h>
00012 #include <wfmath/axisbox.h>
00013 
00014 #include <map>
00015 #include <set>
00016 #include <list>
00017 #include <cmath>
00018 
00019 namespace Mercator {
00020 
00021 class Segment;
00022 class Shader;
00023 class TerrainMod;
00024 class Area;
00025 
00034 class Terrain {
00035   public:
00037     typedef WFMath::AxisBox<2> Rect;
00038 
00040     typedef std::map<int, BasePoint> Pointcolumn;
00042     typedef std::map<int, Segment *> Segmentcolumn;
00043 
00045     typedef std::map<int, Pointcolumn > Pointstore;
00047     typedef std::map<int, Segmentcolumn > Segmentstore;
00048 
00050     typedef std::map<int, Shader *> Shaderstore;
00051 
00053     typedef std::map<Area *, Rect> Areastore;
00054 
00056     typedef std::map<TerrainMod *, Rect> TerrainModstore;
00057 
00059     static const unsigned int DEFAULT = 0x0000;
00061     static const unsigned int SHADED = 0x0001;
00062     // More options go here as bit flags, and below should be a private
00063     // test function
00064   private:
00066     const unsigned int m_options;
00068     const int m_res;
00069 
00071     Pointstore m_basePoints;
00073     Segmentstore m_segments;
00075     Shaderstore m_shaders;
00076   
00078     Areastore m_areas;
00079 
00081     TerrainModstore m_mods;
00082   
00083     void addSurfaces(Segment &);
00084     void shadeSurfaces(Segment &);
00085 
00089     bool isShaded() const {
00090         return ((m_options & SHADED) == SHADED);
00091     }
00092   public:
00094     static const float defaultLevel;
00095 
00096     explicit Terrain(unsigned int options = DEFAULT,
00097                      unsigned int resolution = defaultResolution);
00098     ~Terrain();
00099 
00100     float get(float x, float y) const;
00101     bool getHeightAndNormal(float x, float y, float&, WFMath::Vector<3>&) const;
00102 
00103     bool getBasePoint(int x, int y, BasePoint& z) const;
00104     void setBasePoint(int x, int y, const BasePoint& z);
00105 
00107     void setBasePoint(int x, int y, float z) {
00108         BasePoint bp(z);
00109         setBasePoint(x, y, bp);
00110     }
00111 
00116     Segment * getSegment(float x, float y) const {
00117         int ix = (int)floor(x / m_res);
00118         int iy = (int)floor(y / m_res);
00119         return getSegment(ix, iy);
00120     }
00121 
00122     Segment * getSegment(int x, int y) const;
00123 
00125     const int getResolution() const {
00126         return m_res;
00127     }
00128 
00130     const Segmentstore & getTerrain() const {
00131         return m_segments;
00132     }
00133 
00135     const Pointstore & getPoints() const {
00136         return m_basePoints;
00137     }
00138     
00140     const Shaderstore & getShaders() const {
00141         return m_shaders;
00142     }
00143 
00145     void addShader(Shader * t, int id);
00146     void removeShader(Shader * t, int id);
00147     
00148     TerrainMod * addMod(const TerrainMod &t);
00149     void updateMod(TerrainMod * mod);
00150     void removeMod(TerrainMod * mod);
00151     
00152     void addArea(Area* a);
00153     void updateArea(Area* a);
00154     void removeArea(Area* a);
00155 };
00156 
00157 } // namespace Mercator
00158 
00159 #endif // MERCATOR_TERRAIN_H