I am trying to add specular lighting to my opengl es program which loads 3d model. Its working normally. But whenever I add lighting this happens:
some triangles are becoming black and some are staying white. here is my Vertex and fragment shader code:
"attribute vec4 position;\n"
"attribute vec4 normal;\n"
"attribute vec4 color;\n"
"attribute vec2 texCord;\n"
"varying vec4 vcolor;\n"
"varying vec2 vtexCord;\n"
"varying vec3 s_normal;\n"
"varying vec3 toLightv;\n"
"varying vec3 toCameraV;\n"
"uniform vec3 light_pos;\n"
"uniform mat4 MVP;\n"
"uniform mat4 view;"
"uniform mat4 transform;\n"
"void main()\n"
"{\n"
"gl_Position = MVP * vec4(position.xyz, 1.0);\n"
"vcolor = color;\n"
"vtexCord = texCord;\n"
"s_normal = (transform * vec4(normal.xyz,0.0)).xyz;\n"
"toLightv = light_pos - (MVP * vec4(position.xyz, 1.0)).xyz;\n"
"toCameraV = (view * vec4(0.0,0.0,0.0,1.0)).xyz - (MVP * vec4(position.xyz, 1.0)).xyz;\n"
"}";
`
"precision mediump float;\n"
"varying vec4 vcolor;\n"
"varying vec2 vtexCord;\n"
"varying vec3 s_normal;\n"
"varying vec3 toLightv;\n"
"varying vec3 toCameraV;\n"
"uniform sampler2D s_texr;\n"
"uniform vec3 light_col;\n"
"void main()\n"
"{\n"
// "gl_FragColor = vec4(1.0,0.0,1.0,1.0);\n"
//"gl_FragColor = vec4 (vcolor.xyz,1.0);\n"
"vec3 unitCV = normalize(toCameraV);\n"
"vec3 unitNL = normalize(s_normal);\n"
"vec3 unitLV = normalize(toLightv);\n"
"vec3 lightComing = -unitLV;\n"
"vec3 reflectedL = reflect(lightComing,unitNL);\n"
"float specularFactor = dot(reflectedL,toCameraV);\n"
"specularFactor = max(specularFactor,0.0);\n"
"float dampFactor = pow(specularFactor,1.0);\n"
"vec3 Specular= dampFactor * vec3(1.0,1.0,1.0);\n"
"float nDotl = dot(unitNL,unitLV);"
"vec3 diffuse =max(nDotl,0.1) * vec3(1.0,1.0,1.0);"
// diffuse = diffuse * (1.0 / (1.0 + (0.00000025 * distance * distance)));
"gl_FragColor =vec4(diffuse.xyz,1.0)* texture2D(s_texr, vtexCord)+vec4(Specular.xyz,1.0);"
"};"
I have enabled depth testing and the problem solved.
glEnable(GL_DEPTH_TEST);