Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 330
- in vec4 position_in_viewspace;
- in vec3 normal_in_viewspace;
- in vec2 tex_coord;
- out vec4 color;
- uniform sampler2D diffuse_map;
- uniform sampler2D tree_normal;
- uniform vec4 diffuse = vec4(0,0,0,1);
- uniform struct SLight {
- vec3 position_in_viewspace;
- float radius;
- vec3 material_color_diffuse;
- vec3 material_color_specular;
- vec3 material_color_ambient;
- } light[64];
- uniform int light_count;
- float compute_blinn_term(in vec3 N, in vec3 L, in vec3 V, in float NdotL, in float shininess)
- {
- vec3 H = normalize(L + V);
- float term = dot(N,H);
- term = max(clamp(term, 0.0, 1.0), 0.0);
- term = pow(term, shininess);
- return term;
- }
- vec3 color_from_light(in int light_index, in vec3 N, in vec3 V, in vec3 diffuse)
- {
- vec3 L = (light[light_index].position_in_viewspace - position_in_viewspace.xyz);
- vec3 L_rad = L / light[light_index].radius;
- float light_attenuation = max(0, 1.0 - dot(L_rad,L_rad));
- vec3 light_diffuse = light[light_index].material_color_diffuse;
- vec3 light_specular = light[light_index].material_color_specular;
- vec3 light_ambient = light[light_index].material_color_ambient;
- float NdotL = max(dot(N,L), 0.0);
- float shininess = 0.75;
- float term = compute_blinn_term(N, L, V, NdotL, shininess);
- //Uncomment to debug light attenuation
- //return vec4(1,1,1,1) * light_attenuation;
- return vec3(
- (diffuse * NdotL * light_diffuse * light_attenuation) +
- (diffuse * term * light_specular * light_attenuation) +
- (diffuse * light_ambient * 0.5)
- );
- }
- void main() {
- vec3 N = normalize(normal_in_viewspace);
- //vec3 N = normalize(texture(tree_normal, tex_coord).rgb * 1.0 - vec3(1.0));
- vec3 V = normalize(-position_in_viewspace.xyz);
- vec4 texel = diffuse * texture(diffuse_map, tex_coord);
- if(texel.a < 0.5){
- discard;
- }
- color = texel;
- for(int i = 0; i < light_count; i++)
- {
- color += vec4(color_from_light(i, N, V, diffuse.rgb * vec3(0.05)),1);
- }
- if(color == vec4(0,0,0,1)) {
- color = diffuse;
- }
- const float LOG2 = 1.442695;
- float fog_density = 0.0045;
- vec4 fog_color = vec4(0.431372,0.458823,0.486274, 0.5);
- float z = gl_FragCoord.z / gl_FragCoord.w;
- float fog_factor = exp2( -fog_density*fog_density * z*z * LOG2 );
- fog_factor = clamp(fog_factor, 0.0, 1.0);
- color = mix(fog_color, color, fog_factor);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement