Cafu Engine
1 /*
2 Cafu Engine,
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
10 #include "MapPrimitive.hpp"
11 #include "MaterialSystem/Mesh.hpp"
12 #include "Terrain/Terrain.hpp"
15 class EditorMaterialI;
16 class EditorMatManT;
19 /// The TerrainT class represents a terrain in a map.
20 /// A terrain consists of height data and a material that is used to render the terrain.
21 /// The dimensions of the terrain are described by a bounding box and all tranformations are performed on this bounding box.
22 class MapTerrainT : public MapPrimitiveT
23 {
24  public:
26  /// Constructor.
27  /// 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
28  /// these parameters later on, so you can create an "empty" terrain here and fill it later.
29  /// @param Box The initial bounds of the terrain.
30  /// @param HeightMapFile The heightmap data from which this terrain should be initialized.
31  /// @param Material The material to render this terrain with.
32  MapTerrainT(const BoundingBox3fT& Box=BoundingBox3fT(Vector3fT()), const wxString& HeightMapFile="", EditorMaterialI* Material=NULL);
34  /// The copy constructor for copying a terrain.
35  /// @param Terrain The terrain to copy-construct this terrain from.
36  MapTerrainT(const MapTerrainT& Terrain);
39  // Implementations and overrides for base class methods.
40  MapTerrainT* Clone() const override;
43  /// Sets the bounds of the terrain.
44  /// @param Bounds The new bounds of the terrain.
45  void SetTerrainBounds(const BoundingBox3fT& Bounds);
47  /// Sets the terrain material.
48  /// @param Material New terrain material.
49  void SetMaterial(EditorMaterialI* Material) { m_Material=Material; }
51  /// Gets the terrains current material.
52  /// @return Current terrain material.
53  EditorMaterialI* GetMaterial() const { return m_Material; }
55  /// Gets the terrains heigth data resolution side length.
56  /// @return Height data side length.
57  unsigned long GetResolution() const { return m_Resolution; }
59  /// Gets a constant reference to the terrains height data.
60  /// @return The terrains height data.
61  const ArrayT<unsigned short>& GetHeightData() const { return m_HeightData; }
63  /// Initializes the terrains height data from a file.
64  /// Warning: This will overwrite any data that the terrain currently has.
65  /// @param FileName The file from which the height data should be loaded.
66  void LoadHeightData(const wxString& FileName);
68  /// Traces the given ray and returns the position in the terrains height data when a hit with the terrain has occured.
69  /// @param Source The point from were the trace is emanating.
70  /// @param Direction The direction into which the ray is cast.
71  /// @return Point of intersection in the height map or -1,-1 if ray doesn't intersect with terrain.
72  wxPoint TraceRay(const Vector3fT& Source, const Vector3fT& Direction) const;
74  /// Sets the bounds if the terrain edit tool.
75  /// The tools bounds are calculated from the given parameters.
76  /// @param PosX The x position of the tool in the terrains height data.
77  /// @param PosY The y position of the tool in the terrains height data.
78  /// @param Radius The tools radius.
79  void SetToolBounds(int PosX, int PosY, int Radius);
82  // MapElementT implementation.
83  BoundingBox3fT GetBB() const;
85  void Render2D(Renderer2DT& Renderer) const;
86  void Render3D(Renderer3DT& Renderer) const;
88  bool TraceRay(const Vector3fT& RayOrigin, const Vector3fT& RayDir, float& Fraction, unsigned long& FaceNr) const;
89  bool TracePixel(const wxPoint& Pixel, int Radius, const ViewWindow2DT& ViewWin) const;
91  // Implement the MapElementT transformation methods.
92  TrafoMementoT* GetTrafoState() const override;
93  void RestoreTrafoState(const TrafoMementoT* TM) override;
94  void TrafoMove(const Vector3fT& Delta, bool LockTexCoords) override;
95  void TrafoRotate(const Vector3fT& RefPoint, const cf::math::AnglesfT& Angles, bool LockTexCoords) override;
96  void TrafoScale(const Vector3fT& RefPoint, const Vector3fT& Scale, bool LockTexCoords) override;
97  void TrafoMirror(unsigned int NormalAxis, float Dist, bool LockTexCoords) override;
98  void Transform(const Matrix4x4fT& Matrix, bool LockTexCoords) override;
100  wxString GetDescription() const { return "Terrain"; }
102  void Load_cmap(TextParserT& TP, MapDocumentT& MapDoc, bool IgnoreGroups) override;
103  void Save_cmap(std::ostream& OutFile, unsigned long TerrainNr, const MapDocumentT& MapDoc) const;
105  // The TypeSys related declarations for this class.
106  virtual const cf::TypeSys::TypeInfoT* GetType() const { return &TypeInfo; }
107  static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params);
108  static const cf::TypeSys::TypeInfoT TypeInfo;
111  private:
113  friend class CommandModifyTerrainT; // Changes to a terrain are commited by commands that need friend access.
114  friend class CommandChangeTerrainResT;
115  friend class ToolTerrainEditorT; // The tool needs friend access to the terrain bounds when checking a 3D hit position.
117  /// Returns the terrain object of this map terrain and updates it if necessary.
118  /// @return Terrain object of map terrain.
119  const TerrainT& GetTerrain() const;
122  unsigned long m_Resolution; ///< The resolution (side length) of the terrains height data.
123  ArrayT<unsigned short> m_HeightData; ///< Height data of this terrain.
124  BoundingBox3fT m_TerrainBounds; ///< The bounding-box that describes the spatial dimensions of the terrain.
125  EditorMaterialI* m_Material; ///< The material applied to this terrain.
127  BoundingBox3fT m_ToolBounds; ///< Bounding box of the editor tool being rendered onto the terrain.
128  bool m_RenderEyeDropper; ///< Whether to render the editor tool or the eyedropper tool.
130  mutable bool m_NeedsUpdate; ///< If the m_Terrain member needs to be updated.
131  mutable TerrainT m_Terrain; ///< Our terrain object.
132  mutable MatSys::MeshT m_TerrainMesh; ///< The renderable terrain mesh.
133 };
135 #endif
void RestoreTrafoState(const TrafoMementoT *TM) override
Restores the transform-related state of this element from the given memento.
Definition: MapTerrain.cpp:584
void SetMaterial(EditorMaterialI *Material)
Sets the terrain material.
Definition: MapTerrain.hpp:49
This class provides auxiliary means for rendering a 3D view.
Definition: Renderer3D.hpp:30
This class implements the rendering into a 2D view.
Definition: Renderer2D.hpp:22
This class represents terrains, offering methods for LoD rendering and collision detection.
Definition: Terrain.hpp:17
This class represents a CaWE "map" document.
Definition: MapDocument.hpp:45
const ArrayT< unsigned short > & GetHeightData() const
Gets a constant reference to the terrains height data.
Definition: MapTerrain.hpp:61
void TrafoRotate(const Vector3fT &RefPoint, const cf::math::AnglesfT &Angles, bool LockTexCoords) override
Rotates this element about the given reference point (in world-space).
Definition: MapTerrain.cpp:607
This class adds no functionality of its own, but only exists for proper type separation.
Definition: MapPrimitive.hpp:21
wxPoint TraceRay(const Vector3fT &Source, const Vector3fT &Direction) const
Traces the given ray and returns the position in the terrains height data when a hit with the terrain...
Definition: MapTerrain.cpp:522
This class manages the editor materials for a game configuration.
Definition: EditorMaterialManager.hpp:20
void TrafoScale(const Vector3fT &RefPoint, const Vector3fT &Scale, bool LockTexCoords) override
Scales this element about the given reference point (in world-space).
Definition: MapTerrain.cpp:640
void Transform(const Matrix4x4fT &Matrix, bool LockTexCoords) override
Why does this method not replace all the other Trafo*() methods? This method is the most generic...
Definition: MapTerrain.cpp:679
Definition: ModifyTerrain.hpp:17
An instance of this class encapsulates the transform-related state of a MapElementT.
Definition: MapElement.hpp:39
EditorMaterialI * GetMaterial() const
Gets the terrains current material.
Definition: MapTerrain.hpp:53
Definition: ToolTerrainEdit.hpp:27
void TrafoMove(const Vector3fT &Delta, bool LockTexCoords) override
Translates this element by the given vector (in world-space).
Definition: MapTerrain.cpp:596
MapTerrainT(const BoundingBox3fT &Box=BoundingBox3fT(Vector3fT()), const wxString &HeightMapFile="", EditorMaterialI *Material=NULL)
Definition: MapTerrain.cpp:36
bool TracePixel(const wxPoint &Pixel, int Radius, const ViewWindow2DT &ViewWin) const
This method determines if this map element is intersected/affected by the specified disc in ViewWin...
Definition: MapTerrain.cpp:247
The TerrainT class represents a terrain in a map.
Definition: MapTerrain.hpp:22
MapTerrainT * Clone() const override
The virtual copy constructor.
Definition: MapTerrain.cpp:80
void TrafoMirror(unsigned int NormalAxis, float Dist, bool LockTexCoords) override
Mirrors this element along the given mirror plane (in world-space).
Definition: MapTerrain.cpp:656
Definition: EditorMaterial.hpp:21
Definition: ChangeTerrainRes.hpp:19
unsigned long GetResolution() const
Gets the terrains heigth data resolution side length.
Definition: MapTerrain.hpp:57
void LoadHeightData(const wxString &FileName)
Initializes the terrains height data from a file.
Definition: MapTerrain.cpp:501
Definition: ChildFrameViewWin2D.hpp:24
BoundingBox3fT GetBB() const
Returns the spatial bounding-box of this map element.
Definition: MapTerrain.cpp:93
void SetTerrainBounds(const BoundingBox3fT &Bounds)
Sets the bounds of the terrain.
Definition: MapTerrain.cpp:86
TrafoMementoT * GetTrafoState() const override
Returns a memento that encapsulates the transform-related state of this element.
Definition: MapTerrain.cpp:578
void SetToolBounds(int PosX, int PosY, int Radius)
Sets the bounds if the terrain edit tool.
Definition: MapTerrain.cpp:262
Definition: TypeSys.hpp:52
This class keeps type information (about an entity class that occurs in the game).
Definition: TypeSys.hpp:79
This class represents a polygonal mesh.
Definition: Mesh.hpp:45
This is a class for parsing text.
Definition: TextParser.hpp:21