X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=shaderlib%2Fphong.glsl;h=004232acd503e4c76c607ca6206f2f4e228cdef1;hb=adc26a2e141a2853b6c5025130c46a46cece4b84;hp=90f0ac8ac51ef65f8ed05bc5b44dccdbf9cf28a6;hpb=146df2d02940a04aeafe854bdd981a1489cc80b3;p=libs%2Fgl.git diff --git a/shaderlib/phong.glsl b/shaderlib/phong.glsl index 90f0ac8a..004232ac 100644 --- a/shaderlib/phong.glsl +++ b/shaderlib/phong.glsl @@ -2,19 +2,39 @@ import msp_interface; import common; import shadow; -const bool use_diffuse_map = false; -const bool use_specular = false; -const bool use_specular_map = false; -const bool use_shininess_map = false; -const bool use_emission = false; -const bool use_emission_map = false; -const bool use_reflectivity = false; -const bool use_reflectivity_map = false; -const bool use_sky = false; -const bool use_fog = false; +struct BasicMaterialParameters +{ + vec4 diffuse; + vec4 specular; + vec4 emission; + float shininess; + float reflectivity; +}; + +uniform BasicMaterial +{ + BasicMaterialParameters basic_material; +}; + +uniform sampler2D diffuse_map; +uniform sampler2D specular_map; +uniform sampler2D shininess_map; +uniform sampler2D emission_map; +uniform sampler2D reflectivity_map; + +layout(constant_id=auto) const bool use_diffuse_map = false; +layout(constant_id=auto) const bool use_specular = false; +layout(constant_id=auto) const bool use_specular_map = false; +layout(constant_id=auto) const bool use_shininess_map = false; +layout(constant_id=auto) const bool use_emission = false; +layout(constant_id=auto) const bool use_emission_map = false; +layout(constant_id=auto) const bool use_reflectivity = false; +layout(constant_id=auto) const bool use_reflectivity_map = false; +layout(constant_id=auto) const bool use_sky = false; +layout(constant_id=auto) const bool use_fog = false; #pragma MSP stage(fragment) -vec4 get_diffuse_color() +virtual vec4 get_diffuse_color() { if(use_diffuse_map) return texture(diffuse_map, texcoord.xy); @@ -22,7 +42,7 @@ vec4 get_diffuse_color() return basic_material.diffuse; } -vec3 get_specular_color() +virtual vec3 get_specular_color() { if(use_specular_map) return texture(specular_map, texcoord.xy).rgb; @@ -30,7 +50,7 @@ vec3 get_specular_color() return basic_material.specular.rgb; } -float get_shininess_value() +virtual float get_shininess_value() { if(use_shininess_map) return texture(shininess_map, texcoord.xy).r*255.0; @@ -38,7 +58,7 @@ float get_shininess_value() return basic_material.shininess; } -vec3 get_emission_color() +virtual vec3 get_emission_color() { if(use_emission_map) return texture(emission_map, texcoord.xy).rgb; @@ -46,7 +66,7 @@ vec3 get_emission_color() return basic_material.emission.rgb; } -float get_reflectivity_value() +virtual float get_reflectivity_value() { if(use_reflectivity_map) return texture(reflectivity_map, texcoord.xy).r; @@ -65,10 +85,8 @@ vec3 phong_one_light(vec3 light, vec3 normal, vec3 look, vec3 light_color, vec3 vec3 color = light_color*surface_diffuse*diffuse_intensity; if(use_specular) { - /* The light vector points towards the light, so reflected will point - towards the surface - but so does the look vector. */ - vec3 reflected = reflect(light, normal); - float specular_intensity = pow(max(dot(reflected, look), 0.0), shininess); + vec3 reflected = reflect(look, normal); + float specular_intensity = pow(max(dot(reflected, light), 0.0), shininess); color += light_color*surface_specular*specular_intensity; } return color; @@ -76,15 +94,14 @@ vec3 phong_one_light(vec3 light, vec3 normal, vec3 look, vec3 light_color, vec3 vec3 phong_lighting(vec3 normal, vec3 look, vec3 surface_diffuse, vec3 surface_specular, float shininess) { - vec3 light; - if(use_normal_map) - light = normalize(tbn_light_dir); - else - light = normalize(eye_light_dir); - vec3 color = phong_ambient(surface_diffuse); - float shadow = get_shadow_factor(0); - color += phong_one_light(light, normal, look, light_sources[0].diffuse.rgb, surface_diffuse, surface_specular, shininess)*shadow; + for(int i=0; i