polyadvent

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

lighting.glsl (2817B)


      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 
     18 vec3 hemispherical(vec3 color, vec3 normal) {
     19   vec4 v4_normal = vec4(normal , 1);
     20   vec4 trans_normal = normal_matrix * v4_normal;
     21 
     22   vec3 l = light_dir;
     23   vec3 n = normalize(trans_normal.xyz);
     24 
     25   float costheta = dot(l,n);
     26 
     27   float a = 0.5 + (0.5 * costheta);
     28   return a * light_intensity * color + (1.0-a) * vec3(0.0, 0.0, 0.0) * color;
     29 }
     30 
     31 const float PI = 3.14159265;
     32 
     33 vec3 gamma_correct(vec3 color) {
     34     return pow(color, vec3(1.0/2.2));
     35 }
     36 
     37 vec3 uncharted_tonemap(const vec3 x) {
     38 	const float A = 0.15;
     39 	const float B = 0.50;
     40 	const float C = 0.10;
     41 	const float D = 0.20;
     42 	const float E = 0.02;
     43 	const float F = 0.30;
     44 	return ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F;
     45 }
     46 
     47 vec3 standard_light(vec3 color, vec4 position, vec4 normal) {
     48     // vec3 light_dir = vec3()
     49     const float shiny = 10.0;
     50     const float exposure = 0.2;
     51     float ambient_str = 0.2;
     52     float spec_str = 0.7 * light_intensity;
     53 
     54     vec4 trans_normal = normal_matrix * normal;
     55     vec3 L = light_dir;
     56     vec3 N = normalize(trans_normal.xyz);
     57     float cos_theta = max(0.0, dot(L,N));
     58 
     59     // float light_intensity = light_intensity * 0.01;
     60 
     61     // too much ambient during daytime is making things look weird
     62     // ambient_str =- light_intensity * ambient_str * 0.05;
     63 
     64     vec3 ray = camera_position - position.xyz;
     65     vec3 view_dir = normalize(ray);
     66 
     67     float brightness = cos_theta * light_intensity;
     68 
     69 
     70     // brightness += clouds(position);
     71     // brightness = clamp(brightness, 0.0, 1.0);
     72 
     73     vec3 diffuse = brightness * sun_color;
     74     vec3 ambient = ambient_str * sun_color;
     75 
     76     float spec;
     77     bool blinn = true;
     78     if (blinn) {
     79         const float energy_conservation = ( 8.0 + shiny ) / ( 8.0 * PI );
     80         vec3 halfway_dir = normalize(light_dir + view_dir);   // blinn-phong
     81         spec = energy_conservation * pow(max(dot(normal.xyz, halfway_dir), 0.0), shiny);
     82     }
     83 
     84     else {
     85         const float energy_conservation = ( 2.0 + shiny ) / ( 2.0 * PI );
     86         vec3 reflect_dir = reflect(-light_dir, normal.xyz); // phong
     87         spec = energy_conservation * pow(max(dot(view_dir, reflect_dir), 0.0), shiny);
     88     }
     89     // spec += pow(max(dot(view_dir, reflect_dir), 0.0), 16.0) * 0.5;
     90 
     91     vec3 specular = spec_str * spec * sun_color;
     92     vec3 final = (ambient + diffuse + specular) * color;
     93 
     94     // tone mapping
     95     // final = uncharted_tonemap(final);
     96 
     97     return final;
     98 }
     99 
    100 
    101 #include pbr.glsl