basic.fs (1366B)
1 #version 330 core 2 3 in vec3 frag_pos; 4 5 out vec4 frag_color; 6 7 const vec3 light_color = vec3(1.0, 1.0, 1.0); 8 9 uniform int shade_normals; 10 uniform vec3 view_pos; 11 uniform vec3 pointlight_pos; 12 13 void main() 14 { 15 vec3 dx = dFdx(frag_pos); 16 vec3 dy = dFdy(frag_pos); 17 vec3 normal = normalize(cross(dy, dx)); 18 19 if (shade_normals == 1) 20 { 21 frag_color = vec4(0.5 * (normal + 1.0), 1.0); 22 } 23 else 24 { 25 float slope = (normal.y + 1.0) * 0.5; 26 vec3 object_color = mix(vec3(0.6, 0.7, 0.5), vec3(0.5, 0.6, 0.7), slope); 27 28 float d = length(pointlight_pos - frag_pos); 29 float attenuation = 1.0 / (1.0 + 0.022 * d + 0.0019 * d * d); 30 31 vec3 light_dir = normalize(frag_pos - pointlight_pos); 32 vec3 view_dir = normalize(view_pos - frag_pos); 33 vec3 reflect_dir = reflect(-light_dir, normal); 34 35 float ambient_strength = 0.1; 36 vec3 ambient = ambient_strength * light_color * attenuation; 37 38 float diffuse_strength = max(dot(normal, light_dir), 0.0); 39 vec3 diffuse = diffuse_strength * light_color * attenuation; 40 41 float specular_strength = 0.5; 42 float spec = pow(max(dot(view_dir, reflect_dir), 0.0), 32); 43 vec3 specular = specular_strength * spec * light_color * attenuation; 44 45 frag_color = vec4((ambient + diffuse + specular) * object_color, 1.0); 46 } 47 }