-2

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:

rendering photo

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);"
    "};"
genpfault
  • 51,148
  • 11
  • 85
  • 139
DevSadik
  • 1
  • 1
  • please format all of your code, not just parts of code – jsotola Mar 13 '21 at 07:28
  • my bet is your dragon has inconsistent normals and probably also winding which is verry common on wavefront meshes. To remedy try replace `float nDotl = dot(unitNL,unitLV);` with float `nDotl = abs(dot(unitNL,unitLV));` so direction of normal (inwards/outwards) is not messing up lighting. Also disable `GL_CULL_FACE`. See this [How do I sort the texture positions based on the texture indices given in a Wavefront (.obj) file?](https://stackoverflow.com/a/51722009/2521214) there is a preview how it should look like (at least I think its the same mesh) – Spektre Mar 13 '21 at 07:35
  • It *looks* like you have disabled depth-testing – IWonderWhatThisAPIDoes Mar 13 '21 at 07:40
  • @IWonderWhatThisAPIDoes inconsistent normals/winding with GL_CULL_FACE enabled (by default) looks similar to GL_DEPTH_TEST test off ...without lighting the holes had the same colors as skin (as the back side was seen) however with lighting on those turn to black and make other artifacts – Spektre Mar 13 '21 at 07:42
  • Thanks. After enabling depth-testing the problem solved – DevSadik Mar 14 '21 at 08:26
  • @devsadik please have @ IWonderWhatThisAPIDoes respond with an answer, mark the question as the accepted answer and upvote it! – Brian Topping May 22 '21 at 16:00

1 Answers1

-1

I have enabled depth testing and the problem solved.

glEnable(GL_DEPTH_TEST);
DevSadik
  • 1
  • 1