Skip to main content
Commonmark migration
Source Link

I coded my first glsl shader which manage diffuse and specular effects of a directional light. This is the fragment shader.

#version 120 //matrix uniform mat4 model_matrix; //directional light position in world coordinates uniform vec3 light_position; uniform vec3 eye; //send to fragment shader varying vec4 color; varying vec4 normal; varying vec4 vertex; void main() { float light_diffuse_intensity; float light_specular_intensity; float light_mod; vec3 world_normal; vec3 world_vertex; vec3 world_reflec; vec3 vertex_to_light; vec3 vertex_to_eye; world_normal = (model_matrix * normal).xyz; world_vertex = (model_matrix * vertex).xyz; vertex_to_light = light_position - world_vertex; //vector from vertex to light vertex_to_eye = eye - world_vertex; //vector from vertex to eye world_reflec = 2 * dot(vertex_to_light, normalize(world_normal.xyz)) * normalize(world_normal) - vertex_to_light; //reflection of vertex_to_light light_diffuse_intensity = dot(normalize(vertex_to_light), normalize(world_normal.xyz)); //computing angle between light and normal light_specular_intensity = dot(normalize(vertex_to_eye), normalize(world_reflec)); //computing angle between eye and reflection if (light_diffuse_intensity > 0){ light_mod = light_diffuse_intensity; light_specular_intensity = pow(light_specular_intensity, 8.0); } else{ light_specular_intensity = 0; } gl_FragColor = vec4(mix(color.rgb * light_diffuse_intensity, vec3(1.0,1.0,1.0), light_specular_intensity), 1.0); } 

As you can see from the screenshot below, in the first screen, where I look at the sphere from the direction of the light, the border of the sphere is white because of the specular effect. However if the shader were correct it should not look like that. It should be white only in the middle of the sphere.

In the second screen, I look at both the sphere and the source of the directional light (the yellow sun) and everything looks just fine.

I cannot find the error in my shader, could someone help please?

Thank you very much!

![image of the error][1] [1]: https://i.sstatic.net/SJqsr.pngimage of the error

I coded my first glsl shader which manage diffuse and specular effects of a directional light. This is the fragment shader.

#version 120 //matrix uniform mat4 model_matrix; //directional light position in world coordinates uniform vec3 light_position; uniform vec3 eye; //send to fragment shader varying vec4 color; varying vec4 normal; varying vec4 vertex; void main() { float light_diffuse_intensity; float light_specular_intensity; float light_mod; vec3 world_normal; vec3 world_vertex; vec3 world_reflec; vec3 vertex_to_light; vec3 vertex_to_eye; world_normal = (model_matrix * normal).xyz; world_vertex = (model_matrix * vertex).xyz; vertex_to_light = light_position - world_vertex; //vector from vertex to light vertex_to_eye = eye - world_vertex; //vector from vertex to eye world_reflec = 2 * dot(vertex_to_light, normalize(world_normal.xyz)) * normalize(world_normal) - vertex_to_light; //reflection of vertex_to_light light_diffuse_intensity = dot(normalize(vertex_to_light), normalize(world_normal.xyz)); //computing angle between light and normal light_specular_intensity = dot(normalize(vertex_to_eye), normalize(world_reflec)); //computing angle between eye and reflection if (light_diffuse_intensity > 0){ light_mod = light_diffuse_intensity; light_specular_intensity = pow(light_specular_intensity, 8.0); } else{ light_specular_intensity = 0; } gl_FragColor = vec4(mix(color.rgb * light_diffuse_intensity, vec3(1.0,1.0,1.0), light_specular_intensity), 1.0); } 

As you can see from the screenshot below, in the first screen, where I look at the sphere from the direction of the light, the border of the sphere is white because of the specular effect. However if the shader were correct it should not look like that. It should be white only in the middle of the sphere.

In the second screen, I look at both the sphere and the source of the directional light (the yellow sun) and everything looks just fine.

I cannot find the error in my shader, could someone help please?

Thank you very much!

![image of the error][1] [1]: https://i.sstatic.net/SJqsr.png

I coded my first glsl shader which manage diffuse and specular effects of a directional light. This is the fragment shader.

#version 120 //matrix uniform mat4 model_matrix; //directional light position in world coordinates uniform vec3 light_position; uniform vec3 eye; //send to fragment shader varying vec4 color; varying vec4 normal; varying vec4 vertex; void main() { float light_diffuse_intensity; float light_specular_intensity; float light_mod; vec3 world_normal; vec3 world_vertex; vec3 world_reflec; vec3 vertex_to_light; vec3 vertex_to_eye; world_normal = (model_matrix * normal).xyz; world_vertex = (model_matrix * vertex).xyz; vertex_to_light = light_position - world_vertex; //vector from vertex to light vertex_to_eye = eye - world_vertex; //vector from vertex to eye world_reflec = 2 * dot(vertex_to_light, normalize(world_normal.xyz)) * normalize(world_normal) - vertex_to_light; //reflection of vertex_to_light light_diffuse_intensity = dot(normalize(vertex_to_light), normalize(world_normal.xyz)); //computing angle between light and normal light_specular_intensity = dot(normalize(vertex_to_eye), normalize(world_reflec)); //computing angle between eye and reflection if (light_diffuse_intensity > 0){ light_mod = light_diffuse_intensity; light_specular_intensity = pow(light_specular_intensity, 8.0); } else{ light_specular_intensity = 0; } gl_FragColor = vec4(mix(color.rgb * light_diffuse_intensity, vec3(1.0,1.0,1.0), light_specular_intensity), 1.0); } 

As you can see from the screenshot below, in the first screen, where I look at the sphere from the direction of the light, the border of the sphere is white because of the specular effect. However if the shader were correct it should not look like that. It should be white only in the middle of the sphere.

In the second screen, I look at both the sphere and the source of the directional light (the yellow sun) and everything looks just fine.

I cannot find the error in my shader, could someone help please?

Thank you very much!

image of the error

Tweeted twitter.com/#!/StackGameDev/status/361224793439215616
Source Link
Marco
  • 477
  • 5
  • 11

Directional light shader not behaving as expected

I coded my first glsl shader which manage diffuse and specular effects of a directional light. This is the fragment shader.

#version 120 //matrix uniform mat4 model_matrix; //directional light position in world coordinates uniform vec3 light_position; uniform vec3 eye; //send to fragment shader varying vec4 color; varying vec4 normal; varying vec4 vertex; void main() { float light_diffuse_intensity; float light_specular_intensity; float light_mod; vec3 world_normal; vec3 world_vertex; vec3 world_reflec; vec3 vertex_to_light; vec3 vertex_to_eye; world_normal = (model_matrix * normal).xyz; world_vertex = (model_matrix * vertex).xyz; vertex_to_light = light_position - world_vertex; //vector from vertex to light vertex_to_eye = eye - world_vertex; //vector from vertex to eye world_reflec = 2 * dot(vertex_to_light, normalize(world_normal.xyz)) * normalize(world_normal) - vertex_to_light; //reflection of vertex_to_light light_diffuse_intensity = dot(normalize(vertex_to_light), normalize(world_normal.xyz)); //computing angle between light and normal light_specular_intensity = dot(normalize(vertex_to_eye), normalize(world_reflec)); //computing angle between eye and reflection if (light_diffuse_intensity > 0){ light_mod = light_diffuse_intensity; light_specular_intensity = pow(light_specular_intensity, 8.0); } else{ light_specular_intensity = 0; } gl_FragColor = vec4(mix(color.rgb * light_diffuse_intensity, vec3(1.0,1.0,1.0), light_specular_intensity), 1.0); } 

As you can see from the screenshot below, in the first screen, where I look at the sphere from the direction of the light, the border of the sphere is white because of the specular effect. However if the shader were correct it should not look like that. It should be white only in the middle of the sphere.

In the second screen, I look at both the sphere and the source of the directional light (the yellow sun) and everything looks just fine.

I cannot find the error in my shader, could someone help please?

Thank you very much!

![image of the error][1] [1]: https://i.sstatic.net/SJqsr.png