Pyrogenesis  trunk
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  */
68  const CSimContext& context, const std::vector<float>& xz,
69  SOverlayLine& overlay,
70  bool floating, float heightOffset = 0.25f);
71 
72 /**
73  * Constructs overlay line as a circle with given center and radius, conforming to terrain.
74  *
75  * @param[in] x,z Coordinates of center of circle.
76  * @param[in] radius Radius of circle to construct.
77  * @param[in,out] overlay Updated overlay line representing this circle.
78  * @param[in] floating If true, the circle conforms to water as well.
79  * @param[in] heightOffset Height above terrain to offset the circle.
80  * @param heightOffset The vertical offset to apply to points, to raise the line off the terrain a bit.
81  */
83  const CSimContext& context, float x, float z, float radius,
84  SOverlayLine& overlay,
85  bool floating, float heightOffset = 0.25f);
86 
87 /**
88  * Constructs overlay line as an outlined circle sector (an arc with straight lines between the
89  * endpoints and the circle's center), conforming to terrain.
90  */
92  const CSimContext& context, float x, float z, float radius,
93  float start, float end,
94  SOverlayLine& overlay,
95  bool floating, float heightOffset = 0.25f);
96 
97 /**
98  * Constructs overlay line as rectangle with given center and dimensions, conforming to terrain.
99  *
100  * @param[in] x,z Coordinates of center of rectangle.
101  * @param[in] w,h Width/height dimensions of the rectangle.
102  * @param[in] a Clockwise angle to orient the rectangle.
103  * @param[in,out] overlay Updated overlay line representing this rectangle.
104  * @param[in] floating If true, the rectangle conforms to water as well.
105  * @param[in] heightOffset Height above terrain to offset the rectangle.
106  */
108  const CSimContext& context, float x, float z, float w, float h, float a,
109  SOverlayLine& overlay,
110  bool floating, float heightOffset = 0.25f);
111 
112 /**
113  * Constructs a solid outline of an arbitrarily-aligned bounding @p box.
114  *
115  * @param[in] box
116  * @param[in,out] overlayLine Updated overlay line representing the oriented box.
117  */
118 void ConstructBoxOutline(const CBoundingBoxOriented& box, SOverlayLine& overlayLine);
119 
120 /**
121  * Constructs a solid outline of an axis-aligned bounding @p box.
122  *
123  * @param[in] bound
124  * @param[in,out] overlayLine Updated overlay line representing the AABB.
125  */
126 void ConstructBoxOutline(const CBoundingBoxAligned& box, SOverlayLine& overlayLine);
127 
128 /**
129  * Constructs a simple gimbal outline with the given radius and center.
130  *
131  * @param[in] center
132  * @param[in] radius
133  * @param[in,out] out Updated overlay line representing the gimbal.
134  * @param[in] numSteps The amount of steps to trace a circle's complete outline. Must be a (strictly) positive multiple of four.
135  * For small radii, you can get away with small values; setting this to 4 will create a diamond shape.
136  */
137 void ConstructGimbal(const CVector3D& center, float radius, SOverlayLine& out, size_t numSteps = 16);
138 
139 /**
140  * Constructs 3D axis marker overlay lines for the given coordinate system.
141  * The XYZ axes are colored RGB, respectively.
142  *
143  * @param[in] coordSystem Specifies the coordinate system.
144  * @param[out] outX,outY,outZ Constructed overlay lines for each axes.
145  */
146 void ConstructAxesMarker(const CMatrix3D& coordSystem, SOverlayLine& outX, SOverlayLine& outY, SOverlayLine& outZ);
147 
148 /**
149  * Updates the given points so each point is averaged with its neighbours, resulting in
150  * a somewhat smoother curve, assuming the points are roughly equally spaced.
151  *
152  * @param[in,out] points List of points to smooth.
153  * @param[in] closed if true, then the points are treated as a closed path (the last is connected
154  * to the first).
155  */
156 void SmoothPointsAverage(std::vector<CVector2D>& points, bool closed);
157 
158 /**
159  * Updates the given points to include intermediate points interpolating between the original
160  * control points, using a rounded nonuniform spline.
161  *
162  * @param[in,out] points List of points to interpolate.
163  * @param[in] closed if true, then the points are treated as a closed path (the last is connected
164  * to the first).
165  * @param[in] offset The points are shifted by this distance in a direction 90 degrees clockwise from
166  * the direction of the curve.
167  * @param[in] segmentSamples Amount of intermediate points to sample between every two control points.
168  */
169 void InterpolatePointsRNS(std::vector<CVector2D>& points, bool closed, float offset, int segmentSamples = 4);
170 
171 /**
172  * Creates a dashed line from the given line, dash length, and blank space between.
173  *
174  * @param[in] linePoints List of points specifying the input line.
175  * @param[out] dashedLineOut The dashed line returned as a list of smaller lines
176  * @param[in] dashLength Length of a single dash. Must be strictly positive.
177  * @param[in] blankLength Length of a single blank between dashes. Must be strictly positive.
178  */
179 void ConstructDashedLine(const std::vector<CVector2D>& linePoints, SDashedLine& dashedLineOut,
180  const float dashLength, const float blankLength);
181 
182 /**
183  * Computes angular step parameters @p out_stepAngle and @p out_numSteps, given a @p maxChordLength on a circle of radius @p radius.
184  * The resulting values satisfy @p out_numSteps * @p out_stepAngle = 2*PI.
185  *
186  * This function is used to find the angular step parameters when drawing a circle outline approximated by several connected chords;
187  * 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.
188  * By stating that each chord cannot be longer than a particular length, a certain level of visual smoothness of the resulting circle
189  * outline can be guaranteed independently of the radius of the outline.
190  *
191  * @param radius Radius of the circle. Must be strictly positive.
192  * @param maxChordLength Desired maximum length of individual chords. Must be strictly positive.
193  */
194 void AngularStepFromChordLen(const float maxChordLength, const float radius, float& out_stepAngle, unsigned& out_numSteps);
195 
196 /**
197  * Subdivides a list of @p points into segments of maximum length @p maxSegmentLength that are of equal size between every two
198  * control points. The resulting subdivided list of points is written back to @p points.
199  *
200  * @param points The list of intermediate points to subdivide.
201  * @param maxSegmentLength The maximum length of a single segment after subdivision. Must be strictly positive.
202  * @param closed Should the provided list of points be treated as a closed shape? If true, the resulting list of points will include
203  * extra subdivided points between the last and the first point.
204  */
205 void SubdividePoints(std::vector<CVector2D>& points, float maxSegmentLength, bool closed);
206 
207 } // namespace
208 
209 #endif // INCLUDED_HELPER_RENDER
Definition: Render.h:36
Line-based overlay, with world-space coordinates, rendered in the world potentially behind other obje...
Definition: Overlay.h:35
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:477
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:386
Definition: Vector3D.h:28
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
void ConstructClosedArcOnGround(const CSimContext &context, float x, float z, float radius, float start, float end, SOverlayLine &overlay, bool floating, float heightOffset=0.25f)
Constructs overlay line as an outlined circle sector (an arc with straight lines between the endpoint...
Definition: Render.cpp:135
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
Definition: Matrix3D.h:33
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:128
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:573
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:564
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 ConstructBoxOutline(const CBoundingBoxAligned &box, SOverlayLine &overlayLine)
Constructs a solid outline of an axis-aligned bounding box.
Definition: Render.cpp:205
Definition: Vector2D.h:31
Definition: BoundingBoxAligned.h:35
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:160
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:344
Definition: BoundingBoxOriented.h:30
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:320
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:259
Definition: Render.h:56