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 EntityPtr 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:
113 BasicOceanSystem(Memory::Allocator * allocator, SizeType capacity) : ComponentSystemWithDataPool(allocator, capacity) {}
114
115 void initialize(Context * context) override;
116 void cleanup(Context * context) override;
117 void update(Context * context) override;
118 void postUpdate(Context * context) override;
119
120 TextureHandle getRealTex() const { return DisplacementTexH; }
121 TextureHandle getImagTex() const { return NormalTexH; }
122 TextureHandle getTangentTex() const { return TangentsTexH; }
123
124 float getTileScale() const { return 1.f / fftTileExtent; }
125 float getSignificantWaveHeight() const { return significantWaveHeight; }
126
127 private:
128 float fftTileExtent = 10.f;
129 int fftTileResolutionLog2 = 7;
130 float significantWaveHeight = 4.f;
131 float dominantWavePeriod = 25.f;
132 float windSpeed = 10.f;
133 float windDirection = 0.f;
134 unsigned int phaseShiftNoiseFrequency = 3;
135 unsigned int tilePeriod = 30;
136 BasicOceanWaves waves = BasicOceanWaves::Default;
137
138 struct {
139 float channel0 = std::numeric_limits<float>::quiet_NaN();
140 float channel1 = std::numeric_limits<float>::quiet_NaN();
141 } magnitudes, // Main thread
142 magnitudes_tmp; // Tmp used by worker task
143
144 bool animate = true;
145 bool specsChanged = true;
146
147 bool eightBit = false;
148 bool extraStep = false;
149
150 TaskId oceanTaskGroup = NoTask;
151
152 std::vector<float> P;
153
154 ComplexArray frqH0;
155 ComplexArray frqDz;
156 ComplexArray frqDx;
157 ComplexArray frqDy;
158 ComplexArray frqdDzdu;
159 ComplexArray frqdDzdv;
160
161 ComplexArray spcDz;
162 ComplexArray spcDx;
163 ComplexArray spcDy;
164 ComplexArray spcdDzdu;
165 ComplexArray spcdDzdv;
166
167 std::vector<uint8_t> fftScratch1;
168 std::vector<uint8_t> fftScratch2;
169 std::vector<uint8_t> fftScratch3;
170 std::vector<uint8_t> fftScratch4;
171 std::vector<uint8_t> fftScratch5;
172
173 std::vector<uint8_t> fftScratch;
174
175 TextureHandle DisplacementTexH = TextureHandle::NoHandle;
178 TextureHandle ReflectionTextureH = TextureHandle::NoHandle;
179
182 VariableKey DisplacementKey = NoProperty;
183 VariableKey NormalKey = NoProperty;
184 VariableKey TangentsKey = NoProperty;
185 VariableKey SkyDomeKey = NoProperty;
186 VariableKey PlanarReflectionKey = NoProperty;
187 VariableKey cameraYAxisKey = NoProperty;
188 VariableKey waterColorKey = NoProperty;
189 VariableKey waveDirectionKey = NoProperty;
190 VariableKey significantWaveHeightKey = NoProperty;
191 VariableKey fftTileScaleKey = NoProperty;
192 VariableKey camPlaneDirKey = NoProperty;
193 VariableKey camAzimuthKey = NoProperty;
194 VariableKey seaLevelKey = NoProperty;
195 VariableKey phaseShiftNoiseFrequencyKey = NoProperty;
196 VariableKey phaseShiftNoisePeriodKey = NoProperty;
197 VariableKey reflectionBrightnessKey = NoProperty;
198
199 void setupMaterial();
200 void updateTextureResolution(BasicOceanComponent* oceanComp);
201
202 void setupWaveSpectrum();
203
204 void updateTextures(const float magnitudeIn0, const float magnitudeIn1);
205
206 void updateTileMaterialInstances(const BasicOceanData & oceanData,
207 class AdaptivePlanarGridComponent * gridComp,
208 struct AdaptivePlanarGridData & gridData,
209 const glm::mat4& viewToWorld,
210 const glm::vec2& viewPortSize);
211 };
212 }
213}
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 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
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
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