1#include "../Instruments/MFD/TerrainMapProjection.h"
2#include "../Viewscreen/GeometryBounds.h"
3#include "TerrainGeometry.generated.h"
4#include "NativeUiTest11.h"
5#include <array>
6#include <cstdlib>
7#include <iostream>
8#include <vector>
9
10static int checks = 0;
11static void Check(bool value, const char* message)
12{
13 ++checks;
14 if (!value) { std::cerr << "FAIL: " << message << '\n'; std::exit(1); }
15}
16static constexpr float Radius = 2939.6f;
18struct Vertex {
RiseMath::Vector3 position = {0,0,0};
unsigned char pri_landform = 255; };
19static Vertex patch[64][64];
20static const int centerU[] = {1019,3057,-1019,5095,1019,1019};
21static const int centerV[] = {1019,1019,1019,1019,-1019,3057};
22
23
25{
26 static const RiseMath::Vector3 normal[] = {{0,-1,0},{1,0,0},{-1,0,0},{0,1,0},{0,0,-1},{0,0,1}};
27 static const RiseMath::Vector3 east[] = {{1,0,0},{0,1,0},{0,-1,0},{-1,0,0},{1,0,0},{1,0,0}};
28 static const RiseMath::Vector3 down[] = {{0,0,1},{0,0,1},{0,0,1},{0,0,1},{0,-1,0},{0,1,0}};
29 auto p = normal[face] + east[face] * ((u - centerU[face]) / 1019) + down[face] * ((v - centerV[face]) / 1019);
31 return p * -Radius;
32}
33
34static void MakePatch(int face, int u, int v)
35{
36 for (int x = 0; x < 64; ++x)
37 for (int y = 0; y < 64; ++y)
38 {
39 char rotation;
40 auto tcp = geometry.
CalculateWrap(
short(u+x-31),
short(v+y-31),
char(face), &rotation);
41 patch[x][y] = {};
42 if (tcp.u == 30000 || tcp.v == 30000) continue;
45 patch[x][y].position = p * -Radius;
46 patch[x][y].pri_landform = 0;
47 }
48}
49
51{
58 return view * rotate;
59}
60
61static std::array<RiseMath::Vector2,4> TransformCorners(
const RiseMath::Matrix& m)
62{
63 std::array<RiseMath::Vector2,4> result;
64 const RiseMath::Vector3 texture[] = {{0,0,.1f},{32,0,.1f},{32,32,.1f},{0,32,.1f}};
65 for (int i = 0; i < 4; ++i)
66 {
69 Check(std::fabs(p.z - .1f) < .000001f, "quad changed sprite depth");
70 result[i] = {p.x,p.y};
71 }
72 return result;
73}
74
76{ return (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x); }
77static bool Contains(
const std::array<RiseMath::Vector2,4>& q,
RiseMath::Vector2 p)
78{
79 bool positive = false, negative = false;
80 for (int i = 0; i < 4; ++i)
81 {
82 float c =
Cross(q[i], q[(i+1)%4], p);
83 positive |= c > .0001f; negative |= c < -.0001f;
84 }
85 return !(positive && negative);
86}
87
88static void CheckMap(
int face,
int u,
int v,
RiseMath::Vector3 ship,
int knownMissingSamples = 0)
89{
90 MakePatch(face,u,v);
91 for (float heading : {0.0f, .907571f, 2.4f, 4.066617f, 6.1f})
92 {
93 auto view = MapView(ship,heading);
94 std::vector<std::array<RiseMath::Vector2,4>> quads;
95 for (int x = 0; x < 63; ++x)
96 for (int y = 0; y < 63; ++y)
97 {
98 if ((x-31)*(x-31)+(y-31)*(y-31) > 1024) continue;
101 auto quad = TransformCorners(m);
102 const int dx[] = {0,1,1,0}, dy[] = {0,0,1,1};
103 for (int k = 0; k < 4; ++k)
104 {
106 Check(
RiseMath::Vec2Length(&(p -= quad[k])) < .0001f,
"sprite corner missed its real terrain vertex");
107 }
108 quads.push_back(quad);
109 }
110 Check(!quads.empty() && quads.size() <= 3205, "invalid compacted tile count");
111 if (knownMissingSamples >= 0)
112 {
113 int missing = 0;
114 for (int angle = 0; angle < 720; ++angle)
115 {
118 bool found = false;
119 for (const auto& q : quads) if (Contains(q,p)) { found = true; break; }
120 if (!found) ++missing;
121 }
122 if (missing && heading == 0) std::cout << "KNOWN SOURCE GAP face=" << face << " tcp=" << u << ',' << v << " missing=" << missing << "/720\n";
123 Check(missing <= knownMissingSamples, "map coverage regressed beyond available source geometry");
124 }
125 }
126}
127
128
130{
131 switch(n) { case 0:return p.w+p.x; case 1:return p.w-p.x; case 2:return p.w+p.y;
132 case 3:return p.w-p.y; case 4:return p.z; default:return p.w-p.z; }
133}
135{
136 std::vector<RiseMath::Vector4> polygon;
138 for (int plane = 0; plane < 6 && !polygon.empty(); ++plane)
139 {
140 std::vector<RiseMath::Vector4> clipped;
141 auto previous = polygon.back();
float d0 =
Plane(previous,plane);
142 for (auto current : polygon)
143 {
144 float d1 =
Plane(current,plane);
145 if ((d0 >= 0) != (d1 >= 0)) clipped.push_back(previous + (current-previous)*(d0/(d0-d1)));
146 if (d1 >= 0) clipped.push_back(current);
147 previous = current; d0 = d1;
148 }
149 polygon = std::move(clipped);
150 }
151 return !polygon.empty();
152}
153
154static void CheckLightingRange()
155{
158 Check(bounds.
IsWithinDistance(eye, 4.0f),
"unknown terrain bounds lost local lighting");
161 Check(bounds.
IsWithinDistance(eye, 4.0f),
"tile enclosing the vehicle lost local lighting");
165 Check(bounds.
IsWithinDistance(eye, 4.0f),
"near tile edge used the distant shader");
170 Check(!bounds.
IsWithinDistance(eye, 4.0f),
"diagonally distant tile used the nearby shader");
171
172
173
174 for (int face = 0; face < 6; ++face)
175 for (int sideU : {-1, 0, 1}) for (int sideV : {-1, 0, 1})
176 {
177 const float u = float(centerU[face] + sideU * 1018);
178 const float v = float(centerV[face] + sideV * 1018);
179 const auto origin = FacePosition(face, u, v);
181 for (int x = 0; x < 2; ++x) for (int y = 0; y < 2; ++y)
182 bounds.
Include(FacePosition(face, u + x, v + y) - origin);
183 for (float fraction : {.05f, .5f, .95f})
184 {
185 const auto ground = FacePosition(face, u + fraction, v + fraction);
188 const auto localEye = ground + up * .002f - origin;
189 Check(bounds.
IsWithinDistance(localEye, 4.0f),
"nearby ground lost lighting at a cube face or corner");
190 Check(!bounds.
IsWithinDistance(localEye + up * 20.0f, 4.0f),
"ground far below used the nearby shader");
191 }
192 }
193}
194
195static void CheckCulling()
196{
197 int visible = 0, culled = 0, intersectingWithOriginOutside = 0, oldFalseRejections = 0;
198 for (int face = 0; face < 6; ++face)
199 for (int sideU : {-1,1}) for (int sideV : {-1,1})
200 {
201 int u = centerU[face] + sideU*1018, v = centerV[face] + sideV*1018;
202 MakePatch(face,u,v);
203 auto origin = FacePosition(face,float(u)+.3f,float(v)+.6f);
208 for (int heading = 0; heading < 360; heading += 30)
209 for (float pitch : {-.6f,0.0f,.6f}) for (float roll : {-.7f,.7f})
210 for (float fov : {.5f,1.1f,1.8f})
211 {
213 auto forward = (north*std::cos(a)+east*std::sin(a))*std::cos(pitch)+up*std::sin(pitch);
217 cameraUp = cameraUp*std::cos(roll)+right*std::sin(roll);
218 auto eye = up*.5f,
target = eye+forward;
222 auto m = view*proj;
223
224
228 };
229 auto plotNorth=tangent(patch[31][31].position-patch[31][32].position);
230 auto look=tangent(forward);
232 float oldHeading=std::acos((std::max)(-1.0f,(std::min)(1.0f,cosine)));
236 for (int x = 0; x < 63; x += 3) for (int y = 0; y < 63; y += 3)
237 {
238 if (patch[x][y].pri_landform == 255 || patch[x+1][y].pri_landform == 255 ||
239 patch[x+1][y+1].pri_landform == 255 || patch[x][y+1].pri_landform == 255) continue;
240 RiseMath::Vector3 p[] = {patch[x][y].position-origin,patch[x+1][y].position-origin,
241 patch[x][y+1].position-origin,patch[x+1][y].position-origin,
242 patch[x+1][y+1].position-origin,patch[x][y+1].position-origin};
245 bool reference = TriangleVisible(p,m) || TriangleVisible(p+3,m);
246 Check(!reference || hit,"a triangle intersecting the current view was culled");
247 if (!hit) ++culled;
248 if (reference)
249 {
250 ++visible;
252 index={index.x*oldRotation._11+index.y*oldRotation._21,
253 index.x*oldRotation._12+index.y*oldRotation._22};
254 int ix=
int(index.x),iy=
int(index.y);
255 bool oldVisible=ix>=-31 && ix<32 && iy>=-31 && iy<32 &&
256 ((std::abs(ix)<std::fabs(std::tan(fov*.5f)*iy)+6.25f && iy<=0 && ix*ix+iy*iy<=1024) || ix*ix+iy*iy<4);
257 oldFalseRejections += !oldVisible;
259 for (
int n = 0; n < 6; ++n)
if (
Plane(corner,n) < 0) { ++intersectingWithOriginOutside;
break; }
260 }
261 }
262 }
263 }
264 Check(culled > 10000 && visible > 10000 && intersectingWithOriginOutside > 1000,"culling sweep did not exercise its boundaries");
265 Check(oldFalseRejections>1000,"old flat-index cull did not reproduce premature disappearance");
266 std::cout << "Culling: " << visible << " visible triangles, " << culled << " boxes rejected, "
267 << intersectingWithOriginOutside << " visible plots with origin outside, " << oldFalseRejections << " old false rejections\n";
268}
269
270static void CheckBoundaryCases()
271{
272 MakePatch(0,1019,1019);
273 auto view = MapView(FacePosition(0,1019,1019),0);
275 for (int x : {-1,0,62,63}) for (int y : {-1,0,62,63})
277 (x>=0 && x<63 && y>=0 && y<63),"cell boundary validation failed");
278 patch[32][32].pri_landform = 255;
279 Check(!
TerrainMapProjection::CellTransform(patch,31,31,Radius,view,1,1,{0,0},m),
"missing diagonal was painted as data");
280 patch[32][32].pri_landform = 0; patch[32][32].position = {0,0,0};
281 Check(!
TerrainMapProjection::CellTransform(patch,31,31,Radius,view,1,1,{0,0},m),
"cleared tile was painted");
286 Check(bounds.
IntersectsFrustum(m),
"box enclosing view with all corners outside was rejected");
290 Check(bounds.
IntersectsFrustum(m),
"overhanging structure was culled using its origin");
291}
292
293static void CheckAlignment()
294{
295 for (int face=0;face<6;++face)
296 for (float offset : {0.05f,0.5f,1.5f,15.0f})
297 {
298 auto ship=FacePosition(face,centerU[face]+1019-offset,centerV[face]-1019+offset);
299 auto terrain=FacePosition(face,centerU[face]+1018-offset,centerV[face]-1017+offset);
300 for(float heading : {0.0f,1.0f,3.0f,5.0f}) for(float range : {1.0f,5.0f,20.0f,80.0f})
301 {
302 auto view=MapView(ship,heading);
303 float px=132.5f/range*.5f,py=132.5f/range*.75f;
307 Check(std::fabs(point.x-(137-result.x*px))<.04f && std::fabs(point.y-(201+result.y*py))<.04f,
308 "terrain does not use waypoint bearing/zoom/nonuniform display scale");
309 auto raised=terrain*1.01f;
311 Check(
RiseMath::Vec2Length(&(same-=point))<.04f,
"terrain elevation displaced its map footprint");
312
317 auto ring=ship*std::cos(range/Radius)+east*(Radius*std::sin(range/Radius));
319 Check(std::fabs(
RiseMath::Vec2Length(&ringPoint)-132.5f)<.04f,
"terrain range and ring scale disagree");
320 }
321 }
322}
323
324static void CheckSeamMovement()
325{
326 const auto corner=FacePosition(0,2038,0);
333 int faces=0;
334 for (int step=0;step<=72;++step)
335 {
337 auto ship=corner+(east*std::cos(angle)+north*std::sin(angle))*2;
342 faces|=1<<face;
343 MakePatch(face,tcp.u,tcp.v);
344 float nearest=100;
345 for(int x=0;x<64;++x) for(int y=0;y<64;++y)
346 {
347 if(patch[x][y].pri_landform==255) continue;
348 auto delta=patch[x][y].position-corner;
350 }
351 Check(nearest<.001f,"face crossing lost the common corner vertex");
352 auto view=MapView(ship,.907571f);
354 if(step) {
auto delta=point-previous;Check(
RiseMath::Vec2Length(&delta)<.18f,
"map jumped when the packet face changed");}
355 previous=point;
356 }
357 Check(faces==((1<<0)|(1<<1)|(1<<4)),"movement did not cross all three faces");
358}
359
360static void CheckSpriteRendering(const wchar_t* output)
361{
362 NativeUiTest11 fixture(1100,600); auto* device=&fixture.device;
366 Check(SUCCEEDED(
Rendering::LoadTexture(device,L
"Textures/Instruments/colorBox.png",&texture,1)),
"real terrain texture unavailable");
371 MakePatch(0,2037,0);
372 auto ship = FacePosition(0,2037.8f,.05f);
375 for (int panel=0;panel<2;++panel)
376 {
377 const RECT clip = {panel*550,65,(panel+1)*550,540}; device->SetScissorRect(&clip);
378 for (int x=0;x<63;++x) for (int y=0;y<63;++y)
379 {
380 if ((x-31)*(x-31)+(y-31)*(y-31)>1024) continue;
383 auto p = patch[x][y].position;
384 DWORD color = std::fabs(p.z)>std::fabs(p.x) && std::fabs(p.z)>std::fabs(p.y) ? 0xffa7b19c :
385 (std::fabs(p.x)>std::fabs(p.y) ? 0xff92662e : 0xffc6a572);
386 if (panel==1) { sprite->
SetTransform(&m); sprite->
Draw(texture,&rectangle,
nullptr,&position,color);
continue; }
387
388 const float size=20, angle=-
RiseMath::ToRadian(52), c=std::cos(angle), s=std::sin(angle);
390 for(int k=0;k<4;++k)
391 {
392 float a=(x-31-.8f+dx[k])*size,b=(y-31-.05f+dy[k])*size;
393 q[k]={275+a*c-b*s,300+a*s+b*c};
394 }
396 sprite->
SetTransform(&m); sprite->
Draw(texture,&rectangle,
nullptr,&position,color);
397 }
399 }
402 device->EndScene();
403 const auto pixels=fixture.Pixels();
404 int oldMissing=0,newMissing=0;
405 for(int x=-180;x<=180;++x) for(int y=-180;y<=180;++y)
406 {
407 if(x*x+y*y>180*180) continue;
408 const auto row=pixels.data()+(300+y)*1100;
409 oldMissing += (row[275+x]&0xffffff)==0x10151d;
410 newMissing += (row[825+x]&0xffffff)==0x10151d;
411 }
412 std::cout << "Native D3D11 sprite coverage: before=" << oldMissing << " uncovered pixels, after=" << newMissing << '\n';
413 Check(oldMissing>10000,"flat-net visual did not reproduce the quadrant");
414 Check(newMissing==0,"native D3D11 sprite left cracks in projected cells");
415
416 device->BeginScene();
417 struct LineVertex {
float x,y,z,rhw;
DWORD color;};
418 device->SetTexture(0,nullptr);
420 for(float center : {275.0f,825.0f}) for(float radius : {100.0f,200.0f})
421 {
422 LineVertex points[181];
423 for(
int i=0;i<=180;++i) {
float a=i*
RiseMath::Pi/90;points[i]={center+radius*std::cos(a),300+radius*std::sin(a),0,1,0xffeeeeee};}
425 }
427 Check(SUCCEEDED(
Rendering::CreateFontW(device,22,0,FW_NORMAL,1,FALSE,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,L
"Segoe UI",&font)),
"font creation failed");
428 RECT title={25,12,540,60}; font->
DrawTextW(
nullptr,L
"Before: flat cube grid",-1,&title,DT_LEFT,0xffffffff);
429 title={575,12,1090,60};font->
DrawTextW(
nullptr,L
"After: actual terrain footprints",-1,&title,DT_LEFT,0xffffffff);
430 title={25,552,1090,598};font->
DrawTextW(
nullptr,L
"PM / EP / NP corner | heading 052 | rings 10 / 20 km | synthetic face colors",-1,&title,DT_LEFT,0xffffffff);
431 for(
int center : {275,825}) {title={center-8,285,center+20,320};font->
DrawTextW(
nullptr,L
"+",-1,&title,DT_LEFT,0xffffffff);}
432 font->
Release(); device->EndScene();
433 fixture.Save(output);texture->
Release();sprite->
Release();fixture.DebugClean();
434}
435
436int wmain(int argc, wchar_t** argv)
437{
438 if (argc == 2) { CheckSpriteRendering(argv[1]); return 0; }
439 CheckBoundaryCases();
440 CheckAlignment();
441 CheckSeamMovement();
442 for (int face = 0; face < 6; ++face)
443 {
444
445 CheckMap(face,centerU[face]+10,centerV[face]+10,FacePosition(face,float(centerU[face]+10.3f),float(centerV[face]+10.7f)));
446 for (int x : {-1,1}) for (int y : {-1,1})
447 {
448 int u=centerU[face]+x*1018, v=centerV[face]+y*1018;
449
450
451 static const int missing[6][4] = {{17,17,0,33},{0,0,16,0},{0,17,16,33},
452 {0,0,0,17},{17,0,0,0},{17,50,0,49}};
453 int corner = (x+1)+(y+1)/2;
454 CheckMap(face,u,v,FacePosition(face,float(centerU[face]+x*1019),float(centerV[face]+y*1019)),missing[face][corner]);
455 }
456 }
457
458
459 CheckMap(0,2037,0,FacePosition(0,2037.8f,.05f));
460 CheckMap(1,2038,0,FacePosition(1,2038.05f,.1f));
461 CheckMap(4,2037,-1,FacePosition(4,2037.9f,-.1f));
462 char rotation;
464 Check(tcp.u==2039 && tcp.v==0,"PM closing adjacency regressed at (2038,-1)");
466 Check(tcp.u==2038 && tcp.v==1,"NP closing edge regression");
467 CheckLightingRange();
468 CheckCulling();
469 std::cout << "PASS " << checks << " terrain display checks\n";
470}
SCRIPT_CONTROL SCRIPT_STATE const void int
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
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 Draw(RenderTexture *image, const RECT *rectangle, const RiseMath::Vector3 *center, const RiseMath::Vector3 *position, PackedColor color)
VECTOR2SHORT CalculateCGC(RiseMath::Vector3 position, char cube) const
VECTOR2SHORT CalculateWrap(short u, short v, char cube, char *rotate) const
RiseMath::Vector3 CalculatePlanar(short u, short v)
char CalculateCube(RiseMath::Vector3 position) const
constexpr uint32_t TARGET
constexpr uint32_t SCISSORTESTENABLE
constexpr uint32_t ZENABLE
constexpr DWORD AlphaBlend
constexpr DWORD SortTexture
constexpr DWORD SortBackToFront
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)
HRESULT CreateSprite(RenderDevice *owner, RenderSprite **result)
HRESULT LoadTexture(RenderDevice *owner, const wchar_t *path, RenderTexture **result, UINT levels=0, UINT width=0, UINT height=0)
float Vec3Length(const Vector3 *a)
Vector3 * Vec3Cross(Vector3 *out, const Vector3 *a, const Vector3 *b)
Matrix * MatrixLookAtRH(Matrix *out, const Vector3 *eye, const Vector3 *at, const Vector3 *up)
Vector3 * Vec3TransformCoord(Vector3 *out, const Vector3 *a, const Matrix *m)
float Vec2Length(const Vector2 *a)
float Vec3LengthSq(const Vector3 *a)
Matrix * MatrixLookAtLH(Matrix *out, const Vector3 *eye, const Vector3 *at, const Vector3 *up)
Matrix * MatrixPerspectiveFovRH(Matrix *out, float fov, float aspect, float nearZ, float farZ)
Matrix * MatrixIdentity(Matrix *out)
float Vec3Dot(const Vector3 *a, const Vector3 *b)
Vector4 * Vec3Transform(Vector4 *out, const Vector3 *a, const Matrix *m)
Vector3 * Vec3Normalize(Vector3 *out, const Vector3 *a)
Matrix * MatrixRotationZ(Matrix *out, float value)
RiseMath::Vector2 Project(const RiseMath::Vector3 &terrainPosition, float radius, const RiseMath::Matrix &mapView, float pixelsPerKmX, float pixelsPerKmY, RiseMath::Vector2 center)
bool QuadTransform(const RiseMath::Vector2(&p)[4], float width, float height, float depth, RiseMath::Matrix &transform)
bool CellTransform(const Vertex(&grid)[64][64], int u, int v, float radius, const RiseMath::Matrix &mapView, float pixelsPerKmX, float pixelsPerKmY, RiseMath::Vector2 center, RiseMath::Matrix &transform, float minimumPixels=0)
bool IsWithinDistance(const Vector &point, float distance) const
void Include(const Vector &p)
bool IntersectsFrustum(const Matrix &m, float guardX=0, float guardY=0) const