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_GRAPHICS_OVERLAY
19 : #define INCLUDED_GRAPHICS_OVERLAY
20 :
21 : #include "graphics/Color.h"
22 : #include "graphics/Texture.h"
23 : #include "maths/Vector2D.h"
24 : #include "maths/Vector3D.h"
25 : #include "ps/CStrIntern.h"
26 :
27 : #include <cstddef>
28 : #include <memory>
29 : #include <string>
30 : #include <vector>
31 :
32 : class CFrustum;
33 : class CSimContext;
34 : class CTexturedLineRData;
35 : struct SOverlayDescriptor;
36 :
37 : /**
38 : * Line-based overlay, with world-space coordinates, rendered in the world
39 : * potentially behind other objects. Designed for debug info.
40 : */
41 0 : struct SOverlayLine
42 : {
43 0 : SOverlayLine() : m_Thickness(0.1f) { }
44 :
45 : CColor m_Color;
46 : // Shape is not automatically closed.
47 : std::vector<CVector3D> m_Coords;
48 : // Half-width of the line, in world-space units.
49 : float m_Thickness;
50 :
51 0 : void PushCoords(const CVector3D& v) { m_Coords.emplace_back(v); }
52 :
53 0 : void PushCoords(const float x, const float y, const float z)
54 : {
55 0 : m_Coords.emplace_back(x, y, z);
56 0 : }
57 : };
58 :
59 : /**
60 : * Textured line overlay, with world-space coordinates, rendered in the world onto the terrain.
61 : * Designed for relatively static textured lines, i.e. territory borders, originally.
62 : *
63 : * Once submitted for rendering, instances must not be copied afterwards. The reason is that they
64 : * are assigned rendering data that is unique to the submitted instance, and non-transferable to
65 : * any copies that would otherwise be made. Amongst others, this restraint includes that they must
66 : * not be submitted by their address inside a std::vector storing them by value.
67 : */
68 : struct SOverlayTexturedLine
69 : {
70 : enum LineCapType
71 : {
72 : LINECAP_FLAT, ///< no line ending; abrupt stop of the line (aka. butt ending)
73 :
74 : /**
75 : * Semi-circular line ending. The texture is mapped by curving the left vertical edge
76 : * around the semi-circle's rim. That is, the center point has UV coordinates (0.5;0.5),
77 : * and the rim vertices all have U coordinate 0 and a V coordinate that ranges from 0 to
78 : * 1 as the rim is traversed.
79 : */
80 : LINECAP_ROUND,
81 : LINECAP_SHARP, ///< sharp point ending
82 : LINECAP_SQUARE, ///< square end that extends half the line width beyond the line end
83 : };
84 :
85 0 : SOverlayTexturedLine()
86 0 : : m_Thickness(1.0f), m_Closed(false), m_AlwaysVisible(false),
87 0 : m_StartCapType(LINECAP_FLAT), m_EndCapType(LINECAP_FLAT), m_SimContext(NULL)
88 : { }
89 :
90 : CTexturePtr m_TextureBase;
91 : CTexturePtr m_TextureMask;
92 :
93 : /// Color to apply to the line texture, where indicated by the mask.
94 : CColor m_Color;
95 : /// (x, z) vertex coordinate pairs; y is computed automatically.
96 : std::vector<CVector2D> m_Coords;
97 : /// Half-width of the line, in world-space units.
98 : float m_Thickness;
99 : /// Should this line be treated as a closed loop? If set, any end cap settings are ignored.
100 : bool m_Closed;
101 : /// Should this line be rendered fully visible at all times, even under the SoD?
102 : bool m_AlwaysVisible;
103 :
104 : LineCapType m_StartCapType;
105 : LineCapType m_EndCapType;
106 :
107 : /**
108 : * Simulation context applicable for this overlay line; used to obtain terrain information
109 : * during automatic computation of Y coordinates.
110 : */
111 : const CSimContext* m_SimContext;
112 :
113 : /**
114 : * Cached renderer data, because expensive to compute. Allocated by the renderer when necessary
115 : * for rendering purposes.
116 : *
117 : * Note: the rendering data may be shared between copies of this object to prevent having to
118 : * recompute it, while at the same time maintaining copyability of this object (see also docs on
119 : * CTexturedLineRData).
120 : */
121 : std::shared_ptr<CTexturedLineRData> m_RenderData;
122 :
123 : /**
124 : * Converts a string line cap type into its corresponding LineCap enum value, and returns
125 : * the resulting value. If the input string is unrecognized, a warning is issued and a
126 : * default value is returned.
127 : */
128 : static LineCapType StrToLineCapType(const std::wstring& str);
129 :
130 : /**
131 : * Creates the texture specified by the given overlay descriptor and assigns it to this overlay.
132 : */
133 : void CreateOverlayTexture(const SOverlayDescriptor* overlayDescriptor);
134 :
135 0 : void PushCoords(const float x, const float z) { m_Coords.emplace_back(x, z); }
136 0 : void PushCoords(const CVector2D& v) { m_Coords.push_back(v); }
137 0 : void PushCoords(const std::vector<CVector2D>& points)
138 : {
139 0 : for (const CVector2D& point : points)
140 0 : PushCoords(point);
141 : }
142 :
143 : bool IsVisibleInFrustum(const CFrustum& frustum) const;
144 : };
145 :
146 : /**
147 : * Billboard sprite overlay, with world-space coordinates, rendered on top
148 : * of all other objects. Designed for health bars and rank icons.
149 : */
150 0 : struct SOverlaySprite
151 : {
152 : CTexturePtr m_Texture;
153 : CColor m_Color;
154 : CVector3D m_Position; // base position
155 : float m_X0, m_Y0, m_X1, m_Y1; // billboard corner coordinates, relative to base position
156 : };
157 :
158 : /**
159 : * Rectangular single-quad terrain overlay, in world space coordinates. The vertices of the quad
160 : * are not required to be coplanar; the quad is arbitrarily triangulated with no effort being made
161 : * to find a best fit to the underlying terrain.
162 : */
163 0 : struct SOverlayQuad
164 : {
165 : CTexturePtr m_Texture;
166 : CTexturePtr m_TextureMask;
167 : CVector3D m_Corners[4];
168 : CColor m_Color;
169 : };
170 :
171 : struct SOverlaySphere
172 : {
173 0 : SOverlaySphere() : m_Radius(0) { }
174 :
175 : CVector3D m_Center;
176 : float m_Radius;
177 : CColor m_Color;
178 : };
179 :
180 : enum EOverlayType
181 : {
182 : /// A single textured quad overlay, intended for entities that move around much, like units (e.g. foot soldiers, etc).
183 : DYNAMIC_QUAD,
184 : /// A more complex textured line overlay, composed of several textured line segments.
185 : STATIC_OUTLINE,
186 : };
187 :
188 : struct SOverlayDescriptor
189 : {
190 : EOverlayType m_Type;
191 : CStrIntern m_QuadTexture;
192 : CStrIntern m_QuadTextureMask;
193 : CStrIntern m_LineTexture;
194 : CStrIntern m_LineTextureMask;
195 : float m_LineThickness;
196 : int m_Radius;
197 :
198 186 : SOverlayDescriptor() : m_LineThickness(0) { }
199 : };
200 :
201 : // TODO: OverlayText
202 :
203 : #endif // INCLUDED_GRAPHICS_OVERLAY
|