Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Phong fragment shader phong-tex.frag matched with phong-tex.vert
- #version 330
- // Some drivers require the following
- precision highp float;
- struct lightStruct
- {
- vec4 ambient;
- vec4 diffuse;
- vec4 specular;
- };
- struct materialStruct
- {
- vec4 ambient;
- vec4 diffuse;
- vec4 specular;
- float shininess;
- };
- uniform float attConst;
- uniform float attLinear;
- uniform float attQuadratic;
- in float ex_D;
- uniform lightStruct light;
- uniform materialStruct material;
- uniform sampler2D textureUnit0;
- in vec3 ex_N;
- in vec3 ex_V;
- in vec3 ex_L;
- in vec2 ex_TexCoord;
- layout(location = 0) out vec4 out_Color;
- void main(void) {
- // Ambient intensity
- vec4 ambientI = light.ambient * material.ambient;
- // Diffuse intensity
- vec4 diffuseI = light.diffuse * material.diffuse;
- diffuseI = diffuseI * max(dot(normalize(ex_N),normalize(ex_L)),0);
- // Specular intensity
- // Calculate R - reflection of light
- vec3 R = normalize(reflect(normalize(-ex_L),normalize(ex_N)));
- vec4 specularI = light.specular * material.specular;
- specularI = specularI * pow(max(dot(R,ex_V),0), material.shininess);
- float attenuation = attConst + attLinear * ex_D + attQuadratic * ex_D*ex_D;
- attenuation = 1/attenuation;
- vec4 tmp_Color = (diffuseI + specularI);//attenuation does not
- //affect ambient light
- vec4 litColour = ambientI+vec4(tmp_Color.rgb / attenuation, 1.0);
- // Fragment colour
- out_Color = litColour * texture(textureUnit0, ex_TexCoord);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement