• Lighting maps_练习一


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

     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

  • 相关阅读:
    小程序-小菊花loading
    小程序-跳转
    小程序-拨打电话
    Pluto
    Android外部存储
    iOS微信内存监控
    iOS UITableView左滑操作功能的实现(iOS8-11)
    Hi,腾讯 WeTest 限免开放 Android Oreo 云真机,Android 8.1 可开测!
    揭密微信跳一跳小游戏那些外挂
    腾讯WeTest发布《2017中国移动游戏质量白皮书》,专注手游品质提升
  • 原文地址:https://www.cnblogs.com/ljy08163268/p/11963556.html
Copyright © 2020-2023  润新知