| 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_BRUSH_HPP_INCLUDED |
|---|
| 23 | #define CAFU_MAP_BRUSH_HPP_INCLUDED |
|---|
| 24 | |
|---|
| 25 | #include "MapPrimitive.hpp" |
|---|
| 26 | #include "MapFace.hpp" |
|---|
| 27 | #include "Templates/Array.hpp" |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | class MapBrushT : public MapPrimitiveT |
|---|
| 31 | { |
|---|
| 32 | public: |
|---|
| 33 | |
|---|
| 34 | /// Constructor for creating a MapBrushT from the convex hull over the given vertices. |
|---|
| 35 | /// @param HullVertices The vertices over whose convex hull this brush is built. |
|---|
| 36 | /// @param Material The material to be applied to the new faces. |
|---|
| 37 | /// @param FaceAligned Whether the u and v axes of the brush faces are initialized face aligned or world aligned. |
|---|
| 38 | /// @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. |
|---|
| 39 | /// This is very useful e.g. for the Edit Vertices tool, where edits should generally preserve face data. |
|---|
| 40 | MapBrushT(const ArrayT<Vector3fT>& HullVertices, EditorMaterialI* Material, bool FaceAligned, const MapBrushT* RefBrush=NULL); |
|---|
| 41 | |
|---|
| 42 | /// Constructor for creating a MapBrushT from the intersection of the given planes. |
|---|
| 43 | /// @param Planes The planes whose intersection forms this brush. |
|---|
| 44 | /// @param Material The material to be applied to the new faces. |
|---|
| 45 | /// @param FaceAligned Whether the u and v axes of the brush faces are initialized face aligned or world aligned. |
|---|
| 46 | MapBrushT(const ArrayT<Plane3fT>& Planes, EditorMaterialI* Material, bool FaceAligned); |
|---|
| 47 | |
|---|
| 48 | /// The copy constructor for copying a brush. |
|---|
| 49 | /// @param Brush The brush to copy-construct this brush from. |
|---|
| 50 | MapBrushT(const MapBrushT& Brush); |
|---|
| 51 | |
|---|
| 52 | // Named constructors for loading brushes from map files. |
|---|
| 53 | static MapBrushT* Create_cmap(TextParserT& TP, MapDocumentT& MapDoc, unsigned long EntityNr, unsigned long BrushNr); ///< EntityNr and BrushNr are provided by the caller, just for better error reporting. |
|---|
| 54 | 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. |
|---|
| 55 | 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. |
|---|
| 56 | static MapBrushT* Create_HL2_vmf(TextParserT& TP, EditorMatManT& MatMan); |
|---|
| 57 | |
|---|
| 58 | // Named constructors for obtaining stock brushes. |
|---|
| 59 | static MapBrushT* CreateBlock (const BoundingBox3fT& Box, EditorMaterialI* Material); ///< Named constructor for creating a block brush. |
|---|
| 60 | static MapBrushT* CreateWedge (const BoundingBox3fT& Box, EditorMaterialI* Material); ///< Named constructor for creating a wedge brush. |
|---|
| 61 | static MapBrushT* CreateCylinder(const BoundingBox3fT& Box, const unsigned long NrOfSides, EditorMaterialI* Material); ///< Named constructor for creating a cylinder brush. |
|---|
| 62 | static MapBrushT* CreatePyramid (const BoundingBox3fT& Box, const unsigned long NrOfSides, EditorMaterialI* Material); ///< Named constructor for creating a pyramid brush. |
|---|
| 63 | static MapBrushT* CreateSphere (const BoundingBox3fT& Box, const unsigned long NrOfSides, EditorMaterialI* Material); ///< Named constructor for creating a sphere brush. |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | // Implementations and overrides for base class methods. |
|---|
| 67 | MapBrushT* Clone() const; |
|---|
| 68 | void Assign(const MapElementT* Elem); |
|---|
| 69 | void Render2D(Renderer2DT& Renderer) const; |
|---|
| 70 | void Render3D(Renderer3DT& Renderer) const; |
|---|
| 71 | bool IsTranslucent() const; |
|---|
| 72 | BoundingBox3fT GetBB() const; |
|---|
| 73 | bool TraceRay(const Vector3fT& RayOrigin, const Vector3fT& RayDir, float& Fraction, unsigned long& FaceNr) const; |
|---|
| 74 | bool TracePixel(const wxPoint& Pixel, int Radius, const ViewWindow2DT& ViewWin) const; |
|---|
| 75 | void Save_cmap(std::ostream& OutFile, unsigned long BrushNr, const MapDocumentT& MapDoc) const; |
|---|
| 76 | wxString GetDescription() const; |
|---|
| 77 | |
|---|
| 78 | // Implement the MapElementT transformation methods. |
|---|
| 79 | void TrafoMove(const Vector3fT& Delta); |
|---|
| 80 | void TrafoRotate(const Vector3fT& RefPoint, const cf::math::AnglesfT& Angles); |
|---|
| 81 | void TrafoScale(const Vector3fT& RefPoint, const Vector3fT& Scale); |
|---|
| 82 | void TrafoMirror(unsigned int NormalAxis, float Dist); |
|---|
| 83 | void Transform(const MatrixT& Matrix); |
|---|
| 84 | |
|---|
| 85 | /// This method splits the brush along the given plane and returns the front piece, the back piece, or both. |
|---|
| 86 | /// @param Plane The plane along which to split this brush. |
|---|
| 87 | /// @param Front The address of the pointer to the front piece, if the caller is interested in obtaining it (NULL otherwise). |
|---|
| 88 | /// @param Back The address of the pointer to the back piece, if the caller is interested in obtaining it (NULL otherwise). |
|---|
| 89 | void Split(const Plane3T<float>& Plane, MapBrushT** Front=NULL, MapBrushT** Back=NULL) const; |
|---|
| 90 | |
|---|
| 91 | /// Subtracts from this brush A the given volume B, Result = A \ B. |
|---|
| 92 | /// @param B The volume that is to be subtracted ("carved") from this brush A, defined as another MapBrushT. |
|---|
| 93 | /// @param Result The array in which the subtraction result is returned. |
|---|
| 94 | /// @returns true when this brush A and the carver B intersected and thus the subtraction result is actually different from this brush, |
|---|
| 95 | /// and false otherwise (A and B didn't overlap / intersect at all). In the latter case, the caller should delete all brushes that |
|---|
| 96 | /// the method might have put into the Result array during the course of its computations. |
|---|
| 97 | bool Subtract(const MapBrushT* B, ArrayT<MapBrushT*>& Result) const; |
|---|
| 98 | |
|---|
| 99 | /// Returns the faces of this brush. |
|---|
| 100 | const ArrayT<MapFaceT>& GetFaces() const { return m_Faces; } |
|---|
| 101 | ArrayT<MapFaceT>& GetFaces() { return m_Faces; } |
|---|
| 102 | |
|---|
| 103 | /// Returns whether this brush is valid. TODO: ctors should throw exceptions instead! |
|---|
| 104 | bool IsValid() const { return m_Faces.Size()>=4; } |
|---|
| 105 | |
|---|
| 106 | // The TypeSys related declarations for this class. |
|---|
| 107 | virtual const cf::TypeSys::TypeInfoT* GetType() const { return &TypeInfo; } |
|---|
| 108 | static void* CreateInstance(const cf::TypeSys::CreateParamsT& Params); |
|---|
| 109 | static const cf::TypeSys::TypeInfoT TypeInfo; |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | private: |
|---|
| 113 | |
|---|
| 114 | /// The default constructor for instantiating a map brush. |
|---|
| 115 | /// It is in the "private" section so that only "named constructors" can access it. |
|---|
| 116 | MapBrushT(); |
|---|
| 117 | |
|---|
| 118 | /// A helper method for our constructors. |
|---|
| 119 | /// Called when all faces of this brush are defined by their planes, this method |
|---|
| 120 | /// computes their actual intersection and thus the vertices for each face. |
|---|
| 121 | void CompleteFaceVertices(); |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | ArrayT<MapFaceT> m_Faces; ///< The set of faces whose intersection forms this brush. |
|---|
| 125 | }; |
|---|
| 126 | |
|---|
| 127 | #endif |
|---|