diff options
Diffstat (limited to 'engine-ocean/Resources/Shaders')
-rw-r--r-- | engine-ocean/Resources/Shaders/InventoryShader.frag | 15 | ||||
-rw-r--r-- | engine-ocean/Resources/Shaders/UIShader.frag | 29 | ||||
-rw-r--r-- | engine-ocean/Resources/Shaders/UIShader.vert | 18 | ||||
-rw-r--r-- | engine-ocean/Resources/Shaders/grass.frag | 22 | ||||
-rw-r--r-- | engine-ocean/Resources/Shaders/grass.vert | 25 | ||||
-rw-r--r-- | engine-ocean/Resources/Shaders/grassgeom.geom | 250 | ||||
-rw-r--r-- | engine-ocean/Resources/Shaders/phong.frag | 128 | ||||
-rw-r--r-- | engine-ocean/Resources/Shaders/phong.vert | 41 | ||||
-rw-r--r-- | engine-ocean/Resources/Shaders/skybox.frag | 23 | ||||
-rw-r--r-- | engine-ocean/Resources/Shaders/skybox.vert | 20 | ||||
-rw-r--r-- | engine-ocean/Resources/Shaders/text.frag | 12 | ||||
-rw-r--r-- | engine-ocean/Resources/Shaders/text.vert | 11 | ||||
-rw-r--r-- | engine-ocean/Resources/Shaders/water.frag | 63 | ||||
-rw-r--r-- | engine-ocean/Resources/Shaders/water.vert | 45 |
14 files changed, 702 insertions, 0 deletions
diff --git a/engine-ocean/Resources/Shaders/InventoryShader.frag b/engine-ocean/Resources/Shaders/InventoryShader.frag new file mode 100644 index 0000000..fc89ab1 --- /dev/null +++ b/engine-ocean/Resources/Shaders/InventoryShader.frag @@ -0,0 +1,15 @@ +#version 330 core +in vec2 texCoords; +out vec4 color; + +uniform sampler2D inventoryTexture0; + +void main() +{ + if (texCoords.x < 0.0 || texCoords.x > 1.0 || + texCoords.y < 0.0 || texCoords.y > 1.0) { + discard; + } + + color = texture(inventoryTexture0, texCoords); +} diff --git a/engine-ocean/Resources/Shaders/UIShader.frag b/engine-ocean/Resources/Shaders/UIShader.frag new file mode 100644 index 0000000..f7faef6 --- /dev/null +++ b/engine-ocean/Resources/Shaders/UIShader.frag @@ -0,0 +1,29 @@ +#version 330 core +in vec2 texCoords; +out vec4 color; + +uniform sampler2D guiTexture0; +uniform bool hovering; + + + +void main() +{ + if (texCoords.x < 0.0 || texCoords.x > 1.0 || + texCoords.y < 0.0 || texCoords.y > 1.0) { + discard; + } + + vec4 texel0, texel1; + //color = texture(guiTexture0, texCoords); + + texel0 = texture(guiTexture0, texCoords); + //texel1 = texture(guiTexture1, texCoords); + + color = texel0; + + if (hovering){ + color = mix(color, vec4(0.f, 0.f, 0.f, 1.f), .5f); + } + +} diff --git a/engine-ocean/Resources/Shaders/UIShader.vert b/engine-ocean/Resources/Shaders/UIShader.vert new file mode 100644 index 0000000..27152bd --- /dev/null +++ b/engine-ocean/Resources/Shaders/UIShader.vert @@ -0,0 +1,18 @@ +#version 330 core +layout (location = 0) in vec2 pos; // <vec2 pos, vec2 tex> +out vec2 texCoords; +uniform mat4 transform; +uniform mat4 projection; +uniform vec2 textureScale; +uniform bool hovering; + + + + +void main() +{ + + gl_Position = transform*vec4(pos.x, pos.y, 0.f, 1.f); + texCoords = textureScale*vec2((pos.x+1.f)/2.f, (pos.y+1.f)/2.f); + //texCoords = textureScale * (pos.x - 0.5f) + 0.5f; +} diff --git a/engine-ocean/Resources/Shaders/grass.frag b/engine-ocean/Resources/Shaders/grass.frag new file mode 100644 index 0000000..ac6de94 --- /dev/null +++ b/engine-ocean/Resources/Shaders/grass.frag @@ -0,0 +1,22 @@ +#version 330 core +out vec4 fragColor; +uniform sampler2D grass_texture; +uniform vec3 skyColor; + + +in GS_OUT { + vec2 texCoord; + float visibility; +} fs_in; + +void main(void) +{ + vec4 color = texture(grass_texture, fs_in.texCoord); + if (color.a < 0.4) discard; + + + fragColor = color; + fragColor = mix(vec4(skyColor, 1.f), fragColor, fs_in.visibility); + // fragColor = vec4(0, 0, 1, 1); + +} diff --git a/engine-ocean/Resources/Shaders/grass.vert b/engine-ocean/Resources/Shaders/grass.vert new file mode 100644 index 0000000..647f171 --- /dev/null +++ b/engine-ocean/Resources/Shaders/grass.vert @@ -0,0 +1,25 @@ +#version 330 core +layout (location = 0) in vec3 pos; + +out VS_OUT { + float visibility; +} vs_out; + +uniform mat4 view, projection; +uniform vec4 plane; + +// fog +const float density = .01f; +const float gradient = 4.f; + +void main() { + + gl_ClipDistance[0]=dot(vec4(pos, 1.f), plane); + + vec4 positionRelationToCam = view * vec4(pos, 1.f); + float distance = length(positionRelationToCam.xyz); + float vis = exp(-pow((distance*density), gradient)); + vs_out.visibility = clamp(vis, 0.f, 1.f); + + gl_Position = vec4(pos, 1.0); +} diff --git a/engine-ocean/Resources/Shaders/grassgeom.geom b/engine-ocean/Resources/Shaders/grassgeom.geom new file mode 100644 index 0000000..124d6b3 --- /dev/null +++ b/engine-ocean/Resources/Shaders/grassgeom.geom @@ -0,0 +1,250 @@ +#version 330 core + +layout (points) in; +layout (triangle_strip, max_vertices = 36) out; +uniform mat4 view, projection; +uniform vec3 worldSpace_camPos; +uniform float u_time; +uniform sampler2D wind_texture; +uniform sampler2D meadow_texture; +uniform vec3 playerPos; + +uniform vec4 plane; + + + +// texture atlasing +//uniform float numRows; +//uniform vec2 atlas_offset; + +in VS_OUT { + float visibility; +} gs_in[]; + +out GS_OUT { + vec2 texCoord; + float visibility; +} gs_out; + +// grass shader referenced from: https://vulpinii.github.io/tutorials/grass-modelisation/en/ +///////// +float PI = 3.141592653589793; +mat4 windModel = mat4(1.f); +mat4 trampleModel = mat4(1.f); +int numRows = 1; + +vec2 atlas_offset = vec2(0.f); + +vec2 getOffset(int index, int numRows){ + int column = int(mod(index, numRows)); + float xoffset = float(column)/float(numRows); + + int row = index/numRows; + float yOffset = float(row)/float(numRows); + return vec2(xoffset, yOffset); +} + +mat4 rotX(float a){ + mat4 rx = mat4(1.f); + rx[1] = vec4(0.f, cos(a), -sin(a), 0.f); + rx[2] = vec4(0.f, sin(a), cos(a), 0.f); + + return rx; +} + +mat4 rotY(float a){ + mat4 ry = mat4(1.f); + ry[0] = vec4(cos(a), 0.f, sin(a), 0.f); + ry[2] = vec4(-sin(a), 0.f, cos(a), 0.f); + + return ry; +} + +mat4 rotZ(float a){ + mat4 rz = mat4(1.f); + rz[0] = vec4(cos(a), sin(a), 0.f, 0.f); + rz[1] = vec4(-sin(a), cos(a), 0.f, 0.f); + + return rz; +} + +float randomize(vec2 st){ + return fract(sin(dot(st.xy,vec2(12.9898,78.233)))*43758.5453123); +} + + +void makeQuad(vec4 grass_pos, mat4 crossmodel){ + vec4 vertexPos[4]; + vertexPos[0] = vec4(-.9f, 0.f, 0.f, 0.f); // bottom left + vertexPos[1] = vec4(0.9f, 0.f, 0.f, 0.f); // bottom right + vertexPos[2] = vec4(-0.9f, 1.f, 0.f, 0.f); // upper left + vertexPos[3] = vec4(0.9f, 1.f, 0.f, 0.f); // upper right + + vec2 texCoords[4]; + texCoords[0] = vec2(0.f, 0.f); + texCoords[1] = vec2(1.f, 0.f); + texCoords[2] = vec2(0.f, 1.f); + texCoords[3] = vec2(1.f, 1.f); + + mat4 randomY = rotY(randomize(grass_pos.zx)*PI); + + // will apply wind only to top two corners of quad + mat4 defaultWind = mat4(1.f); + mat4 defaultTrample = mat4(1.f); + + gl_ClipDistance[0]=dot(vec4(grass_pos), plane); + + + vec2 clampedOffset = atlas_offset; + for (int i=0; i<4; i++){ + if (i>=2) { + defaultWind = windModel; + defaultTrample = trampleModel; + + } + + gl_Position = projection*view*(grass_pos + (defaultTrample*defaultWind*randomY*crossmodel*vertexPos[i])); + gs_out.texCoord = (texCoords[i]/numRows) + clampedOffset; + EmitVertex(); + } + + EndPrimitive(); + +} + +struct TrampleInfo { + vec3 trampleOffset; + float windMultiplier; + +}; + + +// trample referenced from NedMakesGames: https://www.youtube.com/watch?v=AmO7k-Lr0XM +TrampleInfo calculateTrample(vec3 entityWorldPos, float maxDistance, float falloff, float pushAwayStrength, float pushDownStrength){ + vec3 offset = vec3(0.f); + float windMultiplier = 1.f; + vec3 distanceVec = gl_in[0].gl_Position.xyz-playerPos; + float distance = length(distanceVec); + + // convert to trample strength + float strength = 1 - pow(clamp(distance / maxDistance, 0.f, 1.f), falloff); + + // apply pushAway offest in xz plane + vec3 xzDistance = vec3(distanceVec.x, 0.f, distanceVec.z); + vec3 pushAwayOffset = normalize(xzDistance) * pushAwayStrength * strength; + + // pushDown offset always points downwards + vec3 squishOffset = vec3(0.f, -1.f, 0.f) * pushDownStrength * strength; + + offset += pushAwayOffset + squishOffset; + + // supress wind when this grass is being trampled + + windMultiplier = min(windMultiplier, 1-strength); + + TrampleInfo info; + info.trampleOffset = offset; + info.windMultiplier = windMultiplier; + + return info; +} + +void makeGrass(int numQuads){ + mat4 model0, model45, modelm45; + model0 = mat4(1.f); + model45 = rotY(radians(45)); + modelm45 = rotY(-radians(45)); + + + // wind calculation + vec2 windDir = vec2(1.f); + float windStrength = .05f; + + // uv coordinates of wind + vec2 uv = gl_in[0].gl_Position.xz/10.f + windDir * u_time; + uv.x = mod(uv.x, 1.0); + uv.y = mod(uv.y, 1.0); + vec4 windSpot = texture(wind_texture, uv); + + // get index in meadow texture atlas depending on where uv is + + vec2 meadow_uv = gl_in[0].gl_Position.xz * .03f; + meadow_uv.x = mod(meadow_uv.x, 1.0); + meadow_uv.y = mod(meadow_uv.y, 1.0); + vec4 meadowSpot = texture(meadow_texture, meadow_uv); + float meadowSpotAccumulate = meadowSpot.r + meadowSpot.g + meadowSpot.b; + + // convert meadowSpot to range 0-maxindex + + numRows = 4; + int index = int(((meadowSpotAccumulate) / (3.f) ) * (16.f)); + if (index < 0) index = 0; + if (index > 15) index = 15; + atlas_offset = getOffset(index, numRows); + + + // matrix that rotates top of grass away from player + TrampleInfo info = calculateTrample(playerPos, 2.f, 5.f, .52f, .51f); + trampleModel = (rotX(info.trampleOffset.x*PI*1.f) * rotZ(info.trampleOffset.z*PI*1.f)); + + // matrix that tilts quad in x and z dir, accordiing to wind dir and force + windModel = (rotX(windSpot.x*PI*.75f*info.windMultiplier - PI*.25f) * rotZ(windSpot.z*PI*.75f*info.windMultiplier - PI*.25f)); + + + // draw number of quads based on level of detail + switch(numQuads){ + case 1: { + makeQuad(gl_in[0].gl_Position, model0); + break; + } + case 2: { + makeQuad(gl_in[0].gl_Position, model45); + makeQuad(gl_in[0].gl_Position, modelm45); + break; + } + default: { + makeQuad(gl_in[0].gl_Position, model0); + makeQuad(gl_in[0].gl_Position, model45); + makeQuad(gl_in[0].gl_Position, modelm45); + break; + } + } + +} + + +const float LOD1 = 5.f; +const float LOD2 = 10.f; +const float LOD3 = 30.f; + + +void main() +{ + gs_out.visibility = gs_in[0].visibility; + + vec3 dist_to_camera = gl_in[0].gl_Position.xyz - worldSpace_camPos; + float dist_length = length(dist_to_camera); + + // add transition for smooth levels + float t = 6.f; + if (dist_length > LOD2) t*=1.5f; + dist_length += (randomize(gl_in[0].gl_Position.xz)*t - t/2.f); + + // change depending on distance + int detailLevel = 3; + if (dist_length > LOD1) detailLevel = 2; + if (dist_length > LOD2) detailLevel = 1; + if (dist_length > LOD3) detailLevel = 0; + + // make grass with transition levels + if (detailLevel != 1 + || (detailLevel == 1 && (int(gl_in[0].gl_Position.x * 10) % 1) == 0 + || (int(gl_in[0].gl_Position.z * 10) % 1) == 0) + || (detailLevel == 2 && (int(gl_in[0].gl_Position.x * 5) % 1) == 0 + || (int(gl_in[0].gl_Position.z * 5) % 1) == 0) + ) { + makeGrass(detailLevel); + } +} + + diff --git a/engine-ocean/Resources/Shaders/phong.frag b/engine-ocean/Resources/Shaders/phong.frag new file mode 100644 index 0000000..a83fc54 --- /dev/null +++ b/engine-ocean/Resources/Shaders/phong.frag @@ -0,0 +1,128 @@ +#version 330 core +// Uniforms for shape information +in vec3 worldSpace_pos; +in vec3 worldSpace_norm; +in vec2 tex_coord; + +in float visibility; + +uniform vec3 skyColor; + +// Object Material Data +uniform int colorSource; // 0 = solid color (objColor), 1 = texture color (objTexture), 2 = per-vertex color (vertColor) +uniform vec3 objColor; +uniform sampler2D objTexture; +in vec3 vertColor; +uniform float shininess; + +// Camera uniform +uniform vec3 worldSpace_camPos; + +// Global Data +uniform vec3 coeffs; // vec3(ka, kd, ks) + +// Light Data +uniform int lightType[16]; // 0 = point light, 1 = directional light +uniform vec3 lightColor[16]; +uniform vec3 lightFunction[16]; // Attenuation coefficients +uniform vec3 worldSpace_lightPos[16]; //Light Positions +uniform vec3 worldSpace_lightDir[16]; //Light Directions +uniform int numLights; // Max number of lights = 8 + +out vec4 fragColor; + +vec3 getToLight(int lightIndex) { + int LIGHT_POINT = 0; + int LIGHT_DIRECTIONAL = 1; + + if (lightType[lightIndex] == LIGHT_POINT) { + return normalize(worldSpace_lightPos[lightIndex] - worldSpace_pos); + } + else if (lightType[lightIndex] == LIGHT_DIRECTIONAL) { + return normalize(-worldSpace_lightDir[lightIndex]); + } + + return vec3(0); +} + +float attenuationFactor(int lightIndex) { + int LIGHT_POINT = 0; + + if (lightType[lightIndex] == LIGHT_POINT) { + vec3 coeffs = lightFunction[lightIndex]; + float d = length(worldSpace_lightPos[lightIndex] - worldSpace_pos); + return 1.0 / (coeffs.x + coeffs.y * d + coeffs.z * d * d); + } + + return 1; +} + +float computeDiffuseIntensity(vec3 worldSpace_toLight) { + // Dot product to get diffuse intensity + return max(dot(worldSpace_toLight, normalize(worldSpace_norm)), 0); +} + +float computeSpecularIntensity(vec3 worldSpace_toLight, vec3 worldSpace_toEye) { + // Guard against pow weirdness when exponent is 0 + if (shininess == 0) { + return 0; + } + + //reflect toLight + vec3 worldSpace_toLightReflected = reflect(-worldSpace_toLight, normalize(worldSpace_norm)); + + //Compute specular intensity using toEye, reflected light, and shininess + return pow(max(dot(worldSpace_toLightReflected, worldSpace_toEye), 0), shininess); +} + +void main() { + // Declare ambient, diffuse, and specular terms + vec3 ambi = vec3(coeffs.x); + vec3 diff = vec3(0.0); + vec3 spec = vec3(0.0); + + + // Compute worldSpace_toEye Vector for specular intensity computation; + vec3 worldSpace_toEye = normalize(worldSpace_camPos - worldSpace_pos); + + + // Compute per-light diffuse and specular contribution + for(int i = 0; i<numLights; i+= 1){ + + // get direction vector to light based on light type + vec3 worldSpace_toLight = getToLight(i); + + float diffuse_intensity = computeDiffuseIntensity(worldSpace_toLight); + float specular_intensity = computeSpecularIntensity(worldSpace_toLight, worldSpace_toEye); + + float att = attenuationFactor(i); + + + diff = diff + diffuse_intensity * lightColor[i] * att; + spec = spec + specular_intensity * lightColor[i] * att; + } + + // Apply global coefficients and object color to the diffuse and specular components + diff = diff * vec3(coeffs.y); + spec = spec * vec3(coeffs.z); + + // Color generated only from light intensities and colors + vec3 tempColor = clamp(ambi + diff + spec, 0, 1); + + // Apply correct object color + if (colorSource == 0 ) { + fragColor = vec4(tempColor * objColor, 1.0); + } + else if (colorSource == 1){ + fragColor = vec4(tempColor * vec3(texture(objTexture, tex_coord)), 1.0); + } + else if (colorSource == 2) { + fragColor = vec4(tempColor * vertColor, 1.0); + } + else{ + fragColor = vec4(tempColor, 1.0); + } + + // mix actual color with fog color + // fragColor = mix(vec4(.77f, .85f, .99f, 1.f), fragColor, visibility); +} diff --git a/engine-ocean/Resources/Shaders/phong.vert b/engine-ocean/Resources/Shaders/phong.vert new file mode 100644 index 0000000..75d2aa1 --- /dev/null +++ b/engine-ocean/Resources/Shaders/phong.vert @@ -0,0 +1,41 @@ +#version 330 core + +// All in object space +layout (location = 0) in vec3 pos; +layout (location = 1) in vec3 norm; +layout (location = 2) in vec2 uv; +layout (location = 3) in vec3 color; + +uniform mat4 model, view, projection; +uniform vec3 worldSpace_camPos; + +out vec3 worldSpace_pos; +out vec3 worldSpace_norm; +out vec2 tex_coord; +out vec3 vertColor; +out float visibility; + +// fog +const float density = .01f; +const float gradient = 4.f; + +// REFLECTION/REFRACTION +uniform vec4 plane; // horizontal plane + +void main() { + worldSpace_pos = vec3(view*model*vec4(pos, 1.0)); + worldSpace_norm = vec3(transpose(inverse(model))*vec4(norm, 0.0)); + + vec4 positionRelationToCam = view * vec4(pos, 1.f); + float distance = length(positionRelationToCam.xyz); + visibility = exp(-pow((distance*density), gradient)); + visibility = clamp(visibility, 0.f, 1.f); + + // gl_ClipDistance[0]=dot(vec4(worldSpace_pos, 1.f), plane); + + + tex_coord = uv; + vertColor = color; + + gl_Position = projection*view*model*vec4(pos, 1.0); +} diff --git a/engine-ocean/Resources/Shaders/skybox.frag b/engine-ocean/Resources/Shaders/skybox.frag new file mode 100644 index 0000000..7589faf --- /dev/null +++ b/engine-ocean/Resources/Shaders/skybox.frag @@ -0,0 +1,23 @@ +#version 330 core + +in vec3 tex_coord; + +uniform samplerCube cubeMap; +uniform vec3 skyColor; + +out vec4 fragColor; + +const float lowerLimit = -50.f; +const float upperLimit = 100.f; + + +void main() { + + //fragColor = vec4(1.f); + vec4 finalColor = texture(cubeMap, tex_coord); + + // blending bottom of skybox to skyColor + float factor = (tex_coord.y - lowerLimit) / (upperLimit - lowerLimit); + factor = clamp(factor, 0.f, 1.f); + fragColor = mix(vec4(skyColor, 1.f), finalColor, factor); +} diff --git a/engine-ocean/Resources/Shaders/skybox.vert b/engine-ocean/Resources/Shaders/skybox.vert new file mode 100644 index 0000000..ddffb10 --- /dev/null +++ b/engine-ocean/Resources/Shaders/skybox.vert @@ -0,0 +1,20 @@ +#version 330 core + +layout (location = 0) in vec3 pos; +//layout (location = 1) in vec2 uv; + +uniform mat4 view, projection, rotation; + +out vec3 tex_coord; + + + +void main() { + + tex_coord = vec3(pos.x, pos.y, -pos.z); + vec4 world_pos = projection*view*rotation*vec4(pos, 1.0); + gl_Position = vec4(world_pos.x, world_pos.y, world_pos.w, world_pos.w); + +// tex_coord = pos; +// gl_Position = projection*view*vec4(pos, 1.0); +} diff --git a/engine-ocean/Resources/Shaders/text.frag b/engine-ocean/Resources/Shaders/text.frag new file mode 100644 index 0000000..782adc6 --- /dev/null +++ b/engine-ocean/Resources/Shaders/text.frag @@ -0,0 +1,12 @@ +#version 330 core +in vec2 TexCoords; +out vec4 color; + +uniform sampler2D text; +uniform vec3 textColor; + +void main() +{ + vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r); + color = vec4(textColor, 1.0) * sampled; +}
\ No newline at end of file diff --git a/engine-ocean/Resources/Shaders/text.vert b/engine-ocean/Resources/Shaders/text.vert new file mode 100644 index 0000000..2f835da --- /dev/null +++ b/engine-ocean/Resources/Shaders/text.vert @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec4 vertex; // <vec2 pos, vec2 tex> +out vec2 TexCoords; + +uniform mat4 projection; + +void main() +{ + gl_Position = projection * vec4(vertex.xy, 0.0, 1.0); + TexCoords = vertex.zw; +} diff --git a/engine-ocean/Resources/Shaders/water.frag b/engine-ocean/Resources/Shaders/water.frag new file mode 100644 index 0000000..b86e3a9 --- /dev/null +++ b/engine-ocean/Resources/Shaders/water.frag @@ -0,0 +1,63 @@ +#version 330 core +// Uniforms for shape information +//in vec3 worldSpace_pos; +//in vec3 worldSpace_norm; + +in float visibility; +in vec4 clipSpace; +in vec2 tex_coord; +in vec3 toCameraVector; + + + +// Camera uniform +uniform vec3 worldSpace_camPos; +uniform sampler2D reflect_texture; +uniform sampler2D refract_texture; +uniform sampler2D du_dv_map; +uniform float moveFactor; // offset that changes over time + +out vec4 fragColor; + +const float distortionStrength = .02f; +const vec4 waterColor = vec4(.06f, .49f, .59f, 1.f); +uniform vec3 skyColor; + + +void main() { + + // screen space coords + vec2 ndc = (clipSpace.xy/clipSpace.w)/2.f + .5f; + + vec2 refractTexCoords = ndc; + vec2 reflectTexCoords = vec2(ndc.x, ndc.y); + + // get associated rg color in the range -1,1 from du_dv map + vec2 distortion1 = (texture(du_dv_map, vec2(tex_coord.x + moveFactor, tex_coord.y)).rg*2.f - 1.f) * distortionStrength; + vec2 distortion2 = (texture(du_dv_map, vec2(-tex_coord.x + moveFactor, tex_coord.y + moveFactor)).rg*2.f - 1.f) * distortionStrength; + vec2 totalDistortion = distortion1 + distortion2; + + refractTexCoords += totalDistortion; + reflectTexCoords += totalDistortion; + + refractTexCoords = clamp(refractTexCoords, 0.001f, .999f); + reflectTexCoords = clamp(reflectTexCoords, 0.001f, .999f); + + // fresnel effect + vec3 viewVector = normalize(toCameraVector); + float refractiveFactor = dot(viewVector, vec3(0.f, 1.f, 0.f)); + refractiveFactor = pow(refractiveFactor, 5.f); // changes how reflective water is + + + + + + vec4 reflect = texture(reflect_texture, reflectTexCoords); + vec4 refract = texture(refract_texture, refractTexCoords); + + + // mix actual color with fog color + fragColor = mix(reflect, refract, refractiveFactor); + fragColor = mix(fragColor, waterColor, .2f); + fragColor = mix(vec4(skyColor, 1.f), fragColor, visibility); +} diff --git a/engine-ocean/Resources/Shaders/water.vert b/engine-ocean/Resources/Shaders/water.vert new file mode 100644 index 0000000..4cf407c --- /dev/null +++ b/engine-ocean/Resources/Shaders/water.vert @@ -0,0 +1,45 @@ +#version 330 core + +// All in object space +layout (location = 0) in vec3 pos; +layout (location = 1) in vec3 norm; +layout (location = 2) in vec2 uv; +layout (location = 3) in vec3 color; + +uniform mat4 model, view, projection; +uniform vec3 worldSpace_camPos; + +//out vec3 worldSpace_norm; +out vec2 tex_coord; +out vec3 vertColor; +out float visibility; +out vec4 clipSpace; +out vec3 toCameraVector; + +// fog +const float density = .01f; +const float gradient = 4.f; + +const float tiling = 6.f; + +vec4 worldSpace_pos; + +// water referenced from: ThinMatrix https://www.youtube.com/watch?v=qgDPSnZPGMA + +void main() { + worldSpace_pos = view*model*vec4(pos, 1.0); + //worldSpace_norm = vec3(transpose(inverse(model))*vec4(norm, 0.0)); + + vec4 positionRelationToCam = view * vec4(pos, 1.f); + float distance = length(positionRelationToCam.xyz); + visibility = exp(-pow((distance*density), gradient)); + visibility = clamp(visibility, 0.f, 1.f); + + tex_coord = uv*tiling; + vertColor = color; + + clipSpace = projection*worldSpace_pos; + gl_Position = clipSpace; + + toCameraVector = worldSpace_camPos - worldSpace_pos.xyz; +} |