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!