Rise
The Vieneo Province
Loading...
Searching...
No Matches
ReentryEnvelope.h
Go to the documentation of this file.
1#pragma once
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6#include <cstring>
7#include <vector>
8
9// Visual proxy, not an ionization, heat-transfer or damage solver. See
10// docs/ReentryEnvelope.md for the Vieneo assumptions and display calibration.
12{
14 inline float Dot(Vector3 a,Vector3 b) {return a.x*b.x+a.y*b.y+a.z*b.z;}
15 inline Vector3 Cross(Vector3 a,Vector3 b) {return {a.y*b.z-a.z*b.y,a.z*b.x-a.x*b.z,a.x*b.y-a.y*b.x};}
16 inline bool Finite(Vector3 v) {return std::isfinite(v.x)&&std::isfinite(v.y)&&std::isfinite(v.z);}
17 inline float Length(Vector3 v) {return std::sqrt(Dot(v,v));}
18 inline float Saturate(float x) {return (std::max)(0.f,(std::min)(1.f,x));}
19 inline float Smooth(float x) {x=Saturate(x);return x*x*(3-2*x);}
20 inline Vector3 Unit(Vector3 v) {return v/Length(v);}
21 // Density is kg/m^3, velocity is km/s. Neither IAS nor pressure ratio is
22 // a substitute here. The kinetic energy flux is a mechanical upper bound;
23 // the two ramps are explicitly artistic visibility choices, not chemistry.
24 inline float Strength(float density,Vector3 airVelocity)
25 {
26 if(!std::isfinite(density)||density<=0||!Finite(airVelocity))return 0;
27 const double speed=Length(airVelocity)*1000.;
28 if(!std::isfinite(speed))return 0;
29 const double fluxMW=.5*density*speed*speed*speed*1e-6;
30 return Smooth(float((speed-1800)/1400))*Smooth(float((fluxMW-.15)/15));
31 }
33 inline Conditions Resolve(float density,Vector3 airVelocity,float strengthOverride)
34 {
35 // GAMMA previews use a ship-fixed, nose-first flow regardless of actual
36 // orbital travel or wind. This only supplies visual inputs; never write
37 // a diagnostic velocity back into propulsion, damage or blackout state.
38 if(strengthOverride>=0)return {strengthOverride,{0,0,-1}};
39 return {Strength(density,airVelocity),
40 Finite(airVelocity)&&Length(airVelocity)>1e-6f ? airVelocity : Vector3(0,0,-1)};
41 }
42 struct State
43 {
44 float strength=0,time=0;
45 uint64_t vehicle=0;
46 void Update(float target,float dt,bool allowed,uint64_t id)
47 {
48 if(!allowed||id!=vehicle||!std::isfinite(target)||!std::isfinite(dt)||dt<0)
49 {strength=time=0;vehicle=id;if(!allowed||!std::isfinite(target)||!std::isfinite(dt)||dt<0)return;}
50 // No residual glow in vacuum, on teleport, or after a simulator exit.
51 if(target<=0){strength=time=0;return;}
52 const float step=(std::min)(dt,.25f);
53 strength+=(Saturate(target)-strength)*(1-std::exp(-step*5));
54 // Artistic flow rate is fixed in simulation time, including GAMMA
55 // previews. Neither orbital speed nor preview brightness scales it.
56 time=std::fmod(time+step,256.f);
57 }
58 };
59 // Radius zero is a point on the visible model; positive radii also allow
60 // simple analytic hulls in renderer tests.
61 struct Sphere {Vector3 center;float radius;};
63 struct Mesh
64 {
65 std::vector<Vertex> vertices;std::vector<uint16_t> indices;
67 };
68 constexpr unsigned Rings=32,Segments=64;
69 template<class Component> inline void AppendPart(std::vector<Sphere>& hull,const Component& part,
70 const std::vector<uint8_t>& bytes,size_t stride,const RiseMath::Matrix& world)
71 {
72 if(stride<sizeof(Vector3))return;
73 const size_t first=part.startvertex,number=size_t(part.primitives)*3;
74 if(first>bytes.size()/stride || number>bytes.size()/stride-first)return;
75 for(size_t i=first;i<first+number;++i)
76 {
77 Vector3 point;std::memcpy(&point,bytes.data()+i*stride,sizeof(point));
78 RiseMath::Vec3TransformCoord(&point,&point,&world);
79 if(Finite(point))hull.push_back({point,0});
80 }
81 }
82 template<class Component> inline std::vector<Sphere> ExteriorHull(const Component* parts,size_t count,
83 const std::vector<uint8_t>& bytes,size_t stride)
84 {
85 std::vector<Sphere> hull;
86 if(!parts || stride<sizeof(Vector3))return hull;
87 for(size_t c=0;c<count;++c)
88 {
89 const auto& part=parts[c];
90 // Non-colliding exterior (53) includes the E-11 nose/windshield.
91 // Cargo interiors and exhaust are not part of the airframe.
92 if(!part.visible || part.type==54 || (part.type>=200 && part.type<=225 && part.type%5==0))continue;
93 AppendPart(hull,part,bytes,stride,part.matWorld);
94 }
95 return hull;
96 }
97 // Radially intersect a bounded set of supporting planes. Selecting a
98 // different winning sphere for adjacent vertices used to stretch broad
99 // triangles across the E-11. Intersections give a continuous enclosing
100 // surface, including non-colliding exterior geometry, without those folds.
101 inline Mesh Generate(const std::vector<Sphere>& spheres,Vector3 windward)
102 {
103 Mesh mesh;
104 if(spheres.empty()||!Finite(windward)||Length(windward)<1e-6f)return mesh;
105 Vector3 low=spheres.front().center,high=low;
106 for(const auto& s:spheres)
107 {
108 if(!Finite(s.center)||!std::isfinite(s.radius)||s.radius<0)return mesh;
109 for(unsigned c=0;c<3;++c)
110 {
111 low[c]=(std::min)(low[c],s.center[c]-s.radius);
112 high[c]=(std::max)(high[c],s.center[c]+s.radius);
113 }
114 }
115 // Independent of vertex density (the detailed cockpit used to pull the
116 // centre forwards) and evaluated in the vehicle frame by Attached().
117 mesh.center=(low+high)*.5f;
118 for(const auto& s:spheres)mesh.radius=(std::max)(mesh.radius,Length(s.center-mesh.center)+s.radius);
119 if(mesh.radius<1e-6f)return {};
120 const auto axis=Unit(windward);
121 mesh.materialWindward=mesh.windward=axis;
122 const auto right=Unit(Cross(axis,std::abs(axis.y)<.9f?Vector3(0,1,0):Vector3(1,0,0)));
123 const auto up=Cross(right,axis);
124 const float standOff=(std::max)(.00075f,mesh.radius*.16f);
125 struct Plane {Vector3 normal;float distance;};
126 std::vector<Plane> planes;
127 const auto addPlane=[&](Vector3 normal)
128 {
129 float distance=0;
130 for(const auto& s:spheres)distance=(std::max)(distance,Dot(normal,s.center-mesh.center)+s.radius);
131 planes.push_back({normal,distance+standOff});
132 };
133 // Fixed material-frame directions avoid arbitrary support-plane motion.
134 for(unsigned i=0;i<128;++i)
135 {
136 const float z=1-2*(float(i)+.5f)/128,phi=float(i)*2.39996323f,r=std::sqrt(1-z*z);
137 addPlane({r*std::cos(phi),r*std::sin(phi),z});
138 }
139 for(Vector3 n:{Vector3(1,0,0),Vector3(-1,0,0),Vector3(0,1,0),Vector3(0,-1,0),Vector3(0,0,1),Vector3(0,0,-1),axis,-axis})addPlane(n);
140 // Project the entire airframe onto the flow-normal plane. Extruding a
141 // section through the centre misses wide/high tail booms and mounts.
142 std::vector<Plane> silhouette;
143 for(unsigned i=0;i<Segments;++i)
144 {
145 const float phi=float(i)*6.283185307f/Segments;
146 const auto n=right*std::cos(phi)+up*std::sin(phi);
147 float extent=0;
148 for(const auto& s:spheres)extent=(std::max)(extent,Dot(n,s.center-mesh.center)+s.radius);
149 silhouette.push_back({n,extent+standOff});
150 }
151 float aft=0;
152 for(const auto& s:spheres)aft=(std::max)(aft,Dot(-axis,s.center-mesh.center)+s.radius);
153 mesh.vertices.reserve((Rings+1)*(Segments+1));
154 for(unsigned ring=0;ring<=Rings;++ring)
155 {
156 const float v=float(ring)/Rings,theta=(std::min)(v,.5f)*3.141592654f;
157 const float axial=ring>=Rings/2?0.f:std::cos(theta);
158 const float radial=ring==0?0.f:std::sin(theta);
159 for(unsigned sector=0;sector<=Segments;++sector)
160 {
161 const float u=float(sector)/Segments,phi=sector==Segments?0.f:u*6.283185307f;
162 const auto side=right*std::cos(phi)+up*std::sin(phi);
163 const auto normal=axis*axial+side*radial;
164 float shoulder=1e30f;
165 for(const auto& plane:silhouette)
166 {
167 const float d=Dot(side,plane.normal);
168 if(d>1e-6f)shoulder=(std::min)(shoulder,plane.distance/d);
169 }
170 float distance=1e30f;
171 for(const auto& plane:planes)
172 {
173 const float d=Dot(normal,plane.normal);
174 if(d>1e-6f)distance=(std::min)(distance,plane.distance/d);
175 }
176 // Broaden the bow into the full projected silhouette. Keep the
177 // shear layer outside the complete hull before recompression.
178 distance+=(std::max)(0.f,shoulder-distance)*Smooth(v*2);
179 const float lee=(std::max)(0.f,v*2-1);
180 const float downstream=(aft+mesh.radius*3.8f)*lee;
181 const float converge=Smooth((downstream-aft-standOff-mesh.radius*.2f)/(mesh.radius*2));
182 const float neck=1+.06f*Smooth(lee*5)-.84f*converge+.05f*Smooth((lee-.85f)/.15f);
183 // A finite open neck avoids a singular bright point/cap. Its
184 // downstream end is broken up and faded in the pixel shader.
185 const auto point=mesh.center+normal*(distance*neck)-axis*downstream;
186 mesh.vertices.push_back({point,normal,u,v,side*(shoulder/mesh.radius),(point-mesh.center)/mesh.radius});
187 if(ring&&sector)
188 {
189 const auto a=uint16_t((ring-1)*(Segments+1)+sector-1),b=uint16_t(a+1);
190 const auto c=uint16_t(a+Segments+1),d=uint16_t(c+1);
191 mesh.indices.insert(mesh.indices.end(),{a,c,b,b,c,d});
192 }
193 }
194 }
195 return mesh;
196 }
197 inline void Place(Mesh& mesh,const RiseMath::Matrix& bodyToWorld,Vector3 origin)
198 {
199 for(auto& vertex:mesh.vertices)
200 {
201 RiseMath::Vec3TransformNormal(&vertex.position,&vertex.position,&bodyToWorld);
202 vertex.position+=origin;
203 RiseMath::Vec3TransformNormal(&vertex.normal,&vertex.normal,&bodyToWorld);
204 }
205 RiseMath::Vec3TransformNormal(&mesh.center,&mesh.center,&bodyToWorld);mesh.center+=origin;
206 RiseMath::Vec3TransformNormal(&mesh.windward,&mesh.windward,&bodyToWorld);
207 // Material anchors stay in the airframe frame, independent of attitude
208 // and camera. Only the visible geometry is placed in world coordinates.
209 }
210 inline Mesh Attached(std::vector<Sphere> hull,Vector3 windward,const RiseMath::Matrix& worldToBody,Vector3 origin)
211 {
212 for(auto& s:hull)
213 {
214 s.center-=origin;
215 RiseMath::Vec3TransformNormal(&s.center,&s.center,&worldToBody);
216 }
217 RiseMath::Vec3TransformNormal(&windward,&windward,&worldToBody);
218 auto mesh=Generate(hull,windward);
219 RiseMath::Matrix bodyToWorld;RiseMath::MatrixInverse(&bodyToWorld,nullptr,&worldToBody);
220 Place(mesh,bodyToWorld,origin);
221 return mesh;
222 }
223}
float Length(Vector3 v)
constexpr unsigned Rings
Vector3 Unit(Vector3 v)
Mesh Attached(std::vector< Sphere > hull, Vector3 windward, const RiseMath::Matrix &worldToBody, Vector3 origin)
constexpr unsigned Segments
Mesh Generate(const std::vector< Sphere > &spheres, Vector3 windward)
void Place(Mesh &mesh, const RiseMath::Matrix &bodyToWorld, Vector3 origin)
float Saturate(float x)
float Strength(float density, Vector3 airVelocity)
Vector3 Cross(Vector3 a, Vector3 b)
float Smooth(float x)
Conditions Resolve(float density, Vector3 airVelocity, float strengthOverride)
void AppendPart(std::vector< Sphere > &hull, const Component &part, const std::vector< uint8_t > &bytes, size_t stride, const RiseMath::Matrix &world)
float Dot(Vector3 a, Vector3 b)
std::vector< Sphere > ExteriorHull(const Component *parts, size_t count, const std::vector< uint8_t > &bytes, size_t stride)
bool Finite(Vector3 v)
Vector3 * Vec3TransformCoord(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition ClientMath.h:109
Vector3 * Vec3TransformNormal(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition ClientMath.h:107
Matrix * MatrixInverse(Matrix *out, float *determinant, const Matrix *a)
Definition ClientMath.h:118
std::vector< Vertex > vertices
std::vector< uint16_t > indices
void Update(float target, float dt, bool allowed, uint64_t id)