cubemap_convolve.shader
author Florian Boesch <pyalot@gmail.com>
Mon, 18 Apr 2011 14:06:37 +0000
changeset 4 174c42423100
parent 2 05b6646eb1db
permissions -rw-r--r--
removed an obsolete smoothstep
     1 /*
     2     :copyright: 2011 by Florian Boesch <pyalot@gmail.com>.
     3     :license: GNU AGPL3, see LICENSE for more details.
     4 */
     5 vertex:
     6     attribute vec3 position;
     7 
     8     void main(void) {
     9         gl_Position = vec4(position, 1.0);
    10     }
    11 
    12 fragment:
    13     uniform samplerCube source;
    14     uniform vec2 viewport;
    15     uniform mat4 inv_proj;
    16     uniform mat3 inv_view_rot;
    17     uniform float specularity;
    18     
    19     const vec3 x = vec3(1.0, 0.0, 0.0);
    20     const vec3 y = vec3(0.0, 1.0, 0.0);
    21     const vec3 z = vec3(0.0, 0.0, 1.0);
    22     
    23     const mat3 front = mat3(x, y, z);
    24     const mat3 back = mat3(x, y, -z);
    25     const mat3 right = mat3(z, y, x);
    26     const mat3 left = mat3(z, y, -x);
    27     const mat3 top = mat3(x, z, y);
    28     const mat3 bottom = mat3(x, z, -y);
    29 
    30     const float size = 16.0;
    31     const float start = ((0.5/size)-0.5)*2.0;
    32     const float end = -start;
    33     const float incr = 2.0/size;
    34     
    35     vec3 get_eye_normal(){
    36         vec2 frag_coord = gl_FragCoord.xy/viewport;
    37         frag_coord = (frag_coord-0.5)*2.0;
    38         vec4 device_normal = vec4(frag_coord, 0.0, 1.0);
    39         vec3 eye_normal = normalize((inv_proj * device_normal).xyz);
    40         vec3 world_normal = normalize(inv_view_rot*eye_normal);
    41         return world_normal;
    42     }
    43 
    44     vec4 sample(mat3 side, vec3 eyedir, vec3 base_ray){
    45         vec3 ray = side*base_ray;
    46         float lambert = max(0.0, dot(ray, eyedir));
    47         float term = pow(lambert, specularity)*base_ray.z;
    48         return vec4(textureCube(source, ray).rgb*term, term);
    49     }
    50 
    51     void main(){
    52         vec4 result = vec4(0.0);
    53         vec3 eyedir = get_eye_normal(), ray;
    54 
    55         for(float xi=start; xi<=end; xi+=incr){
    56             for(float yi=start; yi<=end; yi+=incr){
    57                 ray = normalize((inv_proj * vec4(xi, yi, 0.0, 1.0)).xyz);
    58                 result += sample(front, eyedir, ray);
    59                 result += sample(back, eyedir, ray);
    60                 result += sample(top, eyedir, ray);
    61                 result += sample(bottom, eyedir, ray);
    62                 result += sample(left, eyedir, ray);
    63                 result += sample(right, eyedir, ray);
    64             }
    65         }
    66         result /= result.w;
    67         gl_FragColor = vec4(result.rgb, 1.0);
    68     }