Rise
The Vieneo Province
Toggle main menu visibility
Loading...
Searching...
No Matches
ShipSurfaceLights.h
Go to the documentation of this file.
1
#pragma once
2
#include "
../Rendering/ClientMath.h
"
3
#include "
GeometryBounds.h
"
4
#include <algorithm>
5
#include <array>
6
#include <cstdint>
7
8
namespace
ShipSurfaceLights
9
{
10
// The current vehicle models contain at most 17 nav/strobe emitters.
11
constexpr
unsigned
MaxLights
= 32;
12
constexpr
unsigned
MaxSceneLights
= 4;
13
struct
Frame
14
{
15
unsigned
count
= 0;
16
std::array<RiseMath::Vector4,MaxLights>
positions
{},
directions
{},
colors
{};
17
};
18
struct
Selection
19
{
20
uint32_t
mask
= 0;
21
unsigned
count
= 0;
22
std::array<RiseMath::Vector4,MaxSceneLights>
positions
{},
directions
{},
colors
{};
23
};
24
inline
Selection
Select
(
const
Frame
& frame,
const
GeometryBounds
& local,
const
RiseMath::Matrix
& world)
25
{
26
Selection
result;
27
if
(!frame.
count
)
return
result;
28
GeometryBounds
bounds;
29
bounds.
valid
=local.
valid
;
30
if
(local.
valid
)
for
(
unsigned
axis=0;axis<3;++axis)
31
{
32
bounds.
low
[axis]=bounds.
high
[axis]=world.m[3][axis];
33
for
(
unsigned
i=0;i<3;++i)
34
{
35
const
float
a=local.
low
[i]*world.m[i][axis],b=local.
high
[i]*world.m[i][axis];
36
bounds.
low
[axis]+=(std::min)(a,b);
37
bounds.
high
[axis]+=(std::max)(a,b);
38
}
39
}
40
std::array<float,MaxSceneLights> scores{};
41
std::array<unsigned,MaxSceneLights> indices{};
42
for
(
unsigned
i=0;i<frame.
count
;++i)
43
{
44
const
auto
& p=frame.
positions
[i];
45
float
distanceSquared=0;
46
if
(bounds.
valid
)
for
(
unsigned
axis=0;axis<3;++axis)
47
{
48
const
float
value=axis==0 ? p.x : axis==1 ? p.y : p.z;
49
const
float
delta=(std::max)(bounds.
low
[axis]-value,(std::max)(value-bounds.
high
[axis],0.f));
50
distanceSquared+=delta*delta;
51
}
52
if
(distanceSquared>=p.w*p.w)
continue
;
53
// Rank by a conservative intensity at the nearest point, retaining
54
// deterministic source order on ties. Only active lamps reach here.
55
const
auto
& c=frame.
colors
[i];
56
const
float
score=c.w*(std::max)({c.x,c.y,c.z})
57
*(std::min)(4.f,frame.
directions
[i].w/(std::max)(distanceSquared,1e-8f));
58
if
(score<=0)
continue
;
59
unsigned
slot=0;
60
while
(slot<result.
count
&& score<=scores[slot]) ++slot;
61
if
(slot==
MaxSceneLights
)
continue
;
62
result.
count
=(std::min)(result.
count
+1,
MaxSceneLights
);
63
for
(
unsigned
j=result.
count
-1;j>slot;--j) {scores[j]=scores[j-1];indices[j]=indices[j-1];}
64
scores[slot]=score;indices[slot]=i;
65
}
66
for
(
unsigned
slot=0;slot<result.
count
;++slot)
67
{
68
const
unsigned
i=indices[slot];
69
result.
mask
|=uint32_t(1)<<i;
70
result.
positions
[slot]=frame.
positions
[i];result.
directions
[slot]=frame.
directions
[i];result.
colors
[slot]=frame.
colors
[i];
71
}
72
return
result;
73
}
74
inline
float
Facing
(
const
RiseMath::Vector3
& position,
const
RiseMath::Vector3
& direction,
75
const
RiseMath::Vector3
& eye)
76
{
77
const
auto
toEye=eye-position;
78
const
float
distance=
RiseMath::Vec3Length
(&toEye);
79
return
distance>.000001f ? (std::clamp)(
RiseMath::Vec3Dot
(&direction,&toEye)/distance,0.f,1.f) : 0.f;
80
}
81
template
<
class
Effects>
Frame
Build
(
const
Effects& effects,
const
RiseMath::Matrix
& modelToWorld,
82
unsigned
long
phase,
float
power,
bool
enabled)
83
{
84
Frame
result;
85
if
(!enabled || power<=0)
return
result;
86
power=(std::clamp)(power,0.f,1.f);
87
for
(
const
auto
& source:effects)
88
{
89
// Landing lights have their own renderer. Rotating beacons need a
90
// rotating beam rather than this fixed nav/strobe emitter.
91
if
((source.power!=.5f && source.power!=1.f) || !(source.sequencing&phase))
continue
;
92
if
(result.
count
==
MaxLights
)
break
;
93
RiseMath::Vector3
position,direction;
94
RiseMath::Vec3TransformCoord
(&position,&source.position,&modelToWorld);
95
RiseMath::Vec3TransformNormal
(&direction,&source.normal,&modelToWorld);
96
// The normal only lifts the emitter off its mounting surface;
97
// an omnidirectional bulb does not need an emission axis.
98
if
(
RiseMath::Vec3LengthSq
(&direction)>=.000001f)
99
{
100
RiseMath::Vec3Normalize
(&direction,&direction);
101
position+=direction*.0002f;
102
}
103
else
direction={0,0,0};
104
const
bool
strobe=source.power==.5f;
105
const
unsigned
i=result.
count
++;
106
result.
positions
[i]={position.x,position.y,position.z,strobe ? .060f : .020f};
107
result.
directions
[i]={direction.x,direction.y,direction.z,strobe ? .0001f : .000025f};
108
result.
colors
[i]={source.diffuse.r,source.diffuse.g,source.diffuse.b,power*(strobe ? 1.5f : .35f)};
109
}
110
return
result;
111
}
112
}
ClientMath.h
GeometryBounds.h
RiseMath::Vec3Length
float Vec3Length(const Vector3 *a)
Definition
ClientMath.h:95
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::Vec3TransformNormal
Vector3 * Vec3TransformNormal(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition
ClientMath.h:107
RiseMath::Vec3Dot
float Vec3Dot(const Vector3 *a, const Vector3 *b)
Definition
ClientMath.h:93
RiseMath::Vec3Normalize
Vector3 * Vec3Normalize(Vector3 *out, const Vector3 *a)
Definition
ClientMath.h:103
ShipSurfaceLights
Definition
ShipSurfaceLights.h:9
ShipSurfaceLights::MaxLights
constexpr unsigned MaxLights
Definition
ShipSurfaceLights.h:11
ShipSurfaceLights::Build
Frame Build(const Effects &effects, const RiseMath::Matrix &modelToWorld, unsigned long phase, float power, bool enabled)
Definition
ShipSurfaceLights.h:81
ShipSurfaceLights::MaxSceneLights
constexpr unsigned MaxSceneLights
Definition
ShipSurfaceLights.h:12
ShipSurfaceLights::Facing
float Facing(const RiseMath::Vector3 &position, const RiseMath::Vector3 &direction, const RiseMath::Vector3 &eye)
Definition
ShipSurfaceLights.h:74
ShipSurfaceLights::Select
Selection Select(const Frame &frame, const GeometryBounds &local, const RiseMath::Matrix &world)
Definition
ShipSurfaceLights.h:24
GeometryBounds
Definition
GeometryBounds.h:11
GeometryBounds::high
float high[3]
Definition
GeometryBounds.h:12
GeometryBounds::valid
bool valid
Definition
GeometryBounds.h:13
GeometryBounds::low
float low[3]
Definition
GeometryBounds.h:12
RiseMath::Matrix
Definition
ClientMath.h:80
RiseMath::Vector3
Definition
ClientMath.h:38
ShipSurfaceLights::Frame
Definition
ShipSurfaceLights.h:14
ShipSurfaceLights::Frame::colors
std::array< RiseMath::Vector4, MaxLights > colors
Definition
ShipSurfaceLights.h:16
ShipSurfaceLights::Frame::count
unsigned count
Definition
ShipSurfaceLights.h:15
ShipSurfaceLights::Frame::positions
std::array< RiseMath::Vector4, MaxLights > positions
Definition
ShipSurfaceLights.h:16
ShipSurfaceLights::Frame::directions
std::array< RiseMath::Vector4, MaxLights > directions
Definition
ShipSurfaceLights.h:16
ShipSurfaceLights::Selection
Definition
ShipSurfaceLights.h:19
ShipSurfaceLights::Selection::mask
uint32_t mask
Definition
ShipSurfaceLights.h:20
ShipSurfaceLights::Selection::positions
std::array< RiseMath::Vector4, MaxSceneLights > positions
Definition
ShipSurfaceLights.h:22
ShipSurfaceLights::Selection::count
unsigned count
Definition
ShipSurfaceLights.h:21
ShipSurfaceLights::Selection::directions
std::array< RiseMath::Vector4, MaxSceneLights > directions
Definition
ShipSurfaceLights.h:22
ShipSurfaceLights::Selection::colors
std::array< RiseMath::Vector4, MaxSceneLights > colors
Definition
ShipSurfaceLights.h:22
Viewscreen
ShipSurfaceLights.h
Generated on
for Rise by
1.18.0