All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Common.ShaderLib.BlinnPhongLighting.glsllib Maven / Gradle / Ivy

The newest version!
/*Blinn Phong lighting*/

/*
* Computes diffuse factor (Lambert)
*/
float lightComputeDiffuse(in vec3 norm, in vec3 lightdir){
    return max(0.0, dot(norm, lightdir));
}

/*
* Computes specular factor   (blinn phong) 
*/
float lightComputeSpecular(in vec3 norm, in vec3 viewdir, in vec3 lightdir, in float shiny){
    vec3 H = normalize(viewdir + lightdir);
    float HdotN = max(0.0, dot(H, norm));
    return pow(HdotN, shiny);
}

/*
*Computes standard phong specular lighting
*/
float lightComputeSpecularPhong(in vec3 norm, in vec3 viewdir, in vec3 lightdir, in float shiny){
    vec3 R = reflect(-lightdir, norm);
    return pow(max(dot(R, viewdir), 0.0), shiny);
}


/*
* Computes diffuse and specular factors and pack them in a vec2 (x=diffuse, y=specular)
*/
vec2 computeLighting(in vec3 norm, in vec3 viewDir, in vec3 lightDir, in float attenuation, in float shininess){
   float diffuseFactor = lightComputeDiffuse(norm, lightDir);
   float specularFactor = lightComputeSpecular(norm, viewDir, lightDir, shininess);      
   if (shininess <= 1.0) {
       specularFactor = 0.0; // should be one instruction on most cards ..
   }
   specularFactor *= diffuseFactor;
   return vec2(diffuseFactor, specularFactor) * vec2(attenuation);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy