Cafu Engine
MapBrush.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
4 This project is licensed under the terms of the MIT license.
5 */
6 
7 #ifndef CAFU_MAP_BRUSH_HPP_INCLUDED
8 #define CAFU_MAP_BRUSH_HPP_INCLUDED
9 
10 #include "MapPrimitive.hpp"
11 #include "MapFace.hpp"
12 #include "Templates/Array.hpp"
13 
14 
15 class MapBrushT : public MapPrimitiveT
16 {
17  public:
18 
19  /// Constructor for creating a MapBrushT from the convex hull over the given vertices.
20  /// @param HullVertices The vertices over whose convex hull this brush is built.
21  /// @param Material The material to be applied to the new faces.
22  /// @param FaceAligned Whether the u and v axes of the brush faces are initialized face aligned or world aligned.
23  /// @param RefBrush If non-NULL, the data of the faces of RefBrush is reused as much as possible for the faces of the new brush.
24  /// This is very useful e.g. for the Edit Vertices tool, where edits should generally preserve face data.
25  MapBrushT(const ArrayT<Vector3fT>& HullVertices, EditorMaterialI* Material, bool FaceAligned, const MapBrushT* RefBrush=NULL);
26 
27  /// Constructor for creating a MapBrushT from the intersection of the given planes.
28  /// @param Planes The planes whose intersection forms this brush.
29  /// @param Material The material to be applied to the new faces.
30  /// @param FaceAligned Whether the u and v axes of the brush faces are initialized face aligned or world aligned.
31  MapBrushT(const ArrayT<Plane3fT>& Planes, EditorMaterialI* Material, bool FaceAligned);
32 
33  /// The copy constructor for copying a brush.
34  /// @param Brush The brush to copy-construct this brush from.
35  MapBrushT(const MapBrushT& Brush);
36 
37  // Named constructors for loading brushes from map files.
38  static MapBrushT* Create_cmap(TextParserT& TP, MapDocumentT& MapDoc, unsigned long EntityNr, unsigned long BrushNr, bool IgnoreGroups); ///< EntityNr and BrushNr are provided by the caller, just for better error reporting.
39  static MapBrushT* Create_D3_map(TextParserT& TP, const Vector3fT& Origin, unsigned long EntityNr, unsigned long PrimitiveNr, EditorMatManT& MatMan); ///< EntityNr and PrimitiveNr are provided by the caller, just for better error reporting.
40  static MapBrushT* Create_HL1_map(TextParserT& TP, unsigned long EntityNr, unsigned long BrushNr, EditorMatManT& MatMan); ///< EntityNr and BrushNr are provided by the caller, just for better error reporting.
41  static MapBrushT* Create_HL2_vmf(TextParserT& TP, EditorMatManT& MatMan);
42 
43  // Named constructors for obtaining stock brushes.
44  static MapBrushT* CreateBlock (const BoundingBox3fT& Box, EditorMaterialI* Material); ///< Named constructor for creating a block brush.
45  static MapBrushT* CreateWedge (const BoundingBox3fT& Box, EditorMaterialI* Material); ///< Named constructor for creating a wedge brush.
46  static MapBrushT* CreateCylinder(const BoundingBox3fT& Box, const unsigned long NrOfSides, EditorMaterialI* Material); ///< Named constructor for creating a cylinder brush.
47  static MapBrushT* CreatePyramid (const BoundingBox3fT& Box, const unsigned long NrOfSides, EditorMaterialI* Material); ///< Named constructor for creating a pyramid brush.
48  static MapBrushT* CreateSphere (const BoundingBox3fT& Box, const unsigned long NrOfSides, EditorMaterialI* Material); ///< Named constructor for creating a sphere brush.
49 
50 
51  // Implementations and overrides for base class methods.
52  MapBrushT* Clone() const override;
53  void Render2D(Renderer2DT& Renderer) const;
54  void Render3D(Renderer3DT& Renderer) const;
55  bool IsTranslucent() const;
56  BoundingBox3fT GetBB() const;
57  bool TraceRay(const Vector3fT& RayOrigin, const Vector3fT& RayDir, float& Fraction, unsigned long& FaceNr) const;
58  bool TracePixel(const wxPoint& Pixel, int Radius, const ViewWindow2DT& ViewWin) const;
59  void Save_cmap(std::ostream& OutFile, unsigned long BrushNr, const MapDocumentT& MapDoc) const;
60  wxString GetDescription() const;
61 
62  // Implement the MapElementT transformation methods.
63  TrafoMementoT* GetTrafoState() const override;
64  void RestoreTrafoState(const TrafoMementoT* TM) override;
65  void TrafoMove(const Vector3fT& Delta, bool LockTexCoords) override;
66  void TrafoRotate(const Vector3fT& RefPoint, const cf::math::AnglesfT& Angles, bool LockTexCoords) override;
67  void TrafoScale(const Vector3fT& RefPoint, const Vector3fT& Scale, bool LockTexCoords) override;
68  void TrafoMirror(unsigned int NormalAxis, float Dist, bool LockTexCoords) override;
69  void Transform(const Matrix4x4fT& Matrix, bool LockTexCoords) override;
70 
71  /// This method splits the brush along the given plane and returns the front piece, the back piece, or both.
72  /// @param Plane The plane along which to split this brush.
73  /// @param Front The address of the pointer to the front piece, if the caller is interested in obtaining it (NULL otherwise).
74  /// @param Back The address of the pointer to the back piece, if the caller is interested in obtaining it (NULL otherwise).
75  void Split(const Plane3T<float>& Plane, MapBrushT** Front=NULL, MapBrushT** Back=NULL) const;
76 
77  /// Subtracts from this brush A the given volume B, Result = A \ B.
78  /// @param B The volume that is to be subtracted ("carved") from this brush A, defined as another MapBrushT.
79  /// @param Result The array in which the subtraction result is returned.
80  /// @returns true when this brush A and the carver B intersected and thus the subtraction result is actually different from this brush,
81  /// and false otherwise (A and B didn't overlap / intersect at all). In the latter case, the caller should delete all brushes that
82  /// the method might have put into the Result array during the course of its computations.
83  bool Subtract(const MapBrushT* B, ArrayT<MapBrushT*>& Result) const;
84 
85  /// Returns the faces of this brush.
86  const ArrayT<MapFaceT>& GetFaces() const { return m_Faces; }
87  ArrayT<MapFaceT>& GetFaces() { return m_Faces; }
88 
89  /// Returns whether this brush is valid. TODO: ctors should throw exceptions instead!
90  bool IsValid() const { return m_Faces.Size()>=4; }
91 
92  // The TypeSys related declarations for this class.
93  virtual const cf::TypeSys::TypeInfoT* GetType() const { return &TypeInfo; }
94  static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params);
95  static const cf::TypeSys::TypeInfoT TypeInfo;
96 
97 
98  private:
99 
100  /// The default constructor for instantiating a map brush.
101  /// It is in the "private" section so that only "named constructors" can access it.
102  MapBrushT();
103 
104  /// A helper method for our constructors.
105  /// Called when all faces of this brush are defined by their planes, this method
106  /// computes their actual intersection and thus the vertices for each face.
107  void CompleteFaceVertices();
108 
109 
110  ArrayT<MapFaceT> m_Faces; ///< The set of faces whose intersection forms this brush.
111 };
112 
113 #endif
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
bool IsValid() const
Returns whether this brush is valid. TODO: ctors should throw exceptions instead! ...
Definition: MapBrush.hpp:90
This class represents a CaWE "map" document.
Definition: MapDocument.hpp:45
const ArrayT< MapFaceT > & GetFaces() const
Returns the faces of this brush.
Definition: MapBrush.hpp:86
void TrafoMove(const Vector3fT &Delta, bool LockTexCoords) override
Translates this element by the given vector (in world-space).
Definition: MapBrush.cpp:544
BoundingBox3fT GetBB() const
Returns the spatial bounding-box of this map element.
Definition: MapBrush.cpp:304
void TrafoMirror(unsigned int NormalAxis, float Dist, bool LockTexCoords) override
Mirrors this element along the given mirror plane (in world-space).
Definition: MapBrush.cpp:724
bool Subtract(const MapBrushT *B, ArrayT< MapBrushT * > &Result) const
Subtracts from this brush A the given volume B, Result = A \ B.
Definition: MapBrush.cpp:476
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: MapBrush.cpp:372
void TrafoScale(const Vector3fT &RefPoint, const Vector3fT &Scale, bool LockTexCoords) override
Scales this element about the given reference point (in world-space).
Definition: MapBrush.cpp:649
bool IsTranslucent() const
Returns whether this map element is (entirely or partially) translucent.
Definition: MapBrush.cpp:294
unsigned long Size() const
Get size of array.
Definition: Array.hpp:138
static MapBrushT * Create_D3_map(TextParserT &TP, const Vector3fT &Origin, unsigned long EntityNr, unsigned long PrimitiveNr, EditorMatManT &MatMan)
EntityNr and PrimitiveNr are provided by the caller, just for better error reporting.
Definition: Load_D3_map.cpp:175
static MapBrushT * CreateCylinder(const BoundingBox3fT &Box, const unsigned long NrOfSides, EditorMaterialI *Material)
Named constructor for creating a cylinder brush.
Definition: MapBrush.cpp:181
This class adds no functionality of its own, but only exists for proper type separation.
Definition: MapPrimitive.hpp:21
This class manages the editor materials for a game configuration.
Definition: EditorMaterialManager.hpp:20
void RestoreTrafoState(const TrafoMementoT *TM) override
Restores the transform-related state of this element from the given memento.
Definition: MapBrush.cpp:528
static MapBrushT * CreatePyramid(const BoundingBox3fT &Box, const unsigned long NrOfSides, EditorMaterialI *Material)
Named constructor for creating a pyramid brush.
Definition: MapBrush.cpp:198
An instance of this class encapsulates the transform-related state of a MapElementT.
Definition: MapElement.hpp:39
Definition: MapBrush.hpp:15
bool TraceRay(const Vector3fT &RayOrigin, const Vector3fT &RayDir, float &Fraction, unsigned long &FaceNr) const
Traces a ray against this map element, and returns whether it was hit.
Definition: MapBrush.cpp:316
static MapBrushT * Create_cmap(TextParserT &TP, MapDocumentT &MapDoc, unsigned long EntityNr, unsigned long BrushNr, bool IgnoreGroups)
EntityNr and BrushNr are provided by the caller, just for better error reporting. ...
Definition: LoadSave_cmap.cpp:243
Definition: EditorMaterial.hpp:21
void Split(const Plane3T< float > &Plane, MapBrushT **Front=NULL, MapBrushT **Back=NULL) const
This method splits the brush along the given plane and returns the front piece, the back piece...
Definition: MapBrush.cpp:419
static MapBrushT * CreateSphere(const BoundingBox3fT &Box, const unsigned long NrOfSides, EditorMaterialI *Material)
Named constructor for creating a sphere brush.
Definition: MapBrush.cpp:210
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: MapBrush.cpp:579
Definition: ChildFrameViewWin2D.hpp:24
static MapBrushT * Create_HL1_map(TextParserT &TP, unsigned long EntityNr, unsigned long BrushNr, EditorMatManT &MatMan)
EntityNr and BrushNr are provided by the caller, just for better error reporting. ...
Definition: Load_HL1_map.cpp:93
TrafoMementoT * GetTrafoState() const override
Returns a memento that encapsulates the transform-related state of this element.
Definition: MapBrush.cpp:522
MapBrushT * Clone() const override
The virtual copy constructor.
Definition: MapBrush.cpp:247
static MapBrushT * CreateWedge(const BoundingBox3fT &Box, EditorMaterialI *Material)
Named constructor for creating a wedge brush.
Definition: MapBrush.cpp:165
Definition: Renderer.hpp:16
Definition: TypeSys.hpp:52
This class keeps type information (about an entity class that occurs in the game).
Definition: TypeSys.hpp:79
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: MapBrush.cpp:759
This is a class for parsing text.
Definition: TextParser.hpp:21
static MapBrushT * CreateBlock(const BoundingBox3fT &Box, EditorMaterialI *Material)
Named constructor for creating a block brush.
Definition: MapBrush.cpp:154