Rise
The Vieneo Province
Loading...
Searching...
No Matches
CockpitLightning.h
Go to the documentation of this file.
1#pragma once
2#include "LocalLightning.h"
4#include <vector>
5
7{
8 // Only cockpit geometry uses these slots; restore them before exterior draws.
9 inline constexpr unsigned Slots[2]={4,5};
10 using Lights=std::array<Rendering::FixedLight11,2>;
11 inline float Smooth(float value)
12 {
13 value=(std::clamp)(value,0.f,1.f);
14 return value*value*(3-2*value);
15 }
16 inline float Falloff(float distance,float extinction)
17 {
18 return 64/(64+distance*distance)*(1-Smooth((distance-48)/16))*std::exp(-extinction*distance);
19 }
21 {
23 struct Pane {unsigned component;std::vector<Triangle> triangles;};
24 std::vector<Pane> panes;
25 static bool Intersects(const Triangle& triangle,const RiseMath::Vector3& eye,const RiseMath::Vector3& source)
26 {
27 const auto ray=source-eye,e1=triangle.b-triangle.a,e2=triangle.c-triangle.a;
29 RiseMath::Vec3Cross(&p,&ray,&e2);
30 const float determinant=RiseMath::Vec3Dot(&e1,&p);
31 const float scale=std::sqrt(RiseMath::Vec3LengthSq(&e1)*RiseMath::Vec3LengthSq(&p));
32 if(std::abs(determinant)<=1e-6f*scale || scale==0) return false;
33 const auto offset=eye-triangle.a;
34 const float u=RiseMath::Vec3Dot(&offset,&p)/determinant;
35 if(u<0 || u>1) return false;
36 RiseMath::Vec3Cross(&q,&offset,&e1);
37 const float v=RiseMath::Vec3Dot(&ray,&q)/determinant;
38 if(v<0 || u+v>1) return false;
39 const float t=RiseMath::Vec3Dot(&e2,&q)/determinant;
40 return t>0 && t<1;
41 }
42 public:
43 void Clear(){panes.clear();}
44 template<class Vertex> void Add(unsigned component,const Vertex* vertices,size_t count)
45 {
46 Pane pane{component,{}};
47 pane.triangles.reserve(count/3);
48 for(size_t i=0;i+2<count;i+=3)
49 pane.triangles.push_back({{vertices[i].x,vertices[i].y,vertices[i].z},
50 {vertices[i+1].x,vertices[i+1].y,vertices[i+1].z},
51 {vertices[i+2].x,vertices[i+2].y,vertices[i+2].z}});
52 if(!pane.triangles.empty()) panes.push_back(std::move(pane));
53 }
54 // One representative eye point, two source rays. This gates light through
55 // real moving glass without per-surface ray tests or cockpit shadow maps.
56 // Transform returns null for a hidden/missing pane.
57 template<class Transform> Lights Sample(const LocalLightning::Frame& frame,Transform transform,float exposure=1) const
58 {
59 Lights lights{};
60 if(frame.color.w<=0 || exposure<=0 || panes.empty()) return lights;
61 const RiseMath::Vector3 eye{frame.eye.x,frame.eye.y,frame.eye.z};
62 const RiseMath::Vector3 ground{frame.ground.x,frame.ground.y,frame.ground.z};
63 const RiseMath::Vector3 top{frame.top.x,frame.top.y,frame.top.z};
64 const RiseMath::Vector3 up{frame.up.x,frame.up.y,frame.up.z};
65 const auto cloud=top-eye;
66 const float strength=frame.color.w*(std::clamp)(exposure,0.f,1.f)
67 *(1-Smooth(-RiseMath::Vec3Dot(&cloud,&up)/.6f));
68 if(strength<=0) return lights;
69 const RiseMath::Vector3 sources[]={ground+(top-ground)*.2113248654f,ground+(top-ground)*.7886751346f};
70 bool open[2]{},hasWindow=false;
71 for(const auto& pane:panes)
72 {
73 const auto* world=transform(pane.component);
74 if(!world) continue;
75 hasWindow=true;
76 RiseMath::Matrix inverse;
77 if(!RiseMath::MatrixInverse(&inverse,nullptr,world)) continue;
78 RiseMath::Vector3 localEye,localSources[2];
79 RiseMath::Vec3TransformCoord(&localEye,&eye,&inverse);
80 for(unsigned i=0;i<2;++i)
81 {
82 if(open[i]) continue;
83 RiseMath::Vec3TransformCoord(&localSources[i],&sources[i],&inverse);
84 for(const auto& triangle:pane.triangles)
85 if(Intersects(triangle,localEye,localSources[i])) {open[i]=true;break;}
86 }
87 if(open[0] && open[1]) break;
88 }
89 if(!hasWindow) return lights;
90 float directSum=0;
91 for(unsigned i=0;i<2;++i)
92 {
93 const auto delta=sources[i]-eye;
94 const float distance=(std::max)(RiseMath::Vec3Length(&delta),.0001f);
95 const float direct=open[i] ? .8f*Falloff(distance,frame.eye.w) : 0;
96 directSum+=direct;
97 lights[i].kind=1;
98 lights[i].direction={-delta.x/distance,-delta.y/distance,-delta.z/distance};
99 lights[i].diffuse={frame.color.x*strength*direct,frame.color.y*strength*direct,frame.color.z*strength*direct,0};
100 }
101 // Attenuated cloud bounce plus a small cabin bounce from admitted light.
102 const float fill=strength*(.04f*std::exp(-RiseMath::Vec3LengthSq(&cloud)/288)+.04f*directSum);
103 lights[0].ambient={frame.color.x*fill,frame.color.y*fill,frame.color.z*fill,0};
104 return lights;
105 }
106 };
108 {
111 public:
113 {
114 for(unsigned i=0;i<2;++i){saved[i]=state.lights[Slots[i]];state.lights[Slots[i]]=lights[i];}
115 }
116 ~ScopedLights(){for(unsigned i=0;i<2;++i)state.lights[Slots[i]]=saved[i];}
119 };
120}
void Add(unsigned component, const Vertex *vertices, size_t count)
static bool Intersects(const Triangle &triangle, const RiseMath::Vector3 &eye, const RiseMath::Vector3 &source)
Lights Sample(const LocalLightning::Frame &frame, Transform transform, float exposure=1) const
Rendering::FixedState11 & state
ScopedLights(Rendering::FixedState11 &state, const Lights &lights)
ScopedLights(const ScopedLights &)=delete
ScopedLights & operator=(const ScopedLights &)=delete
float Smooth(float value)
constexpr unsigned Slots[2]
std::array< Rendering::FixedLight11, 2 > Lights
float Falloff(float distance, float extinction)
float Vec3Length(const Vector3 *a)
Definition ClientMath.h:95
Vector3 * Vec3Cross(Vector3 *out, const Vector3 *a, const Vector3 *b)
Definition ClientMath.h:105
Vector3 * Vec3TransformCoord(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition ClientMath.h:109
float Vec3LengthSq(const Vector3 *a)
Definition ClientMath.h:94
Matrix * MatrixInverse(Matrix *out, float *determinant, const Matrix *a)
Definition ClientMath.h:118
float Vec3Dot(const Vector3 *a, const Vector3 *b)
Definition ClientMath.h:93
std::vector< Triangle > triangles
RiseMath::Vector4 eye
RiseMath::Vector4 ground
RiseMath::Vector4 up
RiseMath::Vector4 top
RiseMath::Vector4 color
std::array< FixedLight11, 8 > lights