OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fftwater.hpp
Go to the documentation of this file.
1 /* Copyright (c) 2015-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #ifndef FFTWATER_HPP__
22 #define FFTWATER_HPP__
23 
24 #include "vector_math.h"
25 #include <complex>
26 #include <random>
27 #include <vector>
28 #include <memory>
29 #include "glfft.hpp"
30 #include "common.hpp"
31 
32 using cfloat = std::complex<float>;
33 
34 class FFTWater
35 {
36  private:
37  cfloat phillips(vec2 k, float max_l);
38 
41  unsigned Nx, Nz;
43  float L;
44 
45  void generate_distribution(cfloat *distribution, vec2 size, float amplitude, float max_l);
46 
47  void generate_mipmaps();
48  void compute_ifft();
49  void bake_height_gradient();
50  void update_phase(float time);
51 
52  std::normal_distribution<float> normal_dist{0.0f, 1.0f};
53  std::default_random_engine engine;
54  constexpr static float G = 9.81f;
55 
56  std::vector<cfloat> distribution;
57  std::vector<cfloat> distribution_displacement;
58  std::vector<cfloat> distribution_normal;
59 
63 
68 
72 
75 
76  unsigned texture_index = 0;
77  unsigned normal_levels = 0;
79 
86  std::unique_ptr<GLFFT::FFT> fft_height;
87  std::unique_ptr<GLFFT::FFT> fft_displacement;
88  std::unique_ptr<GLFFT::FFT> fft_normal;
89  void init_gl_fft();
90  void downsample_distribution(cfloat *out, const cfloat *in, unsigned rate_log2);
91  void compute_mipmap(const GLFFT::Program &program, const GLFFT::Texture &texture, GLenum format, unsigned Nx, unsigned Nz, unsigned level);
92  void init_texture(GLFFT::Texture &tex, GLenum format, unsigned levels, unsigned width, unsigned height, GLenum mag_filter, GLenum min_filter);
93 
95 
96  public:
97  FFTWater(
98  float amplitude,
100  uvec2 resolution,
101  vec2 size,
102  vec2 normalmap_freq_mod);
103 
104  void update(float time);
105 
108  GLuint get_normal() const { return normalmap[texture_index].get(); }
110 };
111 
112 #endif
GLFFT::Buffer freq_height
Definition: fftwater.hpp:83
void update_phase(float time)
Definition: fftwater.cpp:143
GLFFT::Program prog_mipmap_height
Definition: fftwater.hpp:65
GLFFT::Texture normalmap[2]
Definition: fftwater.hpp:71
std::normal_distribution< float > normal_dist
Definition: fftwater.hpp:52
unsigned displacement_downsample
Definition: fftwater.hpp:78
GLFFT::Buffer freq_displacement
Definition: fftwater.hpp:84
FFTWater(float amplitude, vec2 wind_velocity, uvec2 resolution, vec2 size, vec2 normalmap_freq_mod)
Definition: fftwater.cpp:33
std::unique_ptr< GLFFT::FFT > fft_displacement
Definition: fftwater.hpp:87
void generate_mipmaps()
Definition: fftwater.cpp:187
std::unique_ptr< GLFFT::FFT > fft_normal
Definition: fftwater.hpp:88
void init_texture(GLFFT::Texture &tex, GLenum format, unsigned levels, unsigned width, unsigned height, GLenum mag_filter, GLenum min_filter)
Definition: fftwater.cpp:288
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
Definition: matrix.h:28
vec2 size
Definition: fftwater.hpp:42
vec2 wind_velocity
Definition: fftwater.hpp:39
GLuint get_normal() const
Definition: fftwater.hpp:108
cfloat phillips(vec2 k, float max_l)
Definition: fftwater.cpp:104
vec2 wind_dir
Definition: fftwater.hpp:40
std::unique_ptr< GLFFT::FFT > fft_height
Definition: fftwater.hpp:86
GLint GLsizei GLsizei GLenum format
Definition: gl2ext.h:179
void compute_ifft()
Definition: fftwater.cpp:176
void init_gl_fft()
Definition: fftwater.cpp:294
vec2 size_normal
Definition: fftwater.hpp:42
GLFFT::Texture displacementmap[2]
Definition: fftwater.hpp:70
GLuint get() const
GLFFT::Texture heightmap[2]
Definition: fftwater.hpp:69
unsigned Nx
Definition: fftwater.hpp:41
void downsample_distribution(cfloat *out, const cfloat *in, unsigned rate_log2)
Definition: fftwater.cpp:76
GLFFT::Program prog_mipmap_gradient_jacobian
Definition: fftwater.hpp:67
unsigned normal_levels
Definition: fftwater.hpp:77
std::vector< cfloat > distribution_normal
Definition: fftwater.hpp:58
unsigned texture_index
Definition: fftwater.hpp:76
GLFFT::Program prog_generate_displacement
Definition: fftwater.hpp:62
float L
Definition: fftwater.hpp:43
GLFFT::Buffer freq_normal
Definition: fftwater.hpp:85
void generate_distribution(cfloat *distribution, vec2 size, float amplitude, float max_l)
Definition: fftwater.cpp:124
unsigned get_displacement_downsample() const
Definition: fftwater.hpp:109
GLFFT::Texture heightdisplacementmap[2]
Definition: fftwater.hpp:73
GLsizei levels
Definition: gl2ext.h:1816
GLFFT::Buffer distribution_buffer_displacement
Definition: fftwater.hpp:81
GLenum GLuint texture
Definition: gl2ext.h:385
GLFFT::Program prog_generate_normal
Definition: fftwater.hpp:61
GLenum GLuint GLintptr GLsizeiptr size
Definition: gl2ext.h:629
GLFFT::Buffer distribution_buffer
Definition: fftwater.hpp:80
GLFFT::Program prog_mipmap_normal
Definition: fftwater.hpp:66
GLuint get_gradient_jacobian() const
Definition: fftwater.hpp:107
unsigned Nz
Definition: fftwater.hpp:41
bool mipmap_fp16
Definition: fftwater.hpp:94
GLenum GLuint GLint level
Definition: gl2ext.h:385
GLuint get_height_displacement() const
Definition: fftwater.hpp:106
std::vector< cfloat > distribution
Definition: fftwater.hpp:56
static constexpr float G
Definition: fftwater.hpp:54
GLint GLsizei width
Definition: gl2ext.h:179
std::default_random_engine engine
Definition: fftwater.hpp:53
void bake_height_gradient()
Definition: fftwater.cpp:235
GLFFT::Buffer distribution_buffer_normal
Definition: fftwater.hpp:82
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
GLFFT::Texture gradientjacobianmap[2]
Definition: fftwater.hpp:74
GLuint program
Definition: gl2ext.h:1475
void compute_mipmap(const GLFFT::Program &program, const GLFFT::Texture &texture, GLenum format, unsigned Nx, unsigned Nz, unsigned level)
Definition: fftwater.cpp:263
GLFFT::Program prog_generate_height
Definition: fftwater.hpp:60
std::complex< float > cfloat
Definition: fftwater.hpp:32
std::vector< cfloat > distribution_displacement
Definition: fftwater.hpp:57
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
GLFFT::Program prog_bake_height_gradient
Definition: fftwater.hpp:64
void update(float time)
Definition: fftwater.cpp:226
uniform float time
Definition: spawn.cs:50