首页 > 解决方案 > Phong 照明留下条纹图案

问题描述

我在 OpenGL 上学习光照,在这个例子中,我使用了phong 方法。一切似乎都按预期工作,但我注意到这种方法会留下一些“条纹”,尤其是在结果颜色的漫反射部分:

顶点着色器:

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 normal;

struct Light{
    vec3 position;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};


uniform mat4 Model;
uniform mat4 View;
uniform mat4 Projection;

uniform Light light;

out vec3 aNormal;
out vec3 FragPos;
out vec3 LightPos;

//Usually you would transform the input into coordinates that fall within OpenGL's visible region
void main()
{

    aNormal = mat3(transpose(inverse(View * Model))) * normal; 
    FragPos = vec3(View * Model * vec4(aPos,1.0));
    gl_Position =  Projection  * vec4(FragPos,1.0);
    LightPos = vec3(View * vec4(light.position,1.0));
}
#version 330 core
out vec4 FragColor;

in vec3 aNormal;
in vec3 FragPos;
in vec3 LightPos;

struct Material{
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
    float shininess;
};

struct Light{
    vec3 position;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

uniform Material material;
uniform Light light;
precision highp float;

void main()
{
    vec3 ambient = material.ambient * light.ambient;
    vec3 norm = normalize(aNormal);
    //Difuse
    vec3 lightDir = normalize(LightPos - FragPos);
    float diff = max(dot(norm,lightDir),0.0);
    vec3 diffuse = (diff * material.diffuse) * light.diffuse;

    vec3 specular = vec3(0);
    //Specular
    if(diff > 0){
        vec3 viewDir = normalize(-FragPos);
        vec3 reflectDir = reflect(-lightDir,norm);
        float spec = pow(max(dot(viewDir,reflectDir),0.0), material.shininess);
        specular = light.specular * (spec * material.specular);
    }

    vec3 result = diffuse + (ambient + specular);
    FragColor = vec4(result, 1.0);
}
Fragment shader:

我想知道为什么会发生这种情况,我怀疑与浮点精度有关,但是我尝试使用precision highp float;它并没有效果。这是什么原因造成的?有没有办法解决它?

标签: c++shaderopengl-3

解决方案


推荐阅读