Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualMrOMGWTF

Posted 14 October 2012 - 12:31 AM

(spherical harmonics or environment mapping are better approaches for ambient lighting though).


Here's little code snippet for SH lighting:
varying vec3 Normal;

const float C1 = 0.429043;
const float C2 = 0.511664;
const float C3 = 0.743125;
const float C4 = 0.886227;
const float C5 = 0.247708;

// Constants for Old Town Square lighting
const vec3 L00  = vec3( 0.871297,  0.875222,  0.864470);
const vec3 L1m1 = vec3( 0.175058,  0.245335,  0.312891);
const vec3 L10  = vec3( 0.034675,  0.036107,  0.037362);
const vec3 L11  = vec3(-0.004629, -0.029448, -0.048028);
const vec3 L2m2 = vec3(-0.120535, -0.121160, -0.117507);
const vec3 L2m1 = vec3( 0.003242,  0.003624,  0.007511);
const vec3 L20  = vec3(-0.028667, -0.024926, -0.020998);
const vec3 L21  = vec3(-0.077539, -0.086325, -0.091591);
const vec3 L22  = vec3(-0.161784, -0.191783, -0.219152);

void main()
{
    vec3 AmbientColor =  C1 * L22 * (Normal.x * Normal.x - Normal.y * Normal.y) +
                    C3 * L20 * Normal.z * Normal.z +
                    C4 * L00 -
                    C5 * L20 +
                    2.0 * C1 * L2m2 * Normal.x * Normal.y +
                    2.0 * C1 * L21  * Normal.x * Normal.z +
                    2.0 * C1 * L2m1 * Normal.y * Normal.z +
                    2.0 * C2 * L11  * Normal.x +
                    2.0 * C2 * L1m1 * Normal.y +   
                    2.0 * C2 * L10  * Normal.z;

    gl_FragCoord = AmbientColor;
}

It was taken from some article about sh... I can't find it.

You can use only 2 sh bands and you will still get nice ambient light. Here's code if you want to use two bands:
varying vec3 Normal;

const float C1 = 0.429043;
const float C2 = 0.511664;
const float C3 = 0.743125;
const float C4 = 0.886227;
const float C5 = 0.247708;

// Constants for Old Town Square lighting
const vec3 L00  = vec3( 0.871297,  0.875222,  0.864470);
const vec3 L1m1 = vec3( 0.175058,  0.245335,  0.312891);
const vec3 L10  = vec3( 0.034675,  0.036107,  0.037362);
const vec3 L11  = vec3(-0.004629, -0.029448, -0.048028);

void main()
{

    vec3 AmbientColor =  C4 * L00 -
                    2.0 * C2 * L11  * Normal.x +
                    2.0 * C2 * L1m1 * Normal.y +   
                    2.0 * C2 * L10  * Normal.z;

    gl_FragCoord = AmbientColor;
}

#1MrOMGWTF

Posted 14 October 2012 - 12:30 AM

Here's little code snippet for SH lighting:
varying vec3 Normal;

const float C1 = 0.429043;
const float C2 = 0.511664;
const float C3 = 0.743125;
const float C4 = 0.886227;
const float C5 = 0.247708;

// Constants for Old Town Square lighting
const vec3 L00  = vec3( 0.871297,  0.875222,  0.864470);
const vec3 L1m1 = vec3( 0.175058,  0.245335,  0.312891);
const vec3 L10  = vec3( 0.034675,  0.036107,  0.037362);
const vec3 L11  = vec3(-0.004629, -0.029448, -0.048028);
const vec3 L2m2 = vec3(-0.120535, -0.121160, -0.117507);
const vec3 L2m1 = vec3( 0.003242,  0.003624,  0.007511);
const vec3 L20  = vec3(-0.028667, -0.024926, -0.020998);
const vec3 L21  = vec3(-0.077539, -0.086325, -0.091591);
const vec3 L22  = vec3(-0.161784, -0.191783, -0.219152);

void main()
{
    vec3 AmbientColor =  C1 * L22 * (Normal.x * Normal.x - Normal.y * Normal.y) +
                    C3 * L20 * Normal.z * Normal.z +
                    C4 * L00 -
                    C5 * L20 +
                    2.0 * C1 * L2m2 * Normal.x * Normal.y +
                    2.0 * C1 * L21  * Normal.x * Normal.z +
                    2.0 * C1 * L2m1 * Normal.y * Normal.z +
                    2.0 * C2 * L11  * Normal.x +
                    2.0 * C2 * L1m1 * Normal.y +   
                    2.0 * C2 * L10  * Normal.z;

    gl_FragCoord = AmbientColor;
}

It was taken from some article about sh... I can't find it.

You can use only 2 sh bands and you will still get nice ambient light. Here's code if you want to use two bands:
varying vec3 Normal;

const float C1 = 0.429043;
const float C2 = 0.511664;
const float C3 = 0.743125;
const float C4 = 0.886227;
const float C5 = 0.247708;

// Constants for Old Town Square lighting
const vec3 L00  = vec3( 0.871297,  0.875222,  0.864470);
const vec3 L1m1 = vec3( 0.175058,  0.245335,  0.312891);
const vec3 L10  = vec3( 0.034675,  0.036107,  0.037362);
const vec3 L11  = vec3(-0.004629, -0.029448, -0.048028);

void main()
{

    vec3 AmbientColor =  C4 * L00 -
                    2.0 * C2 * L11  * Normal.x +
                    2.0 * C2 * L1m1 * Normal.y +   
                    2.0 * C2 * L10  * Normal.z;

    gl_FragCoord = AmbientColor;
}

PARTNERS