c++ - 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;
它并没有效果。这是什么原因造成的?有没有办法解决它?
解决方案
推荐阅读
- javascript - 将 JavaScript 变量发送到 PHP
- java - 用户输入不是调用方法(Java)
- python - 如何使用 distinct() 为 Django ListView 中的每个标题项仅显示来自子模型的一条记录
- permissions - docker-compose 命令执行问题 - 在 /var/www/ 中设置权限
- ruby-on-rails - Rails 语言持久化?语言环境总是路由到 default_locale
- powershell - PowerShell:将 16MB CSV 导入 PowerShell 变量会创建 >600MB 的 PowerShell 内存使用量
- c++ - 如何在 C++ 中通过多种类型从元组中选择多个元素?
- postgresql-9.5 - 是否可以在不关闭数据库的情况下运行 pg_rewind?
- excel - 阻止 Excel 用户使用 X、最小化和最大化按钮关闭电子表格
- java - 在从片段单击按钮时更改 ViewPager2 内 textview 中的文本