Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // VERTEX SHADER
- #version 330
- // Matrices
- uniform mat4 matrixProjection;
- uniform mat4 matrixView;
- uniform mat4 matrixModelView;
- uniform mat4 matrixViewInvert;
- // Materials
- uniform vec3 materialAmbient;
- uniform vec3 materialDiffuse;
- uniform float fogThickness;
- // Water
- uniform float waterLevel;
- uniform int flipTextures;
- // Clipping
- vec4 clipPlane = vec4(0, 0, 0, 0);
- vec4 clipPlane2 = vec4(0, -1, 0, 0);
- //uniform vec4 clipPlane;
- layout (location = 0) in vec3 aVertex;
- layout (location = 2) in vec3 aNormal;
- layout (location = 3) in vec2 aTexCoord;
- layout (location = 4) in vec3 aTangent;
- layout (location = 5) in vec3 aBiTangent;
- out vec4 color;
- out vec4 position;
- out vec3 normal;
- out float fogStrength;
- out vec2 texCoord0;
- out mat3 matrixTangent;
- out float waterDepth;
- out vec4 worldposition;
- // ambient light declaration
- struct AMBIENT
- {
- int on;
- vec3 color;
- };
- uniform AMBIENT lightAmbient;
- // directional light declaration
- struct DIRECTIONAL
- {
- int on;
- vec3 direction;
- vec3 diffuse;
- };
- uniform DIRECTIONAL lightDir;
- // ambient light function
- vec4 AmbientLight(AMBIENT light)
- {
- return vec4(materialAmbient * light.color, 1);
- }
- // directional light function
- vec4 DirectionalLight(DIRECTIONAL light)
- {
- vec4 color = vec4(0, 0, 0, 0);
- vec3 L = normalize(mat3(matrixView) * light.direction);
- float NdotL = dot(normal, L);
- if (NdotL > 0)
- {
- color += vec4 (materialDiffuse * light.diffuse, 1) * NdotL;
- }
- return color;
- }
- void main(void)
- {
- // calculate position
- position = matrixModelView * vec4(aVertex, 1.0);
- gl_Position = matrixProjection * position;
- worldposition = position;
- worldposition = matrixViewInvert * worldposition;
- // calculating color
- color = vec4 (0, 0, 0, 1);
- // calculating normals
- normal = normalize(mat3(matrixModelView) * aNormal);
- // calculating tangents for normal maps
- vec3 tangent = normalize(mat3(matrixModelView) * aTangent);
- vec3 biTangent = normalize(mat3(matrixModelView) * aBiTangent);
- matrixTangent = mat3(tangent, biTangent, normal);
- // calculate the observer's altitude above the observed vertex
- float eyeAlt = dot(-position.xyz, mat3(matrixModelView) * vec3(0, 1, 0));
- // calculating water depth at terrain vertex
- if (flipTextures == 0) waterDepth = waterLevel - aVertex.y;
- else waterDepth = waterLevel + aVertex.y;
- // calculating underwater fog
- fogStrength = exp2(-fogThickness * length(position) * (max(waterDepth, 0) / eyeAlt));
- // prepare texture vertexes to be sent to fragment shader
- texCoord0 = aTexCoord;
- // clipping stuff
- gl_ClipDistance[0] = dot(worldposition, clipPlane);
- gl_ClipDistance[1] = dot(worldposition, clipPlane2);
- if (lightAmbient.on == 1)
- {
- color += AmbientLight(lightAmbient);
- }
- if (lightDir.on == 1)
- {
- color += DirectionalLight(lightDir);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement