Rise
The Vieneo Province
Loading...
Searching...
No Matches
ShipSurfaceLights.h
Go to the documentation of this file.
1#pragma once
3#include "GeometryBounds.h"
4#include <algorithm>
5#include <array>
6#include <cstdint>
7
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}
float Vec3Length(const Vector3 *a)
Definition ClientMath.h:95
Vector3 * Vec3TransformCoord(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition ClientMath.h:109
float Vec3LengthSq(const Vector3 *a)
Definition ClientMath.h:94
Vector3 * Vec3TransformNormal(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition ClientMath.h:107
float Vec3Dot(const Vector3 *a, const Vector3 *b)
Definition ClientMath.h:93
Vector3 * Vec3Normalize(Vector3 *out, const Vector3 *a)
Definition ClientMath.h:103
constexpr unsigned MaxLights
Frame Build(const Effects &effects, const RiseMath::Matrix &modelToWorld, unsigned long phase, float power, bool enabled)
constexpr unsigned MaxSceneLights
float Facing(const RiseMath::Vector3 &position, const RiseMath::Vector3 &direction, const RiseMath::Vector3 &eye)
Selection Select(const Frame &frame, const GeometryBounds &local, const RiseMath::Matrix &world)
std::array< RiseMath::Vector4, MaxLights > colors
std::array< RiseMath::Vector4, MaxLights > positions
std::array< RiseMath::Vector4, MaxLights > directions
std::array< RiseMath::Vector4, MaxSceneLights > positions
std::array< RiseMath::Vector4, MaxSceneLights > directions
std::array< RiseMath::Vector4, MaxSceneLights > colors