Rise
The Vieneo Province
Loading...
Searching...
No Matches
ExternalCameraOverlay.cpp
Go to the documentation of this file.
2#include <algorithm>
3#include <vector>
4
5namespace
6{
7 struct Vertex { float x, y, z, rhw; Rendering::PackedColor color; };
8 struct Point { float x, y; };
9 struct BallPoint { float x, y, z; };
10
11 Point Radial(float x, float y, float radius, float angle)
12 {
13 return {x + radius * std::sin(angle), y - radius * std::cos(angle)};
14 }
15}
16
18{
19 if (!data.vehicleAvailable || !std::isfinite(data.altitudeMslKm))
20 return {L"--", L"ALTITUDE", L""};
21 const bool agl = data.altitudeMslKm < 10.0f;
22 float altitude = agl ? data.altitudeAglKm : data.altitudeMslKm;
23 if (!std::isfinite(altitude))
24 return {L"--", L"ALTITUDE", agl ? L"AGL" : L"MSL"};
25 // The radio-altimeter datum is gear-on-ground; suppress small negative clearances.
26 if (agl) altitude = (std::max)(0.0f, altitude);
27 const bool meters = altitude < 1.0f;
28 wchar_t value[64];
29 swprintf_s(value, meters || altitude >= 100.0f ? L"%.0f" : L"%.2f",
30 meters ? altitude * 1000.0f : altitude);
31 return {value, L"ALTITUDE", agl ? (meters ? L"M / AGL" : L"KM / AGL") : L"KM / MSL"};
32}
33
35{
36 if (!data.vehicleAvailable || (!data.groundVehicle && !std::isfinite(data.altitudeMslKm)))
37 return {L"--", L"SPEED", L""};
38 const bool airspeed = !data.groundVehicle && data.altitudeMslKm < 10.0f;
39 const bool kilometersPerSecond = !data.groundVehicle && !airspeed;
40 const float speed = airspeed ? data.indicatedAirspeedKms : data.groundSpeedKms;
41 wchar_t value[64] = L"--";
42 if (std::isfinite(speed))
43 swprintf_s(value, kilometersPerSecond ? L"%.2f" : L"%.0f",
44 kilometersPerSecond ? speed : speed * (airspeed ? 1000.0f : 3600.0f));
45 return {value, airspeed ? L"AIRSPEED" : L"GROUND SPEED",
46 airspeed ? L"M/S" : kilometersPerSecond ? L"KM/S" : L"KM/H"};
47}
48
50{
52 HRESULT hr = Rendering::CreateFontW(device, 12, 0, FW_MEDIUM, 1, FALSE, DEFAULT_CHARSET,
53 OUT_TT_ONLY_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH, L"Segoe UI", &labelFont);
54 if (SUCCEEDED(hr))
55 hr = Rendering::CreateFontW(device, 24, 0, FW_NORMAL, 1, FALSE, DEFAULT_CHARSET,
56 OUT_TT_ONLY_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH, L"Segoe UI", &valueFont);
57 if (FAILED(hr)) OnDestroyDevice();
58 return hr;
59}
60
66
72
74{
75 if (labelFont) { labelFont->Release(); labelFont = nullptr; }
76 if (valueFont) { valueFont->Release(); valueFont = nullptr; }
77}
78
80 float left, float top, float scale)
81{
82 if (!sprite || !labelFont || !valueFont || !std::isfinite(scale) || scale <= 0)
83 return;
84 const DWORD bright = Rendering::ColorARGB(245, 229, 242, 245);
85 const DWORD muted = Rendering::ColorARGB(215, 154, 180, 188);
86 const DWORD accent = Rendering::ColorARGB(245, 125, 220, 218);
87 const DWORD lineColor = Rendering::ColorARGB(120, 133, 167, 179);
88
89 std::vector<Vertex> vertices;
90 vertices.reserve(9000);
91 const auto triangle = [&](Point a, Point b, Point c, DWORD tint)
92 {
93 for (Point p : {a, b, c})
94 vertices.push_back({left + p.x * scale - 0.5f, top + p.y * scale - 0.5f, 0, 1, tint});
95 };
96 const auto line = [&](Point a, Point b, float width, DWORD tint)
97 {
98 const float dx = b.x - a.x, dy = b.y - a.y;
99 const float length = std::sqrt(dx * dx + dy * dy);
100 if (length < 0.001f) return;
101 const float nx = -dy / length * width * 0.5f, ny = dx / length * width * 0.5f;
102 triangle({a.x + nx, a.y + ny}, {b.x + nx, b.y + ny}, {a.x - nx, a.y - ny}, tint);
103 triangle({a.x - nx, a.y - ny}, {b.x + nx, b.y + ny}, {b.x - nx, b.y - ny}, tint);
104 };
105 const auto arc = [&](float x, float y, float radius, float start, float end, float width, DWORD tint)
106 {
107 const int steps = (std::max)(2, static_cast<int>(std::fabs(end - start) * radius / 3.0f));
108 for (int i = 0; i < steps; ++i)
109 line(Radial(x, y, radius, start + (end - start) * i / steps),
110 Radial(x, y, radius, start + (end - start) * (i + 1) / steps), width, tint);
111 };
112
113 // DialogBase supplies the background and top border; only draw the instruments here.
114 line({208, 16}, {208, 92}, 1, lineColor);
115
116 // Pan is vehicle-relative: the fixed top index is forward; the wedge is the camera.
117 const float pan = std::isfinite(data.panRadians) ? std::fmod(data.panRadians, RiseMath::Pi * 2) : 0;
118 const float fov = std::isfinite(data.horizontalFov) ?
119 (std::max)(0.01f, (std::min)(data.horizontalFov, RiseMath::Pi)) : RiseMath::Pi * 0.5f;
120 const float start = pan - fov * 0.5f, end = pan + fov * 0.5f;
121 for (int i = 0; i < 24; ++i)
122 triangle({63, 55}, Radial(63, 55, 32, start + fov * i / 24),
123 Radial(63, 55, 32, start + fov * (i + 1) / 24), Rendering::ColorARGB(60, 125, 220, 218));
124 arc(63, 55, 34, 0, RiseMath::Pi * 2, 1, lineColor);
125 arc(63, 55, 32, start, end, 2, accent);
126 for (int i = 0; i < 12; ++i)
127 {
128 const float angle = i * RiseMath::Pi / 6;
129 line(Radial(63, 55, 35, angle), Radial(63, 55, i % 3 == 0 ? 40.0f : 38.0f, angle), 1, muted);
130 }
131 line({63, 13}, {63, 19}, 2, bright);
132 line({63, 55}, Radial(63, 55, 29, pan), 1.5f, accent);
133 arc(63, 55, 2.5f, 0, RiseMath::Pi * 2, 2, bright);
134
135 // Side-view camera arrow rotates about its mount. Level reference stays fixed.
136 const float tilt = std::isfinite(data.tiltRadians) ? data.tiltRadians : 0;
137 const auto tilted = [tilt](float x, float y) -> Point
138 {
139 return {158 + x * std::cos(tilt) + y * std::sin(tilt),
140 51 - x * std::sin(tilt) + y * std::cos(tilt)};
141 };
142 arc(158, 51, 22, 0, RiseMath::Pi * 2, 1, lineColor);
143 line({131, 51}, {143, 51}, 1, muted);
144 line({173, 51}, {185, 51}, 1, muted);
145 line({158, 24}, {158, 29}, 1, muted);
146 line({158, 73}, {158, 78}, 1, muted);
147 line(tilted(-12, 0), tilted(11, 0), 2, accent);
148 triangle(tilted(16, 0), tilted(7, -5), tilted(7, 5), accent);
149 line(tilted(-11, -5), tilted(-11, 5), 2, accent);
150
151 // Craft attitude uses the cockpit instrument bus, independently of camera PTZ.
152 // Project a two-tone sphere, so vertical and inverted flight stay unambiguous.
153 constexpr float ballX = 256, ballY = 55, ballRadius = 32;
154 const auto screen = [=](BallPoint p) -> Point
155 {
156 return {ballX + ballRadius * p.x, ballY + ballRadius * p.y};
157 };
158 const bool attitudeAvailable = data.vehicleAvailable &&
159 std::isfinite(data.pitchAttitudeRadians) && std::isfinite(data.rollAttitudeRadians);
160 if (attitudeAvailable)
161 {
162 // Cockpit Sprite::Update negates its rotation; use the same screen-space
163 // roll for both the ball and bank index while leaving pitch unchanged.
164 const float bankAngle = -data.rollAttitudeRadians;
165 const float cp = std::cos(data.pitchAttitudeRadians), sp = std::sin(data.pitchAttitudeRadians);
166 const float cr = std::cos(bankAngle), sr = std::sin(bankAngle);
167 const auto sphere = [&](float latitude, float longitude) -> BallPoint
168 {
169 const float x = std::cos(latitude) * std::sin(longitude);
170 const float y = std::sin(latitude), z = std::cos(latitude) * std::cos(longitude);
171 const float pitchedY = y * cp - z * sp;
172 // Positive pitch lowers the horizon; bankAngle applies the cockpit's
173 // inverse roll convention to this directly projected geometry.
174 return {x * cr + pitchedY * sr, x * sr - pitchedY * cr, y * sp + z * cp};
175 };
176 const auto limb = [](BallPoint a, BallPoint b) -> BallPoint
177 {
178 const float t = a.z / (a.z - b.z);
179 return {a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, 0};
180 };
181 const auto surface = [&](BallPoint a, BallPoint b, BallPoint c, bool sky)
182 {
183 const BallPoint input[] = {a, b, c};
184 BallPoint clipped[4];
185 int count = 0;
186 // Clip at the visible hemisphere before projecting onto the small dial.
187 for (int i = 0; i < 3; ++i)
188 {
189 const auto p = input[i], q = input[(i + 1) % 3];
190 if (p.z >= 0) clipped[count++] = p;
191 if ((p.z >= 0) != (q.z >= 0)) clipped[count++] = limb(p, q);
192 }
193 for (int i = 1; i + 1 < count; ++i)
194 {
195 const float depth = (clipped[0].z + clipped[i].z + clipped[i + 1].z) / 3;
196 const int shade = static_cast<int>((sky ? 198 : 24) + (sky ? 38 : 24) * depth);
197 triangle(screen(clipped[0]), screen(clipped[i]), screen(clipped[i + 1]),
198 Rendering::ColorARGB(255, shade, shade, shade));
199 }
200 };
201 constexpr float step = RiseMath::Pi / 18; // Ten-degree surface patches.
202 for (int latitude = -9; latitude < 9; ++latitude)
203 for (int longitude = 0; longitude < 36; ++longitude)
204 {
205 const auto a = sphere(latitude * step, longitude * step);
206 const auto b = sphere((latitude + 1) * step, longitude * step);
207 const auto c = sphere((latitude + 1) * step, (longitude + 1) * step);
208 const auto d = sphere(latitude * step, (longitude + 1) * step);
209 surface(a, b, c, latitude >= 0);
210 surface(a, c, d, latitude >= 0);
211 }
212 const auto ballLine = [&](BallPoint a, BallPoint b, float width, DWORD tint)
213 {
214 if (a.z < 0 && b.z < 0) return;
215 if ((a.z >= 0) != (b.z >= 0))
216 {
217 const auto intersection = limb(a, b);
218 if (a.z < 0) a = intersection; else b = intersection;
219 }
220 line(screen(a), screen(b), width, tint);
221 };
222 for (int latitude : {-60, -30, 0, 30, 60})
223 for (int longitude = 0; longitude < 144; ++longitude)
224 {
225 const auto a = sphere(RiseMath::ToRadian(static_cast<float>(latitude)), longitude * RiseMath::Tau / 144);
226 const auto b = sphere(RiseMath::ToRadian(static_cast<float>(latitude)), (longitude + 1) * RiseMath::Tau / 144);
227 if (latitude == 0) ballLine(a, b, 2.2f, Rendering::ColorARGB(255, 12, 12, 12));
228 const int shade = latitude > 0 ? 75 : 180;
229 ballLine(a, b, latitude == 0 ? 1.0f : 0.7f, Rendering::ColorARGB(255, shade, shade, shade));
230 }
231 for (int longitude = 0; longitude < 6; ++longitude)
232 for (int latitude = -36; latitude < 36; ++latitude)
233 {
234 const auto a = sphere(latitude * RiseMath::Pi / 72, longitude * RiseMath::Pi / 3);
235 const auto b = sphere((latitude + 1) * RiseMath::Pi / 72, longitude * RiseMath::Pi / 3);
236 const int shade = latitude >= 0 ? 75 : 140;
237 ballLine(a, b, 0.65f, Rendering::ColorARGB(255, shade, shade, shade));
238 }
239
240 // A fixed, outlined craft symbol stays readable over either hemisphere.
241 const Point craft[] = {{ballX - 17, ballY}, {ballX - 7, ballY},
242 {ballX, ballY + 4}, {ballX + 7, ballY}, {ballX + 17, ballY}};
243 for (float width : {3.5f, 1.5f})
244 for (int i = 0; i < 4; ++i)
245 line(craft[i], craft[i + 1], width, width > 2 ? Rendering::ColorARGB(255, 5, 5, 5) : Rendering::ColorARGB(255, 250, 250, 250));
246 // Moving bank index against the fixed marks around the top of the bezel.
247 triangle(Radial(ballX, ballY, 33, bankAngle), Radial(ballX, ballY, 38, bankAngle - 0.075f),
248 Radial(ballX, ballY, 38, bankAngle + 0.075f), Rendering::ColorARGB(255, 235, 235, 235));
249 }
250 else
251 {
252 for (int i = 0; i < 48; ++i)
253 triangle({ballX, ballY}, Radial(ballX, ballY, ballRadius, i * RiseMath::Tau / 48),
254 Radial(ballX, ballY, ballRadius, (i + 1) * RiseMath::Tau / 48), Rendering::ColorARGB(200, 20, 20, 20));
255 line({ballX - 12, ballY - 12}, {ballX + 12, ballY + 12}, 1.5f, muted);
256 line({ballX - 12, ballY + 12}, {ballX + 12, ballY - 12}, 1.5f, muted);
257 }
258 arc(ballX, ballY, 33, 0, RiseMath::Tau, 1.5f, Rendering::ColorARGB(235, 165, 165, 165));
259 for (int angle : {-90, -60, -30, 0, 30, 60, 90})
260 {
261 const float radians = RiseMath::ToRadian(static_cast<float>(angle));
262 line(Radial(ballX, ballY, 36, radians), Radial(ballX, ballY, angle % 60 == 0 ? 41.0f : 39.0f, radians),
263 1, Rendering::ColorARGB(235, 185, 185, 185));
264 }
265
266 // Keep the dialog's batch order and transform across the direct geometry pass.
267 RiseMath::Matrix previous, transform;
268 sprite->GetTransform(&previous);
269 if (FAILED(sprite->End())) return;
270 Rendering::RenderDevice* device = &sprite->Device();
271 Rendering::RenderStateSnapshot* saved = nullptr;
272 if (FAILED(device->CreateStateBlock(0, &saved))) { sprite->Begin(Rendering::SpriteFlags::AlphaBlend); return; }
273 device->SetVertexShader(nullptr);
274 device->SetPixelShader(nullptr);
275 device->SetTexture(0, nullptr);
297 device->DrawPrimitiveUP(Rendering::Primitive::TriangleList, static_cast<UINT>(vertices.size() / 3), vertices.data(), sizeof(Vertex));
298 saved->Apply();
299 saved->Release();
300 if (FAILED(sprite->Begin(Rendering::SpriteFlags::AlphaBlend))) return;
301
302 RiseMath::MatrixScaling(&transform, scale, scale, 1);
303 transform._41 = left;
304 transform._42 = top;
305 sprite->SetTransform(&transform);
306 const auto text = [&](Rendering::RenderFont* font, const wchar_t* value, LONG x, LONG y, LONG width, DWORD tint)
307 {
308 RECT rect = {x, y, x + width, y + 30};
309 // Keep each reading within its column.
310 font->DrawTextW(sprite, value, -1, &rect, DT_LEFT | DT_SINGLELINE, tint);
311 };
312 text(labelFont, L"PAN / FWD", 34, 96, 70, muted);
313 text(labelFont, L"TILT", 146, 10, 36, muted);
314 wchar_t buffer[64];
315 if (std::isfinite(data.zoom) && data.zoom > 0)
316 swprintf_s(buffer, L"%.1f\x00d7", data.zoom);
317 else wcscpy_s(buffer, L"--");
318 text(labelFont, buffer, 145, 85, 54, bright);
319 text(labelFont, L"ATTITUDE", 230, 96, 72, muted);
320
321 text(labelFont, L"HEADING", 314, 17, 54, muted);
322 if (data.vehicleAvailable && std::isfinite(data.headingDegrees))
323 swprintf_s(buffer, L"%03d\x00b0", ExternalCamera::HeadingDegrees(data.headingDegrees));
324 else wcscpy_s(buffer, L"--");
325 text(valueFont, buffer, 312, 37, 56, bright);
326 text(labelFont, L"TRUE", 314, 78, 54, muted);
327
328 const auto altitude = ExternalCamera::FormatAltitude(data);
329 text(labelFont, altitude.label, 378, 17, 70, muted);
330 text(valueFont, altitude.value.c_str(), 376, 37, 72, bright);
331 text(labelFont, altitude.units, 378, 78, 70, muted);
332
333 const auto speed = ExternalCamera::FormatSpeed(data);
334 text(labelFont, speed.label, 458, 17, 78, muted);
335 text(valueFont, speed.value.c_str(), 456, 37, 80, bright);
336 text(labelFont, speed.units, 458, 65, 78, muted);
337 if (data.vehicleAvailable && std::isfinite(data.verticalSpeedKms))
338 swprintf_s(buffer, L"V/S %+.0f M/S", data.verticalSpeedKms * 1000.0f);
339 else wcscpy_s(buffer, L"V/S --");
340 text(labelFont, buffer, 458, 86, 78, muted);
341 sprite->SetTransform(&previous);
342}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
Rendering::RenderFont * valueFont
Rendering::RenderFont * labelFont
HRESULT OnCreateDevice(Rendering::RenderDevice *device)
void Draw(Rendering::RenderSprite *sprite, const ExternalCamera::Readout &data, float left, float top, float scale)
HRESULT CreateStateBlock(UINT type, RenderStateSnapshot **result)
HRESULT SetPixelShader(std::nullptr_t)
HRESULT SetRenderState(DWORD state, DWORD value)
HRESULT SetFVF(DWORD value)
HRESULT DrawPrimitiveUP(Primitive type, UINT count, const void *vertices, UINT stride)
HRESULT SetTextureStageState(DWORD stage, DWORD state, DWORD value)
HRESULT SetTexture(DWORD stage, RenderTexture *texture)
HRESULT SetVertexShader(std::nullptr_t)
INT DrawTextW(RenderSprite *sprite, const wchar_t *text, INT count, RECT *rectangle, DWORD flags, PackedColor color)
HRESULT Begin(DWORD flags)
HRESULT SetTransform(const RiseMath::Matrix *value)
HRESULT GetTransform(RiseMath::Matrix *value) const
int HeadingDegrees(float degrees)
MetricText FormatAltitude(const Readout &data)
MetricText FormatSpeed(const Readout &data)
constexpr uint32_t INVSRCALPHA
constexpr uint32_t SRCALPHA
constexpr uint32_t ADD
constexpr uint32_t NONE
constexpr uint32_t SOLID
constexpr uint32_t COLORWRITEENABLE
constexpr uint32_t SRCBLEND
constexpr uint32_t SEPARATEALPHABLENDENABLE
constexpr uint32_t CULLMODE
constexpr uint32_t ALPHABLENDENABLE
constexpr uint32_t STENCILENABLE
constexpr uint32_t ALPHATESTENABLE
constexpr uint32_t SCISSORTESTENABLE
constexpr uint32_t FOGENABLE
constexpr uint32_t FILLMODE
constexpr uint32_t DESTBLEND
constexpr uint32_t LIGHTING
constexpr uint32_t ZWRITEENABLE
constexpr uint32_t ZENABLE
constexpr uint32_t BLENDOP
constexpr DWORD AlphaBlend
constexpr uint32_t DIFFUSE
constexpr uint32_t DISABLE
constexpr uint32_t SELECTARG1
constexpr uint32_t COLOROP
constexpr uint32_t COLORARG1
constexpr uint32_t ALPHAARG1
constexpr uint32_t ALPHAOP
constexpr uint32_t XYZRHW
constexpr uint32_t DIFFUSE
HRESULT CreateFontW(RenderDevice *owner, INT height, UINT width, UINT weight, UINT mipLevels, BOOL italic, DWORD charset, DWORD precision, DWORD quality, DWORD pitch, const wchar_t *face, RenderFont **result)
constexpr PackedColor ColorARGB(unsigned a, unsigned r, unsigned g, unsigned b)
uint32_t PackedColor
auto ToRadian(T value)
Definition ClientMath.h:13
constexpr float Pi
Definition ClientMath.h:12
constexpr float Tau
Definition ClientMath.h:12
Matrix * MatrixScaling(Matrix *out, float x, float y, float z)
Definition ClientMath.h:124