Rise
The Vieneo Province
Loading...
Searching...
No Matches
TerrainSnow.h
Go to the documentation of this file.
1#pragma once
3#include <unordered_map>
4#include <algorithm>
5
6// Values remain attached to canonical plot coordinates across independent grids.
7// Terrain raster colors are never mutated by weather.
8namespace TerrainSnow
9{
10 inline uint32_t Key(VECTOR2SHORT p) { return uint16_t(p.u) | (uint32_t(uint16_t(p.v)) << 16); }
11 struct Surface { float depthMm=0, ageHours=0, waterMm=0; };
12 inline RiseMath::Vector4 Tint(const Surface& surface,float latitude,float sunLatitude)
13 {
14 const float polar=std::clamp((fabsf(latitude)-fabsf(sunLatitude)-45.f)/20.f,0.f,1.f);
15 const float cover=(std::max)(std::clamp(surface.depthMm/40.f,0.f,1.f),polar);
16 const float age=std::clamp(surface.ageHours/240.f,0.f,1.f);
17 return {(232.f-32.f*age)*cover/255.f,(239.f-34.f*age)*cover/255.f,
18 (250.f-35.f*age)*cover/255.f,cover};
19 }
20 class Store
21 {
22 struct Cell { Surface value,target; uint64_t touched=0; };
23 std::unordered_map<uint32_t,Cell> cells;
24 uint64_t generation=0;
25 public:
26 void Reset(){cells.clear();generation=0;}
27 size_t Size() const {return cells.size();}
28 void Begin(){++generation;}
29 void Retain(VECTOR2SHORT p){auto it=cells.find(Key(p));if(it!=cells.end())it->second.touched=generation;}
30 void Target(VECTOR2SHORT p,const SRenderWeather& sample)
31 {
32 if(!sample.HasVisibility())return;
33 const Surface target{float(sample.snowDepthMm),float(sample.snowAgeHours),sample.surfaceWaterTenthsMm*.1f};
34 auto result=cells.try_emplace(Key(p),Cell{target,target,generation});
35 result.first->second.target=target;result.first->second.touched=generation;
36 }
37 // Call after retaining current weather AND terrain footprints: <=8192 entries.
38 void End(){for(auto i=cells.begin();i!=cells.end();)if(i->second.touched!=generation)i=cells.erase(i);else ++i;}
39 void Advance(float elapsed)
40 {
41 if(!std::isfinite(elapsed)||elapsed<=0)return;
42 const float blend=1.f-expf(-elapsed/3.f);
43 auto approach=[blend](float& current,float target){
44 current+=(target-current)*blend;if(fabsf(target-current)<.001f)current=target;
45 };
46 for(auto& entry:cells){
47 auto& c=entry.second;
48 approach(c.value.depthMm,c.target.depthMm);approach(c.value.ageHours,c.target.ageHours);
49 approach(c.value.waterMm,c.target.waterMm);
50 }
51 }
52 const Surface* At(VECTOR2SHORT p) const
53 {auto it=cells.find(Key(p));return it==cells.end()?nullptr:&it->second.value;}
54 };
55}
void Retain(VECTOR2SHORT p)
Definition TerrainSnow.h:29
std::unordered_map< uint32_t, Cell > cells
Definition TerrainSnow.h:23
const Surface * At(VECTOR2SHORT p) const
Definition TerrainSnow.h:52
size_t Size() const
Definition TerrainSnow.h:27
void Target(VECTOR2SHORT p, const SRenderWeather &sample)
Definition TerrainSnow.h:30
void Advance(float elapsed)
Definition TerrainSnow.h:39
RiseMath::Vector4 Tint(const Surface &surface, float latitude, float sunLatitude)
Definition TerrainSnow.h:12
uint32_t Key(VECTOR2SHORT p)
Definition TerrainSnow.h:10
bool HasVisibility() const
unsigned char snowDepthMm
unsigned char snowAgeHours
unsigned char surfaceWaterTenthsMm