first commit
This commit is contained in:
+57
@@ -0,0 +1,57 @@
|
||||
// Copyright (c) 2016 The UUV Simulator Authors.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// Input parameters
|
||||
uniform sampler2D bumpMap;
|
||||
uniform samplerCube cubeMap;
|
||||
uniform vec4 deepColor;
|
||||
uniform vec4 shallowColor;
|
||||
uniform float fresnelPower;
|
||||
uniform float hdrMultiplier;
|
||||
|
||||
// Input computed in vertex shader
|
||||
varying mat3 rotMatrix;
|
||||
varying vec3 eyeVec;
|
||||
varying vec2 bumpCoord;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
// Apply bump mapping to normal vector to make waves look more detailed:
|
||||
vec4 bump = texture2D(bumpMap, bumpCoord)*2.0 - 1.0;
|
||||
vec3 N = normalize(rotMatrix * bump.xyz);
|
||||
|
||||
// Reflected ray:
|
||||
vec3 E = normalize(eyeVec);
|
||||
vec3 R = reflect(E, N);
|
||||
// Gazebo requires rotated cube map lookup.
|
||||
R = vec3(R.x, R.z, R.y);
|
||||
|
||||
// Get environment color of reflected ray:
|
||||
vec4 envColor = textureCube(cubeMap, R, 0.0);
|
||||
|
||||
// Cheap hdr effect:
|
||||
envColor.rgb *= (envColor.r+envColor.g+envColor.b)*hdrMultiplier;
|
||||
|
||||
// Compute refraction ratio (Fresnel):
|
||||
float facing = 1.0 - dot(-E, N);
|
||||
float refractionRatio = clamp(pow(facing, fresnelPower), 0.0, 1.0);
|
||||
|
||||
// Refracted ray only considers deep and shallow water colors:
|
||||
vec4 waterColor = mix(shallowColor, deepColor, facing);
|
||||
|
||||
// Perform linear interpolation between reflection and refraction.
|
||||
vec4 color = mix(waterColor, envColor, refractionRatio);
|
||||
gl_FragColor = vec4(color.xyz, 0.9);
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
// Copyright (c) 2016 The UUV Simulator Authors.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.s
|
||||
|
||||
// Input parameters
|
||||
uniform vec3 eyePos;
|
||||
uniform float rescale;
|
||||
uniform vec2 bumpScale;
|
||||
uniform vec2 bumpSpeed;
|
||||
uniform float time;
|
||||
uniform float frequency;
|
||||
uniform float amplitude;
|
||||
uniform float steepness;
|
||||
|
||||
// Output variables
|
||||
varying mat3 rotMatrix;
|
||||
varying vec3 eyeVec;
|
||||
varying vec2 bumpCoord;
|
||||
|
||||
// Compute linear combination of Gerstner waves as described in
|
||||
// GPU Gems, chapter 01: "Effective Water Simulation from Physical Models"
|
||||
// http://http.developer.nvidia.com/GPUGems/gpugems_ch01.html
|
||||
|
||||
// Information regarding a single wave
|
||||
struct WaveParameters {
|
||||
float w; // frequency
|
||||
float a; // amplitude
|
||||
float phi; // phase constant of speed
|
||||
vec2 d; // horizontal direction of wave
|
||||
float q; // steepness for Gerstner wave (q=0: rolling sine waves)
|
||||
};
|
||||
|
||||
void main(void)
|
||||
{
|
||||
// Use combination of three waves. Values here are chosen rather arbitrarily.
|
||||
// Other parameters might lead to better-looking waves.
|
||||
|
||||
#define N_WAVES 3
|
||||
WaveParameters waves[N_WAVES];
|
||||
waves[0] = WaveParameters(frequency, 0.6*amplitude, 0.5, vec2(-1, 0), steepness);
|
||||
waves[1] = WaveParameters(3.2*frequency, 0.4*amplitude, 1.7, vec2(-0.7, 0.7), 1.5*steepness);
|
||||
waves[2] = WaveParameters(1.8*frequency, 0.3*amplitude, 1.0, vec2(0.7, 0.7), 0.8*steepness);
|
||||
|
||||
vec4 P = gl_Vertex;
|
||||
|
||||
// Iteratively compute binormal, tangent, and normal vectors:
|
||||
vec3 B = vec3(1.0, 0.0, 0.0);
|
||||
vec3 T = vec3(0.0, 1.0, 0.0);
|
||||
vec3 N = vec3(0.0, 0.0, 1.0);
|
||||
|
||||
// Wave synthesis using linear combination of Gerstner waves
|
||||
for(int i = 0; i < N_WAVES; ++i)
|
||||
{
|
||||
// Evaluate wave equation:
|
||||
float angle = dot(waves[i].d, P.xy)*waves[i].w + time*waves[i].phi;
|
||||
float c = cos(angle);
|
||||
float s = sin(angle);
|
||||
float q = waves[i].q;
|
||||
|
||||
// Displacement of point due to wave (Eq. 9)
|
||||
P.x += q*waves[i].a*c*waves[i].d.x;
|
||||
P.y += q*waves[i].a*c*waves[i].d.y;
|
||||
P.z += waves[i].a*s;
|
||||
|
||||
// Modify normals due to wave displacement (Eq. 10-12)
|
||||
float wa = waves[i].a*waves[i].w;
|
||||
float qwas = q*wa*s;
|
||||
float wac = wa*c;
|
||||
float dx = waves[i].d.x;
|
||||
float dy = waves[i].d.y;
|
||||
float dxy = dx*dy;
|
||||
|
||||
B += vec3(-qwas*dx*dx, -qwas*dxy, wac*dx);
|
||||
T += vec3(-qwas*dxy, -qwas*dy*dy, wac*dy);
|
||||
N += vec3(-dx*wac, -dy*wac, -qwas);
|
||||
}
|
||||
|
||||
// Compute (Surf2World * Rescale) matrix
|
||||
B = normalize(B)*rescale;
|
||||
T = normalize(T)*rescale;
|
||||
N = normalize(N);
|
||||
rotMatrix = mat3(B, T, N);
|
||||
|
||||
gl_Position = gl_ModelViewProjectionMatrix*P;
|
||||
|
||||
// Compute texture coordinates for bump map
|
||||
bumpCoord = gl_MultiTexCoord0.xy*bumpScale + time*bumpSpeed;
|
||||
|
||||
eyeVec = P.xyz - eyePos; // eye position in vertex space
|
||||
}
|
||||
Reference in New Issue
Block a user