-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathOcean.h
More file actions
196 lines (162 loc) · 6.57 KB
/
Copy pathOcean.h
File metadata and controls
196 lines (162 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* SimShip by Edouard Halbert
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License
http://creativecommons.org/licenses/by-nc-nd/4.0/ */
#pragma once
#define NOMINMAX
#include <complex>
#include <vector>
// glad
#include <glad/glad.h>
// glm
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// fftw
#define FFTW_DLL
#include <fftw3/fftw3.h>
#include "Structures.h"
#include "Camera.h"
#include "Shader.h"
#include "Texture.h"
#include "Shapes.h"
#include "Utility.h"
#include "mat4.h"
#include "Sky.h"
using namespace std;
using namespace glm;
struct GridVertex
{
vec3 position;
vec2 texCoord;
};
struct WaveData
{
double time;
double dx, dy, dz;
};
class Ocean
{
public:
Ocean(vec2 wind, Sky* sky);
~Ocean();
void Init();
void InitFrequencies();
GLuint InitTexture2DArray();
void GetWind(vec2 wind);
float EvaluateLambda(vec2 wind);
void EvaluatePersistence(float seconds);
float Phillips(vec2 k);
float JONSWAP(vec2 k);
float JONSWAP2(vec2 k);
float PiersonMoskowitz(vec2 k);
float DonelanBanner(vec2 k);
float Elfouhaily(vec2 k);
float Elfouhaily2(vec2 k);
float TexelMarsenArsloe(vec2 k);
float TexelMarsenArsloe2(vec2 k);
void Update(float t);
void FourierTransform(GLuint spectrum);
bool GetVerticeXZ(vec2 pos, vec3& output);
bool GetVerticeXYZ(vec3 pos, vec3& output);
void GetRecordFromBuoy(vec2 pos, float t);
bool GetWaveByWaveAnalysis(float& waves1_3, float& waveMax, int& nWaves, float& average_period);
vector<vec2>GetCut(int xN);
pair<vector<double>, vector<double>> GetFrequencies();
vector<sResultData> SpectralAnalysis();
vector<sResultData> DirectionalAnalysis();
GLuint GetDisplacementID() { return mTexDisplacements; };
GLuint GetGradientsID() { return mTexGradients; };
GLuint GetFoamBufferID() { return mTexFoamBuffer; };
float * GetPixelsDisplacement() { return mPixelsDisplacement.get(); };
void Render(const float t, Camera& camera, vec3& ShipPosition, float ShipRotation, bool bKelvinWakes, float LWL, float kelvinScale, float shipVelocity, float centerFore, int baseFroude);
// Dimensions
const int FFT_SIZE = 512; // Dimension of the FFT (1024 max)
const int FFT_SIZE_1 = FFT_SIZE + 1;
const int MESH_SIZE = 256; // Dimension of the mesh (number of cells on one side of the grid)
const int MESH_SIZE_1 = MESH_SIZE + 1;
const int PATCH_SIZE = 100; // Size of the mesh grid in meters
const int LengthWave = 60; // Dimension of the wave number
// Parameters
vec2 Wind = { 0.0f, 1.0f }; // Input of wind (no need to be normalized at this stage)
float Amplitude = 1.0f; // Amplitude of the waves
float Lambda = -1.0f; // Factor of choppiness (exagerate the displacements)
vec3 OceanColor;
int iOceanColor = 6;
vector<vec3> vOceanColors = {
vec3(122, 138, 92), // Pornic
vec3( 69, 134, 111), // North sea light
vec3( 37, 66, 57), // North sea dark
vec3( 1, 169, 193), // Carribbean
vec3( 34, 99, 110), // Golf of Morbihan
vec3( 0, 102, 133), // Carribbean
vec3( 1, 53, 75), // MSFS 2024
vec3( 0, 59, 92), // Light blue
vec3( 21, 92, 142), // Channel
vec3( 3, 92, 175), // Pacific
vec3( 10, 33, 55), //
vec3( 21, 61, 111) // Iroise
};
float PersistenceSec = 0.5f;
float PersistenceFactor = 1.0f;
float Transparency = 0.05f;
vector<WaveData> vWaveData; // time, dx, dy, dz
bool bNewData = false;
bool bVisible = true; // rendering of the ocean or not
bool bEnvmap = true;
bool bShowPatch = false;
int NbPatches = 300;
private:
void GetAllJacobians();
void GetSpectrumStats(vector<float>& vS);
void CreateMesh();
void CreateLODMeshes();
void CreateLODMesh(int meshSize, vector<GridVertex>& vertices, vector<unsigned int>& indices);
void GetPatchesDecal(vec2 Position, float w, float h, float Yaw);
void GetPatchVertices();
void WorldToPatch(float x, float z, float& xLocal, float& zLocal, int& i, int& j);
// Compute shaders
unique_ptr<Shader> mShaderSpectrum; // time-updated spectrum
unique_ptr<Shader> mShaderFft; // fast fourier transform
unique_ptr<Shader> mShaderDisplacements; // displacements
unique_ptr<Shader> mShaderGradients; // normals & jacobians
// Environment
Sky * mSky = nullptr;
// Rendering mShader
unique_ptr<Shader> mShaderOcean; // mShader of ocean rendering with instanciated matrices
unique_ptr<Shader> mShaderOceanWake; // mShader of ocean rendering with wake texture applied
// Textures of storage (glTexStorage2D)
GLuint mTexInitialSpectrum = 0; // initial spectrum \tilde{h}_0
GLuint mTexFrequencies = 0; // frequency \omega_i per wave vector
GLuint mTexUpdatedSpectra[2] = { 0 }; // updated spectra \tilde{h}(\mathbf{k},t) and \tilde{\mathbf{D}}(\mathbf{k},t) [reused for FT result]
GLuint mTexTempData = 0; // intermediate data for FFT
GLuint mTexDisplacements = 0; // displacements map
GLuint mTexGradients = 0; // normals & foldings map
GLuint mTexFoamAcc1 = 0; // accumulation buffer of the foam (swap due to readonly and writeonly)
GLuint mTexFoamAcc2 = 0; // accumulation buffer of the foam (swap due to readonly and writeonly)
GLuint mTexFoamBuffer = 0; // either accfoam1 or accfoam2 (the last writeonly which is read for rendering purpose)
// Textures of rendering
unique_ptr<Texture> mTexEnvironment; // environment texture (shaderSky)
unique_ptr<Texture> mTexFoamDesign; // foam texture
unique_ptr<Texture> mTexFoamBubbles; // foam bubbles
unique_ptr<Texture> mTexFoam; // texture of the wake
unique_ptr<Texture> mTexWaterDuDv; // texture to bring noise for the reflection of the ship on the water
GLuint mTexKelvinArray;
// Miscellaneous
float mGravity = 9.81f;
GLuint mVao = 0;
GLuint mVbo = 0;
GLuint mIbo = 0;
int mIndicesCount = 0;
unique_ptr<float[]> mPixelsDisplacement = nullptr;
int iMinPatchDecal = 0;
int iMaxPatchDecal = 0;
int jMinPatchDecal = 0;
int jMaxPatchDecal = 0;
vector<vector<vec3>> mvPatchVertices;
vector<GLuint> mvVAOs;
vector<int> mvMeshSizes = { 256, 128, 32, 8, 4 }; // LOD 0, 1, 2, 3, 4
vector<int> mvIndicesCounts;
vector<double> a_Frequences;
vector<double> a_DensiteSpectrale;
vector<double> a_Directions;
};