Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/SpatialFogWeatherTests.h

Source at documentation-generation time. See Test catalog for fixture associations.

1#pragma once
2
3static void CheckSpatialFogTransport()
4{
5 using namespace SpatialFogWeather;
6 const auto field=[](int face,uint64_t revision,float offset=0) {
7 auto g=std::make_shared<Grid>();g->cube=face;g->u=CloudBaseWeather::CenterU[face];
8 g->v=CloudBaseWeather::CenterV[face];g->revision=revision;
9 for(int y=0;y<64;++y)for(int x=0;x<64;++x)
10 g->cells[y*64+x]={8+offset+.3f*x+.1f*y,2+.1f*x};
11 return g;
12 };
13 for(int face=0;face<6;++face)
14 {
15 auto g=field(face,1);
16 const auto p=Direction(face,g->u,g->v),position=Scale(p,-radiusC);
17 const auto e=CloudBaseWeather::East[face];const Point wind{e.x*.01,e.y*.01,e.z*.01};
18 const auto expected=g->Sample(p);
19 Store one;one.Accept(g,true);one.Advance(60,position,wind);
20 const double angle=.6/radiusC;
21 const auto moved=Add(Scale(p,std::cos(angle)),Scale(wind,-std::sin(angle)/.01));
22 Check(std::abs(one.Sample(moved).visibility-expected.visibility)<.00001,
23 "fog must move downwind by 600 meters in 60 seconds at 10 m/s on every face");
24 Check(std::abs(one.Sample(p).visibility-expected.visibility)>.01,
25 "spatial fog stayed fixed between packets");
26 for(int fps:{30,60,144})
27 {
28 Store frames;frames.Accept(g,true);
29 for(int i=0;i<60*fps;++i)frames.Advance(1.0/fps,position,wind);
30 Check(std::abs(frames.Sample(p).visibility-one.Sample(p).visibility)<.00001,
31 "fog drift depends on frame rate");
32 }
33 const auto still=one.Sample(p);const auto revision=one.Generation();
34 one.Advance(10,position,{});
35 one.Advance(0,position,wind);one.Advance(-1,position,wind);
36 one.Advance(std::numeric_limits<double>::infinity(),position,wind);
37 one.Advance(1,position,{std::numeric_limits<double>::quiet_NaN(),0,0});
38 Check(one.Generation()==revision && one.Sample(p).visibility==still.visibility,
39 "calm or invalid time/wind changed fog displacement");
40 // Opposite wind cancels transport; input wind is not a density correction.
41 one.Advance(60,position,Scale(wind,-1));
42 Check(std::abs(one.Sample(p).visibility-expected.visibility)<.00001,"reversed wind has wrong sign");
43
44 Store corrected;corrected.Accept(g,true);corrected.Advance(60,position,wind);
45 const auto before=corrected.Sample(p);
46 auto next=field(face,2,4);corrected.Accept(next,true);
47 Check(corrected.Sample(p).visibility==before.visibility,"full snapshot snapped fog");
48 corrected.Advance(5,position,{});
49 Check(std::abs(corrected.Sample(p).visibility-(before.visibility+next->Sample(p).visibility)*.5f)<.00001,
50 "full snapshot correction midpoint");
51 const auto halfway=corrected.Sample(p);
52 corrected.Accept(field(face,3,8),true);
53 Check(std::abs(corrected.Sample(p).visibility-halfway.visibility)<.00001,
54 "frequent full snapshots restarted from an invisible endpoint");
55 corrected.Advance(10,position,{});
56 Check(std::abs(corrected.Sample(p).visibility-field(face,3,8)->Sample(p).visibility)<.00001,
57 "full snapshot correction did not converge");
58
59 // Strip overlap has the same world values and the same snapshot age.
60 Store strips;strips.Accept(g,true);strips.Advance(60,position,wind);
61 const auto beforeStrip=strips.Sample(p);
62 auto shifted=std::make_shared<Grid>(*g);++shifted->u;shifted->revision=2;
63 for(int y=0;y<64;++y)for(int x=0;x<63;++x)shifted->cells[y*64+x]=g->cells[y*64+x+1];
64 strips.Accept(shifted,false);
65 Check(std::abs(strips.Sample(p).visibility-beforeStrip.visibility)<.00001,
66 "strip update reset weather age or shifted geography");
67 strips.Advance(60,position,wind);
68 Store reference;reference.Accept(g,true);reference.Advance(120,position,wind);
69 Check(std::abs(strips.Sample(p).visibility-reference.Sample(p).visibility)<.00001,
70 "drift did not continue across a strip update");
71 Check(strips.Sample(Scale(p,-1)).visibility==42.16f,"opposite hemisphere reused local fog");
72 strips.Reset();Check(!strips.Ready(),"fog reset retained a prediction");
73 }
74 // A front may cross a cube seam without changing its speed or direction.
75 auto seam=field(0,1);seam->u=2037;
76 const auto raw=Direction(0,seam->u,seam->v),p=Scale(raw,1/std::sqrt(Dot(raw,raw)));
77 const Point tangent{p.y,-p.x,0},wind=Scale(tangent,-.06);
78 const auto position=Scale(p,-radiusC);Store cross;cross.Accept(seam,true);
79 const double angle=3.6/radiusC;
80 const auto moved=Add(Scale(p,std::cos(angle)),Scale(wind,-std::sin(angle)/.06));
81 cross.Advance(60,position,wind);
82 Check(std::abs(cross.Sample(moved).visibility-seam->Sample(p).visibility)<.0001,
83 "cube seam disrupted fog transport");
84 // Missing coverage fades to fallback instead of stretching the edge cell.
85 Check(seam->Sample(Direction(0,seam->u+40,seam->v)).visibility==42.16f,
86 "uncovered upwind weather was extrapolated");
87}
88
89static void CheckSpatialFogLifecycle(Viewscreen& view,grid& terrain,Receiver& receiver)
90{
91 auto& weather=view.weather;auto& rain=view.cloudVolume.renderer;
94 playerships[0].position={0,-(radiusC+.4f),0};
95 view.posnorml={0,-1,0};view.compassnorth={1,0,0};
96 view.volumeEnabled=true;weather.visibilityOverrideKm=-1;
97 LoadTerrain(view,{1019,1019},0,450);
98 auto packet=StormGrid({1019,1019},0,2000,7,false);
99 for(int q=0;q<64;++q)for(int r=0;r<64;++r)
100 {
101 const size_t offset=17+(q*64+r)*sizeof(SRenderWeather);
102 const uint16_t visibility=uint16_t(5000+q*500);
103 packet[offset+8]=uint8_t(visibility);packet[offset+9]=uint8_t(visibility>>8);
104 // Deliberately disagree with Weather1, which must own fog velocity.
105 packet[offset+10]=80;packet[offset+11]=255;
106 }
107 receiver.Receive(packet);weather.SetWindDirectionRadians(0);weather.SetWindspeedKms(.01f);
109 const auto before=rain.latest.cells[31*64+31];const auto snapshot=TerrainSnapshot();
110 const auto uploads=rain.uploads;
112 const auto after=rain.latest.cells[31*64+31];
113 // Physical +x is decreasing u on this face in render coordinates.
114 const float expected=20.5f-float(std::tan(.6/(radiusC+.4))*1019)*.5f;
115 Check(std::abs(after.z-expected)<.0001f && after.z!=before.z,
116 "production fog packing did not use Weather1 direction/speed at 1x");
117 Check(after.x==before.x && after.y==before.y,
118 "fog advection moved terrain or cloud-base heights");
119 TerrainUnchanged(snapshot);
120 Check(rain.uploads==uploads+1,"fog drift did not refresh GPU grid");
121 weather.PrepareRainCurtains(true);Check(rain.uploads==uploads+1,"same-frame fog uploaded twice");
122
123 auto replacement=packet;Put32(replacement.data()+9,2001);
124 for(int q=0;q<64;++q)for(int r=0;r<64;++r)
125 {const size_t at=17+(q*64+r)*sizeof(SRenderWeather)+8;replacement[at]=0x10;replacement[at+1]=0x27;}
126 receiver.Receive(replacement);weather.PrepareRainCurtains(true);
127 Check(std::abs(rain.latest.cells[31*64+31].z-after.z)<.0001f,"packet receiver snapped rendered fog");
129 Check(std::abs(rain.latest.cells[31*64+31].z-(after.z+10)*.5f)<.0001f,"rendered fog reconciliation midpoint");
131 Check(std::abs(rain.latest.cells[31*64+31].z-10)<.0001f,"rendered fog reconciliation completion");
132
134 Check(!weather.spatialFogWeather.Ready() && !rain.active,"simulator kept fog displacement/transition");
135 Put32(replacement.data()+9,2002);receiver.Receive(replacement);
136 Check(!weather.spatialFogWeather.Ready(),"simulator received weather started fog prediction");
138 Check(rain.active && Near(rain.latest.cells[31*64+31].z,10),"simulator exit replayed old fog");
139 g_bAboveClouds=true;playerships[0].mslKm=100;
141 Check(!weather.spatialFogWeather.Ready() && !rain.active,"orbital coast retained fog prediction");
143 Check(!weather.spatialFogWeather.Ready(),"descending hysteresis band reactivated fog");
145 receiver.Receive(replacement);weather.AdvanceSpatialFog(1);
146 Check(!weather.spatialFogWeather.Ready(),"descent accepted stale token fog");
147 Put16(replacement.data()+7,weather.weatherRequestToken);Put32(replacement.data()+9,2003);
148 receiver.Receive(replacement);weather.AdvanceSpatialFog(0);weather.PrepareRainCurtains(true);
149 Check(rain.active && Near(rain.latest.cells[31*64+31].z,10),"fresh descent failed to restore spatial fog");
150 weather.QueuePlotWeatherReset();Check(!weather.spatialFogWeather.Ready(),"teleport/reset retained fog motion");
151 weather.ResetPlotWeatherConnection();Check(!weather.spatialFogWeather.Ready() && !rain.active,"reconnect retained fog motion");
152 rain.uploads=0;terrain.ElevationChanged();view.compassnorth={0,0,-1};weather.SetWindspeedKms(0);
153}
Rendering::CloudRenderer11 renderer
void Advance(double elapsed, Point physicalPosition, Point physicalWindKms)
void Accept(GridPtr next, bool full)
Cell Sample(Point renderPosition) const
CloudVolumeRenderer cloudVolume
Definition Viewscreen.h:396
RiseMath::Vector3 compassnorth
Definition Viewscreen.h:107
RiseMath::Vector3 posnorml
Definition Viewscreen.h:383
Definition grid.h:14
void ElevationChanged() const
Definition grid.h:55
void QueuePlotWeatherReset()
Definition weather.cpp:1060
void SetWindDirectionRadians(float prmWinddir)
Definition weather.cpp:856
void PrepareRainCurtains(bool exteriorView)
Definition weather.cpp:1375
void SetWindspeedKms(float prmWindspeed)
Definition weather.cpp:881
uint16_t weatherRequestToken
Definition weather.h:85
float visibilityOverrideKm
Definition weather.h:65
SpatialFogWeather::Store spatialFogWeather
Definition weather.h:87
void AdvanceSpatialFog(float elapsed)
Definition weather.cpp:998
void ResetPlotWeatherConnection()
Definition weather.cpp:1008
void UpdatePlotWeatherConditions()
Definition weather.cpp:1085
s_network_objects playerships[MAX_SCAN]
Definition globals.cpp:112
bool g_bAboveClouds
Definition globals.cpp:16
#define radiusC
Definition globals.h:87
constexpr int CenterV[]
constexpr Point East[]
constexpr int CenterU[]
SRenderWeather Sample(const unsigned char *p)
bool Near(const Body &b, const Triangle &t, double extra)
Definition RigidProp.h:141
double Dot(Point a, Point b)
Point Direction(int cube, double u, double v)
Point Add(Point a, Point b)
Point Scale(Point a, double b)
RiseMath::Vector3 position
Definition globals.h:511