Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // FRAGMENT SHADER
- #version 330
- // Matrices
- uniform mat4 matrixView;
- vec3 finalNormal;
- // Materials
- uniform vec3 materialAmbient;
- uniform vec3 materialDiffuse;
- uniform vec3 materialSpecular;
- uniform float shininess;
- //uniform vec3 fogColor;
- uniform float reflectionPower;
- uniform sampler2DShadow shadowMap;
- uniform float B1;
- uniform float B2;
- // Textures
- uniform sampler2D texture0;
- uniform sampler2D textureNormal;
- uniform samplerCube textureCubeMap;
- uniform float NormalMap;
- // Input from Vert
- in vec4 color;
- in vec4 position;
- in vec3 normal;
- //in float fogStrength;
- in vec2 texCoord0;
- in vec3 texCoordCubeMap;
- in mat3 matrixTangent;
- in vec4 shadowCoord;
- // Output
- out vec4 outColor;
- // point light declaration
- struct POINT
- {
- int on;
- vec3 position;
- vec3 diffuse;
- vec3 specular;
- };
- uniform POINT lightPoint, lightPoint2;
- // spot light declaration
- struct SPOT
- {
- int on;
- vec3 position;
- vec3 diffuse;
- vec3 specular;
- vec3 direction;
- float cutoff;
- float attenuation;
- float strength;
- mat4 matrix;
- };
- uniform SPOT lightSpot;
- // point light function
- vec4 PointLight(POINT light)
- {
- // diffuse light
- vec4 color = vec4(0, 0, 0, 1);
- vec3 L = normalize((matrixView * vec4(light.position, 1)) - position).xyz;
- float NdotL = dot(L, finalNormal);
- if (NdotL > 0)
- {
- color += vec4 (light.diffuse * materialDiffuse, 1) * NdotL;
- }
- // specular specular
- vec3 V = normalize(-position.xyz);
- vec3 R = reflect(-L, finalNormal);
- float RdotV = dot(R, V);
- if (NdotL > 0 && RdotV > 0)
- {
- color += vec4(light.specular * materialSpecular * pow(RdotV, shininess), 1);
- }
- return color;
- }
- // spot light function
- vec4 SpotLight(SPOT light)
- {
- vec4 color = vec4(0, 0, 0, 1);
- vec3 L = normalize(light.matrix * vec4(light.position, 1) - position).xyz;
- // diffuse light
- float NdotL = dot(L, finalNormal);
- if (NdotL > 0)
- {
- color += vec4 (materialDiffuse * light.diffuse, 1) * NdotL;
- }
- // specular light
- vec3 V = normalize(-position.xyz);
- vec3 R = reflect(-L, finalNormal);
- float RdotV = dot(R, V);
- if (NdotL > 0 && RdotV > 0)
- {
- color += vec4(materialSpecular * light.specular * pow(RdotV, shininess), 1);
- }
- // spot light
- vec3 D = normalize(mat3(light.matrix) * light.direction);
- float S = dot(-L, D);
- float A = acos(S);
- float cutoff = radians(clamp(light.cutoff, 0, 360));
- if (A < cutoff) S = pow(S, light.attenuation);
- else S = 0;
- // attenuation
- float dist = length(light.matrix * vec4(light.position, 1) - position);
- float att = 1 / (dist * dist) / light.strength;
- return color * S * att;
- }
- void main(void)
- {
- outColor = color;
- if (NormalMap == 1)
- {
- finalNormal = 2.0 * texture(textureNormal, texCoord0).xyz - vec3(1.0, 1.0, 1.0);
- finalNormal = normalize(matrixTangent * finalNormal);
- }
- else finalNormal = normal;
- if (lightPoint.on == 1) outColor += PointLight(lightPoint);
- if (lightPoint2.on == 1) outColor += PointLight(lightPoint2);
- if (lightSpot.on == 1) outColor += SpotLight(lightSpot);
- // calculating shadows (for a source)
- float shadow = 1.0;
- if (B1 == 1)
- {
- if(shadowCoord.w > 0) // if shadowCoord.w < 0 fragment is out of the Light POV
- shadow = 0.5 + 0.5 * textureProj(shadowMap, shadowCoord);
- }
- if (B2 == 1)
- {
- if(shadowCoord.w > 0) // if shadowCoord.w < 0 fragment is out of the Light POV
- shadow = 0.5 + 0.5 * textureProj(shadowMap, shadowCoord);
- }
- outColor *= shadow;
- outColor = mix(outColor * texture(texture0, texCoord0.st), texture(textureCubeMap, texCoordCubeMap), reflectionPower);
- outColor *= texture(texture0, texCoord0);
- //outColor = mix(vec4(fogColor, 1), outColor, fogStrength);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement