| 1 |
#version 330 core |
| 2 |
|
| 3 |
in vec3 vertexPosition; |
| 4 |
in vec3 vertexNormal; |
| 5 |
|
| 6 |
out EyeSpaceVertex { |
| 7 |
vec3 position; |
| 8 |
vec3 normal; |
| 9 |
} vs_out; |
| 10 |
|
| 11 |
uniform mat4 modelView; |
| 12 |
uniform mat3 modelViewNormal; |
| 13 |
uniform mat4 mvp; |
| 14 |
|
| 15 |
uniform float time; |
| 16 |
uniform float amplitude = 1.0; |
| 17 |
uniform float lambda = 20.0; |
| 18 |
uniform float period = 15.0; |
| 19 |
|
| 20 |
// |
| 21 |
// Description : Array and textureless GLSL 3D simplex noise function. |
| 22 |
// Author : Ian McEwan, Ashima Arts. |
| 23 |
// Maintainer : ijm |
| 24 |
// Lastmod : 20110409 (stegu) |
| 25 |
// License : Copyright (C) 2011 Ashima Arts. All rights reserved. |
| 26 |
// Distributed under the MIT License. See LICENSE file. |
| 27 |
// |
| 28 |
|
| 29 |
vec4 permute( vec4 x ) |
| 30 |
{ |
| 31 |
return mod(((x*34.0)+1.0)*x, 289.0); |
| 32 |
} |
| 33 |
|
| 34 |
vec4 taylorInvSqrt( vec4 r ) |
| 35 |
{ |
| 36 |
return 1.79284291400159 - 0.85373472095314 * r; |
| 37 |
} |
| 38 |
|
| 39 |
float snoise(vec3 v) |
| 40 |
{ |
| 41 |
const vec2 C = vec2(1.0/6.0, 1.0/3.0) ; |
| 42 |
const vec4 D = vec4(0.0, 0.5, 1.0, 2.0); |
| 43 |
|
| 44 |
// First corner |
| 45 |
vec3 i = floor(v + dot(v, C.yyy) ); |
| 46 |
vec3 x0 = v - i + dot(i, C.xxx) ; |
| 47 |
|
| 48 |
// Other corners |
| 49 |
vec3 g = step(x0.yzx, x0.xyz); |
| 50 |
vec3 l = 1.0 - g; |
| 51 |
vec3 i1 = min( g.xyz, l.zxy ); |
| 52 |
vec3 i2 = max( g.xyz, l.zxy ); |
| 53 |
|
| 54 |
// x0 = x0 - 0. + 0.0 * C |
| 55 |
vec3 x1 = x0 - i1 + 1.0 * C.xxx; |
| 56 |
vec3 x2 = x0 - i2 + 2.0 * C.xxx; |
| 57 |
vec3 x3 = x0 - 1. + 3.0 * C.xxx; |
| 58 |
|
| 59 |
// Permutations |
| 60 |
i = mod(i, 289.0 ); |
| 61 |
vec4 p = permute( permute( permute( |
| 62 |
i.z + vec4(0.0, i1.z, i2.z, 1.0 )) |
| 63 |
+ i.y + vec4(0.0, i1.y, i2.y, 1.0 )) |
| 64 |
+ i.x + vec4(0.0, i1.x, i2.x, 1.0 )); |
| 65 |
|
| 66 |
// Gradients |
| 67 |
// ( N*N points uniformly over a square, mapped onto an octahedron.) |
| 68 |
float n_ = 1.0/7.0; // N=7 |
| 69 |
vec3 ns = n_ * D.wyz - D.xzx; |
| 70 |
|
| 71 |
vec4 j = p - 49.0 * floor(p * ns.z *ns.z); // mod(p,N*N) |
| 72 |
|
| 73 |
vec4 x_ = floor(j * ns.z); |
| 74 |
vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N) |
| 75 |
|
| 76 |
vec4 x = x_ *ns.x + ns.yyyy; |
| 77 |
vec4 y = y_ *ns.x + ns.yyyy; |
| 78 |
vec4 h = 1.0 - abs(x) - abs(y); |
| 79 |
|
| 80 |
vec4 b0 = vec4( x.xy, y.xy ); |
| 81 |
vec4 b1 = vec4( x.zw, y.zw ); |
| 82 |
|
| 83 |
//vec4 s0 = vec4(lessThan(b0,0.0))*2.0 - 1.0; |
| 84 |
//vec4 s1 = vec4(lessThan(b1,0.0))*2.0 - 1.0; |
| 85 |
vec4 s0 = floor(b0)*2.0 + 1.0; |
| 86 |
vec4 s1 = floor(b1)*2.0 + 1.0; |
| 87 |
vec4 sh = -step(h, vec4(0.0)); |
| 88 |
|
| 89 |
vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ; |
| 90 |
vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ; |
| 91 |
|
| 92 |
vec3 p0 = vec3(a0.xy,h.x); |
| 93 |
vec3 p1 = vec3(a0.zw,h.y); |
| 94 |
vec3 p2 = vec3(a1.xy,h.z); |
| 95 |
vec3 p3 = vec3(a1.zw,h.w); |
| 96 |
|
| 97 |
//Normalise gradients |
| 98 |
vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3))); |
| 99 |
p0 *= norm.x; |
| 100 |
p1 *= norm.y; |
| 101 |
p2 *= norm.z; |
| 102 |
p3 *= norm.w; |
| 103 |
|
| 104 |
// Mix final noise value |
| 105 |
vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0); |
| 106 |
m = m * m; |
| 107 |
return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1), |
| 108 |
dot(p2,x2), dot(p3,x3) ) ); |
| 109 |
} |
| 110 |
|
| 111 |
float height( const in vec3 pos ) |
| 112 |
{ |
| 113 |
// Perturb the y position by a wave function in (x, t) |
| 114 |
const float twoPi = 2.0 * 3.14159265358979323846; |
| 115 |
float k = twoPi / lambda; |
| 116 |
float omega = twoPi / period; |
| 117 |
|
| 118 |
float y = amplitude * sin( k * pos.x - omega * time ); |
| 119 |
|
| 120 |
// Further perturb by a noise function |
| 121 |
y += snoise( 0.27 * vec3( 0.4 * pos.x, 3.0, 2.0 * pos.z - 0.5 * time ) ); |
| 122 |
|
| 123 |
return y; |
| 124 |
} |
| 125 |
|
| 126 |
void main() |
| 127 |
{ |
| 128 |
// Calculate y value based upon input coordinates and time |
| 129 |
vec3 pos = vertexPosition; |
| 130 |
pos.y = height( pos ); |
| 131 |
|
| 132 |
// Estimate normal vector by calculating the y value at small offsets |
| 133 |
// and then taking the cross product of the differences |
| 134 |
float offset = 0.5; |
| 135 |
vec3 posOffsetX = vertexPosition + vec3( offset, 0.0, 0.0 ); |
| 136 |
posOffsetX.y = height( posOffsetX ); |
| 137 |
vec3 posOffsetZ = vertexPosition + vec3( 0.0, 0.0, offset ); |
| 138 |
posOffsetZ.y = height( posOffsetZ ); |
| 139 |
|
| 140 |
vec3 dydx = posOffsetX - pos; |
| 141 |
vec3 dydz = posOffsetZ - pos; |
| 142 |
vec3 normal = cross( dydz, dydx ); |
| 143 |
|
| 144 |
// Transform the position and normal to eye space for lighting |
| 145 |
vs_out.normal = normalize( modelViewNormal * normal ); |
| 146 |
vs_out.position = vec3( modelView * vec4( pos, 1.0 ) ); |
| 147 |
|
| 148 |
gl_Position = mvp * vec4( pos, 1.0 ); |
| 149 |
} |