| 1 |
#version 330 |
| 2 |
|
| 3 |
const float PI = 3.1415926535897932384626433832795; |
| 4 |
|
| 5 |
const float waveLength = 20.0; |
| 6 |
const float waveAmplitude = 1.0; |
| 7 |
const float specularReflectivity = 0.4; |
| 8 |
const float shineDamper = 20.0; |
| 9 |
|
| 10 |
layout(location = 0) in vec2 in_position; |
| 11 |
layout(location = 1) in vec4 in_indicators; |
| 12 |
|
| 13 |
out vec4 pass_clipSpaceGrid; |
| 14 |
out vec4 pass_clipSpaceReal; |
| 15 |
out vec3 pass_normal; |
| 16 |
out vec3 pass_toCameraVector; |
| 17 |
out vec3 pass_specular; |
| 18 |
out vec3 pass_diffuse; |
| 19 |
|
| 20 |
uniform float height; |
| 21 |
uniform vec3 cameraPos; |
| 22 |
uniform float waveTime; |
| 23 |
|
| 24 |
uniform vec3 lightDirection; |
| 25 |
uniform vec3 lightColour; |
| 26 |
uniform vec2 lightBias; |
| 27 |
|
| 28 |
uniform mat4 projectionViewMatrix; |
| 29 |
|
| 30 |
vec3 calcSpecularLighting(vec3 toCamVector, vec3 toLightVector, vec3 normal){ |
| 31 |
vec3 reflectedLightDirection = reflect(-toLightVector, normal); |
| 32 |
float specularFactor = dot(reflectedLightDirection , toCamVector); |
| 33 |
specularFactor = max(specularFactor,0.0); |
| 34 |
specularFactor = pow(specularFactor, shineDamper); |
| 35 |
return specularFactor * specularReflectivity * lightColour; |
| 36 |
} |
| 37 |
|
| 38 |
vec3 calculateDiffuseLighting(vec3 toLightVector, vec3 normal){ |
| 39 |
float brightness = max(dot(toLightVector, normal), 0.0); |
| 40 |
return (lightColour * lightBias.x) + (brightness * lightColour * lightBias.y); |
| 41 |
} |
| 42 |
|
| 43 |
vec3 calcNormal(vec3 vertex0, vec3 vertex1, vec3 vertex2){ |
| 44 |
vec3 tangent = vertex1 - vertex0; |
| 45 |
vec3 bitangent = vertex2 - vertex0; |
| 46 |
return normalize(cross(tangent, bitangent)); |
| 47 |
} |
| 48 |
|
| 49 |
float generateOffset(float x, float z){ |
| 50 |
float radiansX = (x / waveLength + waveTime) * 2.0 * PI; |
| 51 |
float radiansZ = (z / waveLength + waveTime) * 2.0 * PI; |
| 52 |
return waveAmplitude * 0.5 * (sin(radiansZ) + cos(radiansX)); |
| 53 |
} |
| 54 |
|
| 55 |
vec3 applyDistortion(vec3 vertex){ |
| 56 |
float xDistortion = generateOffset(vertex.x, vertex.z); |
| 57 |
float yDistortion = generateOffset(vertex.x, vertex.z); |
| 58 |
float zDistortion = generateOffset(vertex.x, vertex.z); |
| 59 |
return vertex + vec3(xDistortion, yDistortion, zDistortion); |
| 60 |
} |
| 61 |
|
| 62 |
void main(void){ |
| 63 |
|
| 64 |
vec3 currentVertex = vec3(in_position.x, height, in_position.y); |
| 65 |
vec3 vertex1 = currentVertex + vec3(in_indicators.x, 0.0, in_indicators.y); |
| 66 |
vec3 vertex2 = currentVertex + vec3(in_indicators.z, 0.0, in_indicators.w); |
| 67 |
|
| 68 |
pass_clipSpaceGrid = projectionViewMatrix * vec4(currentVertex, 1.0); |
| 69 |
|
| 70 |
currentVertex = applyDistortion(currentVertex); |
| 71 |
vertex1 = applyDistortion(vertex1); |
| 72 |
vertex2 = applyDistortion(vertex2); |
| 73 |
|
| 74 |
pass_normal = calcNormal(currentVertex, vertex1, vertex2); |
| 75 |
|
| 76 |
pass_clipSpaceReal = projectionViewMatrix * vec4(currentVertex, 1.0); |
| 77 |
gl_Position = pass_clipSpaceReal; |
| 78 |
|
| 79 |
pass_toCameraVector = normalize(cameraPos - currentVertex); |
| 80 |
|
| 81 |
vec3 toLightVector = -normalize(lightDirection); |
| 82 |
pass_specular = calcSpecularLighting(pass_toCameraVector, toLightVector, pass_normal); |
| 83 |
pass_diffuse = calculateDiffuseLighting(toLightVector, pass_normal); |
| 84 |
} |
| 85 |
|
| 86 |
// From https://github.com/TheThinMatrix/WaterStep10/blob/master/water/waterRendering/waterVertex.glsl |