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 matrixShadow;
- // Materials
- uniform vec3 materialAmbient;
- uniform vec3 materialDiffuse;
- uniform float fogThickness;
- 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 mat3 matrixTangent;
- out float fogStrength;
- out vec2 texCoord0;
- out vec3 texCoordCubeMap;
- out vec4 shadowCoord;
- // 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;
- // 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);
- // calculating fog
- fogStrength = exp2(-fogThickness * length(position));
- // prepare texture vertexes to be sent to fragment shader
- texCoord0 = aTexCoord;
- // calculating cubemap reflection
- texCoordCubeMap = inverse(mat3(matrixView)) * mix(reflect(position.xyz, normal), normal.xyz, 0.5);
- // calculate shadow coordinate–using the Shadow Matrix
- mat4 matrixModel = inverse(matrixView) * matrixModelView;
- shadowCoord = matrixShadow * matrixModel * vec4(aVertex + aNormal * 0.1, 1);
- if (lightAmbient.on == 1) color += AmbientLight(lightAmbient);
- if (lightDir.on == 1) color += DirectionalLight(lightDir);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement