1 | /* Copyright (C) 2020 Wildfire Games.
|
---|
2 | * This file is part of 0 A.D.
|
---|
3 | *
|
---|
4 | * 0 A.D. is free software: you can redistribute it and/or modify
|
---|
5 | * it under the terms of the GNU General Public License as published by
|
---|
6 | * the Free Software Foundation, either version 2 of the License, or
|
---|
7 | * (at your option) any later version.
|
---|
8 | *
|
---|
9 | * 0 A.D. is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 | *
|
---|
14 | * You should have received a copy of the GNU General Public License
|
---|
15 | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*
|
---|
19 | * CBrush, a class representing a convex object
|
---|
20 | */
|
---|
21 |
|
---|
22 | #ifndef maths_brush_h
|
---|
23 | #define maths_brush_h
|
---|
24 |
|
---|
25 | #include "Vector3D.h"
|
---|
26 |
|
---|
27 | #include "graphics/ShaderProgramPtr.h"
|
---|
28 |
|
---|
29 | #include <vector>
|
---|
30 |
|
---|
31 | class CBoundingBoxAligned;
|
---|
32 | class CFrustum;
|
---|
33 | class CPlane;
|
---|
34 |
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Class CBrush: Represents a convex object, supports some CSG operations.
|
---|
38 | */
|
---|
39 | class CBrush
|
---|
40 | {
|
---|
41 | friend class TestBrush;
|
---|
42 |
|
---|
43 | public:
|
---|
44 | CBrush() { }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * CBrush: Construct a brush from a bounds object.
|
---|
48 | *
|
---|
49 | * @param bounds the CBoundingBoxAligned object to construct the brush from.
|
---|
50 | */
|
---|
51 | CBrush(const CBoundingBoxAligned& bounds);
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * IsEmpty: Returns whether the brush is empty.
|
---|
55 | *
|
---|
56 | * @return @c true if the brush is empty, @c false otherwise
|
---|
57 | */
|
---|
58 | bool IsEmpty() const { return m_Vertices.size() == 0; }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Bounds: Calculate the axis-aligned bounding box for this brush.
|
---|
62 | *
|
---|
63 | * @param result the resulting bounding box is stored here
|
---|
64 | */
|
---|
65 | void Bounds(CBoundingBoxAligned& result) const;
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Slice: Cut the object along the given plane, resulting in a smaller (or even empty) brush representing
|
---|
69 | * the part of the object that lies in front of the plane (as defined by the positive direction of its
|
---|
70 | * normal vector).
|
---|
71 | *
|
---|
72 | * @param plane the slicing plane
|
---|
73 | * @param result the resulting brush is stored here
|
---|
74 | */
|
---|
75 | void Slice(const CPlane& plane, CBrush& result) const;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Intersect: Intersect the brush with the given frustum.
|
---|
79 | *
|
---|
80 | * @param frustum the frustum to intersect with
|
---|
81 | * @param result the resulting brush is stored here
|
---|
82 | */
|
---|
83 | void Intersect(const CFrustum& frustum, CBrush& result) const;
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Render the surfaces of the brush as triangles.
|
---|
87 | */
|
---|
88 | void Render(CShaderProgramPtr& shader) const;
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Render the outline of the brush as lines.
|
---|
92 | */
|
---|
93 | void RenderOutline(CShaderProgramPtr& shader) const;
|
---|
94 |
|
---|
95 | private:
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Returns a copy of the vertices in this brush. Intended for testing purposes; you should not need to use
|
---|
99 | * this method directly.
|
---|
100 | */
|
---|
101 | std::vector<CVector3D> GetVertices() const;
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Writes a vector of the faces in this brush to @p out. Each face is itself a vector, listing the vertex indices
|
---|
105 | * that make up the face, starting and ending with the same index. Intended for testing purposes; you should not
|
---|
106 | * need to use this method directly.
|
---|
107 | */
|
---|
108 | void GetFaces(std::vector<std::vector<size_t> >& out) const;
|
---|
109 |
|
---|
110 | private:
|
---|
111 | static const size_t NO_VERTEX = ~0u;
|
---|
112 |
|
---|
113 | typedef std::vector<CVector3D> Vertices;
|
---|
114 | typedef std::vector<size_t> FaceIndices;
|
---|
115 |
|
---|
116 | /// Collection of unique vertices that make up this shape.
|
---|
117 | Vertices m_Vertices;
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Holds the face definitions of this brush. Each face is a sequence of indices into m_Vertices that starts and ends with
|
---|
121 | * the same vertex index, completing a loop through all the vertices that make up the face. This vector holds all the face
|
---|
122 | * sequences back-to-back, thus looking something like 'x---xy--------yz--z' in the general case.
|
---|
123 | */
|
---|
124 | FaceIndices m_Faces;
|
---|
125 |
|
---|
126 | struct Helper;
|
---|
127 | };
|
---|
128 |
|
---|
129 | #endif // maths_brush_h
|
---|