Line data Source code
1 : /* Copyright (C) 2025 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 : #ifndef INCLUDED_HELPER_RENDER
19 : #define INCLUDED_HELPER_RENDER
20 :
21 : #include "maths/Vector2D.h"
22 :
23 : #include <cstddef>
24 : #include <vector>
25 :
26 : class CBoundingBoxAligned;
27 : class CBoundingBoxOriented;
28 : class CFixedVector3D;
29 : class CMatrix3D;
30 : class CSimContext;
31 : class CVector3D;
32 : struct SOverlayLine;
33 : struct SOverlayTexturedLine;
34 :
35 : struct SDashedLine
36 : {
37 : /// Packed array of consecutive dashes' points. Use m_StartIndices to navigate it.
38 : std::vector<CVector2D> m_Points;
39 :
40 : /**
41 : * Start indices in m_Points of each dash. Dash n starts at point m_StartIndices[n] and ends at the point with index
42 : * m_StartIndices[n+1] - 1, or at the end of the m_Points vector. Use the GetEndIndex(n) convenience method to abstract away the
43 : * difference and get the (exclusive) end index of dash n.
44 : */
45 : std::vector<size_t> m_StartIndices;
46 :
47 : /// Returns the (exclusive) end point index (i.e. index within m_Points) of dash n.
48 0 : size_t GetEndIndex(size_t i)
49 : {
50 : // for the last dash, there is no next starting index, so we need to use the end index of the m_Points array instead
51 0 : return (i < m_StartIndices.size() - 1 ? m_StartIndices[i+1] : m_Points.size());
52 : }
53 : };
54 :
55 : namespace SimRender
56 : {
57 :
58 : /**
59 : * Constructs overlay line from given points, conforming to terrain.
60 : *
61 : * @param[in] xz List of x,z coordinate pairs representing the line.
62 : * @param[in,out] overlay Updated overlay line now conforming to terrain.
63 : * @param[in] floating If true, the line conforms to water as well.
64 : * @param[in] heightOffset Height above terrain to offset the line.
65 : */
66 : void ConstructLineOnGround(
67 : const CSimContext& context, const std::vector<float>& xz,
68 : SOverlayLine& overlay,
69 : bool floating, float heightOffset = 0.25f);
70 :
71 : /**
72 : * Constructs overlay line as a circle with given center and radius, conforming to terrain.
73 : *
74 : * @param[in] x,z Coordinates of center of circle.
75 : * @param[in] radius Radius of circle to construct.
76 : * @param[in,out] overlay Updated overlay line representing this circle.
77 : * @param[in] floating If true, the circle conforms to water as well.
78 : * @param[in] heightOffset Height above terrain to offset the circle.
79 : * @param heightOffset The vertical offset to apply to points, to raise the line off the terrain a bit.
80 : */
81 : void ConstructCircleOnGround(
82 : const CSimContext& context, float x, float z, float radius,
83 : SOverlayLine& overlay,
84 : bool floating, float heightOffset = 0.25f);
85 :
86 : /**
87 : * Constructs overlay line as an outlined circle sector (an arc with straight lines between the
88 : * endpoints and the circle's center), conforming to terrain.
89 : */
90 : void ConstructClosedArcOnGround(
91 : const CSimContext& context, float x, float z, float radius,
92 : float start, float end,
93 : SOverlayLine& overlay,
94 : bool floating, float heightOffset = 0.25f);
95 :
96 : /**
97 : * Constructs overlay line as rectangle with given center and dimensions, conforming to terrain.
98 : *
99 : * @param[in] x,z Coordinates of center of rectangle.
100 : * @param[in] w,h Width/height dimensions of the rectangle.
101 : * @param[in] a Clockwise angle to orient the rectangle.
102 : * @param[in,out] overlay Updated overlay line representing this rectangle.
103 : * @param[in] floating If true, the rectangle conforms to water as well.
104 : * @param[in] heightOffset Height above terrain to offset the rectangle.
105 : */
106 : void ConstructSquareOnGround(
107 : const CSimContext& context, float x, float z, float w, float h, float a,
108 : SOverlayLine& overlay,
109 : bool floating, float heightOffset = 0.25f);
110 :
111 : /**
112 : * Constructs a solid outline of an arbitrarily-aligned bounding @p box.
113 : *
114 : * @param[in] box
115 : * @param[in,out] overlayLine Updated overlay line representing the oriented box.
116 : */
117 : void ConstructBoxOutline(const CBoundingBoxOriented& box, SOverlayLine& overlayLine);
118 :
119 : /**
120 : * Constructs a solid outline of an axis-aligned bounding @p box.
121 : *
122 : * @param[in] bound
123 : * @param[in,out] overlayLine Updated overlay line representing the AABB.
124 : */
125 : void ConstructBoxOutline(const CBoundingBoxAligned& box, SOverlayLine& overlayLine);
126 :
127 : /**
128 : * Constructs a simple gimbal outline with the given radius and center.
129 : *
130 : * @param[in] center
131 : * @param[in] radius
132 : * @param[in,out] out Updated overlay line representing the gimbal.
133 : * @param[in] numSteps The amount of steps to trace a circle's complete outline. Must be a (strictly) positive multiple of four.
134 : * For small radii, you can get away with small values; setting this to 4 will create a diamond shape.
135 : */
136 : void ConstructGimbal(const CVector3D& center, float radius, SOverlayLine& out, size_t numSteps = 16);
137 :
138 : /**
139 : * Constructs 3D axis marker overlay lines for the given coordinate system.
140 : * The XYZ axes are colored RGB, respectively.
141 : *
142 : * @param[in] coordSystem Specifies the coordinate system.
143 : * @param[out] outX,outY,outZ Constructed overlay lines for each axes.
144 : */
145 : void ConstructAxesMarker(const CMatrix3D& coordSystem, SOverlayLine& outX, SOverlayLine& outY, SOverlayLine& outZ);
146 :
147 : /**
148 : * Updates the given points so each point is averaged with its neighbours, resulting in
149 : * a somewhat smoother curve, assuming the points are roughly equally spaced.
150 : *
151 : * @param[in,out] points List of points to smooth.
152 : * @param[in] closed if true, then the points are treated as a closed path (the last is connected
153 : * to the first).
154 : */
155 : void SmoothPointsAverage(std::vector<CVector2D>& points, bool closed);
156 :
157 : /**
158 : * Updates the given points to include intermediate points interpolating between the original
159 : * control points, using a rounded nonuniform spline.
160 : *
161 : * @param[in,out] points List of points to interpolate.
162 : * @param[in] closed if true, then the points are treated as a closed path (the last is connected
163 : * to the first).
164 : * @param[in] offset The points are shifted by this distance in a direction 90 degrees clockwise from
165 : * the direction of the curve.
166 : * @param[in] segmentSamples Amount of intermediate points to sample between every two control points.
167 : */
168 : void InterpolatePointsRNS(std::vector<CVector2D>& points, bool closed, float offset, int segmentSamples = 4);
169 :
170 : /**
171 : * Creates a dashed line from the given line, dash length, and blank space between.
172 : *
173 : * @param[in] linePoints List of points specifying the input line.
174 : * @param[out] dashedLineOut The dashed line returned as a list of smaller lines
175 : * @param[in] dashLength Length of a single dash. Must be strictly positive.
176 : * @param[in] blankLength Length of a single blank between dashes. Must be strictly positive.
177 : */
178 : void ConstructDashedLine(const std::vector<CVector2D>& linePoints, SDashedLine& dashedLineOut,
179 : const float dashLength, const float blankLength);
180 :
181 : /**
182 : * Subdivides a list of @p points into segments of maximum length @p maxSegmentLength that are of equal size between every two
183 : * control points. The resulting subdivided list of points is written back to @p points.
184 : *
185 : * @param points The list of intermediate points to subdivide.
186 : * @param maxSegmentLength The maximum length of a single segment after subdivision. Must be strictly positive.
187 : * @param closed Should the provided list of points be treated as a closed shape? If true, the resulting list of points will include
188 : * extra subdivided points between the last and the first point.
189 : */
190 : void SubdividePoints(std::vector<CVector2D>& points, float maxSegmentLength, bool closed);
191 :
192 : /**
193 : * Sets the coordinates of a rectangular textured overlay, for example used by selection rings of structures.
194 : */
195 : void ConstructTexturedLineBox(SOverlayTexturedLine& overlay, const CVector2D& origin, const CFixedVector3D& rotation, const float sizeX, const float sizeZ);
196 :
197 : /**
198 : * Sets the coordinates of a circular textured overlay, for example by selection rings of units or attack range visualization.
199 : */
200 : void ConstructTexturedLineCircle(SOverlayTexturedLine& overlay, const CVector2D& origin, const float overlay_radius);
201 :
202 : } // namespace
203 :
204 : #endif // INCLUDED_HELPER_RENDER
|