# Strange Phong artifacts around the edge of specular spot

This topic is 1080 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, everyone!

I am trying to create a basic Phong shader for learning purposes, I've implemented it, but it produces some strange rough edges around the specular spot...

Here's the vertex shader I've written (fragment shader does nothing useful for now, all computation in vertex shader temporarily):

#version 300 es

in vec4 position;
in vec3 normal;
in vec2 texcoord0;

out lowp vec4 colorVarying;
out vec2 texcoord;

uniform mat4 modelViewMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;
uniform vec4 diffuseColor;

void main () {

vec3 eyeNormal = normalize (normalMatrix * normal);
vec3 lightPositionNormalized = normalize (vec3 (-1.0, 0.0, 1.));

// Ia = ambient light intensity (light param)
// Ka = ambient reflection constant (material param)
// ambient = Ka * Ia;
// Id = diffuse light intensity (light param)
// Kd = diffuse reflection constant (material param)
// L = direction vector from point on surface to light
// N = normal at point on surface
// diffuse = Kd * (L.N) * Id
// Is = specular light intensity (light param)
// Ks = specular reflection constant (material param)
// alpha = shininess constant (material param)
// R = 2 * (L.N) * N - L = reflection direction vector
// V = direction from point to camera eye
// specular = Ks * (R.V)^alpha * Is
// Ip = ambient + diffuse + specular;

mat4 s = modelViewMatrix * 1.;

float Ka = 1.f;
float Ia = 1.f;
float ambient = Ka * Ia;
float Kd = 1.f;
float Id = 1.f;
float LdotN = max (0., dot (eyeNormal, lightPositionNormalized));
float diffuse = Kd * Id * LdotN;
float Ks = 1.f;
float Is = 1.f;
float alpha = .7f;
vec3 R = normalize (2. * LdotN * eyeNormal - lightPositionNormalized);
vec4 V = -normalize (modelViewMatrix * position);
float specular = Ks * Is * pow (max (dot (V, vec4 (R, 1.)), 0.), alpha);

colorVarying = ambient * diffuseColor + diffuse * diffuseColor + specular * vec4 (1.,1.,1.,1.);
texcoord = texcoord0;
gl_Position = modelViewProjectionMatrix * position);
}



And here is the result... It's a 64 rings x 64 segments uv-sphere, I thought this should be well enough for good smoothing... but, here's what I got (I don't know if it is related to the geometry of the sphere somehow)

[attachment=28397:Screen Shot 2015-08-03 at 03.02.30.png]
[attachment=28398:Screen Shot 2015-08-03 at 03.02.26.png]
[attachment=28399:Screen Shot 2015-08-03 at 03.02.23.png]

And here's a video to see that same sphere from varying distance: https://vid.me/eJfY

Can anybody explain this strange artifact? I don't know where to look for a reason of such an output, all screenshots in the internet have a nice smooth white spot, mine has rough edges somehow... Does anyone have a clue on how to shade with Phong properly? Does anybody recognize familiar symptoms here?
Your help is really appreciated, thanks!

Edited by p1p1

##### Share on other sites

this is because you are doing a vertex lighting, you calculate the lighting for each vertex and then that lighting gets interpolated through the fragments. you should use fragment shader for the lighting so that the lighting gets calculated for each fragment instead of getting interpolated through fragments.

Ok, thanks again for the answer! I'll try to move it to fragment shader and I'll post the results here as soon as it is done... Thank you.

##### Share on other sites

this is because you are doing a vertex lighting, you calculate the lighting for each vertex and then that lighting gets interpolated through the fragments. you should use fragment shader for the lighting so that the lighting gets calculated for each fragment instead of getting interpolated through fragments.

Ok, here's my final result. I finally got it working. Most of the code of my shader was taken from here: http://www.3dgep.com/texturing-and-lighting-with-opengl-and-glsl/ . I just tweaked it a little bit for my naming and structures... Thanks, IYP!

[attachment=28419:Screen Shot 2015-08-04 at 00.38.50.png]

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 29
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631740
• Total Posts
3001962
×