polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

lighting.glsl (2473B)


      1 
      2 
      3 //#include noise.glsl
      4 
      5 
      6 // float clouds(vec3 position) {
      7 //     float cloud_speed = 20000.0;
      8 //     float cloud_pos = time * cloud_speed;
      9 //     float cloud_trans = vec3(cloud_pos, cloud_pos, 0.0);
     10 
     11 //     float clouds = snoise((position + cloud_trans)/800.0);
     12 //     clouds *= max(0.0, dot(light_dir, vec3(0.0,0.0,1.0)));
     13 //     clouds *= 0.35 * exp(clouds * -0.3);
     14 
     15 //     return clouds;
     16 // }
     17 const float PI = 3.14159265;
     18 
     19 vec3 gamma_correct(vec3 color) {
     20     return pow(color, vec3(1.0/2.2));
     21 }
     22 
     23 vec3 uncharted_tonemap(const vec3 x) {
     24 	const float A = 0.15;
     25 	const float B = 0.50;
     26 	const float C = 0.10;
     27 	const float D = 0.20;
     28 	const float E = 0.02;
     29 	const float F = 0.30;
     30 	return ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F;
     31 }
     32 
     33 vec3 standard_light(vec3 color, vec4 position, vec4 normal) {
     34     // vec3 light_dir = vec3()
     35     const float shiny = 10.0;
     36     const float exposure = 0.2;
     37     float ambient_str = 0.2;
     38     float spec_str = 0.7 * light_intensity;
     39 
     40     vec4 trans_normal = normal_matrix * normal;
     41     vec3 L = light_dir;
     42     vec3 N = normalize(trans_normal.xyz);
     43     float cos_theta = max(0.0, dot(L,N));
     44 
     45     // float light_intensity = light_intensity * 0.01;
     46 
     47     // too much ambient during daytime is making things look weird
     48     // ambient_str =- light_intensity * ambient_str * 0.05;
     49 
     50     vec3 ray = camera_position - position.xyz;
     51     vec3 view_dir = normalize(ray);
     52 
     53     float brightness = cos_theta * light_intensity;
     54 
     55 
     56     // brightness += clouds(position);
     57     // brightness = clamp(brightness, 0.0, 1.0);
     58 
     59     vec3 diffuse = brightness * sun_color;
     60     vec3 ambient = ambient_str * sun_color;
     61 
     62     float spec;
     63     bool blinn = true;
     64     if (blinn) {
     65         const float energy_conservation = ( 8.0 + shiny ) / ( 8.0 * PI );
     66         vec3 halfway_dir = normalize(light_dir + view_dir);   // blinn-phong
     67         spec = energy_conservation * pow(max(dot(normal.xyz, halfway_dir), 0.0), shiny);
     68     }
     69 
     70     else {
     71         const float energy_conservation = ( 2.0 + shiny ) / ( 2.0 * PI );
     72         vec3 reflect_dir = reflect(-light_dir, normal.xyz); // phong
     73         spec = energy_conservation * pow(max(dot(view_dir, reflect_dir), 0.0), shiny);
     74     }
     75     // spec += pow(max(dot(view_dir, reflect_dir), 0.0), 16.0) * 0.5;
     76 
     77     vec3 specular = spec_str * spec * sun_color;
     78     vec3 final = (ambient + diffuse + specular) * color;
     79 
     80     // tone mapping
     81     // final = uncharted_tonemap(final);
     82 
     83     return final;
     84 }
     85 
     86 
     87 #include pbr.glsl