Sponza рэндэрлэх оролдлого 9, point light, attenuation

point light хэрэгжүүлэлт






attenuation утгуудыг дараах холбоосоос авч болно
http://www.ogre3d.org/tikiwiki/tiki-index.php?page=-Point%20Light%20Attenuation

Fragment Shader
#version 330
uniform sampler2D material_diffuse_texture;
uniform sampler2D material_normal_texture;
uniform sampler2D material_ambient_texture;
uniform sampler2D material_specular_texture;
uniform sampler2D material_opacity_texture;
uniform float     material_shininess; 
uniform vec4      material_diffuse_color;

uniform int hasOpacityMap;
uniform int hasAmbientMap;

uniform float ambientIntensity;

uniform vec3 lightPosition; // world-space
uniform vec3 spotDir;

in vec2 vTexcoord;
in vec3 vPosition; // world-space
in vec3 vNormal;   // view-space
in vec3 vEyeDir;   // view-space
in vec3 vLightDir; // view-space

out vec4 out_color;

void main(void)
{
    vec3 lightColor = vec3(1.0, 1.0, 1.0);

    vec4 diffuseColor  = texture2D(material_diffuse_texture, vTexcoord);
    vec4 ambientColor  = texture2D(material_ambient_texture, vTexcoord);
    vec4 normalColor   = vec4(texture2D(material_normal_texture , vTexcoord).xyz, 1.0);
    vec4 specularColor = texture2D(material_specular_texture, vTexcoord);
    vec4 opacityColor  = texture2D(material_opacity_texture, vTexcoord);

    if (hasAmbientMap==0) {
        ambientColor = diffuseColor;
    }

    vec4 resAmbientColor  = vec4(0, 0, 0, 0);
    vec4 resDiffuseColor  = vec4(0, 0, 0, 0);
    vec4 resSpecularColor = vec4(0, 0, 0, 0);
    vec4 resultColor      = vec4(0, 0, 0, 0);

    // ambient calculation
    float ambientFraction = 0.05;
    resAmbientColor = vec4((ambientFraction * lightColor * ambientColor.xyz).xyz, 1.0);
    
    vec3 normal = normalize(vNormal);
    vec3 lightDirection = normalize(vLightDir);

    // diffuse calculation, lambertian cosinus
    float lambertcos = max(0.0, dot(normal, lightDirection));
    resDiffuseColor = vec4((lambertcos * lightColor * diffuseColor.xyz).xyz, 1.0);

    // specular calculation, blinn-phong
    float lightDistance = length(lightPosition-vPosition);
    vec3 halfVector = normalize(normalize(vEyeDir) + lightDirection);
    // Blinn-Phong needs shininiess about 4 * Phong shininess
    float specularcoeff = pow(max(0.0, dot(normal, halfVector)), 4.0 * material_shininess); 
    resSpecularColor = vec4((specularcoeff * lightColor * specularColor.xyz).xyz, 1.0);

    float attenuation = 0.0;
    if (lambertcos>0.0) {
        float constant = 1.0;
        float linear   = 0.014;
        float quadric  = 0.0007;
        attenuation = 1.0 / (constant + linear*lightDistance + quadric*lightDistance*lightDistance);
    }    

    resultColor = clamp(resAmbientColor + attenuation * (resDiffuseColor + resSpecularColor), 0.0, 1.0);
    
    
    if (hasOpacityMap!=0){
        if (opacityColor.rgb==vec3(0,0,0))
            discard;
        else
            out_color = resultColor;
    } else {
        out_color = resultColor;
    }
    
}



Popular posts from this blog

Apache Spark + Cassandra. Hello World