Cogs.Core
BasicOceanSystem.h
1#pragma once
2#include <complex>
3#include <vector>
4
5#include "Systems/ComponentSystem.h"
6
7#include "Platform/Instrumentation.h"
8
9#include "Components/Appearance/BasicOceanComponent.h"
10
11#include "Resources/Resources.h"
12
13#include "Services/TaskManager.h"
14
15namespace Cogs
16{
17 namespace Core
18 {
19 class Context;
20
22 {
23 glm::vec4 color;
24 float transparency = 0.0;
25 float seaLevel = 0.0;
26 float reflectionBrightness = 1.f;
27
29 bool transparent = false;
30 bool initialized = false;
31
32 const char * encoding = nullptr;
33 const char * waveVariant = nullptr;
34 const char * beliefSystem = nullptr;
35 const char * reflectionVariant = nullptr;
36 const char* lightModelVariant = nullptr;
37
38 WeakEntityPtr reflectionCamera;
39 EntityPtr defaultReflectionCamera;
40 };
41
44 {
45 public:
47 {
48 resize(0);
49 }
50
51 ComplexArray(size_t N)
52 {
53 resize(N);
54 }
55
56 void resize(size_t N)
57 {
58 this->N = N;
59 storage.resize((2 * N * sizeof(float) + sizeof(Chunk) - 1) / sizeof(Chunk));
60 }
61
62 size_t size() const
63 {
64 return N;
65 }
66
67 float* real()
68 {
69 return reinterpret_cast<float*>(storage.data());
70 }
71
72 const float* real() const
73 {
74 return reinterpret_cast<const float*>(storage.data());
75 }
76
77 float* imag()
78 {
79 return reinterpret_cast<float*>(storage.data()) + N;
80 }
81
82 const float* imag() const
83 {
84 return reinterpret_cast<const float*>(storage.data()) + N;
85 }
86
87 std::complex<float> load(const size_t index) const
88 {
89 return std::complex<float>(reinterpret_cast<const float*>(storage.data())[index],
90 reinterpret_cast<const float*>(storage.data())[N + index]);
91 }
92
93 void store(const size_t index, const std::complex<float> value)
94 {
95 reinterpret_cast<float*>(storage.data())[index] = std::real(value);
96 reinterpret_cast<float*>(storage.data())[N + index] = std::imag(value);
97 }
98
99 protected:
100#ifdef _MSC_VER
101 _declspec(align(32)) struct Chunk { uint8_t data[32]; };
102#elif defined(__GNUC__) || defined(__clang__)
103 struct Chunk { uint8_t data[32]; } __attribute__((aligned(32)));
104#endif
105
106 size_t N = 0;
107 std::vector<Chunk> storage;
108 };
109
110 class BasicOceanSystem : public ComponentSystemWithDataPool<BasicOceanComponent, BasicOceanData>
111 {
112 public:
114
115 BasicOceanSystem(Memory::Allocator * allocator, SizeType capacity) : ComponentSystemWithDataPool(allocator, capacity) {}
116
117 void initialize(Context * context) override;
118 void cleanup(Context * context) override;
119 void update(Context * context) override;
120 void postUpdate(Context * context) override;
121
122 void destroyComponent(ComponentHandle component) override;
123
124 TextureHandle getRealTex() const { return DisplacementTexH; }
125 TextureHandle getImagTex() const { return NormalTexH; }
126 TextureHandle getTangentTex() const { return TangentsTexH; }
127
128 float getTileScale() const { return 1.f / fftTileExtent; }
129 float getSignificantWaveHeight() const { return significantWaveHeight; }
130
131 private:
132 float fftTileExtent = 10.f;
133 int fftTileResolutionLog2 = 7;
134 float significantWaveHeight = 4.f;
135 float dominantWavePeriod = 25.f;
136 float windSpeed = 10.f;
137 float windDirection = 0.f;
138 unsigned int phaseShiftNoiseFrequency = 3;
139 unsigned int tilePeriod = 30;
140 BasicOceanWaves waves = BasicOceanWaves::Default;
141
142 struct {
143 float channel0 = std::numeric_limits<float>::quiet_NaN();
144 float channel1 = std::numeric_limits<float>::quiet_NaN();
145 } magnitudes, // Main thread
146 magnitudes_tmp; // Tmp used by worker task
147
148 bool animate = true;
149 bool specsChanged = true;
150
151 bool eightBit = false;
152 bool extraStep = false;
153
154 TaskId oceanTaskGroup = NoTask;
155
156 std::vector<float> P;
157
158 ComplexArray frqH0;
159 ComplexArray frqDz;
160 ComplexArray frqDx;
161 ComplexArray frqDy;
162 ComplexArray frqdDzdu;
163 ComplexArray frqdDzdv;
164
165 ComplexArray spcDz;
166 ComplexArray spcDx;
167 ComplexArray spcDy;
168 ComplexArray spcdDzdu;
169 ComplexArray spcdDzdv;
170
171 std::vector<uint8_t> fftScratch1;
172 std::vector<uint8_t> fftScratch2;
173 std::vector<uint8_t> fftScratch3;
174 std::vector<uint8_t> fftScratch4;
175 std::vector<uint8_t> fftScratch5;
176
177 std::vector<uint8_t> fftScratch;
178
179 TextureHandle DisplacementTexH = TextureHandle::NoHandle;
182 TextureHandle ReflectionTextureH = TextureHandle::NoHandle;
183
186 VariableKey DisplacementKey = NoProperty;
187 VariableKey NormalKey = NoProperty;
188 VariableKey TangentsKey = NoProperty;
189 VariableKey SkyDomeKey = NoProperty;
190 VariableKey PlanarReflectionKey = NoProperty;
191 VariableKey cameraYAxisKey = NoProperty;
192 VariableKey waterColorKey = NoProperty;
193 VariableKey waveDirectionKey = NoProperty;
194 VariableKey significantWaveHeightKey = NoProperty;
195 VariableKey fftTileScaleKey = NoProperty;
196 VariableKey camPlaneDirKey = NoProperty;
197 VariableKey camAzimuthKey = NoProperty;
198 VariableKey seaLevelKey = NoProperty;
199 VariableKey phaseShiftNoiseFrequencyKey = NoProperty;
200 VariableKey phaseShiftNoisePeriodKey = NoProperty;
201 VariableKey reflectionBrightnessKey = NoProperty;
202
203 void setupMaterial();
204 void updateTextureResolution(BasicOceanComponent* oceanComp);
205
206 void destroyDefaultReflectionCameraIfExists(Context* context, BasicOceanData& oceanData);
207
208 void setupWaveSpectrum();
209
210 void updateTextures(const float magnitudeIn0, const float magnitudeIn1);
211
212 void updateTileMaterialInstances(const BasicOceanData & oceanData,
213 class AdaptivePlanarGridComponent * gridComp,
214 struct AdaptivePlanarGridData & gridData,
215 const glm::mat4& viewToWorld,
216 const glm::vec2& viewPortSize);
217 };
218 }
219}
void initialize(Context *context) override
Initialize the system.
unsigned int tilePeriod
Number of FFT-tiles before ocean repeats itself.
bool eightBit
Use eight bit texture format for wave displacement.
unsigned int phaseShiftNoiseFrequency
Frequency of phase shift noise wrt FFT tile size.
void destroyComponent(ComponentHandle component) override
Destroy the component held by the given handle.
void cleanup(Context *context) override
Provided for custom cleanup logic in derived systems.
float fftTileExtent
Size of FFT-tile in meters, set up setupWaveSpectrum.
Context * context
Pointer to the Context instance the system lives in.
void postUpdate()
Perform post update logic in the system.
void update()
Updates the system state to that of the current frame.
Component system with parallel data per component stored in a pool similar to how the components them...
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Base allocator implementation.
Definition: Allocator.h:30
std::shared_ptr< ComponentModel::Entity > EntityPtr
Smart pointer for Entity access.
Definition: EntityPtr.h:12
std::weak_ptr< ComponentModel::Entity > WeakEntityPtr
Weak Smart pointer for Entity access.
Definition: EntityPtr.h:18
uint16_t VariableKey
Used to lookup material properties.
Definition: Resources.h:46
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
ComponentIndex SizeType
Type used to track the size of pools.
Definition: Component.h:19
Handle to a Component instance.
Definition: Component.h:67
static const ResourceHandle_t NoHandle
Handle representing a default (or none if default not present) resource.
Task id struct used to identify unique Task instances.
Definition: TaskManager.h:20