Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Render.h
Go to the documentation of this file.
1 /* Copyright (C) 2012 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 /**
22  * @file
23  * Helper functions related to rendering
24  */
25 
26 #include "maths/Vector2D.h"
27 
28 class CSimContext;
29 class CVector2D;
30 class CVector3D;
31 class CMatrix3D;
34 struct SOverlayLine;
35 
37 {
38  /// Packed array of consecutive dashes' points. Use m_StartIndices to navigate it.
39  std::vector<CVector2D> m_Points;
40 
41  /**
42  * Start indices in m_Points of each dash. Dash n starts at point m_StartIndices[n] and ends at the point with index
43  * m_StartIndices[n+1] - 1, or at the end of the m_Points vector. Use the GetEndIndex(n) convenience method to abstract away the
44  * difference and get the (exclusive) end index of dash n.
45  */
46  std::vector<size_t> m_StartIndices;
47 
48  /// Returns the (exclusive) end point index (i.e. index within m_Points) of dash n.
49  size_t GetEndIndex(size_t i)
50  {
51  // for the last dash, there is no next starting index, so we need to use the end index of the m_Points array instead
52  return (i < m_StartIndices.size() - 1 ? m_StartIndices[i+1] : m_Points.size());
53  }
54 };
55 
56 namespace SimRender
57 {
58 
59 /**
60  * Constructs overlay line from given points, conforming to terrain.
61  *
62  * @param[in] xz List of x,z coordinate pairs representing the line.
63  * @param[in,out] overlay Updated overlay line now conforming to terrain.
64  * @param[in] floating If true, the line conforms to water as well.
65  * @param[in] heightOffset Height above terrain to offset the line.
66  */
67 void ConstructLineOnGround(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(const CSimContext& context, float x, float z, float radius,
82  SOverlayLine& overlay,
83  bool floating, float heightOffset = 0.25f);
84 
85 /**
86  * Constructs overlay line as rectangle with given center and dimensions, conforming to terrain.
87  *
88  * @param[in] x,z Coordinates of center of rectangle.
89  * @param[in] w,h Width/height dimensions of the rectangle.
90  * @param[in] a Clockwise angle to orient the rectangle.
91  * @param[in,out] overlay Updated overlay line representing this rectangle.
92  * @param[in] floating If true, the rectangle conforms to water as well.
93  * @param[in] heightOffset Height above terrain to offset the rectangle.
94  */
95 void ConstructSquareOnGround(const CSimContext& context, float x, float z, float w, float h, float a,
96  SOverlayLine& overlay,
97  bool floating, float heightOffset = 0.25f);
98 
99 /**
100  * Constructs a solid outline of an arbitrarily-aligned bounding @p box.
101  *
102  * @param[in] box
103  * @param[in,out] overlayLine Updated overlay line representing the oriented box.
104  */
105 void ConstructBoxOutline(const CBoundingBoxOriented& box, SOverlayLine& overlayLine);
106 
107 /**
108  * Constructs a solid outline of an axis-aligned bounding @p box.
109  *
110  * @param[in] bound
111  * @param[in,out] overlayLine Updated overlay line representing the AABB.
112  */
113 void ConstructBoxOutline(const CBoundingBoxAligned& box, SOverlayLine& overlayLine);
114 
115 /**
116  * Constructs a simple gimbal outline with the given radius and center.
117  *
118  * @param[in] center
119  * @param[in] radius
120  * @param[in,out] out Updated overlay line representing the gimbal.
121  * @param[in] numSteps The amount of steps to trace a circle's complete outline. Must be a (strictly) positive multiple of four.
122  * For small radii, you can get away with small values; setting this to 4 will create a diamond shape.
123  */
124 void ConstructGimbal(const CVector3D& center, float radius, SOverlayLine& out, size_t numSteps = 16);
125 
126 /**
127  * Constructs 3D axis marker overlay lines for the given coordinate system.
128  * The XYZ axes are colored RGB, respectively.
129  *
130  * @param[in] coordSystem Specifies the coordinate system.
131  * @param[out] outX,outY,outZ Constructed overlay lines for each axes.
132  */
133 void ConstructAxesMarker(const CMatrix3D& coordSystem, SOverlayLine& outX, SOverlayLine& outY, SOverlayLine& outZ);
134 
135 /**
136  * Updates the given points so each point is averaged with its neighbours, resulting in
137  * a somewhat smoother curve, assuming the points are roughly equally spaced.
138  *
139  * @param[in,out] points List of points to smooth.
140  * @param[in] closed if true, then the points are treated as a closed path (the last is connected
141  * to the first).
142  */
143 void SmoothPointsAverage(std::vector<CVector2D>& points, bool closed);
144 
145 /**
146  * Updates the given points to include intermediate points interpolating between the original
147  * control points, using a rounded nonuniform spline.
148  *
149  * @param[in,out] points List of points to interpolate.
150  * @param[in] closed if true, then the points are treated as a closed path (the last is connected
151  * to the first).
152  * @param[in] offset The points are shifted by this distance in a direction 90 degrees clockwise from
153  * the direction of the curve.
154  * @param[in] segmentSamples Amount of intermediate points to sample between every two control points.
155  */
156 void InterpolatePointsRNS(std::vector<CVector2D>& points, bool closed, float offset, int segmentSamples = 4);
157 
158 /**
159  * Creates a dashed line from the given line, dash length, and blank space between.
160  *
161  * @param[in] linePoints List of points specifying the input line.
162  * @param[out] dashedLineOut The dashed line returned as a list of smaller lines
163  * @param[in] dashLength Length of a single dash. Must be strictly positive.
164  * @param[in] blankLength Length of a single blank between dashes. Must be strictly positive.
165  */
166 void ConstructDashedLine(const std::vector<CVector2D>& linePoints, SDashedLine& dashedLineOut,
167  const float dashLength, const float blankLength);
168 
169 /**
170  * Computes angular step parameters @p out_stepAngle and @p out_numSteps, given a @p maxChordLength on a circle of radius @p radius.
171  * The resulting values satisfy @p out_numSteps * @p out_stepAngle = 2*PI.
172  *
173  * This function is used to find the angular step parameters when drawing a circle outline approximated by several connected chords;
174  * it returns the step angle and number of steps such that the length of each resulting chord is less than or equal to @p maxChordLength.
175  * By stating that each chord cannot be longer than a particular length, a certain level of visual smoothness of the resulting circle
176  * outline can be guaranteed independently of the radius of the outline.
177  *
178  * @param radius Radius of the circle. Must be strictly positive.
179  * @param maxChordLength Desired maximum length of individual chords. Must be strictly positive.
180  */
181 void AngularStepFromChordLen(const float maxChordLength, const float radius, float& out_stepAngle, unsigned& out_numSteps);
182 
183 /**
184  * Subdivides a list of @p points into segments of maximum length @p maxSegmentLength that are of equal size between every two
185  * control points. The resulting subdivided list of points is written back to @p points.
186  *
187  * @param points The list of intermediate points to subdivide.
188  * @param maxSegmentLength The maximum length of a single segment after subdivision. Must be strictly positive.
189  * @param closed Should the provided list of points be treated as a closed shape? If true, the resulting list of points will include
190  * extra subdivided points between the last and the first point.
191  */
192 void SubdividePoints(std::vector<CVector2D>& points, float maxSegmentLength, bool closed);
193 
194 } // namespace
195 
196 #endif // INCLUDED_HELPER_RENDER
Line-based overlay, with world-space coordinates, rendered in the world potentially behind other obje...
Definition: Overlay.h:36
void ConstructDashedLine(const std::vector< CVector2D > &linePoints, SDashedLine &dashedLineOut, const float dashLength, const float blankLength)
Creates a dashed line from the given line, dash length, and blank space between.
Definition: Render.cpp:438
static void out(const wchar_t *fmt,...)
Definition: wdbg_sym.cpp:419
void InterpolatePointsRNS(std::vector< CVector2D > &points, bool closed, float offset, int segmentSamples=4)
Updates the given points to include intermediate points interpolating between the original control po...
Definition: Render.cpp:347
Contains pointers to various &#39;global&#39; objects that are needed by the simulation code, to allow easy access without using real (evil) global variables.
Definition: SimContext.h:32
size_t GetEndIndex(size_t i)
Returns the (exclusive) end point index (i.e. index within m_Points) of dash n.
Definition: Render.h:49
void ConstructCircleOnGround(const CSimContext &context, float x, float z, float radius, SOverlayLine &overlay, bool floating, float heightOffset=0.25f)
Constructs overlay line as a circle with given center and radius, conforming to terrain.
Definition: Render.cpp:70
void ConstructLineOnGround(const CSimContext &context, const std::vector< float > &xz, SOverlayLine &overlay, bool floating, float heightOffset=0.25f)
Constructs overlay line from given points, conforming to terrain.
Definition: Render.cpp:35
void SubdividePoints(std::vector< CVector2D > &points, float maxSegmentLength, bool closed)
Subdivides a list of points into segments of maximum length maxSegmentLength that are of equal size b...
Definition: Render.cpp:534
void AngularStepFromChordLen(const float maxChordLength, const float radius, float &out_stepAngle, unsigned &out_numSteps)
Computes angular step parameters out_stepAngle and out_numSteps, given a maxChordLength on a circle o...
Definition: Render.cpp:525
std::vector< CVector2D > m_Points
Packed array of consecutive dashes&#39; points. Use m_StartIndices to navigate it.
Definition: Render.h:39
std::vector< size_t > m_StartIndices
Start indices in m_Points of each dash.
Definition: Render.h:46
void ConstructSquareOnGround(const CSimContext &context, float x, float z, float w, float h, float a, SOverlayLine &overlay, bool floating, float heightOffset=0.25f)
Constructs overlay line as rectangle with given center and dimensions, conforming to terrain...
Definition: Render.cpp:121
void SmoothPointsAverage(std::vector< CVector2D > &points, bool closed)
Updates the given points so each point is averaged with its neighbours, resulting in a somewhat smoot...
Definition: Render.cpp:305
void ConstructAxesMarker(const CMatrix3D &coordSystem, SOverlayLine &outX, SOverlayLine &outY, SOverlayLine &outZ)
Constructs 3D axis marker overlay lines for the given coordinate system.
Definition: Render.cpp:281
void ConstructBoxOutline(const CBoundingBoxOriented &box, SOverlayLine &overlayLine)
Constructs a solid outline of an arbitrarily-aligned bounding box.
Definition: Render.cpp:190
void ConstructGimbal(const CVector3D &center, float radius, SOverlayLine &out, size_t numSteps=16)
Constructs a simple gimbal outline with the given radius and center.
Definition: Render.cpp:220