首页 > 解决方案 > GLSL 的第二个网格具有奇怪的黑色面

问题描述

平台:Windows10 上下文:OpenGL、glew、Win32

所以我加载了 2 个网格(使用一个简单的 OBJ 解析器,它只读取三角网格),带有顶点、紫外线和法线数据。第一个网格点亮正常。没有黑脸。第二个看起来像这样。 奇怪的影响

    #version 440
    
    in vec3 pos;
    in vec2 tex;
    in vec3 nor;
    
    uniform float Scale;
    uniform mat4 perspective;
    uniform mat4 model;
    
    
    out vec3 normaldir;
    out vec2 texOut;
    out vec3 FragPos;
    
    void main()
    {
        normaldir = normalize(mat3(transpose(inverse(model))) * nor);
        gl_Position = perspective * model * vec4(pos.xyz, 1.0);
        texOut = tex;
        FragPos = vec3(model * vec4(pos, 1.0));
    }
    #version 440
    uniform float Scale;
    uniform sampler2D diffuse;
    uniform sampler2D normal;
    uniform vec3 viewPos;
    //uniform sampler2D normalMap0;
    
    
    in vec3 normaldir;
    in vec2 texOut;
    in vec3 FragPos;
    
    layout(location = 0) out vec4 FragColor0;
    
    
    void main() 
    {
    
    
        vec3 lightPos = {2,6,0};
        lightPos.x = sin(Scale)*5;
        lightPos.z = cos(Scale)*5;
        vec3 lightDir = normalize(lightPos - FragPos); 
    
        vec3 lightColor = {1.0,1.0,1.0};
        float specularStrength = 1.6;
    
        float diff = max(dot(normaldir, lightDir), 0.0);
        vec3 diffuseD = diff * lightColor;
    
    
        vec3 viewDir = normalize(viewPos - FragPos);
        vec3 reflectDir = reflect(-lightDir, normaldir); 
    
        vec3 ambient = {0.0,0.2,0.4};
        
        float spec = pow(max(dot(viewDir, reflectDir), 0.0), 25);
        vec3 specular = specularStrength * spec * lightColor; 
    
        vec3 diffuseCol = texture(diffuse, texOut).xyz;
        vec3 result = (ambient + diffuseD+ specular) * diffuseCol;
    
        FragColor0 = vec4(result, 1.0);
    }

标签: c++openglglsl

解决方案


对不起,我犯了一个非常愚蠢的错误。感谢您对@Rabbid76 的所有支持(是的,我确实颠倒了法线是的)@paddy

问题是绑定普通缓冲区。我绑定glm::vec2 * size而不是glm::vec3 * size法线缓冲区


推荐阅读