Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么一些三角形在我添加照明后变黑?_C++_Opengl Es_Lighting - Fatal编程技术网

C++ 为什么一些三角形在我添加照明后变黑?

C++ 为什么一些三角形在我添加照明后变黑?,c++,opengl-es,lighting,C++,Opengl Es,Lighting,我正在尝试将镜面照明添加到我的opengl es程序中,该程序加载3d模型。它工作正常。但每当我添加照明时,就会发生这种情况: 有些三角形正在变黑,有些三角形保持白色。 以下是我的顶点和片段着色器代码: "attribute vec4 position;\n" "attribute vec4 normal;\n" "attribute vec4 color;\n" "attribute vec2

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

有些三角形正在变黑,有些三角形保持白色。 以下是我的顶点和片段着色器代码:

    "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);

请格式化你所有的代码,不仅仅是代码的一部分。我打赌你的龙有不一致的法线,可能还有缠绕,这在波前网格上非常常见。要补救,请尝试替换float nDotl=dotunitNL,unitLV;浮动nDotl=absdotunitNL,unitLV;所以,正常的向内/向外的方向不会干扰照明。同时禁用GL_CULL_面。看这有一个预览它应该是什么样子至少我认为它是相同的网格它看起来像你禁用了深度-testing@IWonderWhatThisAPIDoes默认情况下启用GL_CULL_FACE的法线/缠绕不一致看起来类似于GL_DEPTH_TEST off…在没有照明的情况下,孔的颜色与背面的皮肤颜色相同然而,当灯光亮起时,它们会变成黑色,并产生其他的人造痕迹。启用深度测试后,问题得到解决