Skip to content
Snippets Groups Projects
Commit ddd27afb authored by Dethcrvsh's avatar Dethcrvsh
Browse files

added simple physical waves

parent 91190007
No related branches found
No related tags found
No related merge requests found
...@@ -31,7 +31,7 @@ void main(void) ...@@ -31,7 +31,7 @@ void main(void)
vec3 nnormal = normalize(normal); vec3 nnormal = normalize(normal);
vec3 reflected = reflect(view, nnormal); vec3 reflected = reflect(view, nnormal);
float R = fresnel(nnormal, view); float R = fresnel(nnormal, view);
vec3 water_color = vec3(0, 0.05, 0.1); vec3 water_color = vec3(0.6, 0.6, 0.9);
vec3 sky_color = texture(sky, sphere_uv(reflected)).rgb; vec3 sky_color = texture(sky, sphere_uv(reflected)).rgb;
......
...@@ -11,9 +11,47 @@ uniform float time; ...@@ -11,9 +11,47 @@ uniform float time;
out vec3 world_pos; out vec3 world_pos;
out vec3 normal; out vec3 normal;
struct Wave {
float L; // Wavelength (distance between waves in world space)
float A; // Amplitude (height from surface to wave crest)
float S; // Speed (distance the crest moves per second)
vec2 D; // Direction (horizontal vector perpendicular to the wave front)
};
const int NUM_WAVES = 3;
Wave waves[NUM_WAVES];
void init_waves() {
waves[0] = Wave(0.2, 0.01, 0.1, vec2(0.5, 0.5));
waves[1] = Wave(0.3, 0.02, 0.15, vec2(1.0, 0.0));
waves[2] = Wave(0.1, 0.015, 0.08, vec2(-0.7, 0.3));
}
/* Get the offset for a single wave */
float get_single_offset(Wave wave, float x, float z, float t) {
float w = 2.0/wave.L; // Frequency
float phase_const = wave.S * w; // Phase constant
return wave.A * sin(dot(wave.D, vec2(x, z)) * w + t * phase_const);
}
/* Get the offset for all the combined waves */
float get_wave_offset(float x, float z, float t) {
float sum = 0.0;
for (int i = 0; i < NUM_WAVES; i++) {
sum += get_single_offset(waves[i], x, z, t);
}
return sum;
}
void main(void) void main(void)
{ {
vec3 offset_pos = vec3(in_Position.x, in_Position.y + 0.05 * sin(10.0 * in_Position.x + time), in_Position.z); init_waves();
//vec3 offset_pos = vec3(in_Position.x, in_Position.y + 0.05 * sin(10.0 * in_Position.x + time), in_Position.z);
float offset = get_wave_offset(in_Position.x, in_Position.z, time);
vec3 offset_pos = vec3(in_Position.x, in_Position.y + offset, in_Position.z);
world_pos = offset_pos; world_pos = offset_pos;
normal = normalize(vec3(-0.05 * cos(10.0 * in_Position.x + time), 1.0, 0.0)); normal = normalize(vec3(-0.05 * cos(10.0 * in_Position.x + time), 1.0, 0.0));
......
...@@ -94,6 +94,12 @@ struct Scene { ...@@ -94,6 +94,12 @@ struct Scene {
if (glutKeyIsDown('f')) { if (glutKeyIsDown('f')) {
pos -= speed * up; pos -= speed * up;
} }
if (glutKeyIsDown('e')) {
pos += speed * up;
}
if (glutKeyIsDown('q')) {
pos -= speed * up;
}
} }
void update_view_matrix() void update_view_matrix()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment