逐像素、逐顶点光照差异对比

逐像素、逐顶点光照差异

逐像素、逐顶点光照差异性主要体现在对于非精细模型,在执行逐顶点光照时,由于点距较大,在进行颜色线性插值的过程中,无法精细平滑过渡,导致效果变差。

1、  逐像素(光照计算过程在片元着色器中完成)
顶点着色

varying vec4 diffuse,ambient;

varying vec3 normal,lightDir,viewDir;

void main()

{  

      normal = normalize(gl_NormalMatrix * gl_Normal);

     

      lightDir = normalize(vec3(gl_LightSource[0].position));

 

      //相机观察向量,顶点->人眼,用于计算镜面反射

      viewDir = normalize(gl_LightSource[0].halfVector.xyz);

 

      diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;

      ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;

      ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;//全局环境光

       

      gl_Position = ftransform();

    }

片元着色

varying vec4 diffuse,ambient;

varying vec3 normal,lightDir, viewDir;

void main()

{

      vec3 n,viewVec;

      float dfse,spec;

     

      vec4 color = ambient;

     

      n = normalize(normal);

   

      dfse = max(dot(n,lightDir),0.0); //漫反射

     

     if (dfse > 0.0)   //没有漫反射,镜面反射也不存在

{

         color += diffuse * dfse;

         viewVec = normalize(viewDir);

         spec = max(dot(n, viewVec),0.0); //镜面反射

 

         color += gl_FrontMaterial.specular *

               gl_LightSource[0].specular *

               pow(spec, gl_FrontMaterial.shininess);

      }

      gl_FragColor = color;

   }

2、  逐顶点光照(光照计算过程在顶点着色器中完成)

顶点着色

varying vec4 color;

void main()

{  

vec4 diffuse,ambient;

vec3 normal,lightDir, viewDir;

 

     normal = normalize(gl_NormalMatrix * gl_Normal);

     

     lightDir = normalize(vec3(gl_LightSource[0].position));

 

      //相机观察向量,顶点->人眼,用于计算镜面反射

      viewDir = normalize(gl_LightSource[0].halfVector.xyz);

 

      diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;

      ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;

      ambient += gl_LightModel.ambient * gl_FrontMaterial.ambient;//全局环境光

       

      float dfse,spec;    

      color = ambient;

      dfse = max(dot(normal,lightDir),0.0); //漫反射

     

     if (dfse > 0.0)   //没有漫反射,镜面反射也不存在

{

         color += diffuse * dfse;

         spec = max(dot(normal, viewDir),0.0); //镜面反射

 

         color += gl_FrontMaterial.specular *

               gl_LightSource[0].specular *

               pow(spec, gl_FrontMaterial.shininess);

      }

      gl_Position = ftransform();

   }

片元着色

varying vec4 color;

void main()

{     

      gl_FragColor = color;

   }

 

效果对比图(逐像素、逐顶点)

 逐像素、逐顶点光照差异对比_第1张图片

你可能感兴趣的:(逐像素光照,逐顶点光照,glsl,三维,开源)