Question

I'm trying to add specular lightning to my android OpenGL project, but the light is only projected on a part of my object, as you can see in my example:

enter image description here

In my fragment shader, I calculate the color as following:

float dot(vec3 v1, vec3 v2)
{
    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
}

void main()
{
    float distSqr = dot(lightDir, lightDir);
    float att = clamp(1.0 - 0.001 * sqrt(distSqr), 0.0, 1.0);
    vec3 L = lightDir * inversesqrt(distSqr);

    vec4 baseColor = texture2D(texture1, tCoord);

    vec3 N = normalize(baseColor.xyz * 2.0 - 1.0);

    vec3 E = normalize(eyeVec);

    vec3 reflectV = reflect(-L, N);

    vec4 ambientTerm = baseColor;

    // base * diffuse * max(dot(N, L), 0.0)
    vec4 diffuseTerm = baseColor * matDiffuse * max(dot(N, L), 0.0);
    //vec4 diffuseTerm = baseColor * gl_FrontMaterial.diffuse * max(dot(N, L), 0.0);

    // spec * pow(max(dot(reflectV, E), 0.0), shininess)
    vec4 specularTerm = matSpecular * pow(max(dot(reflectV, E), 0.0), 5.0);
    //vec4 specularTerm = vec4(1.0, 1.0, 1.0, 1.0) * pow(max(dot(reflectV, E), 0.0), 5.0);

    gl_FragColor = (ambientTerm + diffuseTerm + specularTerm) * att;
}

The light position is: 0, 2, 2

If you need more info, please ask for it.

Was it helpful?

Solution

Problem was the vertex shader.

code was:

EyespaceNormal = vec3(u_mvMatrix * vec4(a_normal, 1.0));
vec3 eyespaceTangent = vec3(u_mvMatrix * vec4(tangent, 1.0));

and should be

EyespaceNormal = vec3(mat3(u_mvMatrix) * a_normal);
vec3 eyespaceTangent = vec3(mat3(u_mvMatrix) * tangent);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top