Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////////////////////////
- ////////////////////VERTEX SHADER////////////////////
- /////////////////////////////////////////////////////
- #version 330
- layout(location = 0) in vec3 inPosition;
- layout(location = 1) in vec3 inNormal;
- layout(location = 2) in vec2 inTexCoord;
- layout(location = 3) in vec3 inTangent;
- layout(location = 4) in vec3 inBitangent;
- out vec2 texCoord;
- out vec3 camDir;
- out vec3 lightDir;
- out float attenuation;
- uniform mat4 viewMatrix;
- uniform mat4 modelMatrix;
- uniform mat4 projectionMatrix;
- uniform mat3 normalMatrix;
- uniform vec3 camPosIn;
- uniform vec3 lightPosIn;
- void main()
- {
- vec3 N = normalize(normalMatrix * inNormal);
- vec3 T = normalize(normalMatrix * inTangent);
- T = normalize(T - (dot(N, T) * N));
- vec3 B = normalize(normalMatrix * inBitangent);
- if (dot(cross(N, T), B) < 0.0)
- T = T * -1.0;
- mat3 TBNMatrix = transpose(mat3(T, B, N));
- vec3 position = vec3((viewMatrix * modelMatrix) * vec4(inPosition, 1.0)); //Vector is a position so W = 1.0. If it were a direction w = 0.0.
- camDir = TBNMatrix * (normalize(camPosIn - position));
- lightDir = TBNMatrix * (normalize(lightPosIn - position));
- texCoord = inTexCoord;
- float radius = 50.0;
- float dist = length(vec3((viewMatrix * modelMatrix) * vec4(lightPosIn, 1.0)) - position);
- attenuation = 1.0 / (1.0 + ((2.0 / radius) * dist) + ((1.0 / (radius * radius)) * (dist * dist)));
- gl_Position = (projectionMatrix * (viewMatrix * modelMatrix)) * vec4(inPosition, 1.0);
- }
- ///////////////////////////////////////////////////////
- ////////////////////FRAGMENT SHADER////////////////////
- ///////////////////////////////////////////////////////
- #version 330
- precision mediump float;
- in vec2 texCoord;
- in vec3 camDir;
- in vec3 lightDir;
- in float attenuation;
- layout (location = 0) out vec4 FragColour;
- uniform sampler2D textureMap;
- uniform sampler2D normalMap;
- uniform sampler2D AOMap;
- uniform sampler2D specMap;
- uniform sampler2D roughMap;
- uniform vec3 lightColour;
- void main() //Blinn-Phong = (Ka * Ia) + (Kd * (Il * (N.L))) + (Ks * (Il * ((N.H)^Ns)))
- {
- vec3 albedo = texture(textureMap, texCoord).rgb;
- vec3 normals = normalize(2.0 * texture(normalMap, texCoord).rgb - 1.0);
- float roughness = texture(roughMap, texCoord).r;
- float spec = texture(specMap, texCoord).r;
- float AO = texture(AOMap, texCoord).r;
- vec3 diffuse = vec3(0.0);
- vec3 specular = vec3(0.0);
- vec3 ambientColour = vec3(0.0);
- for (int i = 0; i < 1; i++)
- {
- vec3 halfAngle = normalize(camDir + lightDir);
- float NdotL = clamp(dot(normals, lightDir), 0.0, 1.0);
- float NdotH = clamp(dot(normals, halfAngle), 0.0, 1.0);
- float specularHighlight = pow(NdotH, 1.0 - roughness);
- vec3 S = spec * (lightColour * specularHighlight);
- vec3 D = (1.0 - roughness) * (lightColour * NdotL);
- diffuse += D * attenuation;
- specular += S * attenuation;
- ambientColour += lightColour * attenuation;
- }
- vec3 ambience = (0.01 * ambientColour) * AO;
- vec3 BlinnPhong = ambience + diffuse + specular;
- vec3 final = albedo * BlinnPhong;
- final = pow(final, vec3(1.0 / 2.2));
- FragColour = vec4(final, 1.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement