-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathOpenGLOcean.h
222 lines (185 loc) · 6.98 KB
/
OpenGLOcean.h
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
This file is a part of Stonefish.
Stonefish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Stonefish is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//
// OpenGLOcean.h
// Stonefish
//
// Created by Patryk Cieslak on 19/07/2017.
// Copyright (c) 2017-2020 Patryk Cieslak. All rights reserved.
//
#ifndef __Stonefish_OpenGLOcean__
#define __Stonefish_OpenGLOcean__
#include "graphics/OpenGLContent.h"
#include <SDL2/SDL_mutex.h>
#include <map>
namespace sf
{
//! A structure holding parameters of ocean generation and rendering.
struct OceanParams
{
unsigned int passes;
unsigned int slopeVarianceSize;
int fftSize;
glm::vec4 gridSizes;
float* spectrum12;
float* spectrum34;
bool propagate; //wave propagation
float wind; //wind speed in meters per second (at 10m above surface)
float omega; //sea state (inverse wave age)
float A; //wave amplitude
float km;
float cm;
float t;
};
#pragma pack(push, 1)
//! A structure representing the ocean currents UBO.
struct OceanCurrentsUBO
{
VelocityFieldUBO currents[MAX_OCEAN_CURRENTS]; //REMARK: type -> 0=uniform,1=jet,2=pipe,10=thruster
glm::vec3 gravity;
GLuint numCurrents;
};
#pragma pack(pop)
class GLSLShader;
class OpenGLCamera;
class OpenGLOceanParticles;
class VelocityField;
//! A class implementing ocean simulation in OpenGL.
class OpenGLOcean
{
public:
//! A constructor.
/*!
\param size the size of the ocean surface mesh [m]
*/
OpenGLOcean(GLfloat size);
//! A destructor.
virtual ~OpenGLOcean();
//! A method that simulates wave propagation.
/*!
\param dt time since last update
*/
virtual void Simulate(GLfloat dt);
//! A method that updates the wave mesh.
/*!
\param cam a pointer to the active camera
*/
virtual void UpdateSurface(OpenGLCamera* cam) = 0;
//! A method that draws the surface of the ocean.
/*!
\param cam a pointer to the active camera
*/
virtual void DrawSurface(OpenGLCamera* cam) = 0;
//! A method that draws the surface of the ocean, seen from underwater.
/*!
\param cam a pointer to the active camera
*/
virtual void DrawBacksurface(OpenGLCamera* cam) = 0;
//! A method that generates the stencil mask.
/*!
\param cam a pointer to the active camera
*/
virtual void DrawUnderwaterMask(OpenGLCamera* cam);
//! A method that draws the distant background of the ocean.
/*!
\param cam a pointer to the active camera
*/
void DrawBackground(OpenGLCamera* cam);
//! A method that draws underwater particles.
/*!
\param cam a pointer to the active camera
*/
void DrawParticles(OpenGLCamera* cam);
//! A method that draw water velocity field.
/*!
\param cam a pointer to the camera
\param velocityMax velocity corresponding to the display limit
*/
void DrawVelocityField(OpenGLCamera* cam, GLfloat velocityMax);
//! A method that draws the underwater bloom and blur effects (scattering).
/*!
\param cam a pointer to the active camera
*/
void ApplySpecialEffects(OpenGLCamera* cam);
//! A method that drawsd the spectrum of waves.
/*!
\param viewportSize size of the active viewport
\param rect a rectangle in which to draw the spectrum on screen
*/
void ShowSpectrum(glm::vec2 viewportSize, glm::vec4 rect);
//! A method used to display a specified texture.
/*!
\param id the id of the texture to display
\param rect a rectangle in which to draw the tecture on screen
*/
void ShowTexture(int id, glm::vec4 rect);
//! A method that updates ocean currents information.
void UpdateOceanCurrentsData(const OceanCurrentsUBO& data);
//! A method to set the type of ocean water.
/*!
\param t type of water
*/
void setWaterType(GLfloat t);
//! A method to set if the particles should be rendered.
/*!
\param enabled a flag specifying if the particles should be rendered
*/
void setParticles(bool enabled);
//! A method returning informing if the particles are enabled.
bool getParticlesEnabled();
//! A method to get wave height at a specified coordinate.
/*!
\param x the x coordinate in world frame [m]
\param y the y coordinate in world frame [m]
\return wave height [m]
*/
virtual GLfloat ComputeWaveHeight(GLfloat x, GLfloat y);
//! A method returning the id of the wave texture.
GLuint getWaveTexture();
//! A method returning the vector of wave grid sizes.
glm::vec4 getWaveGridSizes();
//! A method returning calculated light attenuation coefficient.
glm::vec3 getLightAttenuation();
//! A method calculating Henyey-Greenstein scattering factor.
glm::vec3 getLightScattering();
protected:
virtual void InitializeSimulation();
float sqr(float x);
std::map<OpenGLCamera*, OpenGLOceanParticles*> oceanParticles;
glm::vec3 absorption[64];
glm::vec3 scattering[64];
OceanCurrentsUBO oceanCurrentsUBOData;
GLfloat oceanSize;
OceanParams params;
glm::vec3 lightAbsorption;
glm::vec3 lightScattering;
std::map<std::string, GLSLShader*> oceanShaders;
GLuint oceanFBOs[3];
GLuint oceanTextures[6];
GLuint oceanCurrentsUBO;
private:
GLfloat* ComputeButterflyLookupTable(unsigned int size, unsigned int passes);
int bitReverse(int i, int N);
void computeWeight(int N, int k, float &Wr, float &Wi);
float ComputeSlopeVariance();
float GetSlopeVariance(float kx, float ky, float *spectrumSample);
void GenerateWavesSpectrum();
void GetSpectrumSample(int i, int j, float lengthScale, float kMin, float *result);
float spectrum(float kx, float ky, bool omnispectrum = false);
float omega(float k);
int oceanBoxObj;
bool particlesEnabled;
};
}
#endif