cube_lighting.shader
author Florian Boesch <pyalot@gmail.com>
Mon, 18 Apr 2011 12:02:34 +0000
changeset 2 05b6646eb1db
parent 0 0ce4d14393c2
permissions -rw-r--r--
implemented better irradiance and more parameters
     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, normal;
     7     attribute vec2 texcoord;
     8     varying vec2 v_texcoord;
     9     varying vec3 v_normal;
    10     uniform mat4 proj, view;
    11     uniform mat3 view_rot;
    12 
    13     void main(void) {
    14         gl_Position = proj * view * vec4(position, 1.0);
    15         v_texcoord = texcoord;
    16         v_normal = normal;
    17     }
    18 
    19 fragment:
    20     uniform vec2 viewport;
    21     uniform mat4 inv_proj;
    22     uniform mat3 inv_view_rot;
    23     uniform float shading_mix, reflectivity;
    24 
    25     varying vec2 v_texcoord;
    26     varying vec3 v_normal;
    27     uniform samplerCube diffuse, specular;
    28     
    29     vec3 get_world_normal(){
    30         vec2 frag_coord = gl_FragCoord.xy/viewport;
    31         frag_coord = (frag_coord-0.5)*2.0;
    32         vec4 device_normal = vec4(frag_coord, 0.0, 1.0);
    33         vec3 eye_normal = normalize((inv_proj * device_normal).xyz);
    34         vec3 world_normal = normalize(inv_view_rot*eye_normal);
    35         return world_normal;
    36     }
    37     
    38     void main(void){
    39         vec3 eye_normal = get_world_normal();
    40         vec3 normal = normalize(v_normal);
    41         vec3 specular_normal = reflect(eye_normal, normal);
    42         float reflection = smoothstep(0.0, 1.0, pow(dot(-eye_normal, normal), reflectivity));
    43         vec4 specular_color = textureCube(specular, specular_normal) * reflection;
    44         vec4 diffuse_color = textureCube(diffuse, normal);
    45         vec4 result = mix(specular_color, diffuse_color, shading_mix);
    46         gl_FragColor = vec4(pow(result.rgb, vec3(1.0/2.2)), 1.0);
    47     }