光照

光照 

   光照系统分为三部分:光源、材质、光源环境

   光照类型:镜面反射、漫反射、高光反射(三种光的反射特性)

   光照模型: 环境光(Ambient Light)、  漫射光(Diffuse Light)、镜面光(Specular Light


方向光为例

        #version 330 core

        uniform mat4 MVPMatrix;

        uniform mat3 NormalMatrix;

          in vec4 VertexColor;

          in vec3 VertexNormal;

         in vec4 VertexPosition;

         out vec4 Color;

         out vec3 Normal;

         void main()
        {        Color = VertexColor;

                 Normal = normalize(NormalMatrix * VertexNormal);

                     gl_position = MVPMatrix * VertexPosition;
          }

     片元着色器: 

        #version 330 core

        uniform vec3 ambient;

        uniform vec3 LightColor;

        uniform vec3 LightDirection; 

        uniform vec3 HalfVector;

        uniform float shininess;

        uniform float strength;

         in vec4 Color;

         in vec3 Normal;

         out vec4 FragColor;

         void main()
        {         

            float          diffuse = max(0.0,  dot(Normal, LightDirection));       //点积是判断光照的反射程度

            float     specular  =  max(0.0,  dot(Normal, HalfVector));

             if(diffuse==0.0)   specular =0.0;

             else                      specular = pow(specular, shininess);

             vec3  scatteredLight = LightColor * diffuse + ambient;

             vec3   reflectedLight = LightColor * specular *strength;

             vec3   rgb = min(Color.rgb * scatteredLight + reflectedLight,  vec3(1.0));

                    FlagColor = vec4(rgb,  Color.a);

         }

                    

多光源:

scatteredLight += (Lights[light].Color * diffuse + Lights[light].ambient) * attenuation;

reflectedLight += Lights[light].Color * specular *attenuation;


多光源多材质: 

scatteredLight += (Lights[light].Color * diffuse * Material[MatIndex].diffuse

                           Lights[light].ambient  * Material[MatIndex].ambient) * attenuation;

reflectedLight += Lights[light].Color * specular * Material[MatIndex].specular *attenuation;

rgb = min(Color.rgb * scatteredLight + reflectedLight +Material[MatIndex].emission ,  vec3(1.0));

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

屠变恶龙之人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值