opengl - Ambient and Specular lighting not working correctly in GLSL -
in lighting scene, reason ambient lighting isn't working @ all. whole model same brightness, no matter way facing. tried getting rid of attenuation still has same results. along that, specular lighting shining, no matter camera is. supposed shine based on player position. here screenshot of ambient problem: imgur.com
as can see, part of sphere facing away light (located @ [0.0,4.0,0.0]) same color part facing light. ambient factor supposed 0.2 of fragment color.
vertex shader source:
layout(location = 0) in vec3 positions; layout(location = 1) in vec2 texcoords; layout(location = 2) in vec3 normals; out vec3 new_normal; out vec3 worldpos_out; out vec2 pass_texcoords; struct matrices { mat4 projection; mat4 worldmatrix; mat4 modelmatrix; mat3 normalmatrix; }; uniform matrices mat; void main(void) { pass_texcoords = texcoords; vec4 newposition = vec4(positions, 1); vec4 worldpos = (mat.modelmatrix * newposition); mat4 camera = mat.projection * mat.worldmatrix; gl_position = (camera * worldpos); new_normal = mat.normalmatrix * normals; worldpos_out = worldpos.xyz; }
fragment shader source:
in vec3 new_normal; in vec3 worldpos_out; in vec2 pass_texcoords; out vec4 outcolor; uniform vec3 viewpos; #define max_lights 50 struct material { sampler2d diffusemap; sampler2d specularmap; vec3 specular; float shininess; }; uniform material material; struct light { vec3 position; vec3 color; vec3 ambient; vec3 diffuse; vec3 specular; float radius; }; uniform light lights[max_lights]; uniform int numlights; struct math { float constant; float linear; float quadratic; } math; vec3 applypointlight(light light, vec3 normal, vec3 fragpos, vec3 viewdir, vec3 surfacecolor, vec3 surfacespecular) { vec3 lightdir = normalize(light.position - fragpos); //diffuse shading float diff = max(dot(normal, lightdir), 0.0); //specular shading vec3 reflectdir = reflect(-lightdir, normal); float spec = pow(max(dot(viewdir, reflectdir), 0.0), material.shininess); //attenuation float distance = length(light.position - fragpos); float attenuation = 5.0 / (math.constant + math.linear * distance + math.quadratic * (distance * distance)); vec3 ambient = light.ambient * surfacecolor; vec3 diffuse = light.diffuse * surfacecolor * light.color; vec3 specular = light.specular * surfacespecular * light.color; ambient *= attenuation; diffuse *= attenuation; specular *= attenuation; return (ambient + diffuse + specular); } void main(void) { vec3 surfacecolor = vec3(texture(material.diffusemap, pass_texcoords)); vec3 surfacespecular = vec3(texture(material.specularmap, pass_texcoords)); vec3 unitnormal = normalize(new_normal); vec3 viewdir = normalize(viewpos - worldpos_out); math.constant = 1.0; math.linear = 0.09; math.quadratic = 0.032; vec3 linearcolor; for(int = 0; < numlights; i++) linearcolor += applypointlight(lights[i], unitnormal, worldpos_out, viewdir, surfacecolor, surfacespecular); float gamma = 2.2; vec3 fragcolor; fragcolor.rgb = pow(linearcolor.rgb, vec3(1.0/gamma)); outcolor = vec4(linearcolor, 1.0); }
in applypointlight
function, you're not using diff
, spec
variables, presumably light-dependent changes diffuse , specular. see if following works:
vec3 diffuse = light.diffuse * surfacecolor * light.color * diff; vec3 specular = light.specular * surfacespecular * light.color * spec;
Comments
Post a Comment