Rise
The Vieneo Province
Loading...
Searching...
No Matches
CloudWeatherMap.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cmath>
4#include <cstdint>
5#include <memory>
6#include <vector>
7
8namespace CloudWeather
9{
10 struct Mip
11 {
12 unsigned width=0, height=0;
13 std::vector<uint8_t> pixels;
14 };
15 struct Map
16 {
18 std::vector<Mip> mips;
19 };
20 using MapPtr=std::shared_ptr<const Map>;
21 struct Frame
22 {
24 float blend=1;
25 };
26 inline void BuildMips(Map& map)
27 {
28 while (map.mips.back().width>1 || map.mips.back().height>1)
29 {
30 const auto& source=map.mips.back();
31 Mip mip;
32 mip.width=(std::max)(1u,source.width/2); mip.height=(std::max)(1u,source.height/2);
33 mip.pixels.resize(size_t(mip.width)*mip.height);
34 for(unsigned y=0;y<mip.height;++y) for(unsigned x=0;x<mip.width;++x)
35 {
36 unsigned sum=0;
37 for(unsigned dy=0;dy<2;++dy) for(unsigned dx=0;dx<2;++dx)
38 sum+=source.pixels[(std::min)(source.height-1,y*2+dy)*source.width+(std::min)(source.width-1,x*2+dx)];
39 mip.pixels[y*mip.width+x]=uint8_t((sum+2)/4);
40 }
41 map.mips.push_back(std::move(mip));
42 }
43 }
44 // Render position = negative physical planet position. +Z north, +Y prime
45 // meridian, -X east. This matches Server.NET Library.LatitudeLongitude.
46 inline void UV(float x,float y,float z,float& u,float& v)
47 {
48 constexpr float pi=3.14159265358979323846f;
49 const float length=std::sqrt(x*x+y*y+z*z);
50 u=.5f+std::atan2(-x,y)/(2*pi);
51 v=.5f-std::asin((std::max)(-1.f,(std::min)(1.f,z/(std::max)(length,.001f))))/pi;
52 }
53 inline float Sample(const MapPtr& map,float u,float v)
54 {
55 if (!map || map->mips.empty()) return 0;
56 const auto& m=map->mips.front();
57 const float x=(u-std::floor(u))*m.width-.5f,y=(std::max)(0.f,(std::min)(1.f,v))*m.height-.5f;
58 const int ix=int(std::floor(x)),iy=int(std::floor(y));
59 const float fx=x-ix,fy=y-iy;
60 const auto pixel=[&](int px,int py) {
61 px=(px%int(m.width)+int(m.width))%int(m.width);
62 py=(std::max)(0,(std::min)(int(m.height)-1,py));
63 return float(m.pixels[py*m.width+px])/255.f;
64 };
65 const float a=pixel(ix,iy)*(1-fx)+pixel(ix+1,iy)*fx;
66 const float b=pixel(ix,iy+1)*(1-fx)+pixel(ix+1,iy+1)*fx;
67 return a*(1-fy)+b*fy;
68 }
69 inline float Sample(const Frame& frame,float x,float y,float z)
70 {
71 float u,v; UV(x,y,z,u,v);
72 return Sample(frame.previous,u,v)*(1-frame.blend)+Sample(frame.current,u,v)*frame.blend;
73 }
74}
SCRIPT_CONTROL SCRIPT_STATE const void int
float Sample(const MapPtr &map, float u, float v)
std::shared_ptr< const Map > MapPtr
void UV(float x, float y, float z, float &u, float &v)
void BuildMips(Map &map)
std::vector< Mip > mips
std::vector< uint8_t > pixels