Lighting maps_练习一

强颜欢笑 提交于 2019-12-06 05:12:07

尝试在片段着色器中反转镜面光贴图的颜色值,让木头显示镜面高光而钢制边缘不反光(由于钢制边缘中有一些裂缝,边缘仍会显示一些镜面高光,虽然强度会小很多)

 1 #version 330 core
 2 out vec4 FragColor;
 3 
 4 struct Material {
 5     sampler2D diffuse;
 6     sampler2D specular;
 7     float     shininess;
 8 };  
 9 
10 struct Light {
11     vec3 position;
12 
13     vec3 ambient;
14     vec3 diffuse;
15     vec3 specular;
16 };
17 
18 in vec3 FragPos;  
19 in vec3 Normal;  
20 in vec2 TexCoords;
21   
22 uniform vec3 viewPos;
23 uniform Material material;
24 uniform Light light;
25 
26 void main()
27 {
28     // ambient
29     vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
30       
31     // diffuse 
32     vec3 norm = normalize(Normal);
33     vec3 lightDir = normalize(light.position - FragPos);
34     float diff = max(dot(norm, lightDir), 0.0);
35     vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));  
36     
37     // specular
38     vec3 viewDir = normalize(viewPos - FragPos);
39     vec3 reflectDir = reflect(-lightDir, norm);  
40     float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
41     vec3 specular = light.specular * spec * (vec3(1.0) - vec3(texture(material.specular, TexCoords))); // here we inverse the sampled specular color. Black becomes white and white becomes black.
42         
43     FragColor = vec4(ambient + diffuse + specular, 1.0);  
44 } 
View Code

将片段着色器中vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));改为vec3 specular = light.specular * spec * (vec3(1.0) - vec3(texture(material.specular, TexCoords)));

本来中间的为黑色(0.0, 0.0, 0.0)所以不反光,改完之后中间黑色部分变为(1.0, 1.0, 1.0)强反光,旁边部分虽然不会被减为0但是强度也会变小。

 

2019/11/30

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!