| 1 | /* |
|---|
| 2 | ================================================================================= |
|---|
| 3 | This file is part of Cafu, the open-source game engine and graphics engine |
|---|
| 4 | for multiplayer, cross-platform, real-time 3D action. |
|---|
| 5 | Copyright (C) 2002-2012 Carsten Fuchs Software. |
|---|
| 6 | |
|---|
| 7 | Cafu is free software: you can redistribute it and/or modify it under the terms |
|---|
| 8 | of the GNU General Public License as published by the Free Software Foundation, |
|---|
| 9 | either version 3 of the License, or (at your option) any later version. |
|---|
| 10 | |
|---|
| 11 | Cafu is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
|---|
| 12 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|---|
| 13 | PURPOSE. See the GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with Cafu. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 17 | |
|---|
| 18 | For support and more information about Cafu, visit us at <http://www.cafu.de>. |
|---|
| 19 | ================================================================================= |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | #ifndef CAFU_MAP_TERRAIN_HPP_INCLUDED |
|---|
| 23 | #define CAFU_MAP_TERRAIN_HPP_INCLUDED |
|---|
| 24 | |
|---|
| 25 | #include "MapPrimitive.hpp" |
|---|
| 26 | #include "MaterialSystem/Mesh.hpp" |
|---|
| 27 | #include "Terrain/Terrain.hpp" |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | class EditorMaterialI; |
|---|
| 31 | class EditorMatManT; |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | /// The TerrainT class represents a terrain in a map. |
|---|
| 35 | /// A terrain consists of height data and a material that is used to render the terrain. |
|---|
| 36 | /// The dimensions of the terrain are described by a bounding box and all tranformations are performed on this bounding box. |
|---|
| 37 | class MapTerrainT : public MapPrimitiveT |
|---|
| 38 | { |
|---|
| 39 | public: |
|---|
| 40 | |
|---|
| 41 | /// Constructor. |
|---|
| 42 | /// If no parameters are passed, an even terrain with dummy material and no size is created at 0,0,0. It is possible to adjust all |
|---|
| 43 | /// these parameters later on, so you can create an "empty" terrain here and fill it later. |
|---|
| 44 | /// @param Box The initial bounds of the terrain. |
|---|
| 45 | /// @param HeightMapFile The heightmap data from which this terrain should be initialized. |
|---|
| 46 | /// @param Material The material to render this terrain with. |
|---|
| 47 | MapTerrainT(const BoundingBox3fT& Box=BoundingBox3fT(Vector3fT()), const wxString& HeightMapFile="", EditorMaterialI* Material=NULL); |
|---|
| 48 | |
|---|
| 49 | /// The copy constructor for copying a terrain. |
|---|
| 50 | /// @param Terrain The terrain to copy-construct this terrain from. |
|---|
| 51 | MapTerrainT(const MapTerrainT& Terrain); |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | // Implementations and overrides for base class methods. |
|---|
| 55 | MapTerrainT* Clone() const; |
|---|
| 56 | void Assign(const MapElementT* Elem); |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | /// Sets the bounds of the terrain. |
|---|
| 60 | /// @param Bounds The new bounds of the terrain. |
|---|
| 61 | void SetTerrainBounds(const BoundingBox3fT& Bounds); |
|---|
| 62 | |
|---|
| 63 | /// Sets the terrain material. |
|---|
| 64 | /// @param Material New terrain material. |
|---|
| 65 | void SetMaterial(EditorMaterialI* Material) { m_Material=Material; } |
|---|
| 66 | |
|---|
| 67 | /// Gets the terrains current material. |
|---|
| 68 | /// @return Current terrain material. |
|---|
| 69 | EditorMaterialI* GetMaterial() const { return m_Material; } |
|---|
| 70 | |
|---|
| 71 | /// Gets the terrains heigth data resolution side length. |
|---|
| 72 | /// @return Height data side length. |
|---|
| 73 | unsigned long GetResolution() const { return m_Resolution; } |
|---|
| 74 | |
|---|
| 75 | /// Gets a constant reference to the terrains height data. |
|---|
| 76 | /// @return The terrains height data. |
|---|
| 77 | const ArrayT<unsigned short>& GetHeightData() const { return m_HeightData; } |
|---|
| 78 | |
|---|
| 79 | /// Initializes the terrains height data from a file. |
|---|
| 80 | /// Warning: This will overwrite any data that the terrain currently has. |
|---|
| 81 | /// @param FileName The file from which the height data should be loaded. |
|---|
| 82 | void LoadHeightData(const wxString& FileName); |
|---|
| 83 | |
|---|
| 84 | /// Traces the given ray and returns the position in the terrains height data when a hit with the terrain has occured. |
|---|
| 85 | /// @param Source The point from were the trace is emanating. |
|---|
| 86 | /// @param Direction The direction into which the ray is cast. |
|---|
| 87 | /// @return Point of intersection in the height map or -1,-1 if ray doesn't intersect with terrain. |
|---|
| 88 | wxPoint TraceRay(const Vector3fT& Source, const Vector3fT& Direction) const; |
|---|
| 89 | |
|---|
| 90 | /// Sets the bounds if the terrain edit tool. |
|---|
| 91 | /// The tools bounds are calculated from the given parameters. |
|---|
| 92 | /// @param PosX The x position of the tool in the terrains height data. |
|---|
| 93 | /// @param PosY The y position of the tool in the terrains height data. |
|---|
| 94 | /// @param Radius The tools radius. |
|---|
| 95 | void SetToolBounds(int PosX, int PosY, int Radius); |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | // MapElementT implementation. |
|---|
| 99 | BoundingBox3fT GetBB() const; |
|---|
| 100 | |
|---|
| 101 | void Render2D(Renderer2DT& Renderer) const; |
|---|
| 102 | void Render3D(Renderer3DT& Renderer) const; |
|---|
| 103 | |
|---|
| 104 | bool TraceRay(const Vector3fT& RayOrigin, const Vector3fT& RayDir, float& Fraction, unsigned long& FaceNr) const; |
|---|
| 105 | bool TracePixel(const wxPoint& Pixel, int Radius, const ViewWindow2DT& ViewWin) const; |
|---|
| 106 | |
|---|
| 107 | // Implement the MapElementT transformation methods. |
|---|
| 108 | void TrafoMove(const Vector3fT& Delta); |
|---|
| 109 | void TrafoRotate(const Vector3fT& RefPoint, const cf::math::AnglesfT& Angles); |
|---|
| 110 | void TrafoScale(const Vector3fT& RefPoint, const Vector3fT& Scale); |
|---|
| 111 | void TrafoMirror(unsigned int NormalAxis, float Dist); |
|---|
| 112 | void Transform(const MatrixT& Matrix); |
|---|
| 113 | |
|---|
| 114 | wxString GetDescription() const { return "Terrain"; } |
|---|
| 115 | |
|---|
| 116 | void Load_cmap(TextParserT& TP, MapDocumentT& MapDoc); |
|---|
| 117 | void Save_cmap(std::ostream& OutFile, unsigned long TerrainNr, const MapDocumentT& MapDoc) const; |
|---|
| 118 | |
|---|
| 119 | // The TypeSys related declarations for this class. |
|---|
| 120 | virtual const cf::TypeSys::TypeInfoT* GetType() const { return &TypeInfo; } |
|---|
| 121 | static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params); |
|---|
| 122 | static const cf::TypeSys::TypeInfoT TypeInfo; |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | private: |
|---|
| 126 | |
|---|
| 127 | friend class CommandModifyTerrainT; // Changes to a terrain are commited by commands that need friend access. |
|---|
| 128 | friend class CommandChangeTerrainResT; |
|---|
| 129 | friend class ToolTerrainEditorT; // The tool needs friend access to the terrain bounds when checking a 3D hit position. |
|---|
| 130 | |
|---|
| 131 | /// Returns the terrain object of this map terrain and updates it if necessary. |
|---|
| 132 | /// @return Terrain object of map terrain. |
|---|
| 133 | const TerrainT& GetTerrain() const; |
|---|
| 134 | |
|---|
| 135 | |
|---|
| 136 | unsigned long m_Resolution; ///< The resolution (side length) of the terrains height data. |
|---|
| 137 | ArrayT<unsigned short> m_HeightData; ///< Height data of this terrain. |
|---|
| 138 | BoundingBox3fT m_TerrainBounds; ///< The terrains bounds (in contrary to the bounding box of the terrain and its children!). |
|---|
| 139 | EditorMaterialI* m_Material; ///< The material applied to this terrain. |
|---|
| 140 | |
|---|
| 141 | BoundingBox3fT m_ToolBounds; ///< Bounding box of the editor tool being rendered onto the terrain. |
|---|
| 142 | bool m_RenderEyeDropper; ///< Whether to render the editor tool or the eyedropper tool. |
|---|
| 143 | |
|---|
| 144 | mutable bool m_NeedsUpdate; ///< If the m_Terrain member needs to be updated. |
|---|
| 145 | mutable TerrainT m_Terrain; ///< Our terrain object. |
|---|
| 146 | mutable MatSys::MeshT m_TerrainMesh; ///< The renderable terrain mesh. |
|---|
| 147 | }; |
|---|
| 148 | |
|---|
| 149 | #endif |
|---|