为什么添加光照后有些三角形会变黑?

德萨迪克

我正在尝试向加载 3d 模型的 opengl es 程序添加镜面反射照明。它的工作正常。但是每当我添加照明时,就会发生这种情况:

渲染照片

有些三角形变成黑色,有些则保持白色。这是我的顶点和片段着色器代码:

    "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);"
    "};"
德萨迪克

我启用了深度测试并解决了问题。

glEnable(GL_DEPTH_TEST);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章