Rise
The Vieneo Province
Toggle main menu visibility
Loading...
Searching...
No Matches
CockpitLightning.h
Go to the documentation of this file.
1
#pragma once
2
#include "
LocalLightning.h
"
3
#include "
../Rendering/FixedFunction11.h
"
4
#include <vector>
5
6
namespace
CockpitLightning
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
}
20
class
Apertures
21
{
22
struct
Triangle
{
RiseMath::Vector3
a
,
b
,
c
;};
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
;
28
RiseMath::Vector3
p,q;
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
};
107
class
ScopedLights
108
{
109
Rendering::FixedState11
&
state
;
110
Lights
saved
;
111
public
:
112
ScopedLights
(
Rendering::FixedState11
&
state
,
const
Lights
& lights):
state
(
state
)
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];}
117
ScopedLights
(
const
ScopedLights
&)=
delete
;
118
ScopedLights
&
operator=
(
const
ScopedLights
&)=
delete
;
119
};
120
}
FixedFunction11.h
LocalLightning.h
CockpitLightning::Apertures
Definition
CockpitLightning.h:21
CockpitLightning::Apertures::Add
void Add(unsigned component, const Vertex *vertices, size_t count)
Definition
CockpitLightning.h:44
CockpitLightning::Apertures::Intersects
static bool Intersects(const Triangle &triangle, const RiseMath::Vector3 &eye, const RiseMath::Vector3 &source)
Definition
CockpitLightning.h:25
CockpitLightning::Apertures::Clear
void Clear()
Definition
CockpitLightning.h:43
CockpitLightning::Apertures::panes
std::vector< Pane > panes
Definition
CockpitLightning.h:24
CockpitLightning::Apertures::Sample
Lights Sample(const LocalLightning::Frame &frame, Transform transform, float exposure=1) const
Definition
CockpitLightning.h:57
CockpitLightning::ScopedLights::state
Rendering::FixedState11 & state
Definition
CockpitLightning.h:109
CockpitLightning::ScopedLights::saved
Lights saved
Definition
CockpitLightning.h:110
CockpitLightning::ScopedLights::ScopedLights
ScopedLights(Rendering::FixedState11 &state, const Lights &lights)
Definition
CockpitLightning.h:112
CockpitLightning::ScopedLights::ScopedLights
ScopedLights(const ScopedLights &)=delete
CockpitLightning::ScopedLights::~ScopedLights
~ScopedLights()
Definition
CockpitLightning.h:116
CockpitLightning::ScopedLights::operator=
ScopedLights & operator=(const ScopedLights &)=delete
CockpitLightning
Definition
CockpitLightning.h:7
CockpitLightning::Smooth
float Smooth(float value)
Definition
CockpitLightning.h:11
CockpitLightning::Slots
constexpr unsigned Slots[2]
Definition
CockpitLightning.h:9
CockpitLightning::Lights
std::array< Rendering::FixedLight11, 2 > Lights
Definition
CockpitLightning.h:10
CockpitLightning::Falloff
float Falloff(float distance, float extinction)
Definition
CockpitLightning.h:16
RiseMath::Vec3Length
float Vec3Length(const Vector3 *a)
Definition
ClientMath.h:95
RiseMath::Vec3Cross
Vector3 * Vec3Cross(Vector3 *out, const Vector3 *a, const Vector3 *b)
Definition
ClientMath.h:105
RiseMath::Vec3TransformCoord
Vector3 * Vec3TransformCoord(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition
ClientMath.h:109
RiseMath::Vec3LengthSq
float Vec3LengthSq(const Vector3 *a)
Definition
ClientMath.h:94
RiseMath::MatrixInverse
Matrix * MatrixInverse(Matrix *out, float *determinant, const Matrix *a)
Definition
ClientMath.h:118
RiseMath::Vec3Dot
float Vec3Dot(const Vector3 *a, const Vector3 *b)
Definition
ClientMath.h:93
CockpitLightning::Apertures::Pane
Definition
CockpitLightning.h:23
CockpitLightning::Apertures::Pane::component
unsigned component
Definition
CockpitLightning.h:23
CockpitLightning::Apertures::Pane::triangles
std::vector< Triangle > triangles
Definition
CockpitLightning.h:23
CockpitLightning::Apertures::Triangle
Definition
CockpitLightning.h:22
CockpitLightning::Apertures::Triangle::a
RiseMath::Vector3 a
Definition
CockpitLightning.h:22
CockpitLightning::Apertures::Triangle::b
RiseMath::Vector3 b
Definition
CockpitLightning.h:22
CockpitLightning::Apertures::Triangle::c
RiseMath::Vector3 c
Definition
CockpitLightning.h:22
LocalLightning::Frame
Definition
LocalLightning.h:26
LocalLightning::Frame::eye
RiseMath::Vector4 eye
Definition
LocalLightning.h:27
LocalLightning::Frame::ground
RiseMath::Vector4 ground
Definition
LocalLightning.h:27
LocalLightning::Frame::up
RiseMath::Vector4 up
Definition
LocalLightning.h:27
LocalLightning::Frame::top
RiseMath::Vector4 top
Definition
LocalLightning.h:27
LocalLightning::Frame::color
RiseMath::Vector4 color
Definition
LocalLightning.h:27
Rendering::FixedState11
Definition
FixedFunction11.h:48
Rendering::FixedState11::lights
std::array< FixedLight11, 8 > lights
Definition
FixedFunction11.h:79
RiseMath::Matrix
Definition
ClientMath.h:80
RiseMath::Vector3
Definition
ClientMath.h:38
Viewscreen
CockpitLightning.h
Generated on
for Rise by
1.18.0