Line data Source code
1 : /* Copyright (C) 2026 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 : #include "precompiled.h"
19 :
20 : #include "Render.h"
21 :
22 : #include "graphics/Color.h"
23 : #include "graphics/Overlay.h"
24 : #include "graphics/Terrain.h"
25 : #include "lib/debug.h"
26 : #include "maths/BoundingBoxAligned.h"
27 : #include "maths/BoundingBoxOriented.h"
28 : #include "maths/FixedVector3D.h"
29 : #include "maths/MathUtil.h"
30 : #include "maths/Matrix3D.h"
31 : #include "maths/Quaternion.h"
32 : #include "maths/Vector2D.h"
33 : #include "maths/Vector3D.h"
34 : #include "ps/Profile.h"
35 : #include "simulation2/components/ICmpTerrain.h"
36 : #include "simulation2/components/ICmpWaterManager.h"
37 : #include "simulation2/system/Component.h"
38 : #include "simulation2/system/Entity.h"
39 :
40 : #include <algorithm>
41 : #include <cmath>
42 : #include <numbers>
43 : #include <utility>
44 :
45 0 : void SimRender::ConstructLineOnGround(const CSimContext& context, const std::vector<float>& xz,
46 : SOverlayLine& overlay, bool floating, float heightOffset)
47 : {
48 0 : overlay.m_Coords.clear();
49 :
50 0 : CmpPtr<ICmpTerrain> cmpTerrain(context, SYSTEM_ENTITY);
51 0 : if (!cmpTerrain)
52 0 : return;
53 :
54 0 : if (xz.size() < 2)
55 : return;
56 :
57 0 : float water = 0.f;
58 0 : if (floating)
59 : {
60 0 : CmpPtr<ICmpWaterManager> cmpWaterManager(context, SYSTEM_ENTITY);
61 0 : if (cmpWaterManager)
62 0 : water = cmpWaterManager->GetExactWaterLevel(xz[0], xz[1]);
63 : }
64 :
65 0 : overlay.m_Coords.reserve(xz.size()/2 * 3);
66 :
67 0 : for (size_t i = 0; i < xz.size(); i += 2)
68 : {
69 0 : const float px = xz[i];
70 0 : const float pz = xz[i+1];
71 0 : const float py = std::max(water, cmpTerrain->GetExactGroundLevel(px, pz)) + heightOffset;
72 0 : overlay.PushCoords(px, py, pz);
73 : }
74 : }
75 :
76 0 : static void ConstructCircleOrClosedArc(
77 : const CSimContext& context, float x, float z, float radius,
78 : bool isCircle,
79 : float start, float end,
80 : SOverlayLine& overlay, bool floating, float heightOffset)
81 : {
82 0 : overlay.m_Coords.clear();
83 :
84 0 : CmpPtr<ICmpTerrain> cmpTerrain(context, SYSTEM_ENTITY);
85 0 : if (!cmpTerrain)
86 0 : return;
87 :
88 0 : float water = 0.f;
89 0 : if (floating)
90 : {
91 0 : CmpPtr<ICmpWaterManager> cmpWaterManager(context, SYSTEM_ENTITY);
92 0 : if (cmpWaterManager)
93 0 : water = cmpWaterManager->GetExactWaterLevel(x, z);
94 : }
95 :
96 : // Adapt the circle resolution to look reasonable for small and largeish radiuses
97 0 : size_t numPoints = Clamp<size_t>(radius * (end - start), 12, 48);
98 :
99 0 : if (!isCircle)
100 0 : overlay.m_Coords.reserve((numPoints + 1 + 2) * 3);
101 : else
102 0 : overlay.m_Coords.reserve((numPoints + 1) * 3);
103 :
104 0 : float cy = 0.f;
105 0 : if (!isCircle)
106 : {
107 : // Start at the center point
108 0 : cy = std::max(water, cmpTerrain->GetExactGroundLevel(x, z)) + heightOffset;
109 0 : overlay.PushCoords(x, cy, z);
110 : }
111 :
112 0 : for (size_t i = 0; i <= numPoints; ++i) // use '<=' so it's a closed loop
113 : {
114 0 : float a = start + (float)i * (end - start) / (float)numPoints;
115 0 : float px = x + radius * cosf(a);
116 0 : float pz = z + radius * sinf(a);
117 0 : float py = std::max(water, cmpTerrain->GetExactGroundLevel(px, pz)) + heightOffset;
118 0 : overlay.PushCoords(px, py, pz);
119 : }
120 :
121 0 : if (!isCircle)
122 : {
123 : // Return to the center point
124 0 : overlay.PushCoords(x, cy, z);
125 : }
126 : }
127 :
128 0 : void SimRender::ConstructCircleOnGround(
129 : const CSimContext& context, float x, float z, float radius,
130 : SOverlayLine& overlay, bool floating, float heightOffset)
131 : {
132 0 : ConstructCircleOrClosedArc(context, x, z, radius, true, 0.0f, 2.0f * std::numbers::pi_v<float>, overlay, floating, heightOffset);
133 0 : }
134 :
135 0 : void SimRender::ConstructClosedArcOnGround(
136 : const CSimContext& context, float x, float z, float radius,
137 : float start, float end,
138 : SOverlayLine& overlay, bool floating, float heightOffset)
139 : {
140 0 : ConstructCircleOrClosedArc(context, x, z, radius, false, start, end, overlay, floating, heightOffset);
141 0 : }
142 :
143 : // This method splits up a straight line into a number of line segments each having a length ~= TERRAIN_TILE_SIZE
144 0 : static void SplitLine(std::vector<std::pair<float, float> >& coords, float x1, float y1, float x2, float y2)
145 : {
146 0 : float length = sqrtf(SQR(x1 - x2) + SQR(y1 - y2));
147 0 : size_t pieces = ((int)length) / TERRAIN_TILE_SIZE;
148 0 : if (pieces > 0)
149 : {
150 0 : float xPieceLength = (x1 - x2) / (float)pieces;
151 0 : float yPieceLength = (y1 - y2) / (float)pieces;
152 0 : for (size_t i = 1; i <= (pieces - 1); ++i)
153 : {
154 0 : coords.emplace_back(x1 - (xPieceLength * (float)i), y1 - (yPieceLength * (float)i));
155 : }
156 : }
157 0 : coords.emplace_back(x2, y2);
158 0 : }
159 :
160 0 : void SimRender::ConstructSquareOnGround(const CSimContext& context, float x, float z, float w, float h, float a,
161 : SOverlayLine& overlay, bool floating, float heightOffset)
162 : {
163 0 : overlay.m_Coords.clear();
164 :
165 0 : CmpPtr<ICmpTerrain> cmpTerrain(context, SYSTEM_ENTITY);
166 0 : if (!cmpTerrain)
167 0 : return;
168 :
169 0 : float water = 0.f;
170 0 : if (floating)
171 : {
172 0 : CmpPtr<ICmpWaterManager> cmpWaterManager(context, SYSTEM_ENTITY);
173 0 : if (cmpWaterManager)
174 0 : water = cmpWaterManager->GetExactWaterLevel(x, z);
175 : }
176 :
177 0 : float c = cosf(a);
178 0 : float s = sinf(a);
179 :
180 0 : std::vector<std::pair<float, float> > coords;
181 :
182 : // Add the first vertex, since SplitLine will be adding only the second end-point of the each line to
183 : // the coordinates list. We don't have to worry about the other lines, since the end-point of one line
184 : // will be the starting point of the next
185 0 : coords.emplace_back(x - w/2*c + h/2*s, z + w/2*s + h/2*c);
186 :
187 0 : SplitLine(coords, x - w/2*c + h/2*s, z + w/2*s + h/2*c, x - w/2*c - h/2*s, z + w/2*s - h/2*c);
188 0 : SplitLine(coords, x - w/2*c - h/2*s, z + w/2*s - h/2*c, x + w/2*c - h/2*s, z - w/2*s - h/2*c);
189 0 : SplitLine(coords, x + w/2*c - h/2*s, z - w/2*s - h/2*c, x + w/2*c + h/2*s, z - w/2*s + h/2*c);
190 0 : SplitLine(coords, x + w/2*c + h/2*s, z - w/2*s + h/2*c, x - w/2*c + h/2*s, z + w/2*s + h/2*c);
191 :
192 0 : overlay.m_Coords.reserve(coords.size() * 3);
193 :
194 0 : for (size_t i = 0; i < coords.size(); ++i)
195 : {
196 0 : float px = coords[i].first;
197 0 : float pz = coords[i].second;
198 0 : float py = std::max(water, cmpTerrain->GetExactGroundLevel(px, pz)) + heightOffset;
199 0 : overlay.PushCoords(px, py, pz);
200 : }
201 0 : }
202 :
203 0 : void SimRender::ConstructBoxOutline(const CBoundingBoxAligned& bound, SOverlayLine& overlayLine)
204 : {
205 0 : overlayLine.m_Coords.clear();
206 :
207 0 : if (bound.IsEmpty())
208 : return;
209 :
210 0 : const CVector3D& pMin = bound[0];
211 0 : const CVector3D& pMax = bound[1];
212 :
213 : // floor square
214 0 : overlayLine.PushCoords(pMin.X, pMin.Y, pMin.Z);
215 0 : overlayLine.PushCoords(pMax.X, pMin.Y, pMin.Z);
216 0 : overlayLine.PushCoords(pMax.X, pMin.Y, pMax.Z);
217 0 : overlayLine.PushCoords(pMin.X, pMin.Y, pMax.Z);
218 0 : overlayLine.PushCoords(pMin.X, pMin.Y, pMin.Z);
219 : // roof square
220 0 : overlayLine.PushCoords(pMin.X, pMax.Y, pMin.Z);
221 0 : overlayLine.PushCoords(pMax.X, pMax.Y, pMin.Z);
222 0 : overlayLine.PushCoords(pMax.X, pMax.Y, pMax.Z);
223 0 : overlayLine.PushCoords(pMin.X, pMax.Y, pMax.Z);
224 0 : overlayLine.PushCoords(pMin.X, pMax.Y, pMin.Z);
225 : }
226 :
227 0 : void SimRender::ConstructBoxOutline(const CBoundingBoxOriented& box, SOverlayLine& overlayLine)
228 : {
229 0 : overlayLine.m_Coords.clear();
230 :
231 0 : if (box.IsEmpty())
232 0 : return;
233 :
234 0 : CVector3D corners[8];
235 0 : box.GetCorner(-1, -1, -1, corners[0]);
236 0 : box.GetCorner( 1, -1, -1, corners[1]);
237 0 : box.GetCorner( 1, -1, 1, corners[2]);
238 0 : box.GetCorner(-1, -1, 1, corners[3]);
239 0 : box.GetCorner(-1, 1, -1, corners[4]);
240 0 : box.GetCorner( 1, 1, -1, corners[5]);
241 0 : box.GetCorner( 1, 1, 1, corners[6]);
242 0 : box.GetCorner(-1, 1, 1, corners[7]);
243 :
244 0 : overlayLine.PushCoords(corners[0]);
245 0 : overlayLine.PushCoords(corners[1]);
246 0 : overlayLine.PushCoords(corners[2]);
247 0 : overlayLine.PushCoords(corners[3]);
248 0 : overlayLine.PushCoords(corners[0]);
249 :
250 0 : overlayLine.PushCoords(corners[4]);
251 0 : overlayLine.PushCoords(corners[5]);
252 0 : overlayLine.PushCoords(corners[6]);
253 0 : overlayLine.PushCoords(corners[7]);
254 0 : overlayLine.PushCoords(corners[4]);
255 : }
256 :
257 0 : void SimRender::ConstructGimbal(const CVector3D& center, float radius, SOverlayLine& out, size_t numSteps)
258 : {
259 0 : ENSURE(numSteps > 0 && numSteps % 4 == 0); // must be a positive multiple of 4
260 :
261 0 : out.m_Coords.clear();
262 :
263 0 : size_t fullCircleSteps = numSteps;
264 0 : const float angleIncrement = 2.f * std::numbers::pi_v<float> / fullCircleSteps;
265 :
266 0 : const CVector3D X_UNIT(1, 0, 0);
267 0 : const CVector3D Y_UNIT(0, 1, 0);
268 0 : const CVector3D Z_UNIT(0, 0, 1);
269 0 : CVector3D rotationVector(0, 0, radius); // directional vector based in the center that we will be rotating to get the gimbal points
270 :
271 : // first draw a quarter of XZ gimbal; then complete the XY gimbal; then continue the XZ gimbal and finally add the YZ gimbal
272 : // (that way we can keep a single continuous line)
273 :
274 : // -- XZ GIMBAL (PART 1/2) -----------------------------------------------
275 :
276 0 : CQuaternion xzRotation;
277 0 : xzRotation.FromAxisAngle(Y_UNIT, angleIncrement);
278 :
279 0 : for (size_t i = 0; i < fullCircleSteps/4; ++i) // complete only a quarter of the way
280 : {
281 0 : out.PushCoords(center + rotationVector);
282 0 : rotationVector = xzRotation.Rotate(rotationVector);
283 : }
284 :
285 : // -- XY GIMBAL ----------------------------------------------------------
286 :
287 : // now complete the XY gimbal while the XZ gimbal is interrupted
288 0 : CQuaternion xyRotation;
289 0 : xyRotation.FromAxisAngle(Z_UNIT, angleIncrement);
290 :
291 0 : for (size_t i = 0; i < fullCircleSteps; ++i) // note the <; the last point of the XY gimbal isn't added, because the XZ gimbal will add it
292 : {
293 0 : out.PushCoords(center + rotationVector);
294 0 : rotationVector = xyRotation.Rotate(rotationVector);
295 : }
296 :
297 : // -- XZ GIMBAL (PART 2/2) -----------------------------------------------
298 :
299 : // resume the XZ gimbal to completion
300 0 : for (size_t i = fullCircleSteps/4; i < fullCircleSteps; ++i) // exclude the last point of the circle so the YZ gimbal can add it
301 : {
302 0 : out.PushCoords(center + rotationVector);
303 0 : rotationVector = xzRotation.Rotate(rotationVector);
304 : }
305 :
306 : // -- YZ GIMBAL ----------------------------------------------------------
307 :
308 0 : CQuaternion yzRotation;
309 0 : yzRotation.FromAxisAngle(X_UNIT, angleIncrement);
310 :
311 0 : for (size_t i = 0; i <= fullCircleSteps; ++i)
312 : {
313 0 : out.PushCoords(center + rotationVector);
314 0 : rotationVector = yzRotation.Rotate(rotationVector);
315 : }
316 0 : }
317 :
318 0 : void SimRender::ConstructAxesMarker(const CMatrix3D& coordSystem, SOverlayLine& outX, SOverlayLine& outY, SOverlayLine& outZ)
319 : {
320 0 : outX.m_Coords.clear();
321 0 : outY.m_Coords.clear();
322 0 : outZ.m_Coords.clear();
323 :
324 0 : outX.m_Color = CColor(1, 0, 0, .5f); // X axis; red
325 0 : outY.m_Color = CColor(0, 1, 0, .5f); // Y axis; green
326 0 : outZ.m_Color = CColor(0, 0, 1, .5f); // Z axis; blue
327 :
328 0 : outX.m_Thickness = 0.1f;
329 0 : outY.m_Thickness = 0.1f;
330 0 : outZ.m_Thickness = 0.1f;
331 :
332 0 : CVector3D origin = coordSystem.GetTranslation();
333 0 : outX.PushCoords(origin);
334 0 : outY.PushCoords(origin);
335 0 : outZ.PushCoords(origin);
336 :
337 0 : outX.PushCoords(origin + CVector3D(coordSystem(0,0), coordSystem(1,0), coordSystem(2,0)));
338 0 : outY.PushCoords(origin + CVector3D(coordSystem(0,1), coordSystem(1,1), coordSystem(2,1)));
339 0 : outZ.PushCoords(origin + CVector3D(coordSystem(0,2), coordSystem(1,2), coordSystem(2,2)));
340 0 : }
341 :
342 0 : void SimRender::SmoothPointsAverage(std::vector<CVector2D>& points, bool closed)
343 : {
344 0 : PROFILE("SmoothPointsAverage");
345 :
346 0 : size_t n = points.size();
347 0 : if (n < 2)
348 0 : return; // avoid out-of-bounds array accesses, and leave the points unchanged
349 :
350 0 : std::vector<CVector2D> newPoints;
351 0 : newPoints.resize(points.size());
352 :
353 : // Handle the end points appropriately
354 0 : if (closed)
355 : {
356 0 : newPoints[0] = (points[n-1] + points[0] + points[1]) / 3.f;
357 0 : newPoints[n-1] = (points[n-2] + points[n-1] + points[0]) / 3.f;
358 : }
359 : else
360 : {
361 0 : newPoints[0] = points[0];
362 0 : newPoints[n-1] = points[n-1];
363 : }
364 :
365 : // Average all the intermediate points
366 0 : for (size_t i = 1; i < n-1; ++i)
367 0 : newPoints[i] = (points[i-1] + points[i] + points[i+1]) / 3.f;
368 :
369 0 : points.swap(newPoints);
370 0 : }
371 :
372 0 : static CVector2D EvaluateSpline(float t, CVector2D a0, CVector2D a1, CVector2D a2, CVector2D a3, float offset)
373 : {
374 : // Compute position on spline
375 0 : CVector2D p = a0*(t*t*t) + a1*(t*t) + a2*t + a3;
376 :
377 : // Compute unit-vector direction of spline
378 0 : CVector2D dp = (a0*(3*t*t) + a1*(2*t) + a2).Normalized();
379 :
380 : // Offset position perpendicularly
381 0 : return p + CVector2D(dp.Y*-offset, dp.X*offset);
382 : }
383 :
384 0 : void SimRender::InterpolatePointsRNS(std::vector<CVector2D>& points, bool closed, float offset, int segmentSamples /* = 4 */)
385 : {
386 0 : PROFILE("InterpolatePointsRNS");
387 0 : ENSURE(segmentSamples > 0);
388 :
389 0 : std::vector<CVector2D> newPoints;
390 :
391 : // (This does some redundant computations for adjacent vertices,
392 : // but it's fairly fast (<1ms typically) so we don't worry about it yet)
393 :
394 : // TODO: Instead of doing a fixed number of line segments between each
395 : // control point, it should probably be somewhat adaptive to get a nicer
396 : // curve with fewer points
397 :
398 0 : size_t n = points.size();
399 :
400 0 : if (closed)
401 : {
402 0 : if (n < 1)
403 0 : return; // we need at least a single point to not crash
404 : }
405 : else
406 : {
407 0 : if (n < 2)
408 : return; // in non-closed mode, we need at least n=2 to not crash
409 : }
410 :
411 0 : size_t imax = closed ? n : n-1;
412 0 : newPoints.reserve(imax*segmentSamples);
413 :
414 : // these are primarily used inside the loop, but for open paths we need them outside the loop once to compute the last point
415 0 : CVector2D a0;
416 0 : CVector2D a1;
417 0 : CVector2D a2;
418 0 : CVector2D a3;
419 :
420 0 : for (size_t i = 0; i < imax; ++i)
421 : {
422 : // Get the relevant points for this spline segment; each step interpolates the segment between p1 and p2; p0 and p3 are the points
423 : // before p1 and after p2, respectively; they're needed to compute tangents and whatnot.
424 0 : CVector2D p0; // normally points[(i-1+n)%n], but it's a bit more complicated due to open/closed paths -- see below
425 0 : CVector2D p1 = points[i];
426 0 : CVector2D p2 = points[(i+1)%n];
427 0 : CVector2D p3; // normally points[(i+2)%n], but it's a bit more complicated due to open/closed paths -- see below
428 :
429 0 : if (!closed && (i == 0))
430 : // p0's point index is out of bounds, and we can't wrap around because we're in non-closed mode -- create an artificial point
431 : // that extends p1 -> p0 (i.e. the first segment's direction)
432 0 : p0 = points[0] + (points[0] - points[1]);
433 : else
434 : // standard wrap-around case
435 0 : p0 = points[(i-1+n)%n]; // careful; don't use (i-1)%n here, as the result is machine-dependent for negative operands (e.g. if i==0, the result could be either -1 or n-1)
436 :
437 :
438 0 : if (!closed && (i == n-2))
439 : // p3's point index is out of bounds; create an artificial point that extends p_(n-2) -> p_(n-1) (i.e. the last segment's direction)
440 : // (note that p2's index should not be out of bounds, because in non-closed mode imax is reduced by 1)
441 0 : p3 = points[n-1] + (points[n-1] - points[n-2]);
442 : else
443 : // standard wrap-around case
444 0 : p3 = points[(i+2)%n];
445 :
446 :
447 : // Do the RNS computation (based on GPG4 "Nonuniform Splines")
448 0 : float l1 = (p2 - p1).Length(); // length of spline segment (i)..(i+1)
449 0 : CVector2D s0 = (p1 - p0).Normalized(); // unit vector of spline segment (i-1)..(i)
450 0 : CVector2D s1 = (p2 - p1).Normalized(); // unit vector of spline segment (i)..(i+1)
451 0 : CVector2D s2 = (p3 - p2).Normalized(); // unit vector of spline segment (i+1)..(i+2)
452 0 : CVector2D v1 = (s0 + s1).Normalized() * l1; // spline velocity at i
453 0 : CVector2D v2 = (s1 + s2).Normalized() * l1; // spline velocity at i+1
454 :
455 : // Compute standard cubic spline parameters
456 0 : a0 = p1*2 + p2*-2 + v1 + v2;
457 0 : a1 = p1*-3 + p2*3 + v1*-2 + v2*-1;
458 0 : a2 = v1;
459 0 : a3 = p1;
460 :
461 : // Interpolate at regular points across the interval
462 0 : for (int sample = 0; sample < segmentSamples; sample++)
463 0 : newPoints.push_back(EvaluateSpline(sample/((float) segmentSamples), a0, a1, a2, a3, offset));
464 :
465 : }
466 :
467 0 : if (!closed)
468 : // if the path is open, we should take care to include the last control point
469 : // NOTE: we can't just do push_back(points[n-1]) here because that ignores the offset
470 0 : newPoints.push_back(EvaluateSpline(1.f, a0, a1, a2, a3, offset));
471 :
472 0 : points.swap(newPoints);
473 0 : }
474 :
475 0 : void SimRender::ConstructDashedLine(const std::vector<CVector2D>& keyPoints, SDashedLine& dashedLineOut, const float dashLength, const float blankLength)
476 : {
477 : // sanity checks
478 0 : if (dashLength <= 0)
479 : return;
480 :
481 0 : if (blankLength <= 0)
482 : return;
483 :
484 0 : if (keyPoints.size() < 2)
485 : return;
486 :
487 0 : dashedLineOut.m_Points.clear();
488 0 : dashedLineOut.m_StartIndices.clear();
489 :
490 : // walk the line, counting the total length so far at each node point. When the length exceeds dashLength, cut the last segment at the
491 : // required length and continue for blankLength along the line to start a new dash segment.
492 :
493 : // TODO: we should probably extend this function to also allow for closed lines. I was thinking of slightly scaling the dash/blank length
494 : // so that it fits the length of the curve, but that requires knowing the length of the curve upfront which is sort of expensive to compute
495 : // (O(n) and lots of square roots).
496 :
497 0 : bool buildingDash = true; // true if we're building a dash, false if a blank
498 0 : float curDashLength = 0; // builds up the current dash/blank's length as we walk through the line nodes
499 0 : CVector2D dashLastPoint = keyPoints[0]; // last point of the current dash/blank being built.
500 :
501 : // register the first starting node of the first dash
502 0 : dashedLineOut.m_Points.push_back(keyPoints[0]);
503 0 : dashedLineOut.m_StartIndices.push_back(0);
504 :
505 : // index of the next key point on the path. Must always point to a node that is further along the path than dashLastPoint, so we can
506 : // properly take a direction vector along the path.
507 0 : size_t i = 0;
508 :
509 0 : while(i < keyPoints.size() - 1)
510 : {
511 : // get length of this segment
512 0 : CVector2D segmentVector = keyPoints[i + 1] - dashLastPoint; // vector from our current point along the path to nextNode
513 0 : float segmentLength = segmentVector.Length();
514 :
515 0 : float targetLength = (buildingDash ? dashLength : blankLength);
516 0 : if (curDashLength + segmentLength > targetLength)
517 : {
518 : // segment is longer than the dash length we still have to go, so we'll need to cut it; create a cut point along the segment
519 : // line that is of just the required length to complete the dash, then make it the base point for the next dash/blank.
520 0 : float cutLength = targetLength - curDashLength;
521 0 : CVector2D cutPoint = dashLastPoint + (segmentVector.Normalized() * cutLength);
522 :
523 : // start a new dash or blank in the next iteration
524 0 : curDashLength = 0;
525 0 : buildingDash = !buildingDash; // flip from dash to blank and vice-versa
526 0 : dashLastPoint = cutPoint;
527 :
528 : // don't increment i, we haven't fully traversed this segment yet so we still need to use the same point to take the
529 : // direction vector with in the next iteration
530 :
531 : // this cut point is either the end of the current dash or the beginning of a new dash; either way, we're gonna need it
532 : // in the points array.
533 0 : dashedLineOut.m_Points.push_back(cutPoint);
534 :
535 0 : if (buildingDash)
536 : {
537 : // if we're gonna be building a new dash, then cutPoint is now the base point of that new dash, so let's register its
538 : // index as a start index of a dash.
539 0 : dashedLineOut.m_StartIndices.push_back(dashedLineOut.m_Points.size() - 1);
540 : }
541 :
542 : }
543 : else
544 : {
545 : // the segment from lastDashPoint to keyPoints[i+1] doesn't suffice to complete the dash, so we need to add keyPoints[i+1]
546 : // to this dash's points and continue from there
547 :
548 0 : if (buildingDash)
549 : // still building the dash, add it to the output (we don't need to store the blanks)
550 0 : dashedLineOut.m_Points.push_back(keyPoints[i+1]);
551 :
552 0 : curDashLength += segmentLength;
553 0 : dashLastPoint = keyPoints[i+1];
554 0 : i++;
555 :
556 : }
557 :
558 : }
559 :
560 : }
561 :
562 : // TODO: this serves a similar purpose to SplitLine above, but is more general. Also, SplitLine seems to be implemented more
563 : // efficiently, might be nice to take some cues from it
564 0 : void SimRender::SubdividePoints(std::vector<CVector2D>& points, float maxSegmentLength, bool closed)
565 : {
566 0 : size_t numControlPoints = points.size();
567 0 : if (numControlPoints < 2)
568 0 : return;
569 :
570 0 : ENSURE(maxSegmentLength > 0);
571 :
572 0 : size_t endIndex = numControlPoints;
573 0 : if (!closed && numControlPoints > 2)
574 0 : endIndex--;
575 :
576 0 : std::vector<CVector2D> newPoints;
577 :
578 0 : for (size_t i = 0; i < endIndex; i++)
579 : {
580 0 : const CVector2D& curPoint = points[i];
581 0 : const CVector2D& nextPoint = points[(i+1) % numControlPoints];
582 0 : const CVector2D line(nextPoint - curPoint);
583 0 : CVector2D lineDirection = line.Normalized();
584 :
585 : // include control point i + a list of intermediate points between i and i + 1 (excluding i+1 itself)
586 0 : newPoints.push_back(curPoint);
587 :
588 : // calculate how many intermediate points are needed so that each segment is of length <= maxSegmentLength
589 0 : float lineLength = line.Length();
590 0 : size_t numSegments = (size_t) ceilf(lineLength / maxSegmentLength);
591 0 : float segmentLength = lineLength / numSegments;
592 :
593 0 : for (size_t s = 1; s < numSegments; ++s) // start at one, we already included curPoint
594 : {
595 0 : newPoints.push_back(curPoint + lineDirection * (s * segmentLength));
596 : }
597 : }
598 :
599 0 : points.swap(newPoints);
600 : }
601 :
602 0 : void SimRender::ConstructTexturedLineBox(SOverlayTexturedLine& overlay, const CVector2D& origin,
603 : const CFixedVector3D& rotation, const float sizeX, const float sizeZ)
604 : {
605 0 : float s = sinf(-rotation.Y.ToFloat());
606 0 : float c = cosf(-rotation.Y.ToFloat());
607 :
608 0 : CVector2D unitX(c, s);
609 0 : CVector2D unitZ(-s, c);
610 :
611 : // Add half the line thickness to the radius so that we get an 'outside' stroke of the footprint shape
612 0 : const float halfSizeX = sizeX / 2.f + overlay.m_Thickness / 2.f;
613 0 : const float halfSizeZ = sizeZ / 2.f + overlay.m_Thickness / 2.f;
614 :
615 0 : std::vector<CVector2D> points;
616 0 : points.push_back(CVector2D(origin + unitX * halfSizeX + unitZ * (-halfSizeZ)));
617 0 : points.push_back(CVector2D(origin + unitX * (-halfSizeX) + unitZ * (-halfSizeZ)));
618 0 : points.push_back(CVector2D(origin + unitX * (-halfSizeX) + unitZ * halfSizeZ));
619 0 : points.push_back(CVector2D(origin + unitX * halfSizeX + unitZ * halfSizeZ));
620 :
621 0 : SimRender::SubdividePoints(points, TERRAIN_TILE_SIZE / 3.f, overlay.m_Closed);
622 0 : overlay.PushCoords(points);
623 0 : }
624 :
625 0 : void SimRender::ConstructTexturedLineCircle(SOverlayTexturedLine& overlay, const CVector2D& origin, const float overlay_radius)
626 : {
627 0 : const float radius = overlay_radius + overlay.m_Thickness / 3.f;
628 :
629 0 : size_t numSteps = ceilf(2.f * std::numbers::pi_v<float> * radius / (TERRAIN_TILE_SIZE / 3.f));
630 0 : for (size_t i = 0; i < numSteps; ++i)
631 : {
632 0 : float angle = i * 2.f * std::numbers::pi_v<float> / numSteps;
633 0 : float px = origin.X + radius * sinf(angle);
634 0 : float pz = origin.Y + radius * cosf(angle);
635 :
636 0 : overlay.PushCoords(px, pz);
637 : }
638 0 : }
|